import { test } from '@japa/runner'
import { assert } from '@esm-bundle/chai'
import {
  MatchesSchemaAssertion,
  MatchesSchemaAssertionKind,
} from '../../../../../src/models/http-flows/steps/assertion/MatchesSchemaAssertion.js'

test.group('MatchesSchemaAssertion constructor()', () => {
  test('initializes from the schema', () => {
    const instance = new MatchesSchemaAssertion({
      kind: MatchesSchemaAssertionKind,
      ref: 'test-ref',
      schema: 'test-schema',
      enabled: false,
    })
    assert.equal(instance.schema, 'test-schema')
    assert.equal(instance.ref, 'test-ref')
    assert.isFalse(instance.enabled)
  })

  test('initializes empty class', () => {
    const instance = new MatchesSchemaAssertion()
    assert.equal(instance.kind, MatchesSchemaAssertionKind)
    assert.isUndefined(instance.enabled)
    assert.isUndefined(instance.ref)
    assert.isUndefined(instance.schema)
  })
})

test.group('MatchesSchemaAssertion fromRef()', () => {
  test('sets the ref', () => {
    const instance = MatchesSchemaAssertion.fromRef('test-value')
    assert.equal(instance.ref, 'test-value')
  })

  test('does not set the schema', () => {
    const instance = MatchesSchemaAssertion.fromRef('test-value')
    assert.isUndefined(instance.schema)
  })

  test('sets it enabled', () => {
    const instance = MatchesSchemaAssertion.fromRef('test-value')
    assert.isTrue(instance.enabled)
  })
})

test.group('MatchesSchemaAssertion fromSchema()', () => {
  test('sets the ref', () => {
    const instance = MatchesSchemaAssertion.fromSchema('test-value')
    assert.equal(instance.schema, 'test-value')
  })

  test('does not set the ref', () => {
    const instance = MatchesSchemaAssertion.fromSchema('test-value')
    assert.isUndefined(instance.ref)
  })

  test('sets it enabled', () => {
    const instance = MatchesSchemaAssertion.fromSchema('test-value')
    assert.isTrue(instance.enabled)
  })
})

test.group('MatchesSchemaAssertion checkValidity()', () => {
  test('returns true when has ref', () => {
    const instance = MatchesSchemaAssertion.fromRef('test-value')
    assert.isTrue(instance.checkValidity())
  })

  test('returns true when has schema', () => {
    const instance = MatchesSchemaAssertion.fromSchema('test-value')
    assert.isTrue(instance.checkValidity())
  })

  test('returns false when no ref or schema', () => {
    const instance = new MatchesSchemaAssertion()
    assert.isFalse(instance.checkValidity())
  })
})

test.group('MatchesSchemaAssertion toJSON()', () => {
  test('serializes the kind', () => {
    const instance = new MatchesSchemaAssertion()
    assert.equal(instance.kind, MatchesSchemaAssertionKind)
  })

  test('serializes the ref when set', () => {
    const instance = MatchesSchemaAssertion.fromRef('test-value')
    assert.equal(instance.ref, 'test-value')
  })

  test('ignores the ref when not set', () => {
    const instance = new MatchesSchemaAssertion()
    assert.isUndefined(instance.ref)
  })

  test('serializes the schema when set', () => {
    const instance = MatchesSchemaAssertion.fromSchema('test-value')
    assert.equal(instance.schema, 'test-value')
  })

  test('ignores the schema when not set', () => {
    const instance = new MatchesSchemaAssertion()
    assert.isUndefined(instance.schema)
  })

  test('serializes the enabled when set', () => {
    const instance = new MatchesSchemaAssertion()
    instance.enabled = true
    assert.isTrue(instance.enabled)
  })

  test('ignores the enabled when not set', () => {
    const instance = new MatchesSchemaAssertion()
    assert.isUndefined(instance.enabled)
  })
})
