import { test } from '@japa/runner'
import { EvalFunctions } from '../../../../src/runtime/variables/EvalFunctions.js'

const cacheTarget = {}

test.group('EncodeURIComponent()', () => {
  test('throws when no argument', ({ assert }) => {
    assert.throws(() => {
      EvalFunctions.EncodeURIComponent(cacheTarget, undefined)
    })
  })

  test('encodes first argument', ({ assert }) => {
    const result = EvalFunctions.EncodeURIComponent(cacheTarget, ['+'])
    assert.equal(result, '%2B')
  })

  test('ignores other arguments', ({ assert }) => {
    const result = EvalFunctions.EncodeURIComponent(cacheTarget, ['+', '+'])
    assert.equal(result, '%2B')
  })
})

test.group('DecodeURIComponent()', () => {
  test('throws when no argument', ({ assert }) => {
    assert.throws(() => {
      EvalFunctions.DecodeURIComponent(cacheTarget, undefined)
    })
  })

  test('decodes first argument', ({ assert }) => {
    const result = EvalFunctions.DecodeURIComponent(cacheTarget, ['%2B'])
    assert.equal(result, '+')
  })

  test('ignores other arguments', ({ assert }) => {
    const result = EvalFunctions.DecodeURIComponent(cacheTarget, ['%2B', '%2B'])
    assert.equal(result, '+')
  })
})

test.group('Btoa()', () => {
  test('throws when no argument', ({ assert }) => {
    assert.throws(() => {
      EvalFunctions.Btoa(cacheTarget, undefined)
    })
  })

  test('encodes the first argument', ({ assert }) => {
    const result = EvalFunctions.Btoa(cacheTarget, ['test', 'other'])
    assert.equal(result, 'dGVzdA==')
  })
})

test.group('Atob()', () => {
  test('throws when no argument', ({ assert }) => {
    assert.throws(() => {
      EvalFunctions.Atob(cacheTarget, undefined)
    })
  })

  test('decodes the first argument', ({ assert }) => {
    const result = EvalFunctions.Atob(cacheTarget, ['dGVzdA==', 'other'])
    assert.equal(result, 'test')
  })
})
