Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/tests/bugabinga/statusline_spec.lua

Raw
require('bugabinga.statusline')
local icon = require('std.icon')

describe('statusline', function()
  describe('plugins_component', function()
    before_each(function()
      BugabingaStatusline.reset()
    end)

    after_each(function()
      BugabingaStatusline.reset()
    end)

    it('filters out empty string results from registered components', function()
      BugabingaStatusline.register(function() return 'foo' end)
      BugabingaStatusline.register(function() return '' end)
      BugabingaStatusline.register(function() return 'bar' end)
      BugabingaStatusline.register(function() return '' end)
      local result = BugabingaStatusline.plugins_component()
      assert.equal('foo bar', result)
    end)

    it('returns empty string when all components return empty', function()
      BugabingaStatusline.register(function() return '' end)
      BugabingaStatusline.register(function() return '' end)
      local result = BugabingaStatusline.plugins_component()
      assert.equal('', result)
    end)
  end)

  describe('file_component', function()
    it('names a buffer that has no name instead of the working directory', function()
      vim.cmd 'enew'
      local shown = BugabingaStatusline.render()

      assert.is_true(shown:find('[No Name]', 1, true) ~= nil)
      assert.is_nil(shown:find(vim.fn.fnamemodify(vim.uv.cwd() or '', ':~'), 1, true))
    end)

    it('names a buffer that has one', function()
      vim.cmd 'enew'
      vim.api.nvim_buf_set_name(0, 'pivi://work')

      assert.is_true(BugabingaStatusline.render():find('pivi://work', 1, true) ~= nil)
    end)
  end)

  describe('git_component', function()
    local initial_cwd

    before_each(function()
      initial_cwd = vim.uv.cwd()
      BugabingaStatusline._get_cache().git_branch = {}
    end)

    after_each(function()
      BugabingaStatusline._get_cache().git_branch = {}
    end)

    it('returns empty string on first call (async loading)', function()
      local result = BugabingaStatusline.git_component()
      assert.equal('', result)
    end)

    it('returns cached branch string when available', function()
      local fake_branch = icon.branch .. ' test-branch'
      BugabingaStatusline._get_cache().git_branch[initial_cwd] = fake_branch
      local result = BugabingaStatusline.git_component()
      assert.equal(fake_branch, result)
    end)

    it('DirChanged clears git branch cache', function()
      local fake_branch = icon.branch .. ' test-branch'
      BugabingaStatusline._get_cache().git_branch[initial_cwd] = fake_branch
      assert(vim.tbl_count(BugabingaStatusline._get_cache().git_branch) > 0, 'cache should have entry before')
      vim.api.nvim_exec_autocmds('DirChanged', {})
      assert.equal(0, vim.tbl_count(BugabingaStatusline._get_cache().git_branch), 'cache should be cleared after DirChanged')
    end)
  end)
end)