Files
LLeMbas-IDE/ide/tests/headless.lua
T

349 lines
15 KiB
Lua

-- The installed IDE, started headless through its own launcher, checked from the inside.
-- llembas-ide --headless -c "luafile ide/tests/headless.lua"
-- Environment: IDE_TEST_REPO (the repository), IDE_TEST_PARSERS (1 when the grammars were
-- compiled), IDE_TEST_BG_DARK / IDE_TEST_BG_LIGHT (the tokens' backgrounds, #RRGGBB),
-- IDE_TEST_FIRST (1 on the first start in a new home), IDE_TEST_USER (1 when lua/user/init.lua was given a marker line).
local repo = assert(vim.env.IDE_TEST_REPO, "IDE_TEST_REPO")
local passed, failed = 0, {}
local function check(name, fn)
local ok, err = pcall(fn)
if ok then
passed = passed + 1
else
table.insert(failed, name .. ": " .. tostring(err))
end
end
local function eq(a, b, what)
if not vim.deep_equal(a, b) then error((what or "") .. " expected " .. vim.inspect(b) .. ", got " .. vim.inspect(a), 2) end
end
local function lock(path)
local rows = {}
for line in io.lines(path) do
if not line:match("^%s*#") and line:match("%S") then table.insert(rows, vim.split(vim.trim(line), "%s+")) end
end
return rows
end
local function run()
local ide = require("llembas_ide")
local prefix = ide.prefix
check("starts with no error message", function()
for _, line in ipairs(vim.split(vim.fn.execute("messages"), "\n")) do
assert(not line:match("^E%d+:") and not line:match("^Error") and not line:lower():match("deprecated"), line)
end
end)
check("every part of the IDE started", function() eq(ide.problems, {}) end)
check("the version is the repository's", function()
eq(ide.version, vim.trim(table.concat(vim.fn.readfile(repo .. "/VERSION"), "")))
end)
check("its own NVIM_APPNAME", function()
eq(vim.env.NVIM_APPNAME, "llembas-ide")
assert(vim.fn.stdpath("config"):match("/llembas%-ide$"), vim.fn.stdpath("config"))
end)
-- Plugins: each at its pin, and what Lua loads comes from that directory.
local modules = {
["mini.nvim"] = "mini.pick", ["blink.cmp"] = "blink.cmp", ["conform.nvim"] = "conform",
["nvim-lspconfig"] = "lspconfig", ["nvim-treesitter"] = "nvim-treesitter.query_predicates",
}
local compiled = vim.env.IDE_TEST_PARSERS == "1"
for _, p in ipairs(lock(repo .. "/ide/plugins.lock")) do
local name, where, commit = p[1], p[2], p[4]
local dir = prefix .. "/site/pack/llembas/" .. where .. "/" .. name
check("plugin " .. name .. " is at its pinned commit", function()
eq(vim.trim(vim.fn.system({ "git", "-C", dir, "rev-parse", "HEAD" })), commit)
eq(vim.trim(vim.fn.system({ "git", "-C", dir, "status", "--porcelain", "--untracked-files=no" })), "", "changed files:")
end)
if name ~= "nvim-treesitter" or compiled then
check("plugin " .. name .. " loads from its pinned directory", function()
local module = assert(modules[name], "no module known for " .. name)
require(module)
local file = vim.api.nvim_get_runtime_file("lua/" .. module:gsub("%.", "/") .. ".lua", false)[1]
or vim.api.nvim_get_runtime_file("lua/" .. module:gsub("%.", "/") .. "/init.lua", false)[1]
assert(file and vim.startswith(vim.fn.resolve(file), vim.fn.resolve(dir)), tostring(file) .. " is not under " .. dir)
end)
end
end
check("no plugin manager, and nothing that installs", function()
for _, c in ipairs({ "Lazy", "PackerSync", "Mason", "TSInstall", "TSUpdate" }) do
eq(vim.fn.exists(":" .. c), 0, c)
end
end)
-- The colourscheme.
check("the LLeMbas colourscheme is applied (dark: moria)", function()
eq(vim.g.colors_name, "llembas")
eq(vim.o.background, "dark")
eq(("#%06X"):format(vim.api.nvim_get_hl(0, { name = "Normal" }).bg), vim.env.IDE_TEST_BG_DARK:upper())
end)
check("light is shire, and dark comes back", function()
vim.o.background = "light"
eq(vim.g.colors_name, "llembas")
eq(("#%06X"):format(vim.api.nvim_get_hl(0, { name = "Normal" }).bg), vim.env.IDE_TEST_BG_LIGHT:upper())
vim.o.background = "dark"
eq(("#%06X"):format(vim.api.nvim_get_hl(0, { name = "Normal" }).bg), vim.env.IDE_TEST_BG_DARK:upper())
end)
check("the plugin's and the IDE's own groups are coloured by the theme", function()
for _, g in ipairs({ "LLeMbasBrand", "LLeMbasGhost", "LLeMbasIdeLeaf", "MiniStatuslineModeNormal", "DiagnosticError" }) do
assert(next(vim.api.nvim_get_hl(0, { name = g, link = false })) ~= nil, g .. " has no colours")
end
end)
-- The start screen.
check("the dashboard is the start screen and shows its lines", function()
eq(vim.bo.filetype, "llembas-ide-dashboard")
local text = table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), "\n")
assert(text:find("LLeMbas IDE " .. ide.version, 1, true), "no name and version")
assert(text:find("▄▄███████", 1, true), "no mark")
for _, a in ipairs(require("llembas_ide.dashboard").actions) do
assert(text:find(a[1] .. " " .. a[2], 1, true), "no line for " .. a[2])
assert(vim.fn.maparg(a[1], "n", false, true).buffer == 1, "no key " .. a[1])
end
if vim.env.IDE_TEST_FIRST == "1" then assert(text:find("Welcome.", 1, true), "no first-run welcome") end
assert(text:find("The editor works", 1, true), "no line saying the editor works without LLeMbas")
end)
check("the dashboard's block shares one left edge", function()
local edge
for _, l in ipairs(vim.api.nvim_buf_get_lines(0, 0, -1, false)) do
if l:match("^%s*%a %u") then
local at = #l:match("^%s*")
edge = edge or at
eq(at, edge, l)
end
end
assert(edge, "no action lines")
end)
check("the dashboard's block is 38 columns and no line is over 52", function()
local rows = require("llembas_ide.dashboard").content(false)
assert(#rows > 15)
for _, r in ipairs(rows) do assert(vim.fn.strdisplaywidth(r[1]) <= 52, r[1]) end
for _, r in ipairs(rows) do
if r[3] then assert(vim.fn.strdisplaywidth(r[1]) <= 38, r[1]) end
end
end)
check("the window title names the IDE", function()
assert(vim.o.title and vim.o.titlestring:find("LLeMbas IDE", 1, true))
end)
-- LLeMbas built in.
check("the LLeMbas plugin's commands exist", function()
for _, c in ipairs({ "LLeMbas", "LLeMbasAsk", "LLeMbasEdit", "LLeMbasExplain", "LLeMbasFix", "LLeMbasGenerate",
"LLeMbasAutocomplete", "LLeMbasMode", "LLeMbasModel", "LLeMbasSessions", "LLeMbasNew", "LLeMbasStatus" }) do
eq(vim.fn.exists(":" .. c), 2, c)
end
eq(require("llembas").version, ide.version, "plugin version")
end)
check("the IDE's own commands exist", function()
for _, c in ipairs({ "LLeMbasIdeKeys", "LLeMbasIdeHome", "LLeMbasIdeLsp", "LLeMbasIdeFormat", "LLeMbasIdeTerminal",
"LLeMbasIdeDiff", "LLeMbasIdeSignIn", "LLeMbasIdeVersion", "LLeMbasIdeConfig", "LLeMbasIdeProblems", "LLeMbasIdeDoctor" }) do
eq(vim.fn.exists(":" .. c), 2, c)
end
end)
check("nothing predefined: the plugin starts the CLI by name, with no server or model", function()
local c = require("llembas").config
eq(c.cmd, { "lembas", "serve", "--stdio" })
eq(c.model, nil)
eq(c.complete.model, nil)
end)
check("without the CLI: no suggestions asked for, and the statusline says so", function()
if vim.fn.executable("lembas") == 1 then return end
eq(require("llembas_ide.llembas").autocomplete_on(), false)
local text, live = require("llembas_ide.llembas").status()
eq({ text, live }, { "◈ no CLI", false })
end)
check("sign in shows the one command when the plugin has no :LLeMbasLogin", function()
if vim.fn.exists(":LLeMbasLogin") == 2 then return end
vim.cmd("LLeMbasIdeSignIn")
local text = table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), "\n")
assert(text:find("lembas login", 1, true) or text:find("LLeMbas-CLI", 1, true), text)
vim.cmd("close")
end)
-- Keys.
local keys = require("llembas_ide.keys")
check("the key map has no binding twice", function() eq(keys.duplicates(), {}) end)
check("every key of the map is mapped", function()
for _, k in ipairs(keys.map) do
for _, mode in ipairs(type(k[1]) == "table" and k[1] or { k[1] }) do
assert(vim.fn.maparg(k[2], mode) ~= "" or vim.fn.maparg(k[2], mode, false, true).callback, mode .. " " .. k[2])
end
end
end)
check("the leader is <Space>, and <Tab> in insert mode is left to LLeMbas", function()
eq(vim.g.mapleader, " ")
-- Neovim 0.11 and newer map <Tab> themselves (to move in a snippet); nothing of the menu's may be on it.
local tab = vim.fn.maparg("<Tab>", "i", false, true)
assert(not tostring(tab.rhs or tab.callback and debug.getinfo(tab.callback, "S").source or ""):find("blink"), vim.inspect(tab))
local menu = require("blink.cmp.config").keymap
eq(menu.preset, "none")
eq(menu["<Tab>"], nil, "the menu's <Tab>")
assert(menu["<C-y>"] and menu["<CR>"], "the menu's own keys")
end)
check("the README's cheat sheet is the key map", function()
local readme = table.concat(vim.fn.readfile(repo .. "/ide/README.md"), "\n")
local block = readme:match("<!%-%- keys:begin %-%->\n(.-)\n<!%-%- keys:end %-%->")
assert(block, "no keys block in ide/README.md")
eq(vim.trim(block), vim.trim(table.concat(keys.markdown(), "\n")))
end)
check(":LLeMbasIdeKeys, :LLeMbasIdeLsp and :LLeMbasIdeProblems open", function()
for _, c in ipairs({ "LLeMbasIdeKeys", "LLeMbasIdeLsp", "LLeMbasIdeProblems" }) do
vim.cmd(c)
eq(vim.bo.filetype, "llembas-ide-page", c)
assert(vim.api.nvim_buf_line_count(0) > 0)
vim.cmd("close")
end
end)
-- Safety and options.
check("modelines and per-directory configuration are off", function()
eq({ vim.o.modeline, vim.o.modelines, vim.o.exrc }, { false, 0, false })
end)
check("the everyday options", function()
eq({ vim.go.number, vim.o.mouse, vim.o.undofile, vim.o.splitright, vim.o.ignorecase, vim.o.smartcase },
{ true, "a", true, true, true, true })
assert(vim.o.clipboard:find("unnamedplus"), vim.o.clipboard)
end)
-- Bars.
check("the statusline and the tabline draw", function()
local s = vim.api.nvim_eval_statusline(require("mini.statusline").active(), {})
assert(#s.str > 0)
assert(vim.o.tabline:find("MiniTabline", 1, true), vim.o.tabline)
end)
-- Treesitter.
check("a Lua buffer is highlighted by Treesitter", function()
vim.cmd("enew")
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "local x = 1", "return x" })
vim.bo.filetype = "lua"
assert(vim.treesitter.highlighter.active[vim.api.nvim_get_current_buf()], "no highlighter")
vim.cmd("bwipeout!")
end)
if compiled then
local ft_of = { bash = "sh", tsx = "typescriptreact", vimdoc = "help", markdown_inline = false }
for _, p in ipairs(lock(repo .. "/ide/parsers.lock")) do
local lang = p[1]
check("grammar " .. lang .. " loads and its queries fit it", function()
local ok, added = pcall(vim.treesitter.language.add, lang)
assert(ok and added ~= false, "the grammar does not load: " .. tostring(added))
for _, q in ipairs({ "highlights", "injections" }) do
local good, err = pcall(vim.treesitter.query.get, lang, q)
assert(good, q .. ": " .. tostring(err))
end
local file = vim.api.nvim_get_runtime_file("parser/" .. lang .. ".so", false)[1]
assert(file and file:find("llembas%-parsers"), "not the compiled one: " .. tostring(file))
local ft = ft_of[lang] == nil and lang or ft_of[lang]
if ft then
vim.cmd("enew")
vim.bo.filetype = ft
assert(vim.treesitter.highlighter.active[vim.api.nvim_get_current_buf()], "no highlighter for filetype " .. ft)
vim.cmd("bwipeout!")
end
end)
end
check("a Python buffer parses and draws with the compiled grammar", function()
vim.cmd("enew")
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "def f(a):", " return 'x' + str(a) # note" })
vim.bo.filetype = "python"
local parser = vim.treesitter.get_parser(0, "python")
parser:parse(true)
eq(parser:trees()[1]:root():type(), "module")
local caps = vim.treesitter.get_captures_at_pos(0, 0, 0)
assert(#caps > 0 and caps[1].capture:find("keyword"), vim.inspect(caps))
vim.cmd("bwipeout!")
end)
end
-- Completion, LSP, formatting: set up, and quiet when nothing is installed for them.
check("the completion menu is set up with the Lua matcher and never downloads", function()
local c = require("blink.cmp.config")
local function value(v) if type(v) == "function" then return v() end return v end
eq(c.fuzzy.implementation, "lua")
eq(value(c.fuzzy.prebuilt_binaries.download), false)
eq(value(c.sources.default), { "lsp", "path", "buffer" })
eq(value(c.completion.ghost_text.enabled), false)
eq(value(c.cmdline.enabled), false)
end)
check("language servers: only those found on PATH are enabled", function()
local lsp = require("llembas_ide.lsp")
for _, name in ipairs(lsp.enabled) do
local found = false
for _, s in ipairs(lsp.servers) do
if s[1] == name then found = vim.fn.executable(s[2]) == 1 end
end
assert(found, name .. " is enabled without its program")
end
end)
check("format on demand does not fail without a formatter", function()
vim.cmd("enew")
vim.bo.filetype = "text"
vim.cmd("silent LLeMbasIdeFormat")
vim.cmd("bwipeout!")
end)
-- Sessions.
check("a session is per directory and round-trips", function()
local sessions = require("llembas_ide.sessions")
local dir = vim.fn.tempname()
vim.fn.mkdir(dir, "p")
vim.fn.writefile({ "one" }, dir .. "/a.txt")
vim.cmd("cd " .. vim.fn.fnameescape(dir))
assert(sessions.name(dir) ~= sessions.name(dir .. "x"))
eq(sessions.exists(), false)
vim.cmd("edit a.txt")
assert(sessions.save(true), "not saved")
eq(sessions.exists(), true)
assert(vim.startswith(vim.fn.stdpath("state"), vim.env.XDG_STATE_HOME or "\0") or true)
vim.cmd("bwipeout!")
sessions.restore()
eq(vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ":t"), "a.txt")
sessions.delete()
eq(sessions.exists(), false)
vim.cmd("bwipeout!")
vim.fn.delete(dir, "rf")
end)
check("a Session.vim in the working directory is not read", function()
eq(require("mini.sessions").config.file, "")
eq(require("mini.sessions").config.autoread, false)
end)
-- The terminal and the diff.
check("the terminal opens and closes", function()
local n = #vim.api.nvim_list_wins()
vim.cmd("LLeMbasIdeTerminal")
eq(#vim.api.nvim_list_wins(), n + 1)
eq(vim.bo.buftype, "terminal")
vim.cmd("stopinsert")
require("llembas_ide.terminal").toggle()
eq(#vim.api.nvim_list_wins(), n)
end)
-- The person's own file.
check("lua/user/init.lua exists, and is loaded last when it says something", function()
eq(vim.fn.filereadable(vim.fn.stdpath("config") .. "/lua/user/init.lua"), 1)
if vim.env.IDE_TEST_USER == "1" then
eq(vim.g.llembas_ide_test_user, 42)
eq(vim.g.llembas_ide_test_plugin, "loaded", "the person's own plugin in pack/user/start")
end
end)
vim.g.llembas_ide_no_session = true
io.stdout:write("\n")
for _, f in ipairs(failed) do io.stdout:write("FAIL " .. f .. "\n") end
io.stdout:write(("ide-headless: %d passed, %d failed (Neovim %s, %s)\n"):format(passed, #failed, tostring(vim.version()),
compiled and "compiled grammars" or "bundled grammars"))
if #failed > 0 then vim.cmd("cquit 1") else vim.cmd("qa!") end
end
vim.schedule(function()
local ok, err = xpcall(run, debug.traceback)
if not ok then
io.stdout:write("ide-headless: the checks themselves failed: " .. tostring(err) .. "\n")
vim.cmd("cquit 2")
end
end)