---@diagnostic disable: duplicate-set-field, need-check-nil, param-type-mismatch, assign-type-mismatch local fold = require 'bugabinga.pivi.fold' 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 describe( 'pivi fold', function() local bufnr before_each( function() bufnr = transcript.ensure 'folding' transcript.clear( bufnr ) end ) after_each( function() transcript.forget_all() fake.close_extra_windows() end ) it( 'gives every multi-line entry its own fold', function() local tool = block( bufnr, 'tool', { tool = 'read', }, { 'read a.lua', 'one', 'two', } ) local first, last = entry.range( bufnr, tool ) local win = transcript.show( bufnr ) local rows = vim.api.nvim_win_call( win, function() local found = {} for row = first, last do vim.v.lnum = row table.insert( found, fold.expr() ) end return found end ) assert.same( { '>1', '1', '1', }, rows ) end ) it( 'leaves a single-line entry unfolded', function() block( bufnr, 'tool', { tool = 'read', }, { 'read a.lua', } ) local win = transcript.show( bufnr ) local level = vim.api.nvim_win_call( win, function() vim.v.lnum = 1 return fold.expr() end ) assert.equal( '0', level ) end ) it( 'treats only voluminous tool entries as collapsible on arrival', function() assert.is_true( fold.voluminous { kind = 'tool', first = 1, last = 4, } ) assert.is_false( fold.voluminous { kind = 'tool', first = 1, last = 1, } ) assert.is_false( fold.voluminous { kind = 'answer', first = 1, last = 9, } ) assert.is_false( fold.voluminous { kind = 'error', first = 1, last = 4, } ) end ) it( 'collapses a tool result on arrival and keeps the answer open', function() local tool = block( bufnr, 'tool', { tool = 'read', }, { 'read a.lua', 'one', 'two', } ) local answer = block( bufnr, 'answer', {}, { 'first', 'second', 'third', } ) local win = transcript.show( bufnr ) fold.apply( bufnr ) local tool_first = select( 1, entry.range( bufnr, tool ) ) local answer_first = select( 1, entry.range( bufnr, answer ) ) vim.api.nvim_win_call( win, function() assert.is_true( fold.closed( tool_first ) ) assert.is_false( fold.closed( answer_first ) ) end ) end ) it( 'stands in for a collapsed entry with one readable line', function() local tool = block( bufnr, 'tool', { tool = 'read', }, { 'read a.lua', 'one', 'two', } ) local first, last = entry.range( bufnr, tool ) local win = transcript.show( bufnr ) local shown = vim.api.nvim_win_call( win, function() vim.v.foldstart = first vim.v.foldend = last return fold.text() end ) assert.is_true( shown:find( 'read a.lua', 1, true ) ~= nil ) assert.is_true( shown:find( '2 more lines', 1, true ) ~= nil ) end ) it( 'expands a collapsed entry and never collapses it again by itself', function() local tool = block( bufnr, 'tool', { tool = 'read', }, { 'read a.lua', 'one', 'two', } ) local win = transcript.show( bufnr ) fold.apply( bufnr ) local first = select( 1, entry.range( bufnr, tool ) ) vim.api.nvim_win_call( win, function() assert.is_true( fold.toggle( bufnr, first ) ) assert.is_false( fold.closed( first ) ) end ) fold.apply( bufnr ) vim.api.nvim_win_call( win, function() assert.is_false( fold.closed( first ) ) end ) end ) it( 'never alters transcript text when an entry is toggled', function() local tool = block( bufnr, 'tool', { tool = 'read', }, { 'read a.lua', 'one', 'two', } ) local win = transcript.show( bufnr ) local before = transcript.lines( bufnr ) local first = select( 1, entry.range( bufnr, tool ) ) vim.api.nvim_win_call( win, function() fold.toggle( bufnr, first ) fold.toggle( bufnr, first ) end ) assert.same( before, transcript.lines( bufnr ) ) assert.equal( 'tool', entry.at( bufnr, first ).kind ) end ) it( 'publishes collapsing as an intent and claims no key for it', function() local published for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( bufnr, 'n' ) ) do if mapping.lhs == '(pivi-toggle-entry)' then published = mapping end end assert.is_table( published ) end ) end )