---@diagnostic disable: duplicate-set-field, need-check-nil, param-type-mismatch, assign-type-mismatch local transcript = require 'bugabinga.pivi.transcript' describe( 'pivi transcript', function() after_each( function() transcript.forget_all() end ) it( 'creates one scratch buffer per name and reuses it', function() local first = transcript.ensure 'work' local again = transcript.ensure 'work' local other = transcript.ensure 'other' assert.equal( first, again ) assert.is_not.equal( first, other ) assert.equal( 'nofile', vim.bo[first].buftype ) assert.equal( transcript.filetype, vim.bo[first].filetype ) end ) it( 'keeps the buffer unmodifiable between writes', function() local bufnr = transcript.ensure 'work' transcript.line( bufnr, 'hello' ) assert.is_false( vim.bo[bufnr].modifiable ) end ) it( 'continues the final line when a chunk has no newline', function() local bufnr = transcript.ensure 'work' transcript.append( bufnr, 'Hel' ) transcript.append( bufnr, 'lo ' ) transcript.append( bufnr, 'world' ) assert.same( { 'Hello world', }, transcript.lines( bufnr ) ) end ) it( 'breaks lines on embedded newlines', function() local bufnr = transcript.ensure 'work' transcript.append( bufnr, 'one\ntwo\nthree' ) assert.same( { 'one', 'two', 'three', }, transcript.lines( bufnr ) ) end ) it( 'appends complete lines', function() local bufnr = transcript.ensure 'work' transcript.line( bufnr, 'first' ) transcript.line( bufnr, 'second' ) assert.same( { 'first', 'second', '', }, transcript.lines( bufnr ) ) end ) it( 'shows the transcript without stealing the current window', function() local before = vim.api.nvim_get_current_win() local bufnr = transcript.ensure 'work' local win = transcript.show( bufnr ) assert.is_number( win ) assert.is_not.equal( before, win ) assert.equal( before, vim.api.nvim_get_current_win() ) assert.equal( bufnr, vim.api.nvim_win_get_buf( win ) ) assert.equal( win, transcript.show( bufnr ) ) vim.api.nvim_win_close( win, true ) end ) it( 'gives its own window an identity without touching other windows', function() local working = vim.api.nvim_get_current_win() local before = vim.wo[working].winbar local bufnr = transcript.ensure 'work' local win = transcript.show( bufnr ) assert.is_true( vim.wo[win].winbar:find( 'work', 1, true ) ~= nil ) assert.is_true( vim.wo[win].wrap ) assert.is_true( vim.wo[win].cursorline ) assert.is_false( vim.wo[win].number ) assert.equal( before, vim.wo[working].winbar ) vim.api.nvim_win_close( win, true ) end ) it( 'forgets and deletes a transcript buffer', function() local bufnr = transcript.ensure 'work' transcript.forget 'work' assert.is_false( vim.api.nvim_buf_is_valid( bufnr ) ) assert.is_not.equal( bufnr, transcript.ensure 'work' ) end ) it( 'keeps a tailing window pinned to the bottom as text streams', function() local bufnr = transcript.ensure 'work' local working = vim.api.nvim_get_current_win() local win = transcript.show( bufnr ) vim.api.nvim_set_current_win( win ) for index = 1, 50 do transcript.line( bufnr, 'line ' .. index ) end local last = vim.api.nvim_buf_line_count( bufnr ) assert.equal( last, vim.api.nvim_win_get_cursor( win )[1] ) transcript.line( bufnr, 'one more' ) assert.equal( vim.api.nvim_buf_line_count( bufnr ), vim.api.nvim_win_get_cursor( win )[1] ) vim.api.nvim_set_current_win( working ) vim.api.nvim_win_close( win, true ) end ) it( 'leaves a reader who scrolled up where they are while text streams', function() local bufnr = transcript.ensure 'work' local working = vim.api.nvim_get_current_win() local win = transcript.show( bufnr ) for index = 1, 50 do transcript.line( bufnr, 'line ' .. index ) end vim.api.nvim_win_set_cursor( win, { 5, 0, } ) transcript.line( bufnr, 'appended while reading' ) assert.equal( 5, vim.api.nvim_win_get_cursor( win )[1] ) assert.equal( working, vim.api.nvim_get_current_win() ) transcript.replace( bufnr, 0, 1, { 'rewritten first line', } ) assert.equal( 5, vim.api.nvim_win_get_cursor( win )[1] ) vim.api.nvim_win_close( win, true ) end ) end )