---@diagnostic disable: duplicate-set-field, need-check-nil, param-type-mismatch, assign-type-mismatch local entry = require 'bugabinga.pivi.entry' local transcript = require 'bugabinga.pivi.transcript' describe( 'pivi entry', function() local bufnr before_each( function() bufnr = transcript.ensure 'entries' transcript.clear( bufnr ) end ) after_each( function() transcript.forget_all() end ) it( 'refuses an unknown kind', function() assert.has_error( function() entry.open( bufnr, 'nonsense' ) end ) end ) it( 'keeps its bounds while it grows', function() local id = entry.open( bufnr, 'answer' ) entry.line( bufnr, id, 'first' ) local first = select( 1, entry.range( bufnr, id ) ) entry.line( bufnr, id, 'second' ) entry.line( bufnr, id, 'third' ) local opened, last = entry.range( bufnr, id ) assert.equal( first, opened ) assert.equal( first + 2, last ) end ) it( 'keeps its bounds while later entries are appended', function() local answer = entry.open( bufnr, 'answer' ) entry.line( bufnr, answer, 'the answer' ) local first, last = entry.range( bufnr, answer ) for index = 1, 5 do local tool = entry.open( bufnr, 'tool', { tool = 'read', } ) entry.line( bufnr, tool, 'tool ' .. index ) entry.close( bufnr, tool ) end assert.same( { first, last, }, { entry.range( bufnr, answer ) } ) assert.equal( 'the answer', transcript.lines( bufnr )[first] ) end ) it( 'begins every entry on its own line', function() local prompt = entry.open( bufnr, 'prompt' ) entry.line( bufnr, prompt, 'question' ) local answer = entry.open( bufnr, 'answer' ) entry.line( bufnr, answer, 'reply' ) local _, prompt_last = entry.range( bufnr, prompt ) local answer_first = select( 1, entry.range( bufnr, answer ) ) assert.is_true( answer_first > prompt_last ) end ) it( 'lists entries in buffer order with their kind and metadata', function() local prompt = entry.open( bufnr, 'prompt', { text = 'do it', } ) entry.line( bufnr, prompt, 'do it' ) local tool = entry.open( bufnr, 'tool', { tool = 'edit', } ) entry.line( bufnr, tool, 'edit greet.lua' ) local listed = entry.list( bufnr ) assert.equal( 2, #listed ) assert.equal( 'prompt', listed[1].kind ) assert.equal( 'do it', listed[1].meta.text ) assert.equal( 'tool', listed[2].kind ) assert.equal( 'edit', listed[2].meta.tool ) assert.is_true( listed[1].first < listed[2].first ) end ) it( 'finds the entry covering a line', function() local answer = entry.open( bufnr, 'answer' ) entry.line( bufnr, answer, 'one' ) entry.line( bufnr, answer, 'two' ) local tool = entry.open( bufnr, 'tool', { tool = 'read', } ) entry.line( bufnr, tool, 'read a.lua' ) local first, last = entry.range( bufnr, answer ) assert.equal( answer, entry.at( bufnr, first ).id ) assert.equal( answer, entry.at( bufnr, last ).id ) assert.equal( tool, entry.at( bufnr, select( 1, entry.range( bufnr, tool ) ) ).id ) end ) it( 'reports no entry outside every range', function() assert.is_nil( entry.at( bufnr, 1 ) ) end ) it( 'annotates an entry without changing its text', function() local tool = entry.open( bufnr, 'tool', { tool = 'read', } ) entry.line( bufnr, tool, 'read a.lua' ) local before = transcript.lines( bufnr ) entry.annotate( bufnr, tool, { result = 'contents', failed = false, } ) assert.equal( 'contents', entry.meta( bufnr, tool ).result ) assert.equal( 'read', entry.meta( bufnr, tool ).tool ) assert.same( before, transcript.lines( bufnr ) ) end ) it( 'forgets its entries when the transcript is cleared', function() local answer = entry.open( bufnr, 'answer' ) entry.line( bufnr, answer, 'gone soon' ) assert.equal( 1, #entry.list( bufnr ) ) transcript.clear( bufnr ) assert.same( {}, entry.list( bufnr ) ) end ) end )