Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/tests/bugabinga/pivi/layout_spec.lua

Raw
---@diagnostic disable: duplicate-set-field, need-check-nil, param-type-mismatch, assign-type-mismatch, redundant-parameter
local layout = require 'bugabinga.pivi.layout'

-- The editor size a headless test runs at is fixed, so the decision is tested
-- as a decision and the windows it produces are verified in a real terminal.
local at = function( columns, lines ) return layout.plan( columns, lines ) end

describe( 'pivi layout', function()
  local original

  before_each( function()
    original = vim.g.pivi_layout
  end )

  after_each( function()
    vim.g.pivi_layout = original
  end )

  describe( 'where the transcript goes', function()
    it( 'puts it beside the source on a wide editor', function()
      local chosen = at( 192, 43 )

      assert.equal( 'beside', chosen.orientation )
      assert.is_number( chosen.width )
      assert.is_nil( chosen.height )
    end )

    it( 'stacks it under the source when the source would be squeezed', function()
      local chosen = at( 100, 30 )

      assert.equal( 'below', chosen.orientation )
      assert.is_number( chosen.height )
      assert.is_nil( chosen.width )
    end )

    it( 'stacks it on a narrow editor rather than splitting into columns', function()
      assert.equal( 'below', at( 80, 24 ).orientation )
      assert.equal( 'below', at( 60, 20 ).orientation )
    end )

    it( 'switches to beside exactly where the source still fits', function()
      local settings = layout.settings()
      local edge = settings.transcript_min + settings.source_min + 1

      assert.equal( 'below', at( edge - 1, 40 ).orientation )
      assert.equal( 'beside', at( edge, 40 ).orientation )
    end )
  end )

  describe( 'how wide the transcript is', function()
    it( 'keeps prose readable rather than proportional on a narrow editor', function()
      local settings = layout.settings()
      assert.equal( settings.transcript_min, at( 120, 40 ).width )
    end )

    it( 'stops growing once prose is wide enough', function()
      local settings = layout.settings()
      assert.equal( settings.transcript_max, at( 400, 60 ).width )
    end )

    it( 'takes a share in between', function()
      local chosen = at( 192, 43 )
      local settings = layout.settings()

      assert.is_true( chosen.width > settings.transcript_min )
      assert.is_true( chosen.width < settings.transcript_max )
    end )

    it( 'always leaves the source enough room to be code', function()
      local settings = layout.settings()
      for columns = 60, 400, 7 do
        local chosen = at( columns, 40 )
        if chosen.orientation == 'beside' then
          assert.is_true( columns - chosen.width - 1 >= settings.source_min,
            ( 'source squeezed at %d columns' ):format( columns ) )
        end
      end
    end )
  end )

  describe( 'how tall the prompt is', function()
    it( 'grows with the editor, within reason', function()
      local settings = layout.settings()

      assert.equal( settings.prompt_min, at( 200, 12 ).prompt )
      assert.is_true( at( 200, 43 ).prompt > settings.prompt_min )
      assert.equal( settings.prompt_max, at( 200, 200 ).prompt )
    end )

    it( 'never asks for more room than the editor has', function()
      for lines = 6, 60, 3 do
        local chosen = at( 200, lines )
        assert.is_true( chosen.prompt < lines, ( 'prompt too tall at %d lines' ):format( lines ) )
      end
    end )
  end )

  describe( 'stacked height', function()
    it( 'leaves the source visible', function()
      for lines = 10, 60, 5 do
        local chosen = at( 80, lines )
        assert.equal( 'below', chosen.orientation )
        assert.is_true( chosen.height < lines, ( 'transcript too tall at %d lines' ):format( lines ) )
      end
    end )
  end )

  it( 'lets the configuration override every judgement', function()
    vim.g.pivi_layout = { transcript_min = 20, transcript_max = 30, source_min = 10, }

    local chosen = at( 100, 40 )

    assert.equal( 'beside', chosen.orientation )
    assert.equal( 30, chosen.width )
  end )

  it( 'never carves a new window out of one of its own', function()
    local transcript = require 'bugabinga.pivi.transcript'
    local working = vim.api.nvim_get_current_win()

    local bufnr = transcript.ensure 'hosting'
    local shown = transcript.show( bufnr )
    assert.is_number( shown )

    -- With the cursor inside the transcript, the next window still comes out
    -- of the window the user works in.
    vim.api.nvim_set_current_win( shown )
    assert.equal( working, layout.host() )

    vim.api.nvim_set_current_win( working )
    transcript.forget_all()
  end )

  it( 'reports the windows it owns and no others', function()
    vim.cmd 'enew'
    local ordinary = vim.api.nvim_get_current_buf()

    local found = layout.windows()

    for _, win in ipairs( found.transcript ) do
      assert.is_not.equal( ordinary, vim.api.nvim_win_get_buf( win ) )
    end
    for _, win in ipairs( found.prompt ) do
      assert.is_not.equal( ordinary, vim.api.nvim_win_get_buf( win ) )
    end
  end )
end )