import { test } from '@japa/runner'
import sinon from 'sinon'
import { Cookies } from '../../../../src/mocking/legacy/Cookies.js'
import type { ARCCookie } from '../../../../src/legacy.js'

test.group('cookie()', (group) => {
  let cookies: Cookies

  group.each.setup(() => {
    cookies = new Cookies()
  })

  test('returns an object', ({ assert }) => {
    const result = cookies.cookie()
    assert.typeOf(result, 'object')
  })

  test('has the {property} property of a type {type}')
    .with([
      { property: '_id', type: 'string' },
      { property: 'created', type: 'number' },
      { property: 'updated', type: 'number' },
      { property: 'expires', type: 'number' },
      { property: 'maxAge', type: 'number' },
      { property: 'name', type: 'string' },
      { property: 'value', type: 'string' },
      { property: 'domain', type: 'string' },
      { property: 'hostOnly', type: 'boolean' },
      { property: 'httpOnly', type: 'boolean' },
      { property: 'lastAccess', type: 'number' },
      { property: 'path', type: 'string' },
      { property: 'persistent', type: 'boolean' },
    ])
    .run(({ assert }, { property, type }) => {
      const result = cookies.cookie()
      assert.typeOf(result[property as keyof ARCCookie], type)
    })
})

test.group('cookies()', (group) => {
  let cookies: Cookies

  group.each.setup(() => {
    cookies = new Cookies()
  })

  test('returns an array', ({ assert }) => {
    const result = cookies.cookies()
    assert.typeOf(result, 'array')
  })

  test('has default number of requests', ({ assert }) => {
    const result = cookies.cookies()
    assert.lengthOf(result, 25)
  })

  test('has requested number of items', ({ assert }) => {
    const result = cookies.cookies(5)
    assert.lengthOf(result, 5)
  })

  test('calls generateHeaderSetObject()', ({ assert }) => {
    const spy = sinon.spy(cookies, 'cookie')
    cookies.cookies(5)
    assert.equal(spy.callCount, 5)
  })
})
