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

test.group('JmespathReader', () => {
  test('returns a value for a simple path', async ({ assert }) => {
    const payload = JSON.stringify({ a: 'b', c: 'd' })
    const reader = new JmespathReader()
    await reader.writePayload(payload)

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

  test('returns a value for a deep path', async ({ assert }) => {
    const payload = JSON.stringify({ a: { b: 'c' } })
    const reader = new JmespathReader()
    await reader.writePayload(payload)

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

  test('searches for the value', async ({ assert }) => {
    const payload = JSON.stringify([
      { name: 'Seattle', state: 'WA' },
      { name: 'New York', state: 'NY' },
      { name: 'Bellevue', state: 'WA' },
      { name: 'Olympia', state: 'WA' },
    ])
    const reader = new JmespathReader()
    await reader.writePayload(payload)

    const result = await reader.getValue("[?state == 'WA'].name | [1]")
    assert.equal(result, 'Bellevue')
  })
})
