---@diagnostic disable: duplicate-set-field, need-check-nil, param-type-mismatch, assign-type-mismatch local detail = require 'bugabinga.pivi.detail' local entry = require 'bugabinga.pivi.entry' local transcript = require 'bugabinga.pivi.transcript' local fake = dofile 'tests/helpers/pivi_fake.lua' local block = function( bufnr, kind, meta, lines ) local id = entry.open( bufnr, kind, meta ) for _, text in ipairs( lines ) do entry.line( bufnr, id, text ) end entry.close( bufnr, id ) return id end local text_of = function( lines ) return table.concat( lines, '\n' ) end describe( 'pivi detail', function() local bufnr, win before_each( function() bufnr = transcript.ensure 'inspecting' transcript.clear( bufnr ) win = transcript.show( bufnr ) vim.api.nvim_set_current_win( win ) end ) after_each( function() transcript.forget_all() fake.close_extra_windows() end ) it( 'describes a tool call with its arguments, outcome and result', function() local tool = block( bufnr, 'tool', { tool = 'read', args = { path = 'a.lua', line = 3, }, cwd = '/work', failed = false, result = { content = { { type = 'text', text = 'file contents', }, }, }, }, { 'read a.lua', 'file contents', } ) local described = text_of( detail.describe( bufnr, entry.at( bufnr, select( 1, entry.range( bufnr, tool ) ) ) ) ) assert.is_true( described:find( 'tool call', 1, true ) ~= nil ) assert.is_true( described:find( 'read', 1, true ) ~= nil ) assert.is_true( described:find( '/work/a.lua:3', 1, true ) ~= nil ) assert.is_true( described:find( 'succeeded', 1, true ) ~= nil ) assert.is_true( described:find( 'file contents', 1, true ) ~= nil ) end ) it( 'reports a failed tool call as failed', function() local tool = block( bufnr, 'tool', { tool = 'edit', failed = true, }, { 'edit a.lua', } ) local described = text_of( detail.describe( bufnr, entry.at( bufnr, select( 1, entry.range( bufnr, tool ) ) ) ) ) assert.is_true( described:find( 'failed', 1, true ) ~= nil ) end ) it( 'shows the text of an answer', function() local answer = block( bufnr, 'answer', {}, { 'a distinctive reply', } ) local described = text_of( detail.describe( bufnr, entry.at( bufnr, select( 1, entry.range( bufnr, answer ) ) ) ) ) assert.is_true( described:find( 'a distinctive reply', 1, true ) ~= nil ) end ) it( 'shows an error message', function() local failure = block( bufnr, 'error', { error = 'overloaded', }, { 'overloaded', } ) local described = text_of( detail.describe( bufnr, entry.at( bufnr, select( 1, entry.range( bufnr, failure ) ) ) ) ) assert.is_true( described:find( 'overloaded', 1, true ) ~= nil ) end ) it( 'keeps a voluminous result readable and points back at the entry', function() local many = {} for index = 1, 200 do table.insert( many, 'line ' .. index ) end local tool = block( bufnr, 'tool', { tool = 'bash', result = { content = { { type = 'text', text = table.concat( many, '\n' ), }, }, }, }, { 'bash', } ) local described = detail.describe( bufnr, entry.at( bufnr, select( 1, entry.range( bufnr, tool ) ) ) ) assert.is_true( #described < 200 ) assert.is_true( text_of( described ):find( 'the entry itself holds the rest', 1, true ) ~= nil ) end ) it( 'inspects without leaving the transcript', function() local tool = block( bufnr, 'tool', { tool = 'read', args = { path = 'a.lua', }, }, { 'read a.lua', } ) local first = select( 1, entry.range( bufnr, tool ) ) vim.api.nvim_win_set_cursor( win, { first, 0, } ) local float, floating = detail.inspect( bufnr, first ) assert.is_number( float ) assert.equal( win, vim.api.nvim_get_current_win() ) assert.equal( first, vim.api.nvim_win_get_cursor( win )[1] ) assert.same( { 'read a.lua', }, vim.list_slice( transcript.lines( bufnr ), first, first ) ) if floating and vim.api.nvim_win_is_valid( floating ) then vim.api.nvim_win_close( floating, true ) end end ) it( 'inspects nothing outside an entry', function() assert.is_nil( detail.inspect( bufnr, 1 ) ) end ) it( 'publishes inspection as an intent', function() local published for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( bufnr, 'n' ) ) do if mapping.lhs == '(pivi-inspect-entry)' then published = mapping end end assert.is_table( published ) end ) end )