Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/tests/bugabinga/pivi/jsonl_spec.lua

Raw
---@diagnostic disable: duplicate-set-field, need-check-nil, param-type-mismatch, assign-type-mismatch
local jsonl = require 'bugabinga.pivi.jsonl'

local FIXTURES = {
  'tests/fixtures/pivi/rpc-no-llm.jsonl',
  'tests/fixtures/pivi/rpc-provider-error.jsonl',
}

local read_fixture = function( path )
  return table.concat( vim.fn.readfile( path ), '\n' ) .. '\n'
end

describe( 'pivi jsonl', function()
  it( 'splits records on line feed only', function()
    local splitter = jsonl.splitter()
    local records = splitter.feed '{"a":1}\n{"b":2}\n'
    assert.same( { '{"a":1}', '{"b":2}', }, records )
  end )

  it( 'strips a trailing carriage return', function()
    local splitter = jsonl.splitter()
    local records = splitter.feed '{"a":1}\r\n'
    assert.same( { '{"a":1}', }, records )
  end )

  it( 'never splits on unicode line separators inside strings', function()
    local splitter = jsonl.splitter()
    local payload = '{"text":"before\u{2028}after\u{2029}end"}'
    local records = splitter.feed( payload .. '\n' )
    assert.equal( 1, #records )
    local value = jsonl.parse( records[1] )
    assert.equal( 'before\u{2028}after\u{2029}end', value.text )
  end )

  it( 'buffers an unterminated record until its line feed arrives', function()
    local splitter = jsonl.splitter()
    assert.same( {}, splitter.feed '{"a"' )
    assert.same( {}, splitter.feed ':1}' )
    assert.same( { '{"a":1}', }, splitter.feed '\n' )
    assert.equal( '', splitter.buffered() )
  end )

  it( 'returns the unterminated remainder on finish', function()
    local splitter = jsonl.splitter()
    splitter.feed '{"a":1}'
    assert.same( { '{"a":1}', }, splitter.finish() )
    assert.same( {}, splitter.finish() )
  end )

  it( 'ignores empty records and nil chunks', function()
    local splitter = jsonl.splitter()
    assert.same( {}, splitter.feed( nil ) )
    assert.same( { '{"a":1}', }, splitter.feed '\n\n{"a":1}\n' )
  end )

  it( 'reports a decode failure instead of throwing', function()
    local value, failure = jsonl.parse 'not json'
    assert.is_nil( value )
    assert.is_string( failure )
  end )

  it( 'rejects records that are not JSON objects', function()
    local value, failure = jsonl.parse '42'
    assert.is_nil( value )
    assert.equal( 'record is not a JSON object', failure )
  end )

  for _, fixture in ipairs( FIXTURES ) do
    it( 'recovers the same records under random chunk boundaries: ' .. fixture, function()
      local payload = read_fixture( fixture )

      local whole = jsonl.splitter()
      local expected = whole.feed( payload )
      assert.is_true( #expected > 0 )

      math.randomseed( 42 )
      for _ = 1, 40 do
        local splitter = jsonl.splitter()
        local records = {}
        local index = 1
        while index <= #payload do
          local size = math.random( 1, 97 )
          local chunk = payload:sub( index, index + size - 1 )
          index = index + size
          for _, record in ipairs( splitter.feed( chunk ) ) do
            table.insert( records, record )
          end
        end
        for _, record in ipairs( splitter.finish() ) do
          table.insert( records, record )
        end
        assert.same( expected, records )
      end
    end )
  end

  it( 'parses every record of a recorded pi run', function()
    local splitter = jsonl.splitter()
    for _, record in ipairs( splitter.feed( read_fixture( FIXTURES[1] ) ) ) do
      local value, failure = jsonl.parse( record )
      assert.is_nil( failure )
      assert.equal( 'response', value.type )
    end
  end )
end )