import { test } from '@japa/runner'
import { IResponse, createLogger, CoreEngine, IRequestTime } from '../../../../src/index.js'

const logger = createLogger()

test.group('Timings tests', () => {
  test('has the stats object', async ({ assert, httpConfig }) => {
    const engine = new CoreEngine(
      {
        url: `http://localhost:${httpConfig.httpPort}/v1/get`,
        method: 'GET',
      },
      { logger }
    )
    const info = await engine.send()
    const response = info.response as IResponse
    const timing = response.timings as IRequestTime
    assert.typeOf(timing, 'object')
  })
  ;(['connect', 'receive', 'send', 'wait', 'dns', 'ssl'] as (keyof IRequestTime)[]).forEach((prop) => {
    test(`has the ${prop} value`, async ({ assert, httpConfig }) => {
      const engine = new CoreEngine(
        {
          url: `http://localhost:${httpConfig.httpPort}/v1/get`,
          method: 'GET',
        },
        { logger }
      )
      const info = await engine.send()
      const response = info.response as IResponse
      const timing = response.timings as IRequestTime
      assert.typeOf(timing[prop], 'number')
    })
  })

  test('has the stats time for ssl', async ({ assert }) => {
    const engine = new CoreEngine(
      {
        url: 'https://www.google.com/',
        method: 'GET',
      },
      { logger }
    )
    const info = await engine.send()
    const response = info.response as IResponse
    const timing = response.timings as IRequestTime
    assert.isAbove(timing.ssl as number, -1)
  })
})
