import { test } from '@japa/runner'
import { RequestTime, IRequestTime, Kind as RequestTimeKind } from '../../../src/models/RequestTime.js'

test.group('Models > RequestTime > constructor()', (group) => {
  let schema: IRequestTime
  group.each.setup(() => {
    schema = {
      kind: RequestTimeKind,
      blocked: 1,
      connect: 2,
      dns: 3,
      receive: 4,
      send: 5,
      wait: 6,
      ssl: 7,
    }
  })

  test('creates default values', ({ assert }) => {
    const result = new RequestTime()
    assert.equal(result.kind, RequestTimeKind)
    assert.equal(result.connect, -1)
    assert.equal(result.receive, -1)
    assert.equal(result.send, -1)
    assert.equal(result.wait, -1)
    assert.equal(result.blocked, -1)
    assert.equal(result.dns, -1)
    assert.equal(result.ssl, -1)
  })

  test('creates the values from the schema', ({ assert }) => {
    const result = new RequestTime(schema)
    assert.equal(result.kind, RequestTimeKind)
    assert.equal(result.connect, schema.connect)
    assert.equal(result.receive, schema.receive)
    assert.equal(result.send, schema.send)
    assert.equal(result.wait, schema.wait)
    assert.equal(result.blocked, schema.blocked)
    assert.equal(result.dns, schema.dns)
    assert.equal(result.ssl, schema.ssl)
  })

  test('creates the values from the JSON schema string', ({ assert }) => {
    const result = new RequestTime(JSON.stringify(schema))
    assert.equal(result.kind, RequestTimeKind)
    assert.equal(result.connect, schema.connect)
    assert.equal(result.receive, schema.receive)
    assert.equal(result.send, schema.send)
    assert.equal(result.wait, schema.wait)
    assert.equal(result.blocked, schema.blocked)
    assert.equal(result.dns, schema.dns)
    assert.equal(result.ssl, schema.ssl)
  })
})

test.group('Models > RequestTime > toJSON()', (group) => {
  let schema: IRequestTime
  let instance: RequestTime
  group.each.setup(() => {
    schema = {
      kind: RequestTimeKind,
      blocked: 1,
      connect: 2,
      dns: 3,
      receive: 4,
      send: 5,
      wait: 6,
      ssl: 7,
    }
    instance = new RequestTime(schema)
  })

  test('serializes the kind', ({ assert }) => {
    const result = instance.toJSON()
    assert.equal(result.kind, RequestTimeKind)
  })

  test('serializes the blocked', ({ assert }) => {
    const result = instance.toJSON()
    assert.equal(result.blocked, schema.blocked)
  })

  test('serializes the connect', ({ assert }) => {
    const result = instance.toJSON()
    assert.equal(result.connect, schema.connect)
  })

  test('serializes the dns', ({ assert }) => {
    const result = instance.toJSON()
    assert.equal(result.dns, schema.dns)
  })

  test('serializes the receive', ({ assert }) => {
    const result = instance.toJSON()
    assert.equal(result.receive, schema.receive)
  })

  test('serializes the send', ({ assert }) => {
    const result = instance.toJSON()
    assert.equal(result.send, schema.send)
  })

  test('serializes the wait', ({ assert }) => {
    const result = instance.toJSON()
    assert.equal(result.wait, schema.wait)
  })

  test('serializes the ssl', ({ assert }) => {
    const result = instance.toJSON()
    assert.equal(result.ssl, schema.ssl)
  })

  test('does not serialize the ssl when missing', ({ assert }) => {
    delete instance.ssl
    const result = instance.toJSON()
    assert.isUndefined(result.ssl)
  })
})

test.group('Models > RequestTime > total()', (group) => {
  let instance: RequestTime
  group.each.setup(() => {
    instance = new RequestTime({
      blocked: 1,
      connect: 2,
      dns: 3,
      receive: 4,
      send: 5,
      wait: 6,
      ssl: 7,
    })
  })

  test('returns the sum of all values', ({ assert }) => {
    const result = instance.total()
    assert.equal(result, 28)
  })
})
