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

test.group('url()', (group) => {
  let urls: Urls

  group.each.setup(() => {
    urls = new Urls()
  })

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

  test('has the {property} property of a type {type}')
    .with([
      { property: 'time', type: 'number' },
      { property: 'cnt', type: 'number' },
      { property: '_id', type: 'string' },
    ])
    .run(({ assert }, { property, type }) => {
      const result = urls.url()
      assert.typeOf(result[property as keyof ARCUrlHistory], type)
    })
})

test.group('urls()', (group) => {
  let urls: Urls

  group.each.setup(() => {
    urls = new Urls()
  })

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

  test('returns the default number of items', ({ assert }) => {
    const result = urls.urls()
    assert.lengthOf(result, 25)
  })

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

  test('calls url()', ({ assert }) => {
    const spy = sinon.spy(urls, 'url')
    urls.urls(5)
    assert.equal(spy.callCount, 5)
  })
})
