local tests = {} local function test(name, fn) tests[#tests + 1] = { name = name, fn = fn } end local function fail(message) error(message, 2) end local function assert_equal(expected, actual, context) if expected ~= actual then fail(("%s\nexpected: %s\nactual: %s"):format(context or "values differ", vim.inspect(expected), vim.inspect(actual))) end end local function assert_truthy(value, context) if not value then fail(context or "expected a truthy value") end end local devicons = assert(_G.statusline_test_devicons) local statusline = require("statusline") local mode = require("statusline.components.mode") local cursor = require("statusline.components.cursor") local cwd = require("statusline.components.cwd") local diagnostics = require("statusline.components.diagnostics") local filetype = require("statusline.components.filetype") local lsp = require("statusline.components.lsp") local macro = require("statusline.components.macro") local CTRL_V = string.char(22) local CTRL_S = string.char(19) local function expected_cwd_component(directory) local display = vim.fn.fnamemodify(directory, ":~"):gsub("%%", "%%%%") return ("%%#StatusCwd# %s %%*"):format(display) end local function expected_left(mode_code) return mode.render(mode_code) .. " " .. expected_cwd_component(vim.fn.getcwd(-1, 0)) end local function render_with_current_filetype(value) local buffer = vim.api.nvim_get_current_buf() local original = vim.api.nvim_get_option_value("filetype", { buf = buffer }) vim.api.nvim_set_option_value("filetype", value, { buf = buffer }) local ok, rendered = xpcall(statusline.render, debug.traceback) vim.api.nvim_set_option_value("filetype", original, { buf = buffer }) if not ok then error(rendered) end return rendered end local function with_replaced_field(object, key, replacement, fn) local original = object[key] object[key] = replacement local ok, result = xpcall(fn, debug.traceback) object[key] = original if not ok then error(result) end return result end local function with_mocked_lsp_clients(get_clients, fn) return with_replaced_field(vim.lsp, "get_clients", get_clients, fn) end local function with_mocked_recording(register, fn) return with_replaced_field(vim.fn, "reg_recording", function() return register end, fn) end local function with_mocked_diagnostic_counts(counts, fn) diagnostics.invalidate() local ok, result = xpcall(function() return with_replaced_field(diagnostics, "count", function() return counts end, fn) end, debug.traceback) diagnostics.invalidate() if not ok then error(result) end return result end local function diagnostic_counts(error_count, warn_count, info_count, hint_count) return { [vim.diagnostic.severity.ERROR] = error_count, [vim.diagnostic.severity.WARN] = warn_count, [vim.diagnostic.severity.INFO] = info_count, [vim.diagnostic.severity.HINT] = hint_count, } end local diagnostic_fixture_id = 0 local function with_workspace_diagnostics(diagnostic_lists, fn) diagnostic_fixture_id = diagnostic_fixture_id + 1 local buffer = vim.api.nvim_create_buf(false, true) local name = vim.fs.joinpath( vim.fn.getcwd(-1, 0), ".statusline-diagnostic-fixture-" .. diagnostic_fixture_id .. ".lua" ) vim.api.nvim_buf_set_name(buffer, name) local namespaces = {} for index, list in ipairs(diagnostic_lists) do local namespace = vim.api.nvim_create_namespace( ("statusline-diagnostic-fixture-%d-%d"):format(diagnostic_fixture_id, index) ) namespaces[#namespaces + 1] = namespace vim.diagnostic.set(namespace, buffer, list) end local ok, result = xpcall(fn, debug.traceback) for _, namespace in ipairs(namespaces) do vim.diagnostic.reset(namespace, buffer) end vim.api.nvim_buf_delete(buffer, { force = true }) if not ok then error(result) end return result end test("rejects Neovim versions older than 0.12", function() local original_version = vim.version vim.version = function() return { major = 0, minor = 11, patch = 9 } end local ok, err = pcall(statusline.setup) vim.version = original_version assert_equal(false, ok, "setup should reject Neovim 0.11") assert_truthy(tostring(err):find("requires Neovim 0.12 or newer", 1, true), "version error should state the requirement") assert_truthy(tostring(err):find("found 0.11.9", 1, true), "version error should include the detected version") end) test("resolves every documented Neovim 0.12 mode code", function() local cases = { { "n", "NORMAL", "StatusModeNormal" }, { "no", "OPERATOR", "StatusModeOperatorPending" }, { "nov", "OPERATOR", "StatusModeOperatorPending" }, { "noV", "OPERATOR", "StatusModeOperatorPending" }, { "no" .. CTRL_V, "OPERATOR", "StatusModeOperatorPending" }, { "niI", "NORMAL", "StatusModeNormal" }, { "niR", "NORMAL", "StatusModeNormal" }, { "niV", "NORMAL", "StatusModeNormal" }, { "nt", "T-NORMAL", "StatusModeTerminalNormal" }, { "ntT", "T-NORMAL", "StatusModeTerminalNormal" }, { "v", "VISUAL", "StatusModeVisual" }, { "vs", "VISUAL", "StatusModeVisual" }, { "V", "V-LINE", "StatusModeVisualLine" }, { "Vs", "V-LINE", "StatusModeVisualLine" }, { CTRL_V, "V-BLOCK", "StatusModeVisualBlock" }, { CTRL_V .. "s", "V-BLOCK", "StatusModeVisualBlock" }, { "s", "SELECT", "StatusModeSelect" }, { "S", "S-LINE", "StatusModeSelectLine" }, { CTRL_S, "S-BLOCK", "StatusModeSelectBlock" }, { "i", "INSERT", "StatusModeInsert" }, { "ic", "INSERT", "StatusModeInsert" }, { "ix", "INSERT", "StatusModeInsert" }, { "R", "REPLACE", "StatusModeReplace" }, { "Rc", "REPLACE", "StatusModeReplace" }, { "Rx", "REPLACE", "StatusModeReplace" }, { "Rv", "V-REPLACE", "StatusModeVirtualReplace" }, { "Rvc", "V-REPLACE", "StatusModeVirtualReplace" }, { "Rvx", "V-REPLACE", "StatusModeVirtualReplace" }, { "c", "COMMAND", "StatusModeCommand" }, { "cr", "COMMAND", "StatusModeCommand" }, { "cv", "EX", "StatusModeEx" }, { "cvr", "EX", "StatusModeEx" }, { "r", "PROMPT", "StatusModePrompt" }, { "rm", "MORE", "StatusModeMore" }, { "r?", "CONFIRM", "StatusModeConfirm" }, { "!", "SHELL", "StatusModeShell" }, { "t", "TERMINAL", "StatusModeTerminal" }, } for _, case in ipairs(cases) do local label, highlight = mode.resolve(case[1]) assert_equal(case[2], label, "wrong label for mode " .. vim.inspect(case[1])) assert_equal(case[3], highlight, "wrong highlight for mode " .. vim.inspect(case[1])) end end) test("falls back for unrecognized future modes", function() local label, highlight = mode.resolve("n-future") assert_equal("UNKNOWN", label) assert_equal("StatusModeUnknown", highlight) end) test("applies one semantic mode text override to every alias without changing highlights", function() statusline.setup({ mode_text = { normal = "N" } }) for _, mode_code in ipairs({ "n", "niI", "niR", "niV" }) do local label, highlight = mode.resolve(mode_code) assert_equal("N", label, "the normal override should apply to " .. mode_code) assert_equal("StatusModeNormal", highlight, "the normal override should not change highlights") end statusline.setup() end) test("uses partial mode text overrides in complete output with default fallbacks", function() statusline.setup({ mode_text = { normal = "N" } }) local fallback_label, fallback_highlight = mode.resolve("i") local complete = render_with_current_filetype("") statusline.setup() assert_equal("INSERT", fallback_label, "unconfigured semantic modes should retain their default text") assert_equal("StatusModeInsert", fallback_highlight) assert_equal( "%#StatusModeNormal# N %* " .. expected_cwd_component(vim.fn.getcwd(-1, 0)) .. "%=" .. cursor.render(), complete, "the configured text should appear in the complete statusline" ) end) test("escapes percent characters in mode text and accepts empty text", function() statusline.setup({ mode_text = { visual = "100%", insert = "", }, }) local visual = mode.render("v") local insert = mode.render("i") statusline.setup() assert_equal("%#StatusModeVisual# 100%% %*", visual) assert_equal("%#StatusModeInsert# %*", insert, "an empty label should retain the mode box padding") end) test("copies, replaces, and clears configured mode text", function() local text = { normal = "FIRST", insert = "OLD-INSERT", } statusline.setup({ mode_text = text }) text.normal = "MUTATED" local copied = mode.render("n") statusline.setup({ mode_text = { insert = "NEW-INSERT" } }) local removed_by_replacement = mode.render("n") local replacement = mode.render("i") statusline.setup({ lsp_exclude = {} }) local removed_by_omission = mode.render("i") statusline.setup({ mode_text = { normal = "AGAIN" } }) statusline.setup() local removed_by_clear = mode.render("n") assert_equal("%#StatusModeNormal# FIRST %*", copied, "setup should copy the caller's mode text map") assert_equal( "%#StatusModeNormal# NORMAL %*", removed_by_replacement, "a later mode text map should replace the prior map" ) assert_equal("%#StatusModeInsert# NEW-INSERT %*", replacement) assert_equal( "%#StatusModeInsert# INSERT %*", removed_by_omission, "omitting mode_text should restore default labels" ) assert_equal("%#StatusModeNormal# NORMAL %*", removed_by_clear, "setup() should clear mode text overrides") end) test("renders the working directory with padding and home contraction", function() local directory = vim.fs.joinpath(vim.fn.expand("~"), "statusline.nvim-cwd-test") assert_equal("%#StatusCwd# ~/statusline.nvim-cwd-test %*", cwd.render(directory)) end) test("escapes percent characters in the working directory", function() local directory = vim.fs.joinpath(vim.fn.expand("~"), "statusline.nvim-%-cwd-test") assert_equal("%#StatusCwd# ~/statusline.nvim-%%-cwd-test %*", cwd.render(directory)) end) test("renders diagnostic severity boxes in fixed order with exact padding and highlights", function() local rendered = with_mocked_diagnostic_counts(diagnostic_counts(2, 3, 4, 5), function() return diagnostics.render("/workspace") end) assert_equal( "%#StatusDiagnosticError# 󰅚 2 %* %#StatusDiagnosticWarn# 󰀪 3 %* %#StatusDiagnosticInfo# 󰋽 4 %* %#StatusDiagnosticHint# 󰌶 5 %*", rendered ) end) test("omits individual zero-count diagnostic severity boxes", function() local rendered = with_mocked_diagnostic_counts(diagnostic_counts(0, 7, 0, 1), function() return diagnostics.render("/workspace") end) assert_equal("%#StatusDiagnosticWarn# 󰀪 7 %* %#StatusDiagnosticHint# 󰌶 1 %*", rendered) end) test("omits the complete diagnostics component when every count is zero", function() local rendered = with_mocked_diagnostic_counts(diagnostic_counts(0, 0, 0, 0), function() return diagnostics.render("/workspace") end) assert_equal(nil, rendered) end) test("caches populated and empty rendered diagnostics for one exact directory", function() local calls = {} diagnostics.invalidate() with_replaced_field(diagnostics, "count", function(directory) calls[#calls + 1] = directory if directory == "/workspace" then return diagnostic_counts(2, 0, 0, 0) end return diagnostic_counts(0, 0, 0, 0) end, function() local populated = diagnostics.render("/workspace") assert_equal(populated, diagnostics.render("/workspace"), "a populated result should be reused") assert_equal(1, #calls, "a populated result should be counted once") assert_equal(nil, diagnostics.render("/empty")) assert_equal(nil, diagnostics.render("/empty"), "an empty result should also be reused") assert_equal(2, #calls, "an empty result should be counted once") end) diagnostics.invalidate() end) test("directory changes and explicit invalidation force diagnostic recounts", function() local calls = {} diagnostics.invalidate() with_replaced_field(diagnostics, "count", function(directory) calls[#calls + 1] = directory return diagnostic_counts(#calls, 0, 0, 0) end, function() local first = diagnostics.render("/workspace") local second = diagnostics.render("/workspace/.") local third = diagnostics.render("/workspace") diagnostics.invalidate() local invalidated = diagnostics.render("/workspace") assert_truthy(first:find(" 1 ", 1, true), "the first directory should be counted") assert_truthy(second:find(" 2 ", 1, true), "the exact directory key should select the cache") assert_truthy(third:find(" 3 ", 1, true), "the single-entry cache should replace prior directories") assert_truthy(invalidated:find(" 4 ", 1, true), "explicit invalidation should force a recount") assert_equal(4, #calls) assert_equal("/workspace", calls[1]) assert_equal("/workspace/.", calls[2]) assert_equal("/workspace", calls[3]) assert_equal("/workspace", calls[4]) end) diagnostics.invalidate() end) test("aggregates diagnostics only from normalized named workspace buffers", function() local root = vim.fn.tempname() local workspace = vim.fs.joinpath(root, "workspace") local buffers = { [1] = workspace, [2] = vim.fs.joinpath(workspace, "nested", "..", "nested", "file.lua"), [3] = workspace .. "-sibling/file.lua", [4] = vim.fs.joinpath(root, "outside", "file.lua"), [5] = "", [6] = vim.fs.joinpath(workspace, "invalid.lua"), [7] = vim.fs.joinpath(workspace, "second.lua"), } local duplicate = { lnum = 0, col = 0, message = "duplicate", severity = vim.diagnostic.severity.ERROR, source = "source-one", namespace = 11, } local diagnostics_by_buffer = { [1] = { duplicate, vim.deepcopy(duplicate) }, [2] = { { severity = vim.diagnostic.severity.WARN, source = "source-two", namespace = 22 }, { severity = vim.diagnostic.severity.INFO, source = "source-two", namespace = 22 }, }, [3] = { { severity = vim.diagnostic.severity.HINT } }, [4] = { { severity = vim.diagnostic.severity.HINT } }, [5] = { { severity = vim.diagnostic.severity.HINT } }, [6] = { { severity = vim.diagnostic.severity.HINT } }, [7] = { { severity = vim.diagnostic.severity.ERROR, source = "source-three", namespace = 33 }, { severity = vim.diagnostic.severity.WARN, source = "source-four", namespace = 44 }, { severity = vim.diagnostic.severity.HINT, source = "source-four", namespace = 44 }, }, } local original_list_bufs = vim.api.nvim_list_bufs local original_buf_is_valid = vim.api.nvim_buf_is_valid local original_buf_get_name = vim.api.nvim_buf_get_name local original_diagnostic_count = vim.diagnostic.count local queried_buffers = {} vim.api.nvim_list_bufs = function() return { 1, 2, 3, 4, 5, 6, 7 } end vim.api.nvim_buf_is_valid = function(buffer) return buffer ~= 6 end vim.api.nvim_buf_get_name = function(buffer) if buffer == 6 then error("invalid buffers must not be queried") end return buffers[buffer] end vim.diagnostic.count = function(...) local call = { n = select("#", ...), ... } queried_buffers[#queried_buffers + 1] = call local counts = {} for _, diagnostic in ipairs(diagnostics_by_buffer[call[1]]) do counts[diagnostic.severity] = (counts[diagnostic.severity] or 0) + 1 end return counts end local ok, counts = xpcall(function() return diagnostics.count(vim.fs.joinpath(workspace, "directory", "..")) end, debug.traceback) vim.api.nvim_list_bufs = original_list_bufs vim.api.nvim_buf_is_valid = original_buf_is_valid vim.api.nvim_buf_get_name = original_buf_get_name vim.diagnostic.count = original_diagnostic_count if not ok then error(counts) end assert_equal(3, counts[vim.diagnostic.severity.ERROR], "duplicates and multiple buffers should aggregate") assert_equal(2, counts[vim.diagnostic.severity.WARN], "multiple diagnostic sources should aggregate") assert_equal(1, counts[vim.diagnostic.severity.INFO]) assert_equal(1, counts[vim.diagnostic.severity.HINT]) assert_equal(3, #queried_buffers, "only the workspace itself and nested named buffers should be queried") assert_equal(1, queried_buffers[1][1]) assert_equal(2, queried_buffers[2][1]) assert_equal(7, queried_buffers[3][1]) for _, call in ipairs(queried_buffers) do assert_equal(1, call.n, "diagnostic count queries should not restrict sources, namespaces, or severities") end end) test("caches normalized diagnostic paths and refreshes changed or invalidated buffers", function() local buffer = 99999 local workspace = vim.fs.joinpath(vim.fn.tempname(), "workspace") local first_name = vim.fs.joinpath(workspace, "nested", "..", "first.lua") local second_name = vim.fs.joinpath(workspace, "nested", "..", "second.lua") local current_name = first_name local original_list_bufs = vim.api.nvim_list_bufs local original_buf_is_valid = vim.api.nvim_buf_is_valid local original_buf_get_name = vim.api.nvim_buf_get_name local original_diagnostic_count = vim.diagnostic.count local original_normalize = vim.fs.normalize local normalize_calls = {} diagnostics.invalidate_buffer(buffer) vim.api.nvim_list_bufs = function() return { buffer } end vim.api.nvim_buf_is_valid = function() return true end vim.api.nvim_buf_get_name = function() return current_name end vim.diagnostic.count = function() return {} end vim.fs.normalize = function(path) normalize_calls[path] = (normalize_calls[path] or 0) + 1 return original_normalize(path) end local ok, err = xpcall(function() diagnostics.count(workspace) diagnostics.count(workspace) assert_equal(1, normalize_calls[workspace], "an unchanged workspace should be normalized once") assert_equal(1, normalize_calls[first_name], "an unchanged buffer name should be normalized once") current_name = second_name diagnostics.count(workspace) assert_equal(1, normalize_calls[second_name], "renaming a buffer should refresh its normalized path") diagnostics.invalidate_buffer(buffer) diagnostics.count(workspace) assert_equal(2, normalize_calls[second_name], "invalidating a buffer should discard its normalized path") end, debug.traceback) vim.api.nvim_list_bufs = original_list_bufs vim.api.nvim_buf_is_valid = original_buf_is_valid vim.api.nvim_buf_get_name = original_buf_get_name vim.diagnostic.count = original_diagnostic_count vim.fs.normalize = original_normalize diagnostics.invalidate_buffer(buffer) if not ok then error(err) end end) test("renaming a diagnostic buffer updates cached workspace membership", function() statusline.setup() local root = vim.fn.tempname() local workspace = vim.fs.joinpath(root, "workspace") local outside = vim.fs.joinpath(root, "outside") vim.fn.mkdir(workspace, "p") vim.fn.mkdir(outside, "p") workspace = assert(vim.uv.fs_realpath(workspace)) outside = assert(vim.uv.fs_realpath(outside)) local namespace = vim.api.nvim_create_namespace("statusline-diagnostic-rename") local buffer = vim.api.nvim_create_buf(false, true) vim.api.nvim_buf_set_name(buffer, vim.fs.joinpath(outside, "before.lua")) vim.diagnostic.set(namespace, buffer, { { lnum = 0, col = 0, message = "renamed diagnostic", severity = vim.diagnostic.severity.ERROR }, }) local ok, err = xpcall(function() diagnostics.invalidate() assert_equal(nil, diagnostics.render(workspace), "an outside buffer should not be counted") vim.api.nvim_buf_call(buffer, function() vim.cmd("file " .. vim.fn.fnameescape(vim.fs.joinpath(workspace, "inside.lua"))) end) assert_equal( "%#StatusDiagnosticError# 󰅚 1 %*", diagnostics.render(workspace), "BufFilePost should include a buffer renamed into the workspace" ) vim.api.nvim_buf_call(buffer, function() vim.cmd("file " .. vim.fn.fnameescape(vim.fs.joinpath(outside, "after.lua"))) end) assert_equal( nil, diagnostics.render(workspace), "BufFilePost should exclude a buffer renamed out of the workspace" ) end, debug.traceback) vim.diagnostic.reset(namespace, buffer) if vim.api.nvim_buf_is_valid(buffer) then vim.api.nvim_buf_delete(buffer, { force = true }) end vim.fn.delete(root, "rf") diagnostics.invalidate() vim.wait(10) if not ok then error(err) end end) test("omits the macro box when no macro is being recorded", function() assert_equal(nil, with_mocked_recording("", macro.render)) end) test("renders the exact macro register with outer padding", function() assert_equal("%#StatusMacro# RECORDING @q %*", with_mocked_recording("q", macro.render)) end) test("escapes percent characters in the recording register", function() assert_equal("%#StatusMacro# RECORDING @%% %*", with_mocked_recording("%", macro.render)) end) test("uses the tab-local working directory instead of a window-local directory", function() local root = vim.fn.tempname() local tab_directory = vim.fs.joinpath(root, "tab") local window_directory = vim.fs.joinpath(root, "window") vim.fn.mkdir(tab_directory, "p") vim.fn.mkdir(window_directory, "p") tab_directory = assert(vim.uv.fs_realpath(tab_directory)) window_directory = assert(vim.uv.fs_realpath(window_directory)) local original_tab = vim.api.nvim_get_current_tabpage() vim.cmd("tabnew") local test_tab = vim.api.nvim_get_current_tabpage() local tab_buffer local window_buffer local ok, rendered = xpcall(function() vim.cmd("tcd " .. vim.fn.fnameescape(tab_directory)) vim.cmd("lcd " .. vim.fn.fnameescape(window_directory)) assert_equal(window_directory, vim.fn.getcwd(0, 0), "the test window should have its own local cwd") assert_equal(tab_directory, vim.fn.getcwd(-1, 0), "the tab-local cwd should ignore the window-local cwd") local namespace = vim.api.nvim_create_namespace("statusline-tab-cwd-diagnostics") tab_buffer = vim.api.nvim_create_buf(false, true) vim.api.nvim_buf_set_name(tab_buffer, vim.fs.joinpath(tab_directory, "nested.lua")) vim.diagnostic.set(namespace, tab_buffer, { { lnum = 0, col = 0, message = "tab diagnostic", severity = vim.diagnostic.severity.ERROR }, }) window_buffer = vim.api.nvim_create_buf(false, true) vim.api.nvim_buf_set_name(window_buffer, vim.fs.joinpath(window_directory, "window.lua")) vim.diagnostic.set(namespace, window_buffer, { { lnum = 0, col = 0, message = "window diagnostic", severity = vim.diagnostic.severity.HINT }, }) return statusline.render() end, debug.traceback) if tab_buffer and vim.api.nvim_buf_is_valid(tab_buffer) then vim.api.nvim_buf_delete(tab_buffer, { force = true }) end if window_buffer and vim.api.nvim_buf_is_valid(window_buffer) then vim.api.nvim_buf_delete(window_buffer, { force = true }) end if vim.api.nvim_tabpage_is_valid(test_tab) then vim.api.nvim_set_current_tabpage(test_tab) vim.cmd("tabclose!") end if vim.api.nvim_tabpage_is_valid(original_tab) then vim.api.nvim_set_current_tabpage(original_tab) end vim.fn.delete(root, "rf") if not ok then error(rendered) end assert_equal( mode.render("n") .. " " .. expected_cwd_component(tab_directory) .. " %#StatusDiagnosticError# 󰅚 1 %*%=" .. cursor.render(), rendered, "the complete statusline and diagnostics should use the tab-local cwd" ) end) test("renders filetypes with an icon and calls the required devicons API", function() devicons.calls = {} assert_equal("%#StatusFiletype#  lua %*", filetype.render("lua")) assert_equal(1, #devicons.calls, "the icon lookup should happen once") assert_equal(1, devicons.calls[1].n, "get_icon_by_filetype should receive no default-icon option") assert_equal("lua", devicons.calls[1][1], "the exact filetype should be passed to devicons") end) test("renders filetypes without an icon and omits an empty filetype", function() devicons.calls = {} assert_equal("%#StatusFiletype# textonly %*", filetype.render("textonly")) assert_equal(1, #devicons.calls, "a missing icon mapping should still perform the lookup") assert_equal("textonly", devicons.calls[1][1]) assert_equal(nil, filetype.render("")) assert_equal(1, #devicons.calls, "an empty filetype should not perform an icon lookup") end) test("escapes percent characters in dynamic filetype content", function() devicons.icons["percent%type"] = "%icon" local rendered = filetype.render("percent%type") devicons.icons["percent%type"] = nil assert_equal("%#StatusFiletype# %%icon percent%%type %*", rendered) end) test("uses a configured icon in component and complete statusline output", function() devicons.calls = {} statusline.setup({ filetype_icons = { lua = "CUSTOM", }, }) local component = filetype.render("lua") local complete = render_with_current_filetype("lua") local call_count = #devicons.calls statusline.setup() assert_equal("%#StatusFiletype# CUSTOM lua %*", component) assert_equal( expected_left("n") .. "%=%#StatusFiletype# CUSTOM lua %* %#StatusCursorPos# %l:%c %*", complete ) assert_equal(0, call_count, "a configured icon should take precedence without querying devicons") end) test("uses an empty configured icon to suppress the devicon", function() devicons.calls = {} statusline.setup({ filetype_icons = { lua = "", }, }) local rendered = filetype.render("lua") local call_count = #devicons.calls statusline.setup() assert_equal("%#StatusFiletype# lua %*", rendered) assert_equal(0, call_count, "an empty override should not query devicons") end) test("continues to use devicons for unmapped filetypes", function() devicons.calls = {} statusline.setup({ filetype_icons = { markdown = "CUSTOM-MD", }, }) local with_icon = filetype.render("lua") local without_icon = filetype.render("textonly") local calls = vim.deepcopy(devicons.calls) statusline.setup() assert_equal("%#StatusFiletype#  lua %*", with_icon) assert_equal("%#StatusFiletype# textonly %*", without_icon) assert_equal(2, #calls, "each unmapped filetype should query devicons") assert_equal("lua", calls[1][1]) assert_equal("textonly", calls[2][1]) assert_equal(1, calls[1].n, "the lookup should not request a default icon") assert_equal(1, calls[2].n, "the lookup should not request a default icon") end) test("escapes percent characters in a configured icon", function() devicons.calls = {} statusline.setup({ filetype_icons = { ["percent%type"] = "%custom", }, }) local rendered = filetype.render("percent%type") local call_count = #devicons.calls statusline.setup() assert_equal("%#StatusFiletype# %%custom percent%%type %*", rendered) assert_equal(0, call_count, "a configured percent icon should not query devicons") end) test("copies, replaces, and clears configured filetype icons", function() devicons.calls = {} local icons = { lua = "FIRST", markdown = "OLD-MD", } statusline.setup({ filetype_icons = icons }) icons.lua = "MUTATED" local copied = filetype.render("lua") statusline.setup({ filetype_icons = { markdown = "NEW-MD", }, }) local removed_by_replacement = filetype.render("lua") local replacement = filetype.render("markdown") statusline.setup() local removed_by_clear = filetype.render("markdown") local devicon_after_clear = filetype.render("lua") local calls = vim.deepcopy(devicons.calls) assert_equal("%#StatusFiletype# FIRST lua %*", copied, "setup should copy the caller's map") assert_equal("%#StatusFiletype#  lua %*", removed_by_replacement, "a later map should replace the prior map") assert_equal("%#StatusFiletype# NEW-MD markdown %*", replacement) assert_equal("%#StatusFiletype# markdown %*", removed_by_clear, "setup() should clear prior overrides") assert_equal("%#StatusFiletype#  lua %*", devicon_after_clear) assert_equal(3, #calls, "only entries absent from the current map should query devicons") assert_equal("lua", calls[1][1]) assert_equal("markdown", calls[2][1]) assert_equal("lua", calls[3][1]) end) test("rejects invalid setup options without replacing valid configuration", function() statusline.setup({ mode_text = { normal = "VALID-MODE" }, filetype_icons = { lua = "VALID" }, lsp_exclude = { "hidden-ls" }, }) devicons.calls = {} local cases = { { options = "invalid", message = "options must be a table" }, { options = { mode_text = "invalid" }, message = "mode_text must be a table" }, { options = { mode_text = { [1] = "invalid" } }, message = "mode_text keys must be strings" }, { options = { mode_text = { future = "invalid" } }, message = 'mode_text has unknown key "future"', }, { options = { mode_text = { normal = false } }, message = 'mode_text["normal"] must be a string', }, { options = { filetype_icons = "invalid" }, message = "filetype_icons must be a table" }, { options = { filetype_icons = { [1] = "invalid" } }, message = "filetype_icons keys must be strings", }, { options = { filetype_icons = { lua = false } }, message = 'filetype_icons["lua"] must be a string', }, { options = { lsp_exclude = "invalid" }, message = "lsp_exclude must be a table" }, { options = { lsp_exclude = { hidden = "hidden-ls" } }, message = "lsp_exclude must be a list", }, { options = { lsp_exclude = { "hidden-ls", visible = "visible-ls" } }, message = "lsp_exclude must be a list", }, { options = { lsp_exclude = { [1] = "hidden-ls", [3] = "visible-ls" } }, message = "lsp_exclude must be a list", }, { options = { lsp_exclude = { false } }, message = "lsp_exclude[1] must be a string", }, } for _, case in ipairs(cases) do local ok, err = pcall(statusline.setup, case.options) assert_equal(false, ok, "invalid setup should fail") assert_truthy(tostring(err):find(case.message, 1, true), "setup error should include " .. vim.inspect(case.message)) assert_equal( "%#StatusModeNormal# VALID-MODE %*", mode.render("n"), "an invalid setup should preserve the last valid mode text map" ) assert_equal( "%#StatusFiletype# VALID lua %*", filetype.render("lua"), "an invalid setup should preserve the last valid icon map" ) local rendered_lsp = with_mocked_lsp_clients(function() return { { name = "hidden-ls" }, { name = "visible-ls" }, } end, function() return lsp.render(1) end) assert_equal( "%#StatusLsp# visible-ls %*", rendered_lsp, "an invalid setup should preserve the last valid LSP exclusions" ) end local call_count = #devicons.calls statusline.setup() assert_equal(0, call_count, "preserved custom mappings should continue to avoid devicons") end) test("omits the LSP box when no clients are attached", function() local queried_buffer local rendered = with_mocked_lsp_clients(function(options) queried_buffer = options.bufnr return {} end, function() return lsp.render(42) end) assert_equal(nil, rendered) assert_equal(42, queried_buffer, "the component should query its target buffer") end) test("renders one LSP client with outer padding", function() local rendered = with_mocked_lsp_clients(function() return { { name = "lua_ls" } } end, function() return lsp.render(1) end) assert_equal("%#StatusLsp# lua_ls %*", rendered) end) test("renders all LSP clients in returned order with the exact separator", function() local rendered = with_mocked_lsp_clients(function() return { { name = "lua_ls" }, { name = "null-ls" }, { name = "lua_ls" }, } end, function() return lsp.render(1) end) assert_equal("%#StatusLsp# lua_ls │ null-ls │ lua_ls %*", rendered) end) test("escapes percent characters in LSP client names", function() local rendered = with_mocked_lsp_clients(function() return { { name = "client%one" }, { name = "%client" }, } end, function() return lsp.render(1) end) assert_equal("%#StatusLsp# client%%one │ %%client %*", rendered) end) test("excludes exact LSP names while preserving remaining order and duplicates", function() statusline.setup({ lsp_exclude = { "null-ls", "hidden%ls" }, }) local rendered = with_mocked_lsp_clients(function() return { { name = "lua_ls" }, { name = "null-ls" }, { name = "client%one" }, { name = "lua_ls" }, { name = "hidden%ls" }, } end, function() return lsp.render(1) end) statusline.setup() assert_equal("%#StatusLsp# lua_ls │ client%%one │ lua_ls %*", rendered) end) test("matches excluded LSP names case-sensitively", function() statusline.setup({ lsp_exclude = { "lua_ls" } }) local rendered = with_mocked_lsp_clients(function() return { { name = "Lua_ls" }, { name = "lua_ls" }, } end, function() return lsp.render(1) end) statusline.setup() assert_equal("%#StatusLsp# Lua_ls %*", rendered) end) test("omits the LSP box when every attached client is excluded", function() statusline.setup({ lsp_exclude = { "lua_ls", "null-ls" } }) local rendered = with_mocked_lsp_clients(function() return { { name = "lua_ls" }, { name = "null-ls" }, } end, function() return { component = lsp.render(1), complete = render_with_current_filetype("lua"), } end) statusline.setup() assert_equal(nil, rendered.component) assert_equal( expected_left("n") .. "%=%#StatusFiletype#  lua %* %#StatusCursorPos# %l:%c %*", rendered.complete, "an all-filtered LSP box should leave no extra separator" ) end) test("copies, replaces, and clears excluded LSP names", function() local clients = { { name = "first-ls" }, { name = "second-ls" }, } local function render_clients() return with_mocked_lsp_clients(function() return clients end, function() return lsp.render(1) end) end local exclusions = { "first-ls" } statusline.setup({ lsp_exclude = exclusions }) exclusions[1] = "second-ls" local copied = render_clients() statusline.setup({ lsp_exclude = { "second-ls" } }) local replacement = render_clients() statusline.setup() local cleared = render_clients() assert_equal("%#StatusLsp# second-ls %*", copied, "setup should copy the caller's exclusion list") assert_equal("%#StatusLsp# first-ls %*", replacement, "a later list should replace prior exclusions") assert_equal( "%#StatusLsp# first-ls │ second-ls %*", cleared, "setup() should clear prior LSP exclusions" ) end) test("renders exact padded and aligned components for an empty filetype", function() assert_equal("%#StatusModeNormal# NORMAL %*", mode.render("n")) assert_equal(1, select("#", mode.render("n")), "the mode component should return only its rendered box") assert_equal("%#StatusCursorPos# %l:%c %*", cursor.render()) assert_equal( expected_left("n") .. "%=%#StatusCursorPos# %l:%c %*", render_with_current_filetype(""), "the CWD separator and alignment should each be added once" ) end) test("keeps the cursor-position highlight independent of the active mode", function() local rendered = with_replaced_field(vim.api, "nvim_get_mode", function() return { mode = "i", blocking = false } end, function() return render_with_current_filetype("") end) assert_equal( expected_left("i") .. "%=%#StatusCursorPos# %l:%c %*", rendered, "changing modes should affect the mode box but not the CWD or cursor-position boxes" ) end) test("renders the exact complete statusline for a filetype with an icon", function() assert_equal( expected_left("n") .. "%=%#StatusFiletype#  lua %* %#StatusCursorPos# %l:%c %*", render_with_current_filetype("lua"), "the left and right boxes should have one unhighlighted separator" ) end) test("places aggregated workspace diagnostic boxes after the CWD and before alignment", function() local rendered = with_workspace_diagnostics({ { { lnum = 0, col = 0, message = "first error", severity = vim.diagnostic.severity.ERROR, source = "one" }, { lnum = 0, col = 0, message = "duplicate error", severity = vim.diagnostic.severity.ERROR, source = "one" }, { lnum = 1, col = 0, message = "warning", severity = vim.diagnostic.severity.WARN, source = "one" }, }, { { lnum = 2, col = 0, message = "info", severity = vim.diagnostic.severity.INFO, source = "two" }, { lnum = 3, col = 0, message = "hint", severity = vim.diagnostic.severity.HINT, source = "two" }, }, }, function() return render_with_current_filetype("") end) assert_equal( expected_left("n") .. " %#StatusDiagnosticError# 󰅚 2 %* %#StatusDiagnosticWarn# 󰀪 1 %* %#StatusDiagnosticInfo# 󰋽 1 %* %#StatusDiagnosticHint# 󰌶 1 %*" .. "%=%#StatusCursorPos# %l:%c %*", rendered, "diagnostic sources and namespaces should aggregate on the left without moving alignment" ) end) test("no workspace diagnostics leave the complete statusline unchanged", function() assert_equal( expected_left("n") .. "%=%#StatusFiletype#  lua %* %#StatusCursorPos# %l:%c %*", render_with_current_filetype("lua") ) end) test("no recording leaves the complete statusline unchanged", function() local rendered = with_mocked_recording("", function() return render_with_current_filetype("lua") end) assert_equal( expected_left("n") .. "%=%#StatusFiletype#  lua %* %#StatusCursorPos# %l:%c %*", rendered ) end) test("places the macro box immediately before LSP, filetype, and cursor boxes", function() local rendered = with_mocked_recording("q", function() return with_mocked_lsp_clients(function() return { { name = "lua_ls" }, { name = "null-ls" }, } end, function() return render_with_current_filetype("lua") end) end) assert_equal( expected_left("n") .. "%=%#StatusMacro# RECORDING @q %* %#StatusLsp# lua_ls │ null-ls %* %#StatusFiletype#  lua %* %#StatusCursorPos# %l:%c %*", rendered, "all right-side boxes should be separated by one unhighlighted space" ) end) test("places the LSP box before the filetype and cursor boxes", function() local rendered = with_mocked_lsp_clients(function() return { { name = "lua_ls" }, { name = "null-ls" }, } end, function() return render_with_current_filetype("lua") end) assert_equal( expected_left("n") .. "%=%#StatusLsp# lua_ls │ null-ls %* %#StatusFiletype#  lua %* %#StatusCursorPos# %l:%c %*", rendered, "all right-side boxes should be separated by one unhighlighted space" ) end) test("composes the macro box without LSP clients", function() local rendered = with_mocked_recording("a", function() return render_with_current_filetype("lua") end) assert_equal( expected_left("n") .. "%=%#StatusMacro# RECORDING @a %* %#StatusFiletype#  lua %* %#StatusCursorPos# %l:%c %*", rendered ) end) test("composes the macro box without a filetype", function() local rendered = with_mocked_recording("b", function() return with_mocked_lsp_clients(function() return { { name = "lua_ls" } } end, function() return render_with_current_filetype("") end) end) assert_equal( expected_left("n") .. "%=%#StatusMacro# RECORDING @b %* %#StatusLsp# lua_ls %* %#StatusCursorPos# %l:%c %*", rendered ) end) test("composes the macro box when both LSP and filetype are absent", function() local rendered = with_mocked_recording("c", function() return render_with_current_filetype("") end) assert_equal( expected_left("n") .. "%=%#StatusMacro# RECORDING @c %* %#StatusCursorPos# %l:%c %*", rendered ) end) test("keeps the LSP box when the filetype is empty", function() local rendered = with_mocked_lsp_clients(function() return { { name = "lua_ls" } } end, function() return render_with_current_filetype("") end) assert_equal( expected_left("n") .. "%=%#StatusLsp# lua_ls %* %#StatusCursorPos# %l:%c %*", rendered, "omitting filetype should not leave an extra separator" ) end) test("renders the exact complete statusline for a filetype without an icon", function() assert_equal( expected_left("n") .. "%=%#StatusFiletype# textonly %* %#StatusCursorPos# %l:%c %*", render_with_current_filetype("textonly"), "a missing icon mapping should fall back to the exact filetype" ) end) local function render_current_terminal(terminal_filetype) local window = vim.api.nvim_get_current_win() local original_buffer = vim.api.nvim_win_get_buf(window) local terminal_buffer = vim.api.nvim_create_buf(false, true) vim.api.nvim_open_term(terminal_buffer, {}) vim.api.nvim_set_option_value("filetype", terminal_filetype, { buf = terminal_buffer }) vim.api.nvim_win_set_buf(window, terminal_buffer) local ok, rendered = xpcall(statusline.render, debug.traceback) vim.api.nvim_win_set_buf(window, original_buffer) vim.api.nvim_buf_delete(terminal_buffer, { force = true }) if not ok then error(rendered) end return rendered end test("keeps workspace diagnostics consistent in active, inactive, and terminal statuslines", function() local rendered = with_workspace_diagnostics({ { { lnum = 0, col = 0, message = "workspace error", severity = vim.diagnostic.severity.ERROR }, }, }, function() local active = render_with_current_filetype("") local terminal = render_current_terminal("lua") local current_window = vim.api.nvim_get_current_win() local target_buffer = vim.api.nvim_create_buf(false, true) vim.api.nvim_set_option_value("filetype", "lua", { buf = target_buffer }) vim.cmd("belowright split") local target_window = vim.api.nvim_get_current_win() vim.api.nvim_win_set_buf(target_window, target_buffer) vim.api.nvim_set_current_win(current_window) local previous_statusline_winid = vim.g.statusline_winid vim.g.statusline_winid = target_window local ok, inactive = xpcall(statusline.render, debug.traceback) vim.g.statusline_winid = previous_statusline_winid vim.api.nvim_win_close(target_window, true) vim.api.nvim_buf_delete(target_buffer, { force = true }) if not ok then error(inactive) end return { active = active, inactive = inactive, terminal = terminal } end) local diagnostic_box = " %#StatusDiagnosticError# 󰅚 1 %*" assert_equal( expected_left("n") .. diagnostic_box .. "%=" .. cursor.render(), rendered.active, "the active statusline should show workspace diagnostics" ) assert_equal( expected_left("n") .. diagnostic_box .. "%=%#StatusFiletype#  lua %* " .. cursor.render(), rendered.inactive, "an inactive target buffer should not change workspace diagnostics" ) assert_equal( expected_left("nt") .. diagnostic_box .. "%=%#StatusFiletype#  lua %*", rendered.terminal, "a terminal target buffer should not hide workspace diagnostics" ) end) test("renders a terminal filetype but omits the cursor component", function() assert_equal( expected_left("nt") .. "%=%#StatusFiletype#  lua %*", render_current_terminal("lua"), "terminal buffers should retain the CWD and a nonempty filetype box" ) end) test("renders LSP clients in terminals while still omitting the cursor", function() local rendered = with_mocked_lsp_clients(function() return { { name = "term-ls" } } end, function() return render_current_terminal("lua") end) assert_equal( expected_left("nt") .. "%=%#StatusLsp# term-ls %* %#StatusFiletype#  lua %*", rendered, "terminal buffers should retain CWD, LSP, and filetype boxes but omit the cursor box" ) end) test("keeps macro recording visible in terminals while omitting the cursor", function() local rendered = with_mocked_recording("q", function() return with_mocked_lsp_clients(function() return { { name = "term-ls" } } end, function() return render_current_terminal("lua") end) end) assert_equal( expected_left("nt") .. "%=%#StatusMacro# RECORDING @q %* %#StatusLsp# term-ls %* %#StatusFiletype#  lua %*", rendered, "terminal buffers should retain macro, LSP, and filetype boxes but omit the cursor box" ) end) test("keeps the CWD in an empty-filetype terminal", function() assert_equal( expected_left("nt"), render_current_terminal(""), "an empty-filetype terminal should retain both always-visible left-side boxes" ) end) test("uses statusline_winid for inactive LSP clients while retaining editor-wide macro state", function() local current_window = vim.api.nvim_get_current_win() local current_buffer = vim.api.nvim_win_get_buf(current_window) local original_filetype = vim.api.nvim_get_option_value("filetype", { buf = current_buffer }) vim.api.nvim_set_option_value("filetype", "textonly", { buf = current_buffer }) local target_buffer = vim.api.nvim_create_buf(false, true) vim.api.nvim_set_option_value("filetype", "lua", { buf = target_buffer }) vim.cmd("belowright split") local target_window = vim.api.nvim_get_current_win() vim.api.nvim_win_set_buf(target_window, target_buffer) vim.api.nvim_set_current_win(current_window) local queried_buffer local previous_statusline_winid = vim.g.statusline_winid vim.g.statusline_winid = target_window local rendered = with_mocked_recording("q", function() return with_mocked_lsp_clients(function(options) queried_buffer = options.bufnr if options.bufnr == target_buffer then return { { name = "target-ls" } } end return { { name = "active-ls" } } end, statusline.render) end) vim.g.statusline_winid = previous_statusline_winid vim.api.nvim_win_close(target_window, true) vim.api.nvim_buf_delete(target_buffer, { force = true }) vim.api.nvim_set_option_value("filetype", original_filetype, { buf = current_buffer }) assert_equal(target_buffer, queried_buffer, "LSP lookup should use the inactive target's buffer") assert_equal( expected_left("n") .. "%=%#StatusMacro# RECORDING @q %* %#StatusLsp# target-ls %* %#StatusFiletype#  lua %* %#StatusCursorPos# %l:%c %*", rendered, "the inactive target should retain editor-wide macro state while supplying its clients and filetype" ) end) test("uses statusline_winid for an inactive terminal's filetype and buftype", function() local current_window = vim.api.nvim_get_current_win() local current_buffer = vim.api.nvim_win_get_buf(current_window) local original_filetype = vim.api.nvim_get_option_value("filetype", { buf = current_buffer }) vim.api.nvim_set_option_value("filetype", "textonly", { buf = current_buffer }) local terminal_buffer = vim.api.nvim_create_buf(false, true) vim.api.nvim_open_term(terminal_buffer, {}) vim.api.nvim_set_option_value("filetype", "lua", { buf = terminal_buffer }) vim.cmd("belowright split") local terminal_window = vim.api.nvim_get_current_win() vim.api.nvim_win_set_buf(terminal_window, terminal_buffer) vim.api.nvim_set_current_win(current_window) local previous_statusline_winid = vim.g.statusline_winid vim.g.statusline_winid = terminal_window local ok, rendered = xpcall(statusline.render, debug.traceback) vim.g.statusline_winid = previous_statusline_winid vim.api.nvim_win_close(terminal_window, true) vim.api.nvim_buf_delete(terminal_buffer, { force = true }) vim.api.nvim_set_option_value("filetype", original_filetype, { buf = current_buffer }) if not ok then error(rendered) end assert_equal( expected_left("n") .. "%=%#StatusFiletype#  lua %*", rendered, "the inactive target should retain the CWD and supply both buffer options" ) end) local highlight_groups = { "StatusCursorPos", "StatusCwd", "StatusFiletype", "StatusLsp", "StatusMacro", "StatusModeNormal", "StatusModeOperatorPending", "StatusModeInsert", "StatusModeVisual", "StatusModeVisualLine", "StatusModeVisualBlock", "StatusModeSelect", "StatusModeSelectLine", "StatusModeSelectBlock", "StatusModeReplace", "StatusModeVirtualReplace", "StatusModeCommand", "StatusModeEx", "StatusModePrompt", "StatusModeMore", "StatusModeConfirm", "StatusModeShell", "StatusModeTerminal", "StatusModeTerminalNormal", "StatusModeUnknown", } local diagnostic_highlight_links = { StatusDiagnosticError = "DiagnosticError", StatusDiagnosticWarn = "DiagnosticWarn", StatusDiagnosticInfo = "DiagnosticInfo", StatusDiagnosticHint = "DiagnosticHint", } test("setup is idempotent and preserves editor-owned settings", function() vim.api.nvim_set_hl(0, "StatusModeInsert", { fg = "#123456", bold = true }) vim.api.nvim_set_option_value("laststatus", 1, { scope = "global" }) vim.api.nvim_set_option_value("statusline", "LOCAL STATUSLINE", { scope = "local", win = 0 }) statusline.setup() statusline.setup() assert_equal( "%!v:lua.require('statusline').render()", vim.api.nvim_get_option_value("statusline", { scope = "global" }), "setup should install the global statusline expression" ) assert_equal(1, vim.api.nvim_get_option_value("laststatus", { scope = "global" }), "setup should not change laststatus") assert_equal( "LOCAL STATUSLINE", vim.api.nvim_get_option_value("statusline", { scope = "local", win = 0 }), "setup should not replace a window-local statusline" ) local events = { "ColorScheme", "RecordingEnter", "RecordingLeave", "DiagnosticChanged", "BufFilePost", "BufDelete", "BufWipeout", } for _, event in ipairs(events) do local autocmds = vim.api.nvim_get_autocmds({ group = "statusline.nvim", event = event }) assert_equal(1, #autocmds, "repeated setup should leave one " .. event .. " autocommand") end end) test("successful setup clears cached diagnostic output", function() statusline.setup() local calls = 0 local counts = diagnostic_counts(1, 0, 0, 0) with_replaced_field(diagnostics, "count", function() calls = calls + 1 return counts end, function() local before = diagnostics.render("/setup-cache") counts = diagnostic_counts(0, 2, 0, 0) assert_equal(before, diagnostics.render("/setup-cache"), "the old value should remain cached") statusline.setup() local after = diagnostics.render("/setup-cache") assert_truthy(after:find(" 2 ", 1, true), "setup should expose the refreshed warning count") end) diagnostics.invalidate() assert_equal(2, calls, "setup should force exactly one fresh count") end) test("buffer lifecycle events invalidate diagnostic state and coalesce redraws", function() statusline.setup() vim.wait(10) local directory = "/lifecycle-cache" local counts = diagnostic_counts(1, 0, 0, 0) local count_calls = 0 local invalidated = {} local scheduled = {} local commands = {} local buffer = vim.api.nvim_get_current_buf() local original_invalidate_buffer = diagnostics.invalidate_buffer with_replaced_field(diagnostics, "count", function() count_calls = count_calls + 1 return counts end, function() with_replaced_field(diagnostics, "invalidate_buffer", function(invalidated_buffer) invalidated[#invalidated + 1] = invalidated_buffer original_invalidate_buffer(invalidated_buffer) end, function() with_replaced_field(vim, "schedule", function(callback) scheduled[#scheduled + 1] = callback end, function() with_replaced_field(vim, "cmd", function(command) commands[#commands + 1] = command end, function() diagnostics.render(directory) local events = { { name = "BufFilePost", counts = diagnostic_counts(0, 2, 0, 0), expected = 2 }, { name = "BufDelete", counts = diagnostic_counts(0, 0, 3, 0), expected = 3 }, { name = "BufWipeout", counts = diagnostic_counts(0, 0, 0, 4), expected = 4 }, } for _, event in ipairs(events) do counts = event.counts vim.api.nvim_exec_autocmds(event.name, { buffer = buffer, modeline = false }) local rendered = diagnostics.render(directory) assert_truthy( rendered:find(" " .. event.expected .. " ", 1, true), event.name .. " should invalidate the aggregate cache synchronously" ) end assert_equal(0, #commands, "lifecycle redraws should wait for the scheduled callback") assert_equal(1, #scheduled, "same-turn lifecycle events should enqueue one redraw") scheduled[1]() end) end) end) end) diagnostics.invalidate() assert_equal(4, count_calls, "each lifecycle invalidation should force a recount") assert_equal(3, #invalidated) for _, invalidated_buffer in ipairs(invalidated) do assert_equal(buffer, invalidated_buffer) end assert_equal(1, #commands, "the coalesced callback should redraw statuslines once") assert_equal("redrawstatus", commands[1]) end) test("DiagnosticChanged invalidates immediately and coalesces statusline redraws", function() statusline.setup() vim.wait(10) local directory = "/diagnostic-event-cache" local counts = diagnostic_counts(1, 0, 0, 0) local count_calls = 0 local scheduled = {} local commands = {} with_replaced_field(diagnostics, "count", function() count_calls = count_calls + 1 return counts end, function() with_replaced_field(vim, "schedule", function(callback) scheduled[#scheduled + 1] = callback end, function() with_replaced_field(vim, "cmd", function(command) commands[#commands + 1] = command end, function() diagnostics.render(directory) counts = diagnostic_counts(0, 5, 0, 0) vim.api.nvim_exec_autocmds("DiagnosticChanged", { modeline = false, data = { diagnostics = {} }, }) vim.api.nvim_exec_autocmds("DiagnosticChanged", { modeline = false, data = { diagnostics = {} }, }) local refreshed = diagnostics.render(directory) assert_truthy(refreshed:find(" 5 ", 1, true), "invalidation should precede the redraw") assert_equal(2, count_calls, "rendering before the redraw should observe fresh diagnostics") assert_equal(0, #commands, "DiagnosticChanged should not redraw synchronously") assert_equal(1, #scheduled, "multiple diagnostic events should enqueue one redraw") scheduled[1]() end) end) end) diagnostics.invalidate() assert_equal(1, #commands) assert_equal("redrawstatus", commands[1]) end) test("recording autocmds schedule a statusline redraw", function() statusline.setup() local scheduled = {} with_replaced_field(vim, "schedule", function(callback) scheduled[#scheduled + 1] = callback end, function() vim.api.nvim_exec_autocmds("RecordingEnter", { modeline = false }) vim.api.nvim_exec_autocmds("RecordingLeave", { modeline = false }) end) assert_equal(2, #scheduled, "both recording events should schedule a callback") local commands = {} with_replaced_field(vim, "cmd", function(command) commands[#commands + 1] = command end, function() for _, callback in ipairs(scheduled) do callback() end end) assert_equal(2, #commands, "each scheduled callback should issue one command") assert_equal("redrawstatus", commands[1], "RecordingEnter should redraw statuslines") assert_equal("redrawstatus", commands[2], "RecordingLeave should redraw statuslines") end) test("defines default links without replacing user highlights", function() for _, group in ipairs(highlight_groups) do local definition = vim.api.nvim_get_hl(0, { name = group, link = true, create = false }) if group == "StatusModeInsert" then assert_equal(nil, definition.link, "the user-defined Insert highlight should not become a link") assert_equal(0x123456, definition.fg, "the user-defined Insert foreground should remain") assert_equal(true, definition.bold, "the user-defined Insert style should remain") else assert_equal("StatusLine", definition.link, group .. " should default-link to StatusLine") assert_equal(true, definition.default, group .. " should be marked as a default highlight") end end end) test("default-links diagnostic highlights to Neovim severity groups", function() statusline.setup() for group, link in pairs(diagnostic_highlight_links) do local definition = vim.api.nvim_get_hl(0, { name = group, link = true, create = false }) assert_equal(link, definition.link, group .. " should default-link to " .. link) assert_equal(true, definition.default, group .. " should be marked as a default highlight") end end) test("preserves user-defined diagnostic highlights across setup and ColorScheme", function() local definitions = { StatusDiagnosticError = { fg = "#110001", bold = true }, StatusDiagnosticWarn = { fg = "#220002", italic = true }, StatusDiagnosticInfo = { fg = "#330003", underline = true }, StatusDiagnosticHint = { fg = "#440004", reverse = true }, } for group, definition in pairs(definitions) do vim.api.nvim_set_hl(0, group, definition) end statusline.setup() statusline.setup() vim.api.nvim_exec_autocmds("ColorScheme", { modeline = false, pattern = "test-colorscheme" }) for group, expected in pairs(definitions) do local actual = vim.api.nvim_get_hl(0, { name = group, link = true, create = false }) assert_equal(nil, actual.link, group .. " should not become a link") for property, value in pairs(expected) do local expected_value = property == "fg" and tonumber(value:sub(2), 16) or value assert_equal(expected_value, actual[property], group .. " should preserve " .. property) end end end) test("repeated setup preserves user-defined component highlights", function() local cases = { { group = "StatusFiletype", color = "#abcdef", style = "italic" }, { group = "StatusCwd", color = "#c0ffee", style = "bold" }, { group = "StatusCursorPos", color = "#fedcba", style = "underline" }, { group = "StatusLsp", color = "#aabbcc", style = "reverse" }, { group = "StatusMacro", color = "#ff1234", style = "bold" }, } for _, case in ipairs(cases) do vim.api.nvim_set_hl(0, case.group, { fg = case.color, [case.style] = true }) end statusline.setup() statusline.setup() for _, case in ipairs(cases) do local definition = vim.api.nvim_get_hl(0, { name = case.group, link = true, create = false }) assert_equal(nil, definition.link, case.group .. " should not become a link") assert_equal(tonumber(case.color:sub(2), 16), definition.fg, case.group .. " should preserve its foreground") assert_equal(true, definition[case.style], case.group .. " should preserve its style") end end) test("restores missing default links after a colorscheme change", function() local statusline_groups = { "StatusCursorPos", "StatusCwd", "StatusFiletype", "StatusLsp", "StatusMacro", "StatusModeVisual", } for _, group in ipairs(statusline_groups) do vim.api.nvim_set_hl(0, group, {}) end for group in pairs(diagnostic_highlight_links) do vim.api.nvim_set_hl(0, group, {}) end vim.api.nvim_exec_autocmds("ColorScheme", { modeline = false, pattern = "test-colorscheme" }) for _, group in ipairs(statusline_groups) do local definition = vim.api.nvim_get_hl(0, { name = group, link = true, create = false }) assert_equal("StatusLine", definition.link, group .. " should restore its missing default link") end for group, link in pairs(diagnostic_highlight_links) do local definition = vim.api.nvim_get_hl(0, { name = group, link = true, create = false }) assert_equal(link, definition.link, group .. " should restore its missing default link") end local insert = vim.api.nvim_get_hl(0, { name = "StatusModeInsert", link = true, create = false }) assert_equal(nil, insert.link, "the ColorScheme autocommand should preserve user highlights") assert_equal(0x123456, insert.fg) end) local failures = 0 for _, case in ipairs(tests) do local ok, err = xpcall(case.fn, debug.traceback) if ok then vim.api.nvim_out_write("ok - " .. case.name .. "\n") else failures = failures + 1 vim.api.nvim_err_writeln("not ok - " .. case.name .. "\n" .. err) end end if failures > 0 then vim.api.nvim_err_writeln(("%d test(s) failed"):format(failures)) vim.cmd("cquit 1") else vim.api.nvim_out_write(("%d tests passed\n"):format(#tests)) vim.cmd("qa!") end