import { test } from '@japa/runner'
import { PayloadPointer } from '../../../src/index.js'

test.group('PayloadPointer', () => {
  test('returns a value for a json - exact match', async ({ assert }) => {
    const payload = JSON.stringify({ a: 'b', c: 'd' })
    const reader = new PayloadPointer('a', payload, 'application/json')

    const result = await reader.getValue()
    assert.equal(result, 'b')
  })

  test('returns a value for an x-json', async ({ assert }) => {
    const payload = JSON.stringify({ a: 'b', c: 'd' })
    const reader = new PayloadPointer('a', payload, 'application/x-json')

    const result = await reader.getValue()
    assert.equal(result, 'b')
  })

  test('returns a value for an xml - exact match', async ({ assert }) => {
    const payload = '<xml><a>b</a><c>d</c></xml>'
    const reader = new PayloadPointer('/xml/a', payload, 'application/xml')
    const result = await reader.getValue()
    assert.equal(result, 'b')
  })

  test('returns a value for an atom-xml', async ({ assert }) => {
    const payload = '<xml><a>b</a><c>d</c></xml>'
    const reader = new PayloadPointer('/xml/a', payload, 'application/atom-xml')
    const result = await reader.getValue()
    assert.equal(result, 'b')
  })

  test('returns a value for an x-www-form-urlencoded', async ({ assert }) => {
    const payload = 'a=b&c=d'
    const reader = new PayloadPointer('a', payload, 'x-www-form-urlencoded')
    const result = await reader.getValue()
    assert.equal(result, 'b')
  })

  test('returns undefined when no mime', async ({ assert }) => {
    const payload = JSON.stringify({ a: 'b', c: 'd' })
    const reader = new PayloadPointer('a', payload)

    const result = await reader.getValue()
    assert.isUndefined(result)
  })

  test('returns undefined when no payload', async ({ assert }) => {
    const reader = new PayloadPointer('a', undefined, 'application/json')

    const result = await reader.getValue()
    assert.isUndefined(result)
  })
})
