Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/tests/bugabinga/pivi/editor/transcript_spec.lua

Raw
---@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_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 )
end )