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

test.group('rule()', (group) => {
  let rules: HostRules

  group.each.setup(() => {
    rules = new HostRules()
  })

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

  test('has the {property} property of a type {type}')
    .with([
      { property: '_id', type: 'string' },
      { property: 'from', type: 'string' },
      { property: 'to', type: 'string' },
      { property: 'enabled', type: 'boolean' },
      { property: 'comment', type: 'string' },
    ])
    .run(({ assert }, { property, type }) => {
      const result = rules.rule()
      assert.typeOf(result[property as keyof ARCHostRule], type)
    })
})

test.group('rules()', (group) => {
  let rules: HostRules

  group.each.setup(() => {
    rules = new HostRules()
  })

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

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

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

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