'use strict'

const { it } = require('node:test')

const assert = require('assert')
const yaml = require('js-yaml')

it('Listener informed on a very simple scalar.', function () {
  const history = []
  function l (eventType, state) {
    history.push([eventType, state.position])
  }

  yaml.load('a_simple_scalar', { listener: l })

  // 2 open events then 2 close events
  assert.strictEqual(history.length, 4)
  assert.strictEqual(history[0][0], 'open')
  assert.strictEqual(history[1][0], 'open')
  assert.strictEqual(history[2][0], 'close')
  assert.strictEqual(history[3][0], 'close')
  assert.strictEqual(history[0][1], 0)
  assert.strictEqual(history[3][1], 16)
})

it('Listener informed on a map with a list.', function () {
  const history = []
  function l (eventType, state) {
    history.push([eventType, state.position, state.result])
  }

  yaml.load('{ a: 1, b: [ 0, xyz ] }', { listener: l })

  let i = -1
  assert.strictEqual(history[++i][0], 'open') // doc
  assert.strictEqual(history[++i][0], 'open') // map

  assert.strictEqual(history[++i][0], 'open') // key
  assert.strictEqual(history[++i][0], 'close')
  assert.strictEqual(history[i][2], 'a')

  assert.strictEqual(history[++i][0], 'open') // a value
  assert.strictEqual(history[++i][0], 'close')
  assert.strictEqual(history[i][2], 1)

  assert.strictEqual(history[++i][0], 'open') // key
  assert.strictEqual(history[++i][0], 'close')
  assert.strictEqual(history[i][2], 'b')

  assert.strictEqual(history[++i][0], 'open') // b value (list)
  assert.strictEqual(history[++i][0], 'open') // item in list
  assert.strictEqual(history[++i][0], 'close')
  assert.strictEqual(history[i][2], 0)
  assert.strictEqual(history[++i][0], 'open') // item in list
  assert.strictEqual(history[++i][0], 'close')

  assert.strictEqual(history[++i][0], 'close') // b value (list) end
  assert.deepStrictEqual(history[i][2], [0, 'xyz'])

  assert.strictEqual(history[++i][0], 'close') // map end
  assert.strictEqual(history[++i][0], 'close') // doc end

  assert.strictEqual(history.length, ++i)
})
