-- The four bars, tested on the plugin alone (no distribution): what is on the screen at three -- sizes, the keys, the prompt's intent and attachment, the settings lines, the tree, what is -- remembered, and what is said to the terminal. Against the stand-in agent. -- Run by scripts/test-nvim.sh, with a screen (no -l), so that the bars are read as drawn: -- nvim --headless -u NONE -i NONE --cmd "set rtp+=nvim" -c "luafile nvim/tests/layout.lua" local here = vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":p:h") local repo = vim.fs.normalize(here .. "/../..") vim.opt.rtp:append(repo .. "/nvim") local levels = here .. "/agent_levels.py" local child = here .. "/child.lua" local work = vim.env.LLEMBAS_NVIM_TEST_WORK local own_work = false if not work or work == "" then -- Run by hand: a state directory of the test's own all the same. work = vim.fn.tempname() own_work = true vim.fn.mkdir(work .. "/state", "p") vim.env.XDG_STATE_HOME = work .. "/state" end work = vim.uv.fs_realpath(work) local tmp = work .. "/layout-project" local log = work .. "/layout-agent.log" vim.fn.mkdir(tmp .. "/src/deep", "p") vim.env.FAKE_AGENT_LOG = log vim.env.PYTHONDONTWRITEBYTECODE = "1" -- Whatever terminal this is run in says nothing about itself to the plugin. for _, name in ipairs({ "LLEMBAS_TERM", "LLEMBAS_TTY", "TMUX", "STY", "ZELLIJ", "KITTY_WINDOW_ID", "GHOSTTY_RESOURCES_DIR", "TERM_PROGRAM" }) do vim.env[name] = nil end vim.env.TERM = "dumb" local failed, ran = 0, 0 local function say(text) io.stdout:write(text .. "\n") end local function check(name, cond, extra) ran = ran + 1 if cond then say("ok " .. name) else failed = failed + 1 say("FAIL " .. name .. (extra ~= nil and ("\n " .. (type(extra) == "string" and extra or vim.inspect(extra))) or "")) end end local function wait(cond, ms) return vim.wait(ms or 8000, cond, 20) end local function t(keys) return vim.api.nvim_replace_termcodes(keys, true, false, true) end local function feed(keys) vim.api.nvim_feedkeys(t(keys), "mx", false) end local function sent() local out = {} for _, line in ipairs(vim.fn.filereadable(log) == 1 and vim.fn.readfile(log) or {}) do out[#out + 1] = vim.json.decode(line) end return out end local function calls(method) local out = {} for _, m in ipairs(sent()) do if m.method == method then out[#out + 1] = m.params end end return out end local function last_call(method) local c = calls(method); return c[#c] end local function row(r) vim.cmd("redraw") local out = {} for c = 1, vim.o.columns do out[#out + 1] = vim.fn.screenstring(r, c) end return table.concat(out) end local function top() return row(1) end local function bottom() return row(vim.o.lines - 1) end local function rtrim(s) return (s:gsub("%s+$", "")) end --- `text` in exactly `w` cells, padded with spaces (the tests' own, not the layout's). local function cell(text, w) return text .. (" "):rep(w - vim.fn.strdisplaywidth(text)) end local function width(s) return vim.fn.strdisplaywidth(s) end local function keys_part() return rtrim(vim.fn.strcharpart(bottom(), 0, vim.o.columns - (vim.o.columns < 60 and 15 or vim.o.columns < 120 and 20 or 35))) end --- Neovim's own segment at the right end. At full width its last six cells are the pending --- operator's (`%S`, Neovim's own text, which keys fed by a test leave things in): not compared. local function vim_part() local n = vim.o.columns < 60 and 15 or vim.o.columns < 120 and 20 or 35 return vim.fn.strcharpart(bottom(), vim.o.columns - n, n == 35 and 29 or n) end local function wins() local out = {} for _, w in ipairs(vim.api.nvim_tabpage_list_wins(0)) do if vim.api.nvim_win_get_config(w).relative == "" then local b = vim.api.nvim_win_get_buf(w) local ft = vim.bo[b].filetype local name = vim.api.nvim_buf_get_name(b) local kind = ft == "llembas-tree" and "tree" or ft == "llembas-settings" and "settings" or name:match("llembas://chat$") and "chat" or name:match("llembas://prompt$") and "prompt" or "code" out[kind] = { win = w, width = vim.api.nvim_win_get_width(w), height = vim.api.nvim_win_get_height(w) } end end return out end local function kinds(w) local names = vim.tbl_keys(w) table.sort(names) return table.concat(names, " ") end local function resize(cols, rows) vim.o.columns, vim.o.lines = cols, rows vim.api.nvim_exec_autocmds("VimResized", {}) vim.wait(60) vim.cmd("redraw") end local said_msgs = {} local function was_said(text) return table.concat(said_msgs, "|"):find(text, 1, true) ~= nil end --- Run a child Neovim (nvim/tests/child.lua) in `dir` with `env`; returns its stdout, stderr and code. local function run_child(dir, env) local e = vim.tbl_extend("force", { PYTHONDONTWRITEBYTECODE = "1", LLEMBAS_TEST_REPO = repo, TERM = "dumb" }, env or {}) local done = vim.system({ vim.v.progpath, "--headless", "-u", "NONE", "-i", "NONE", "--cmd", "set rtp+=" .. repo .. "/nvim", "-c", "luafile " .. child }, { cwd = dir, env = e, text = false }):wait(40000) return done.stdout or "", done.stderr or "", done.code end local function child_json(dir, env) local out, err, code = run_child(dir, env) local line = out:match("RESULT (%b{})") local ok, data = pcall(vim.json.decode, line or "", { luanil = { object = true, array = true } }) return ok and data or { failed = true, out = out, err = err, code = code } end local function run() vim.notify = function(m) said_msgs[#said_msgs + 1] = tostring(m) end vim.g.mapleader = " " vim.cmd("cd " .. vim.fn.fnameescape(tmp)) vim.fn.writefile({ "def a():", " return 1", "", "def b():", " return 2" }, tmp .. "/sample.py") vim.fn.writefile({ "x = 1" }, tmp .. "/src/util.py") vim.fn.writefile({ "y = 2" }, tmp .. "/src/deep/inner.py") vim.fn.writefile({ "# readme" }, tmp .. "/README.md") vim.fn.writefile({ "secret" }, tmp .. "/.env.example") vim.cmd("runtime plugin/llembas.lua") local L = require("llembas") local chat = require("llembas.chat") local layout = require("llembas.layout") local keys = require("llembas.keys") local tree = require("llembas.tree") local lines = require("llembas.lines") local store = require("llembas.store") local C = require("llembas.complete") -- ── The plugin on its own: nothing of the bars unless asked for. ──────────────────────────────── L.setup({ cmd = { "python3", levels } }) check("alone: without `bars` no tabline, no statusline of ours, no window but the one", vim.o.tabline == "" and not vim.o.statusline:find("llembas", 1, true) and #vim.api.nvim_tabpage_list_wins(0) == 1) check("alone: without `keys` no key is mapped", vim.fn.maparg("", "n") == "" and vim.fn.maparg("la", "n") == "" and vim.fn.maparg("", "n") == "") check("alone: the public functions are there", type(L.settings) == "function" and type(L.setting_lines) == "function" and type(L.choices) == "function" and type(L.set) == "function" and type(L.prompt) == "function" and type(L.intent) == "function" and type(L.effort) == "function" and type(L.ai) == "function" and type(L.menu) == "function") local s0 = L.settings() check("alone: settings() says idle, no session, AI and suggestions on, the intent ask", s0.state == "idle" and s0.session == false and s0.ai == true and s0.autocomplete == true and s0.intent == "ask" and s0.model == nil, s0) -- ── One option turns the bars on. 160×45. ─────────────────────────────────────────────────────── vim.o.columns, vim.o.lines = 160, 45 L.setup({ bars = true }) vim.cmd("edit " .. vim.fn.fnameescape(tmp .. "/sample.py")) local fbuf = vim.api.nvim_get_current_buf() vim.wait(100) local w = wins() check("160×45: the windows are tree, code, settings, chat and prompt", kinds(w) == "chat code prompt settings tree", kinds(w)) check("160×45: the tree is 26 columns, the right bar 42, the code has the rest", w.tree.width == 26 and w.chat.width == 42 and w.settings.width == 42 and w.prompt.width == 42 and w.code.width == 160 - 26 - 42 - 2, w) check("160×45: the settings are four lines, the prompt three and its chip's row", w.settings.height == 4 and w.prompt.height == 4, w) check("160×45: the focus is in the code", vim.api.nvim_get_current_buf() == fbuf) check("160×45: the top bar, cell for cell", top() == " ● idle │ " .. cell("no conversation yet", 160 - 2 - 17 - 3 - 3 - 32 - 3 - 8 - 3 - 6) .. " │ " .. cell("— · — · —", 32) .. " │ ctx — │ AC on ", top()) check("160×45: the bottom bar in the code shows the keys that always arrive (no chord was seen)", keys_part() == " Spc la ask · Tab accept suggestion · Alt+→ one word · Spc lp menu · Alt+←→ bars · Spc lb tree · Spc ll chat", keys_part()) check("160×45: and Neovim's own state at its right end, in cells of fixed width", vim_part() == " NOR" .. (" "):rep(19) .. "1:1 ", "[" .. vim_part() .. "]") check("the bars are exactly as wide as the screen", width(top()) == 160 and width(bottom()) == 160) -- Every width from 30 to 220: the rows are exactly that wide, never wrapped, never short. local bad = {} for cols = 30, 220 do if width(layout.top_text(cols)) ~= cols then bad[#bad + 1] = "top " .. cols end for _, where in ipairs({ "code", "tree", "prompt", "settings", "chat", "change" }) do local seg = cols < 60 and 15 or cols < 120 and 20 or 35 if width(layout.bottom_text(where, cols)) > cols - seg then bad[#bad + 1] = where .. " " .. cols end end end check("every width from 30 to 220: the top bar is exactly that wide and the keys never pass their room", #bad == 0, bad) local _, cut = layout.fit({ { text = "a title that is much too long to fit", flex = true, min = 8, prio = 2 }, { text = "x", width = 5, prio = 1 } }, 24) check("a text that does not fit its cell is cut with an ellipsis; nothing wraps", cut == " a title that … │ x " and width(cut) == 24, "[" .. cut .. "]") -- ── The keys: one table. ──────────────────────────────────────────────────────────────────────── local missing = {} local function mapped(mode, lhs, buf) local m = vim.fn.maparg(lhs, mode, false, true) if next(m) == nil then return false end if buf then return m.buffer == 1 end return true end for _, k in ipairs(keys.table) do if not k.virtual then if k.chord then for _, mode in ipairs({ "n", "x", "i" }) do if not mapped(mode, k.chord) then missing[#missing + 1] = mode .. " " .. k.chord end end end if not k.scope then for _, lhs in ipairs(keys.alts(k)) do for _, mode in ipairs(k.modes or { "n" }) do if not mapped(mode, keys.lhs(lhs)) then missing[#missing + 1] = mode .. " " .. keys.lhs(lhs) end end end end end end check("keys: every chord and every key after the prefix of the table is mapped", #missing == 0, missing) local in_table = {} for _, k in ipairs(keys.table) do for _, lhs in ipairs(type(k.chord) == "table" and k.chord or { k.chord }) do in_table[t(lhs)] = true end for _, lhs in ipairs(keys.alts(k)) do in_table[t(keys.lhs(lhs))] = true end end local strays = {} for _, mode in ipairs({ "n", "x", "i" }) do for _, m in ipairs(vim.api.nvim_get_keymap(mode)) do if (m.desc or ""):match("^LLeMbas: ") and not in_table[t(m.lhs)] and t(m.lhs) ~= t(L.config.complete.trigger) then strays[#strays + 1] = mode .. " " .. m.lhs end end end check("keys: every mapping of LLeMbas's is in the table (but the one that asks for a suggestion, a setting)", #strays == 0, strays) local shown_unbound = {} for _, where in ipairs({ "code", "tree", "prompt", "settings", "chat", "change" }) do for _, form in ipairs({ true, false }) do for _, k in ipairs(keys.for_bar(where, form)) do local e = keys.by_id[k.id] if not (e.chord or #keys.alts(e) > 0) then shown_unbound[#shown_unbound + 1] = where .. " " .. k.id end end end end check("keys: nothing the bottom bar can show is without a key", #shown_unbound == 0, shown_unbound) check("keys: a key is written as the owner's table writes it", keys.written("") == "^⇧A" and keys.written("") == "Alt+^⇧Y" and keys.written("") == "^Tab" and keys.written("") == "Alt+^M" and keys.written("") == "Alt+←" and keys.written("a") == "Spc la" and keys.written("") == "Enter", { keys.written(""), keys.written("a") }) -- The bars' own keys, in their buffers. local scoped = {} local bufs = { tree = tree.buf(), settings = lines.buf(), prompt = chat.ibuf, chat = chat.tbuf } for _, k in ipairs(keys.table) do if k.scope and bufs[k.scope] and not k.virtual then for _, lhs in ipairs(keys.alts(k)) do local m = vim.api.nvim_buf_call(bufs[k.scope], function() return { vim.fn.maparg(lhs, "n", false, true).buffer, vim.fn.maparg(lhs, "i", false, true).buffer } end) if m[1] ~= 1 and m[2] ~= 1 then scoped[#scoped + 1] = k.scope .. " " .. lhs end end end end check("keys: every key of the tree, the settings lines, the prompt and the transcript is mapped in its buffer", #scoped == 0, scoped) -- ── The bottom bar follows the focus. ─────────────────────────────────────────────────────────── layout.focus("tree") check("focus tree: the bar shows the tree's keys", keys_part() == " Enter open · a new · r rename · d delete · / filter · Alt+→ code · . hidden files · Spc la ask · Spc lp menu · Spc lb tree", keys_part()) feed("") check("Alt+→ from the tree is the code", vim.api.nvim_get_current_buf() == fbuf and layout.where() == "code") feed("") check("Alt+→ from the code is the prompt", vim.api.nvim_get_current_buf() == chat.ibuf and layout.where() == "prompt") check("focus prompt: the bar shows the prompt's keys", keys_part() == " Enter send · Tab ask/rewrite/add · Spc lm mode · Spc lM model · Esc back to code · Spc l. stop", keys_part()) feed("") check("Alt+← from the prompt is the code", vim.api.nvim_get_current_buf() == fbuf) vim.api.nvim_set_current_win(w.settings.win) check("focus settings: the bar shows the settings' keys", keys_part() == " Enter change · ↑↓ move · Esc back · Alt+← code", keys_part()) vim.api.nvim_set_current_win(w.chat.win) check("focus transcript: the bar shows the transcript's keys", keys_part() == " Enter open file · K tool output · i prompt · Alt+← code · Spc l. stop · Spc la ask · Spc lp menu · Spc ll chat", keys_part()) layout.focus("code") -- A chord that arrives proves the terminal reports them: from then the bar shows the chords. check("chords: none seen, none assumed (no terminal says it reports them)", keys.chords() == false) feed("") check("Ctrl+Shift+E hides the tree", wait(function() return not tree.is_open() end) and wins().code.width == 160 - 43) check("chords: one arrived, so they arrive", keys.chords() == true) check("chords: that is remembered for this terminal", store.terminal(keys._terminal_id()) == true, keys._terminal_id()) feed("") check("Ctrl+Shift+E shows it again, 26 columns", wait(function() return tree.is_open() end) and wins().tree.width == 26) check("code, chords: the owner's line", keys_part() == " ^⇧A ask · Tab accept suggestion · Alt+→ one word · ^⇧P menu · Alt+←→ bars · ^⇧E tree · ^⇧D chat", keys_part()) layout.focus("tree") check("tree, chords: the owner's line", vim.startswith(keys_part(), " Enter open · a new · r rename · d delete · / filter · Alt+→ code"), keys_part()) layout.focus("chat") check("prompt, chords: the owner's line", vim.startswith(keys_part(), " Enter send · Tab ask/rewrite/add · ^Tab mode · ^⇧M model · Esc back to code"), keys_part()) vim.api.nvim_set_current_win(wins().settings.win) check("settings, chords: the owner's line", vim.startswith(keys_part(), " Enter change · ↑↓ move · Esc back"), keys_part()) layout.focus("code") feed("") check("Ctrl+Shift+D hides the right bar: settings, chat and prompt", wait(function() return not chat.is_open() end) and kinds(wins()) == "code tree", kinds(wins())) feed("") check("Ctrl+Shift+D shows it again", wait(function() return chat.is_open() end) and kinds(wins()) == "chat code prompt settings tree", kinds(wins())) check("the focus stayed in the code", vim.api.nvim_get_current_buf() == fbuf) -- ── The prompt: its prefix, Tab, the attachment. ──────────────────────────────────────────────── local function prompt_rows() local pw = wins().prompt.win local pos = vim.fn.win_screenpos(pw) local out = {} for r = pos[1], pos[1] + vim.api.nvim_win_get_height(pw) - 1 do out[#out + 1] = vim.fn.strcharpart(row(r), pos[2] - 1, vim.api.nvim_win_get_width(pw)) end return out end local pr = prompt_rows() check("prompt: the row above it is the chip's and is empty; the first line begins with what Enter does", rtrim(pr[1]) == "" and pr[2] == "ask ›" .. (" "):rep(42 - 5), pr) layout.focus("chat") feed("") check("Tab in an empty prompt: rewrite", L.intent() == "rewrite" and vim.startswith(prompt_rows()[2], "rewrite › "), prompt_rows()) feed("") check("Tab again: add", L.intent() == "add" and vim.startswith(prompt_rows()[2], "add › "), prompt_rows()) feed("i") check("Tab again, while typing: ask", L.intent() == "ask" and vim.startswith(prompt_rows()[2], "ask › "), prompt_rows()) check("Tab in an empty prompt leaves it empty", table.concat(vim.api.nvim_buf_get_lines(chat.ibuf, 0, -1, false), "") == "") vim.api.nvim_buf_set_lines(chat.ibuf, 0, -1, false, { "some text" }) vim.bo[chat.ibuf].expandtab = false feed("A") check("Tab with text in the prompt is Neovim's own Tab, and what Enter does stays", L.intent() == "ask" and vim.api.nvim_buf_get_lines(chat.ibuf, 0, -1, false)[1] == "some text\t", vim.api.nvim_buf_get_lines(chat.ibuf, 0, -1, false)) vim.api.nvim_buf_set_lines(chat.ibuf, 0, -1, false, { "a long line that is wider than the bar and so goes on to a second row of the prompt" }) local wrapped = prompt_rows() check("a long prompt keeps one left edge: the rows under the first are indented by the prefix", vim.startswith(wrapped[2], "ask › a long") and vim.startswith(wrapped[3], (" "):rep(10)) and wrapped[3]:sub(11, 11) ~= " ", wrapped) vim.api.nvim_buf_set_lines(chat.ibuf, 0, -1, false, {}) feed("") check("Esc in the prompt is back in the code", vim.api.nvim_get_current_buf() == fbuf) -- A selection comes along. vim.api.nvim_win_set_cursor(0, { 1, 0 }) feed("Vj") check("Ctrl+Shift+A with a selection: the prompt has the focus and the chip names the lines", vim.api.nvim_get_current_buf() == chat.ibuf and rtrim(prompt_rows()[1]) == " sample.py:1-2", prompt_rows()) check("…and it is an ask", L.intent() == "ask" and L.settings().attachment == "sample.py:1-2") vim.api.nvim_buf_set_lines(chat.ibuf, 0, -1, false, { "META what is this?" }) feed("") check("Enter sends", wait(function() return last_call("session/prompt") ~= nil end) and wait(function() return not L._state.busy end)) local p = last_call("session/prompt") local ed = ((p._meta or {}).lembas or {}).editor check("ask: the question goes with the selection as the editor's context", ed and ed.selection and ed.selection.start == 1 and ed.selection["end"] == 2 and ed.selection.text == "def a():\n return 1" and p.prompt[#p.prompt].text == "META what is this?", p) check("ask: the transcript names what came along", chat.text():find("[sample.py:1-2]\nMETA what is this?", 1, true) ~= nil, chat.text()) check("ask: the chip is gone and the prompt is empty", rtrim(prompt_rows()[1]) == "" and L.settings().attachment == nil and table.concat(vim.api.nvim_buf_get_lines(chat.ibuf, 0, -1, false), "") == "") check("a session started: the top bar and the settings lines say the model, the mode, the effort and the context", wait(function() return L.settings().context == 4 end) and top():find("│ " .. cell("model-a · manual · default", 32) .. " │ ctx 4% │ AC on ", 1, true) ~= nil and vim.deep_equal(lines.lines(), { "model model-a", "mode manual", "effort default", "autocomplete on" }), { top(), lines.lines() }) check("idle again", vim.startswith(top(), " ● idle │ "), top()) -- A bare cursor: the line comes along as a point. layout.focus("code") vim.api.nvim_win_set_cursor(0, { 4, 0 }) feed(" la") check("la without a selection: the cursor's line comes along as a point", vim.api.nvim_get_current_buf() == chat.ibuf and rtrim(prompt_rows()[1]) == " sample.py:4", prompt_rows()) feed("") -- Rewrite: the selection is rewritten in place, shown as changes. vim.api.nvim_win_set_cursor(0, { 1, 0 }) feed("Vj le") check("le with a selection: the prompt says rewrite and carries the lines", vim.api.nvim_get_current_buf() == chat.ibuf and L.intent() == "rewrite" and vim.startswith(prompt_rows()[2], "rewrite › ") and rtrim(prompt_rows()[1]) == " sample.py:1-2", prompt_rows()) vim.api.nvim_buf_set_lines(chat.ibuf, 0, -1, false, { "REPLACE def a():\\n return 100" }) local before = #calls("session/prompt") feed("") check("rewrite: the lines go to the rewrite, not to the chat", wait(function() return last_call("_lembas/edit") ~= nil end) and last_call("_lembas/edit").text == "def a():\n return 1" and #calls("session/prompt") == before, last_call("_lembas/edit")) check("rewrite: shown in the file as a change, nothing written", wait(function() return require("llembas.diff").pending(fbuf) == 1 end) and vim.api.nvim_buf_get_lines(fbuf, 1, 2, false)[1] == " return 1") check("rewrite: the focus is in the code, and what Enter does is ask again", vim.api.nvim_get_current_buf() == fbuf and L.intent() == "ask" and L.settings().attachment == nil) check("on a proposed change, chords: the owner's line", wait(function() return keys_part() == " ^⇧Y accept · ^⇧J reject · ]h next · Alt+^⇧Y accept all · [h previous · ^⇧P menu" end), keys_part()) local change = {} for _, k in ipairs(keys.table) do if k.scope == "change" then for _, lhs in ipairs(keys.alts(k)) do if vim.fn.maparg(lhs, "n", false, true).buffer ~= 1 then change[#change + 1] = lhs end end end end check("keys: with a change shown, the change's own keys are mapped in the buffer", #change == 0, change) feed("") check("Ctrl+Shift+Y accepts it", vim.api.nvim_buf_get_lines(fbuf, 1, 2, false)[1] == " return 100" and require("llembas.diff").pending(fbuf) == 0) check("…and the bar is the code's again", wait(function() return vim.startswith(keys_part(), " ^⇧A ask") end), keys_part()) -- Add: new code at the point. vim.api.nvim_win_set_cursor(0, { 3, 0 }) feed(" lg") check("lg: the prompt says add", L.intent() == "add" and vim.startswith(prompt_rows()[2], "add › ") and rtrim(prompt_rows()[1]) == " sample.py:3", prompt_rows()) vim.api.nvim_buf_set_lines(chat.ibuf, 0, -1, false, { "REPLACE # a comment" }) feed("") check("add: asked for at the point, and shown as a change", wait(function() return require("llembas.diff").pending(fbuf) == 1 end) and last_call("_lembas/edit").instruction == "At this line, REPLACE # a comment", last_call("_lembas/edit")) feed("") check("Ctrl+Shift+J rejects it", require("llembas.diff").pending(fbuf) == 0 and vim.api.nvim_buf_get_lines(fbuf, 2, 3, false)[1] == "") -- ── Tab's three jobs never collide. ───────────────────────────────────────────────────────────── C._typing = function() return true end vim.api.nvim_win_set_cursor(0, { 5, 12 }) C.request(true) check("a suggestion is shown, and is the buffer's while it is", wait(function() return C.visible() end) and vim.fn.maparg("", "i", false, true).buffer == 1 and vim.fn.maparg("", "i", false, true).buffer == 1) C.accept() check("accepted: is nobody's in the code again (Neovim's own)", not C.visible() and vim.fn.maparg("", "i", false, true).buffer ~= 1) check("Alt+→ in the code without a suggestion is the bars' key, with one it was the suggestion's", (vim.fn.maparg("", "i", false, true).desc or ""):find("bar on the right", 1, true) ~= nil) vim.cmd("silent undo") -- ── The settings lines: Enter opens the choices in place. ────────────────────────────────────── vim.api.nvim_set_current_win(wins().settings.win) vim.api.nvim_win_set_cursor(0, { 2, 0 }) feed("") check("Enter on mode: its choices open under the line, in the bar, the current one marked", wait(function() return #lines.lines() == 8 end) and vim.deep_equal(vim.list_slice(lines.lines(), 2, 6), { "mode manual", " ● manual", " ○ edit", " ○ auto", " ○ plan" }) and wins().settings.height == 8 and vim.api.nvim_win_get_cursor(0)[1] == 3, { lines.lines(), wins().settings }) check("…and no window floats in the middle of the screen", (function() for _, win in ipairs(vim.api.nvim_list_wins()) do if vim.api.nvim_win_get_config(win).relative ~= "" then return false end end return true end)()) feed("j") check("Enter on a choice sets it through the agent, the list closes, the line and the top bar follow", wait(function() return L._state.mode == "edit" end) and last_call("session/set_mode").modeId == "edit" and wait(function() return lines.lines()[2] == "mode edit" and #lines.lines() == 4 end) and top():find("model-a · edit · default", 1, true) ~= nil and wins().settings.height == 4, { lines.lines(), top() }) vim.api.nvim_win_set_cursor(0, { 3, 0 }) feed("") check("Enter on effort: the levels", wait(function() return #lines.lines() == 4 + #L.EFFORTS end) and lines.lines()[4] == " ○ off", lines.lines()) feed("") check("Esc closes the choices and stays in the lines", #lines.lines() == 4 and layout.where() == "settings") L.set("effort", "high") check("effort: set through the agent (`_lembas/configure`), and shown", wait(function() return (last_call("_lembas/configure") or {}).effort == "high" end) and wait(function() return lines.lines()[3] == "effort high" end) and top():find("model-a · edit · high", 1, true) ~= nil, { lines.lines(), top() }) said_msgs = {} L.set("effort", "xhigh") check("effort: a level the model does not offer is said, with the level it runs on", wait(function() return was_said("does not offer the effort xhigh; it runs on medium") end) and L.settings().effort == "medium", said_msgs) L.set("effort", "high") wait(function() return L.settings().effort == "high" end) vim.api.nvim_win_set_cursor(0, { 1, 0 }) feed("") check("Enter on model: the account's models, by name", wait(function() return #lines.lines() == 7 end) and vim.deep_equal(vim.list_slice(lines.lines(), 2, 4), { " ● Model A", " ○ Model B", " ○ Model C" }), lines.lines()) feed("j") check("model: set through the agent, and shown", wait(function() return L._state.model == "example/model-b" end) and wait(function() return lines.lines()[1] == "model model-b" end), lines.lines()) vim.api.nvim_win_set_cursor(0, { 4, 0 }) feed("j") check("autocomplete: off from its line; the top bar says so", wait(function() return L.settings().autocomplete == false end) and wait(function() return lines.lines()[4] == "autocomplete off" end) and top():find("│ AC off ", 1, true) ~= nil, { lines.lines(), top() }) L.set("autocomplete", "on") feed("") check("Esc from the lines is back in the code", vim.api.nvim_get_current_buf() == fbuf) feed("") check("Ctrl+Tab switches to the next mode", wait(function() return L._state.mode == "auto" end), L._state.mode) -- The state the top bar says. layout.focus("chat") vim.api.nvim_buf_set_lines(chat.ibuf, 0, -1, false, { "SLOW" }) feed("") check("a turn runs: the top bar says what the agent does", wait(function() return top():match("^ ◐ %a+ ") ~= nil end) and (L.settings().state == "writing" or L.settings().state == "thinking" or L.settings().state == "tools"), top()) feed("") check("Ctrl+Shift+G stops it", wait(function() return not L._state.busy end) and wait(function() return vim.startswith(top(), " ● idle ") end), top()) local asked vim.ui.select = function(items, opts, cb) asked = { items = items, opts = opts, cb = cb } end vim.api.nvim_buf_set_lines(chat.ibuf, 0, -1, false, { "PERM" }) feed("") check("the agent asks: the top bar says it waits for you", wait(function() return asked ~= nil end) and wait(function() return vim.startswith(top(), " ● waiting for you │ ") end), top()) asked.cb(asked.items[1]) wait(function() return not L._state.busy end) -- The menu has every command and every setting, effort among them. asked = nil feed("") local menu = {} for _, i in ipairs(asked and asked.items or {}) do menu[#menu + 1] = asked.opts.format_item(i) end local menu_text = table.concat(menu, "\n") check("Ctrl+Shift+P: the menu has the settings — model, mode, effort, autocomplete — and the commands with their keys", menu_text:find("Model: example/model-b", 1, true) and menu_text:find("Mode: auto", 1, true) and menu_text:find("Effort: high", 1, true) and menu_text:find("Autocomplete: on", 1, true) and menu_text:find("Tree: shown", 1, true) and menu_text:find("Stop the running turn [^⇧G]", 1, true) and menu_text:find("Earlier conversations [Spc ls]", 1, true), menu_text) local in_menu = {} for _, k in ipairs(keys.table) do if k.run and not k.scope and not menu_text:find(k.desc, 1, true) then in_menu[#in_menu + 1] = k.id end end check("…every action of the key table is in the menu", #in_menu == 0, in_menu) for _, i in ipairs(asked.items) do if i.text:match("^Effort") then local inner vim.ui.select = function(items, _, cb) inner = items; cb("low") end i.run() check("menu: Effort opens the levels, and the one picked is set", inner and vim.deep_equal(inner, L.EFFORTS) and wait(function() return L.settings().effort == "low" end), inner) end end layout.focus("code") -- ── 100×30 and 80×24: the bars fold. ──────────────────────────────────────────────────────────── vim.api.nvim_win_set_cursor(0, { 1, 0 }) vim.bo[fbuf].modified = false resize(100, 30) w = wins() check("100×30: the chat folds away, the tree stays", kinds(w) == "code tree" and w.tree.width == 26 and w.code.width == 73, w) check("100×30: the top bar, narrower cells", top() == " ● idle │ " .. cell("conversation", 100 - 2 - 17 - 3 - 3 - 32 - 3 - 8 - 3 - 6) .. " │ " .. cell("model-b · auto · low", 32) .. " │ ctx 4% │ AC on ", top()) check("100×30: the bottom bar is cut by priority, and Neovim's state is the short one", keys_part() == " ^⇧A ask · Tab accept suggestion · Alt+→ one word · ^⇧P menu · Alt+←→ bars" and vim_part() == " NOR" .. (" "):rep(10) .. "1:1 ", { keys_part(), "[" .. vim_part() .. "]" }) check("100×30: both rows are 100 wide", width(top()) == 100 and width(bottom()) == 100) check("100×30: the layout says what is folded", layout.options().folded.chat == true and layout.options().folded.tree == false) feed("") check("100×30: Ctrl+Shift+D shows the chat all the same", wait(function() return chat.is_open() end) and wins().chat.width == 42, wins()) feed("") wait(function() return not chat.is_open() end) L._state.busy = false resize(80, 24) w = wins() check("80×24: only the code, all 80 columns", kinds(w) == "code" and w.code.width == 80, w) check("80×24: the top bar", top() == " ● idle │ " .. cell("conversation", 80 - 2 - 9 - 3 - 3 - 26 - 3 - 8 - 3 - 6) .. " │ " .. cell("model-b · auto · low", 26) .. " │ ctx 4% │ AC on ", top()) check("80×24: the bottom bar", keys_part() == " ^⇧A ask · Tab accept suggestion · Alt+→ one word · ^⇧P menu" and vim_part() == " NOR" .. (" "):rep(10) .. "1:1 ", { keys_part(), "[" .. vim_part() .. "]" }) check("80×24: both rows are 80 wide", width(top()) == 80 and width(bottom()) == 80) feed("") check("80×24: Alt+→ opens the chat and goes to its prompt", chat.is_open() and vim.api.nvim_get_current_buf() == chat.ibuf) feed("") resize(50, 12) check("50×12: still one row each, exactly as wide, the least needed gone", width(top()) == 50 and width(bottom()) == 50 and vim.startswith(top(), " ● idle │ ") and vim_part() == " NOR" .. (" "):rep(5) .. "1:1 ", { top(), bottom() }) resize(160, 14) w = wins() check("160×14: too low for the settings lines; the prompt is one row", kinds(w) == "chat code prompt tree" and w.prompt.height == 2, w) resize(160, 45) check("160×45 again: everything is back", kinds(wins()) == "chat code prompt settings tree", kinds(wins())) -- Neovim's own state in the bottom bar. layout.focus("code") vim.api.nvim_buf_set_lines(fbuf, 0, 1, false, { "def a(): # changed" }) feed("qq") check("modified and recording: the flags are in their cells", vim_part() == " NOR + @q" .. (" "):rep(14) .. "1:1 ", "[" .. vim_part() .. "]") feed("q") vim.api.nvim_win_set_cursor(0, { 2, 4 }) check("the cursor's line and column", vim_part():sub(-10) == " 2:5 ", "[" .. vim_part() .. "]") vim.cmd("silent! undo") -- ── The tree. ─────────────────────────────────────────────────────────────────────────────────── check("tree: the directory as lines, directories first, the file you are in shown", vim.deep_equal(tree.lines(), { " ▸ src/", " README.md", " sample.py" }), tree.lines()) layout.focus("tree") vim.api.nvim_win_set_cursor(0, { 1, 0 }) feed("") check("tree: Enter opens a directory", vim.deep_equal(tree.lines(), { " ▾ src/", " ▸ deep/", " util.py", " README.md", " sample.py" }), tree.lines()) feed("") check("tree: ← closes it", tree.lines()[1] == " ▸ src/" and #tree.lines() == 3, tree.lines()) feed("") check("tree: → opens it and leaves it open", tree.lines()[1] == " ▾ src/" and #tree.lines() == 5, tree.lines()) vim.api.nvim_win_set_cursor(0, { 3, 0 }) feed("") check("tree: Enter on a file opens it in the code's window", vim.api.nvim_buf_get_name(0) == tmp .. "/src/util.py" and layout.where() == "code") vim.cmd("edit " .. vim.fn.fnameescape(tmp .. "/src/deep/inner.py")) check("tree: the file you are in is followed — its directories open, its row under the tree's cursor", tree.lines()[3] == " inner.py" and vim.api.nvim_win_get_cursor(tree.win())[1] == 3, tree.lines()) layout.focus("tree") feed(".") check("tree: . shows hidden files", vim.tbl_contains(tree.lines(), " .env.example"), tree.lines()) feed(".") check("tree: . hides them again", not vim.tbl_contains(tree.lines(), " .env.example")) tree.set_filter("inner") check("tree: a filter shows the files whose path has the text", vim.deep_equal(tree.lines(), { " src/deep/inner.py" }), tree.lines()) tree.set_filter("") vim.api.nvim_win_set_cursor(0, { 1, 0 }) tree.create("new/thing.py") check("tree: a new file, its directories made, opened", vim.fn.filereadable(tmp .. "/src/new/thing.py") == 1 and vim.api.nvim_buf_get_name(0) == tmp .. "/src/new/thing.py", tree.lines()) layout.focus("tree") tree.create("../../../outside.py") tree.create("/somewhere/else.py") check("tree: nothing is made outside the tree's directory", vim.fn.filereadable(vim.fs.dirname(tmp) .. "/outside.py") == 0 and vim.fn.isdirectory("/somewhere") == 0) vim.api.nvim_win_set_cursor(0, { 1, 0 }) tree.create("pkg/") check("tree: a name ending in / makes a directory", vim.fn.isdirectory(tmp .. "/src/pkg") == 1) for i, l in ipairs(tree.lines()) do if l:find("thing.py", 1, true) then vim.api.nvim_win_set_cursor(tree.win(), { i, 0 }) end end tree.rename("src/pkg/renamed.py") check("tree: rename moves the file, and its open buffer with it", vim.fn.filereadable(tmp .. "/src/pkg/renamed.py") == 1 and vim.fn.filereadable(tmp .. "/src/new/thing.py") == 0 and vim.fn.bufnr(tmp .. "/src/pkg/renamed.py") ~= -1, tree.lines()) local question tree._confirm = function(q) question = q; return false end tree._trash = function() return nil end tree.delete() check("tree: delete asks first, and says when there is no trash; a no deletes nothing", question == "Delete src/pkg/renamed.py for good? (No trash was found on this system.)" and vim.fn.filereadable(tmp .. "/src/pkg/renamed.py") == 1, question) local trash = work .. "/trash.sh" vim.fn.writefile({ "#!/bin/sh", 'mkdir -p "' .. work .. '/trashed" && mv "$1" "' .. work .. '/trashed/"' }, trash) vim.fn.setfperm(trash, "rwx------") tree._trash = function() return { trash } end tree._confirm = function(q) question = q; return true end tree.delete() check("tree: with a trash the file goes there", question == "Move src/pkg/renamed.py to the trash?" and vim.fn.filereadable(tmp .. "/src/pkg/renamed.py") == 0 and vim.fn.filereadable(work .. "/trashed/renamed.py") == 1, question) if vim.fn.executable("git") == 1 then vim.fn.system({ "git", "-C", tmp, "init", "-q", "-b", "trunk" }) vim.fn.system({ "git", "-C", tmp, "add", "README.md" }) tree.refresh() check("tree: one git mark a row — a new file, an added one, a directory with changes inside", wait(function() return vim.tbl_contains(tree.lines(), "? sample.py") end, 5000) and vim.tbl_contains(tree.lines(), "A README.md") and tree.lines()[1] == "• ▾ src/", tree.lines()) check("the top bar's right end has the branch when it fits", wait(function() return top():find("│ trunk ", 1, true) ~= nil end, 3000), top()) end layout.focus("code") vim.cmd("edit " .. vim.fn.fnameescape(tmp .. "/sample.py")) -- ── What a model or a file's name says is text. ───────────────────────────────────────────────── local title = L._state.title L._state.title = "50% done\nnext %#Error#line %{1+1}" check("a title is shown as text on one line: no line break, no highlight or expression of its own, the bar as wide as ever", top():find("│ 50% done next %#Error#line %{1+1} ", 1, true) ~= nil and width(top()) == 160, top()) L._state.title = title local odd = tmp .. "/odd\nname.py" if pcall(vim.fn.writefile, { "" }, odd) and vim.uv.fs_stat(odd) then tree.refresh() check("a file's name with a line break in it is one row of the tree", vim.tbl_contains(tree.lines(), " odd?name.py") or vim.tbl_contains(tree.lines(), "? odd?name.py"), tree.lines()) vim.fn.delete(odd) tree.refresh() end vim.cmd("tabnew") check("a second tab page: the top bar says which one this is", top():find("│ tab 2/2 ", 1, true) ~= nil and width(top()) == 160, top()) vim.cmd("tabclose") vim.cmd("2,3LLeMbasPrompt rewrite") check(":2,3LLeMbasPrompt rewrite: the lines come along and the prompt says rewrite", L.intent() == "rewrite" and L.settings().attachment == "sample.py:2-3" and vim.api.nvim_get_current_buf() == chat.ibuf, L.settings()) L.intent("ask") feed("") -- ── AI off: a plain editor. ───────────────────────────────────────────────────────────────────── local agent = L._state.conn and L._state.conn.proc.pid feed("") check("Ctrl+Shift+I: LLeMbas is off — the agent is stopped, the chat put away, the top bar says so", L.settings().ai == false and L._state.conn == nil and wait(function() return not chat.is_open() end) and vim.startswith(top(), " ● AI off │ ") and kinds(wins()) == "code tree", { top(), kinds(wins()) }) check("AI off: the agent's process ends", wait(function() return not (agent and vim.uv.kill(agent, 0) == 0) end, 7000)) local n_complete = #calls("_lembas/complete") C.request(false) vim.wait(200) check("AI off: no suggestion is asked for", #calls("_lembas/complete") == n_complete) said_msgs = {} feed("") check("AI off: asking says that it is off, and opens nothing", was_said("LLeMbas is off") and not chat.is_open(), said_msgs) feed("") check("Ctrl+Shift+I again: on, and the chat is back", L.settings().ai == true and wait(function() return chat.is_open() end)) -- ── Each bar can be switched off. ─────────────────────────────────────────────────────────────── layout.toggle("top", false) check("the top bar off: Neovim's own tabline setting is back", vim.o.showtabline ~= 2 and vim.o.tabline == "") layout.toggle("bottom", false) check("the bottom bar off: Neovim's own statusline is back", vim.o.statusline == "") layout.toggle("top", true) layout.toggle("bottom", true) check("…and on again", vim.startswith(top(), " ● idle") and vim.startswith(keys_part(), " ^⇧A ask"), { top(), keys_part() }) -- ── Remembered across restarts, per directory. ────────────────────────────────────────────────── local dir_a, dir_b, dir_c = work .. "/dir-a", work .. "/dir-b", work .. "/dir-c" for _, d in ipairs({ dir_a, dir_b, dir_c }) do vim.fn.mkdir(d, "p") end local state_home = work .. "/remember-state" vim.fn.mkdir(state_home, "p") local env = { XDG_STATE_HOME = state_home, FAKE_AGENT_LOG = work .. "/remember.log", PATH = vim.env.PATH, HOME = vim.env.HOME } local function step(dir, name, more) return child_json(dir, vim.tbl_extend("force", env, { STEP = name }, more or {})) end local r = step(dir_a, "change") check("remember: in a first directory the mode, the effort, suggestions and the bars are changed", r.mode == "edit" and r.effort == "high" and r.autocomplete == false and r.tree == false and r.chat_width == 46, r) r = step(dir_b, "look") check("remember: another directory knows nothing of that", r.mode == nil and r.effort == nil and r.autocomplete == true and r.tree == true and r.chat_width == 42, r) r = step(dir_b, "change_b") check("remember: and is changed in its own way", r.mode == "plan" and r.tree == true, r) r = step(dir_a, "look") check("remember: the first directory, started again, has what was chosen there — before any session", r.mode == "edit" and r.effort == "high" and r.autocomplete == false and r.tree == false and r.chat_width == 46 and r.tree_open == false, r) r = step(dir_a, "session") check("remember: a session there starts on that mode and that effort", r.new_mode == "edit" and r.new_effort == "high" and r.mode == "edit" and r.effort == "high", r) r = step(dir_b, "look") check("remember: the second directory has its own", r.mode == "plan" and r.effort == nil and r.autocomplete == true and r.tree == true, r) r = step(dir_a, "default") r = step(dir_c, "look") check("remember: made the default, the first directory's choices are what a new directory starts with", r.mode == "edit" and r.effort == "high" and r.autocomplete == false and r.tree == false, r) r = step(dir_b, "look") check("remember: and a directory keeps what it chose itself (the mode), over the default it now takes the rest from", r.mode == "plan" and r.effort == "high" and r.tree == false, r) r = step(dir_a, "ai_off") r = step(dir_a, "look") check("remember: AI off is remembered too", r.ai == false, r) r = step(dir_b, "cd", { CD_TO = dir_a }) check("remember: after :cd the new directory's choices hold — AI off, its mode, its suggestions", r.ai == false and r.mode == "edit" and r.autocomplete == false, r) r = step(dir_a, "cd", { CD_TO = dir_b }) check("remember: and :cd back to a directory with AI on turns it on, with that directory's mode", r.ai == true and r.mode == "plan", r) local f = io.open(state_home .. "/nvim/llembas/settings.json") local stored = f and vim.json.decode(f:read("*a")) or {} if f then f:close() end check("remember: one file in Neovim's state directory, with a machine-wide level and one per directory, and no model in it", stored.global and stored.global.mode == "edit" and stored.directories[dir_a].mode == "edit" and stored.directories[dir_b].mode == "plan" and stored.directories[dir_a].model == nil and stored.global.model == nil, stored) local perm = vim.uv.fs_stat(state_home .. "/nvim/llembas/settings.json") check("remember: the file is the person's alone (0600)", perm and bit.band(perm.mode, 511) == 384, perm and bit.band(perm.mode, 511)) -- ── The terminal: the user var. ───────────────────────────────────────────────────────────────── local set, clear = "\27]1337;SetUserVar=llembas_ide=MQ==\7", "\27]1337;SetUserVar=llembas_ide=\7" check("user var: the bytes are OSC 1337 SetUserVar with base64 of 1, and an empty value to clear", layout.uservar_bytes("1") == set and layout.uservar_bytes("") == clear) local _, err = run_child(dir_c, vim.tbl_extend("force", env, { STEP = "start", LLEMBAS_TERM = "1" })) check("user var: in the LLeMbas terminal it is set on start and cleared on exit, and nothing else is written", err == set .. clear, vim.inspect(err)) _, err = run_child(dir_c, vim.tbl_extend("force", env, { STEP = "start", LLEMBAS_TTY = "pane-1" })) check("user var: $LLEMBAS_TTY counts as well", err == set .. clear, vim.inspect(err)) _, err = run_child(dir_c, vim.tbl_extend("force", env, { STEP = "start" })) check("user var: in any other terminal nothing is written", err == "", vim.inspect(err)) _, err = run_child(dir_c, vim.tbl_extend("force", env, { STEP = "start", LLEMBAS_TERM = "1", TMUX = "/tmp/tmux-1/default,1,0" })) check("user var: inside tmux it is wrapped for tmux to pass on", err == "\27Ptmux;\27\27]1337;SetUserVar=llembas_ide=MQ==\7\27\\" .. "\27Ptmux;\27\27]1337;SetUserVar=llembas_ide=\7\27\\", vim.inspect(err)) _, err = run_child(dir_c, vim.tbl_extend("force", env, { STEP = "plain", LLEMBAS_TERM = "1" })) check("user var: a plugin set up without keys or bars says nothing, in that terminal either", err == "", vim.inspect(err)) r = step(dir_c, "chords", { LLEMBAS_TERM = "1" }) check("chords: in the LLeMbas terminal the bar shows the chords from the start", r.chords == true and r.guess == true, r) r = step(dir_c, "chords", { TMUX = "/tmp/tmux-1/default,1,0", KITTY_WINDOW_ID = "1" }) check("chords: behind a multiplexer they are not assumed", r.guess == false, r) r = step(dir_c, "chords", { KITTY_WINDOW_ID = "1" }) check("chords: a terminal known to report them is taken at its word", r.guess == true, r) end vim.schedule(function() local ok, err = xpcall(run, debug.traceback) if not ok then failed = failed + 1 say("FAIL the checks themselves stopped: " .. tostring(err)) end local leftover = vim.fn.execute("messages") if leftover:match("E%d+:") then failed = failed + 1 say("FAIL an error message was left: " .. leftover) end say(("RAN %d, failed %d (layout)"):format(ran, failed)) pcall(function() require("llembas").restart(true) end) vim.cmd("cd /") if own_work then vim.fn.delete(work, "rf") end vim.cmd(failed == 0 and "qall!" or "cquit!") end)