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

test.group('variable()', (group) => {
  let variables: Variables

  group.each.setup(() => {
    variables = new Variables()
  })

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

  test('has the {property} property of a type {type}')
    .with([
      { property: 'enabled', type: 'boolean' },
      { property: 'value', type: 'string' },
      { property: 'name', type: 'string' },
      { property: '_id', type: 'string' },
      { property: 'environment', type: 'string' },
    ])
    .run(({ assert }, { property, type }) => {
      const result = variables.variable()
      assert.typeOf(result[property as keyof ARCVariable], type)
    })

  test('always creates "default" environment', ({ assert }) => {
    const result = variables.variable({
      defaultEnv: true,
    })
    assert.equal(result.environment, 'default')
  })

  test('always creates random environment', ({ assert }) => {
    const result = variables.variable({
      randomEnv: true,
    })
    assert.notEqual(result.environment, 'default')
  })
})

test.group('listVariables()', (group) => {
  let variables: Variables

  group.each.setup(() => {
    variables = new Variables()
  })

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

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

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

  test('calls variable()', ({ assert }) => {
    const spy = sinon.spy(variables, 'variable')
    variables.listVariables(5)
    assert.equal(spy.callCount, 5)
  })
})
