import { test } from '@japa/runner'
import { assert } from '@esm-bundle/chai'
import {
  SubstringStep,
  SubstringStepKind,
} from '../../../../../src/models/http-flows/steps/transformation/SubstringStep.js'

test.group('SubstringStep constructor()', () => {
  test('initializes from values', () => {
    const instance = new SubstringStep(10, 20)
    assert.equal(instance.start, 10)
    assert.equal(instance.end, 20)
    assert.isTrue(instance.enabled)
  })

  test('initializes from the schema', () => {
    const instance = new SubstringStep({
      kind: SubstringStepKind,
      start: 5,
      end: 10,
      enabled: false,
    })
    assert.equal(instance.start, 5)
    assert.equal(instance.end, 10)
    assert.isFalse(instance.enabled)
  })

  test('initializes empty class', () => {
    const instance = new SubstringStep()
    assert.equal(instance.kind, SubstringStepKind)
    assert.isUndefined(instance.enabled)
    assert.isUndefined(instance.start)
    assert.isUndefined(instance.end)
  })
})

test.group('SubstringStep checkValidity()', () => {
  test('returns true with start set', () => {
    const instance = new SubstringStep(10)
    assert.isTrue(instance.checkValidity())
  })

  test('returns false when has no start', () => {
    const instance = new SubstringStep()
    assert.isFalse(instance.checkValidity())
  })
})

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

  test('serializes the start when set', () => {
    const instance = new SubstringStep(10)
    assert.equal(instance.start, 10)
  })

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

  test('serializes the end when set', () => {
    const instance = new SubstringStep(10, 20)
    assert.equal(instance.end, 20)
  })

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

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

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