From 6f1d304fd71c10ebbce83577a54d204f9bc3ef7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Bene=C5=A1?= Date: Tue, 1 Dec 2026 19:15:13 +0000 Subject: [PATCH] The LLeMbas layer's tests, without a terminal llembas/tests/run.lua runs the layer against a stand-in for the terminal; scripts/test-lua.sh runs it with lua5.4, lua, luajit or nvim -l. Co-Authored-By: Claude Fable 5.2 --- llembas/tests/fake_wezterm.lua | 184 +++++++ llembas/tests/run.lua | 926 +++++++++++++++++++++++++++++++++ scripts/test-lua.sh | 33 ++ 3 files changed, 1143 insertions(+) create mode 100644 llembas/tests/fake_wezterm.lua create mode 100644 llembas/tests/run.lua create mode 100755 scripts/test-lua.sh diff --git a/llembas/tests/fake_wezterm.lua b/llembas/tests/fake_wezterm.lua new file mode 100644 index 000000000..7136f0e27 --- /dev/null +++ b/llembas/tests/fake_wezterm.lua @@ -0,0 +1,184 @@ +-- LLeMbas TTY: a stand-in for the terminal's `wezterm` module, for the tests. +-- It records what the layer asks the terminal to do and does none of it, +-- except chmod, which is run for real so that the file mode can be checked. + +local F = {} + +function F.new(env) + env = env or {} + local w = { + GLOBAL = {}, + target_triple = env.target_triple or 'x86_64-unknown-linux-gnu', + executable_dir = env.executable_dir or '/nonexistent/bin', + home_dir = env.home_dir or '/nonexistent/home', + handlers = {}, + logs = {}, + spawned = {}, + child_results = env.child_results or {}, + } + + -- wezterm.action.Name(args) and wezterm.action.Name { … } give a table + -- that names the action; nothing more is needed to check what was bound. + w.action = setmetatable({}, { + __index = function(_, name) + return function(args) + return { action = name, args = args } + end + end, + }) + + function w.on(name, fn) + w.handlers[name] = w.handlers[name] or {} + table.insert(w.handlers[name], fn) + end + + function w.emit(name, ...) + local result + for _, fn in ipairs(w.handlers[name] or {}) do + result = fn(...) + end + return result + end + + function w.log_warn(...) + w.logs[#w.logs + 1] = table.concat({ ... }, ' ') + end + w.log_info = w.log_warn + w.log_error = w.log_warn + + function w.config_builder() + return {} + end + + function w.format(items) + local out = {} + for _, item in ipairs(items) do + if type(item) == 'table' and item.Text then + out[#out + 1] = item.Text + end + end + return table.concat(out) + end + + function w.run_child_process(args) + w.spawned[#w.spawned + 1] = args + if args[1] == 'chmod' then + local ok = os.execute(string.format("chmod %s '%s'", args[2], args[3])) + return ok == true or ok == 0, '', '' + end + local key = table.concat(args, ' ', 2) + local r = w.child_results[key] + if r then + return r[1], r[2], '' + end + return false, '', 'not found' + end + + function w.read_dir(dir) + local list = {} + local p = io.popen(string.format("ls -1 '%s' 2>/dev/null", dir)) + if p then + for name in p:lines() do + list[#list + 1] = dir .. '/' .. name + end + p:close() + end + return list + end + + function w.json_parse(text) + if env.json_parse then + return env.json_parse(text) + end + error 'not json' + end + + return w +end + +-- A pane that records what is sent to it. +function F.pane(spec) + local p = { + id = spec.id or 1, + vars = spec.vars or {}, + sent = {}, + splits = {}, + spec = spec, + } + function p:pane_id() + return self.id + end + function p:get_user_vars() + return self.vars + end + function p:get_metadata() + return spec.metadata + end + function p:is_alt_screen_active() + return spec.alt == true + end + function p:get_semantic_zones() + self.read_zones = true + return spec.zones or {} + end + function p:get_text_from_semantic_zone(zone) + self.read_text = true + return zone.text + end + function p:get_lines_as_text() + self.read_screen = true + return spec.screen or '' + end + function p:get_current_working_dir() + return spec.cwd + end + function p:get_foreground_process_name() + return spec.program + end + function p:get_domain_name() + return spec.domain or 'local' + end + function p:tab() + return spec.tab + end + function p:send_text(text) + self.sent[#self.sent + 1] = { 'text', text } + end + function p:send_paste(text) + self.sent[#self.sent + 1] = { 'paste', text } + end + function p:split(args) + self.splits[#self.splits + 1] = args + local new = F.pane { id = (spec.next_id or 100) + #self.splits } + self.last_split = new + return new + end + return p +end + +function F.tab(panes) + local t = { list = panes } + function t:panes() + return self.list + end + for _, p in ipairs(panes) do + p.spec.tab = t + end + return t +end + +function F.window(id) + local win = { id = id or 1, actions = {}, status = nil } + function win:window_id() + return self.id + end + function win:perform_action(action, pane) + self.actions[#self.actions + 1] = { action = action, pane = pane } + end + function win:set_right_status(text) + self.status = text + end + return win +end + +return F diff --git a/llembas/tests/run.lua b/llembas/tests/run.lua new file mode 100644 index 000000000..d8c661ed5 --- /dev/null +++ b/llembas/tests/run.lua @@ -0,0 +1,926 @@ +-- LLeMbas TTY: tests of the LLeMbas layer, without a terminal. +-- +-- scripts/test-lua.sh (finds a Lua and gives this a scratch directory) +-- +-- Runs under Lua 5.1 (LuaJIT, `nvim -l`) and Lua 5.4. The layer's decisions +-- are in llembas.core and are tested directly; llembas (init.lua) is run +-- against a stand-in for the terminal (fake_wezterm.lua). + +local here = (arg and arg[0] or 'llembas/tests/run.lua'):match('^(.*)[/\\][^/\\]+$') or '.' +package.path = here .. '/?.lua;' .. here .. '/../lua/?.lua;' .. here .. '/../lua/?/init.lua;' .. package.path + +local real_getenv = os.getenv +local TMP = real_getenv 'LLEMBAS_TEST_TMP' +if not TMP or TMP == '' then + io.stderr:write 'run.lua: set LLEMBAS_TEST_TMP to an empty scratch directory (scripts/test-lua.sh does)\n' + os.exit(2) +end + +local fake = require 'fake_wezterm' +local core = require 'llembas.core' + +local passed, failed = 0, 0 +local current = '' + +local function show(v) + if type(v) == 'string' then + return string.format('%q', v) + end + return tostring(v) +end + +local function check(cond, what) + if cond then + passed = passed + 1 + else + failed = failed + 1 + io.stderr:write(string.format('FAIL %s: %s\n', current, what or '?')) + end +end + +local function eq(got, want, what) + check(got == want, string.format('%s: got %s, want %s', what or 'value', show(got), show(want))) +end + +local function test(name, fn) + current = name + local ok, err = pcall(fn) + if not ok then + failed = failed + 1 + io.stderr:write(string.format('FAIL %s: error: %s\n', name, tostring(err))) + end +end + +local function sh(cmd) + local p = io.popen(cmd) + local out = p:read '*a' + p:close() + return (out:gsub('%s+$', '')) +end + +local function read_file(path) + local f = io.open(path, 'r') + if not f then + return nil + end + local s = f:read '*a' + f:close() + return s +end + +local function write_file(path, text) + local f = assert(io.open(path, 'w')) + f:write(text or '') + f:close() +end + +---------------------------------------------------------------------------- +-- core: text + +test('clean removes control characters and escape sequences', function() + eq(core.clean('a\27[31mb\7c', false), 'a[31mbc', 'ESC and BEL') + eq(core.clean('a\nb\tc\rd', false), 'a b c d', 'one line') + eq(core.clean('a\nb\tc\rd', true), 'a\nb\tcd', 'multi line keeps \\n and \\t') + eq(core.clean('x\194\155y', false), 'xy', 'C1 CSI (U+009B)') + eq(core.clean('x\194\157y', true), 'xy', 'C1 OSC (U+009D)') + eq(core.clean('del\127!', false), 'del!', 'DEL') + eq(core.clean(nil), '', 'nil') + eq(core.clean(42), '', 'number') +end) + +test('clean keeps UTF-8 and replaces what is not', function() + eq(core.clean('žltý kôň ✓ 🙂', false), 'žltý kôň ✓ 🙂', 'valid') + eq(core.clean('a\255b', false), 'a\239\191\189b', 'invalid byte') + eq(core.clean('a\226\130', false), 'a\239\191\189\239\191\189', 'cut sequence') + eq(core.clean('\192\128', false), '\239\191\189\239\191\189', 'overlong NUL') + eq(core.clean('\237\160\128', false), '\239\191\189\239\191\189\239\191\189', 'surrogate') +end) + +test('cap_chars and one_line', function() + eq(core.cap_chars('abcdef', 3), 'abc…', 'cut') + eq(core.cap_chars('abc', 3), 'abc', 'exact') + eq(core.cap_chars('žžžž', 2), 'žž…', 'never inside a character') + eq(core.one_line(' hi\27]0;x\7 there \n', 64), 'hi]0;x there', 'trimmed and cleaned') + eq(#core.one_line(string.rep('x', 500)), 64 + #'…', 'default cap') +end) + +test('tail', function() + eq(core.tail('a\nb\nc\nd', 2), 'c\nd', 'lines') + eq(core.tail('a\nb\nc\nd\n', 2), 'c\nd\n', 'final newline is not a line') + eq(core.tail('a\nb', 5), 'a\nb', 'fewer lines than asked') + eq(core.tail('one\ntwo\nthree', nil, 7), 'three', 'bytes cut at a line start') + eq(core.tail('abcdefgh', nil, 3), 'fgh', 'bytes, one line') + eq(core.tail('až', nil, 1), '', 'never starts inside a character') + eq(core.tail('', 3, 10), '', 'empty') +end) + +---------------------------------------------------------------------------- +-- core: JSON + +test('json_encode', function() + eq(core.json_encode { b = 1, a = 'x' }, '{"a":"x","b":1}', 'keys sorted') + eq(core.json_encode({ b = 1, a = 'x' }, { 'b' }), '{"b":1,"a":"x"}', 'key order') + eq(core.json_encode { 1, 'two', true, false }, '[1,"two",true,false]', 'array') + eq(core.json_encode(core.array {}), '[]', 'empty array') + eq(core.json_encode {}, '{}', 'empty object') + eq(core.json_encode { x = core.null }, '{"x":null}', 'null') + eq(core.json_encode 'a"b\\c\nd\te', '"a\\"b\\\\c\\nd\\te"', 'escapes') + eq(core.json_encode 'a\27[0mb', '"a[0mb"', 'no control character reaches the file') + eq(core.json_encode 'a\255', '"a\239\191\189"', 'invalid UTF-8 replaced') + eq(core.json_encode(1.5), '1.5', 'fraction') + eq(core.json_encode(0 / 0), 'null', 'NaN') + eq(core.json_encode(3), '3', 'integer has no fraction') +end) + +---------------------------------------------------------------------------- +-- core: options + +test('options: defaults', function() + local o, w = core.options(nil) + eq(#w, 0, 'no warnings') + eq(o.enabled, true, 'enabled') + eq(o.autocomplete, false, 'autocomplete is off') + eq(o.keys.ask.key, 'A', 'ask key') + eq(o.keys.ask.mods, 'CTRL|SHIFT', 'ask mods') + eq(o.split.size, 0.45, 'split size') + eq(o.context.blocks, 5, 'blocks') + eq(o.context.output_lines, 200, 'output lines') + eq(o.context.output_bytes, 16384, 'output bytes') + eq(o.context.alt_screen, false, 'alt screen') +end) + +test('options: merge, turn off, clamp', function() + local o, w = core.options { + session_bar = false, + keys = { ask = { key = 'Space', mods = 'CTRL' }, resume = false }, + context = { blocks = 99, output_lines = 0 }, + split = { size = 0.3 }, + autocomplete = true, + cli = '/opt/lembas', + } + eq(#w, 0, 'no warnings') + eq(o.session_bar, false, 'bar off') + eq(o.tab_marker, true, 'untouched option stays') + eq(o.keys.ask.key, 'Space', 'key replaced') + eq(o.keys.resume, false, 'key off') + eq(o.keys.private.key, 'H', 'other keys stay') + eq(o.context.blocks, 20, 'blocks capped at 20') + eq(o.context.output_lines, 1, 'lines at least 1') + eq(o.split.size, 0.3, 'size') + eq(o.split.direction, 'Right', 'direction stays') + eq(o.cli, '/opt/lembas', 'cli') +end) + +test('options: mistakes warn and never throw', function() + local o, w = core.options { + sesion_bar = false, + palette = 'yes', + keys = { ask = 'A', nope = false }, + split = { direction = 'Sideways', size = -1 }, + new_session_in = 'window', + bar = { fields = { 'model', 'nonsense' } }, + context = 'all', + cli = 7, + } + check(#w >= 8, 'a warning for each: got ' .. #w) + eq(o.palette, true, 'wrong type keeps the default') + eq(o.keys.ask.key, 'A', 'bad key keeps the default') + eq(o.split.direction, 'Right', 'direction') + eq(o.split.size, 0.45, 'size') + eq(o.new_session_in, 'split', 'new_session_in') + eq(#o.bar.fields, 1, 'unknown field dropped') + eq(o.context.blocks, 5, 'context untouched') + eq(o.cli, nil, 'cli') + local _, w2 = core.options 'nonsense' + eq(#w2, 1, 'not a table') +end) + +---------------------------------------------------------------------------- +-- core: the session bar + +test('session_from_vars', function() + eq(core.session_from_vars {}, nil, 'no vars') + eq(core.session_from_vars { WEZTERM_PROG = 'vim' }, nil, 'other vars') + eq(core.session_from_vars(nil), nil, 'nil') + local s = core.session_from_vars { + lembas_session = 'abc123', + lembas_model = 'big\27[31m-model\7', + lembas_mode = 'auto', + lembas_state = 'thinking', + lembas_context = '41.6', + lembas_title = string.rep('t', 300), + } + eq(s.session, 'abc123', 'session') + eq(s.model, 'big[31m-model', 'escape sequence defused') + eq(s.state, 'thinking', 'state') + eq(s.context, 42, 'context rounded') + check(#s.title <= 80 + #'…', 'title capped') + eq(core.session_from_vars({ lembas_session = 'x', lembas_state = 'rm -rf' }).state, nil, 'unknown state dropped') + eq(core.session_from_vars({ lembas_session = 'x', lembas_context = '250' }).context, nil, 'context out of range') + eq(core.session_from_vars({ lembas_session = 'x', lembas_context = '%s%n' }).context, nil, 'context not a number') + eq(core.session_from_vars({ lembas_session = 'x', lembas_context = '7%' }).context, 7, 'context with a percent sign') + eq(core.session_from_vars({ lembas_state = 'idle' }).state, 'idle', 'state alone is enough') +end) + +test('bar_parts and bar_items', function() + local fields = { 'model', 'mode', 'state', 'context' } + eq(core.bar_items(nil, fields, false), nil, 'no session, not private: nothing') + local s = core.session_from_vars { + lembas_session = 'x', + lembas_model = 'm1', + lembas_mode = 'edit', + lembas_state = 'asking', + lembas_context = '12', + lembas_spend = '0.02', + } + local parts = core.bar_parts(s, fields, false) + eq(#parts, 4, 'four parts') + eq(parts[1].text, 'm1', 'model') + eq(parts[3].text, 'needs you', 'asking in words') + eq(parts[3].attention, true, 'asking stands out') + eq(parts[4].text, 'ctx 12%', 'context') + eq(#core.bar_parts(s, { 'spend', 'bogus' }, false), 1, 'only fields that exist') + local p = core.bar_parts(nil, fields, true) + eq(#p, 1, 'private alone') + eq(p[1].text, 'private', 'private text') + local items = core.bar_items(s, fields, true) + local text = {} + for _, item in ipairs(items) do + if type(item) == 'table' and item.Text then + text[#text + 1] = item.Text + check(not item.Text:find '[%c]', 'no control character in a Text item') + end + end + eq(table.concat(text), ' m1 · edit · needs you · ctx 12% · private ', 'whole bar') +end) + +test('tab_title and find_session_pane', function() + local asking = { pane_id = 2, user_vars = { lembas_session = 's', lembas_state = 'asking' } } + local idle = { pane_id = 3, user_vars = { lembas_session = 's', lembas_state = 'idle' } } + local shell = { pane_id = 1, user_vars = {} } + eq(core.tab_title { tab_index = 0, panes = { shell, idle }, active_pane = { title = 'zsh' } }, nil, 'nobody asks') + eq(core.tab_title(nil), nil, 'nil') + local items = core.tab_title( + { tab_index = 1, tab_title = '', panes = { shell, asking }, active_pane = { title = 'zsh\27]0;evil\7' } }, + { max_width = 16 } + ) + check(type(items) == 'table', 'asking: items') + local text = '' + for _, item in ipairs(items) do + if type(item) == 'table' and item.Text then + text = text .. item.Text + end + end + eq(text, ' ● 2: zsh]0;ev… ', 'marked, indexed, cut to 16 cells, cleaned') + eq(core.find_session_pane({ shell, idle }, 1), 3, 'the other pane') + eq(core.find_session_pane({ shell }, 1), nil, 'none') + eq(core.find_session_pane({ idle, asking }, 3), 2, 'not the one the person is in') + eq(core.find_session_pane({ idle }, 3), 3, 'only itself') +end) + +---------------------------------------------------------------------------- +-- core: terminal context + +local function zone(kind, y, text, y2) + -- the command line starts after the prompt, on the same row + local x = kind == 'Input' and 2 or 0 + return { semantic_type = kind, start_y = y, start_x = x, end_y = y2 or y, end_x = 10, text = text } +end + +local function text_of(z) + return z.text +end + +local LIM = { blocks = 5, output_lines = 200, output_bytes = 16384 } + +test('blocks_from_zones: commands and their output', function() + local zones = { + zone('Prompt', 0, '$ '), + zone('Input', 0, 'ls'), + zone('Output', 1, 'a\nb\n', 2), + zone('Prompt', 3, '$ '), + zone('Input', 3, 'cd /tmp'), + zone('Prompt', 4, '$ '), + zone('Input', 4, 'make test '), + zone('Output', 5, 'ok\27[0m\n', 9), + zone('Prompt', 10, '$ '), + zone('Input', 10, 'still typi'), + } + local blocks = core.blocks_from_zones(zones, text_of, LIM) + eq(#blocks, 3, 'three run commands; the line being typed is not one') + eq(blocks[1].command, 'ls', 'first') + eq(blocks[1].output, 'a\nb', 'output, trailing space trimmed') + eq(blocks[2].command, 'cd /tmp', 'a command with no output') + eq(blocks[2].output, '', 'empty output') + eq(blocks[3].command, 'make test', 'trimmed') + eq(blocks[3].output, 'ok[0m', 'escape defused') + eq(blocks[3].exit, core.null, 'exit is null') +end) + +test('blocks_from_zones: order, limits, nothing', function() + local zones = {} + for i = 1, 30 do + zones[#zones + 1] = zone('Output', i * 3 + 1, 'out' .. i) + zones[#zones + 1] = zone('Input', i * 3, 'cmd' .. i) + zones[#zones + 1] = zone('Prompt', i * 3, '$ ') + end + local blocks = core.blocks_from_zones(zones, text_of, LIM) + eq(#blocks, 5, 'the last five') + eq(blocks[1].command, 'cmd26', 'sorted by position, oldest first') + eq(blocks[5].output, 'out30', 'newest last') + local called = 0 + core.blocks_from_zones(zones, function(z) + called = called + 1 + return z.text + end, LIM) + eq(called, 10, 'text is read only for the blocks that are used') + eq(#core.blocks_from_zones({}, text_of, LIM), 0, 'no zones') + eq(#core.blocks_from_zones(nil, text_of, LIM), 0, 'nil') + eq(#core.blocks_from_zones(zones, text_of, { blocks = 0, output_lines = 1, output_bytes = 256 }), 0, 'zero blocks asked') + eq(#core.blocks_from_zones({ zone('Output', 1, 'x'), zone('Prompt', 2, '$ ') }, text_of, LIM), 0, 'output with no command') + local long = {} + for i = 1, 500 do + long[i] = 'line ' .. i + end + local b = core.blocks_from_zones( + { zone('Input', 0, 'seq'), zone('Output', 1, table.concat(long, '\n')) }, + text_of, + { blocks = 5, output_lines = 3, output_bytes = 16384 } + ) + eq(b[1].output, 'line 498\nline 499\nline 500', 'output cut to its tail') + local wide = core.blocks_from_zones( + { zone('Input', 0, 'x'), zone('Output', 1, string.rep('y', 5000)) }, + text_of, + { blocks = 5, output_lines = 200, output_bytes = 1024 } + ) + eq(#wide[1].output, 1024, 'output cut to its bytes') +end) + +test('context and encode_context', function() + local blocks = core.blocks_from_zones({ zone('Input', 0, 'ls'), zone('Output', 1, 'a') }, text_of, LIM) + local ctx = core.context { pane = 7, cwd = '/home/u/p', shell = 'zsh', blocks = blocks, screen = 'ignored' } + eq( + core.encode_context(ctx), + '{"version":1,"pane":7,"cwd":"/home/u/p","shell":"zsh","blocks":[{"command":"ls","exit":null,"output":"a"}]}', + 'with blocks: no screen' + ) + local bare = core.context { pane = 1, blocks = core.array {}, screen = 'top\nbottom' } + eq(core.encode_context(bare), '{"version":1,"pane":1,"cwd":"","blocks":[],"screen":"top\\nbottom"}', 'no integration: screen, empty blocks') + eq(core.encode_context(core.context { pane = 2 }), '{"version":1,"pane":2,"cwd":"","blocks":[]}', 'nothing at all') +end) + +test('encode_context stays under the limit', function() + local blocks = core.array {} + for i = 1, 20 do + blocks[i] = { command = 'c' .. i, exit = core.null, output = string.rep('o', 16000) } + end + local ctx = core.context { pane = 1, cwd = '/', blocks = blocks } + local json = core.encode_context(ctx) + check(#json < 256 * 1024, 'under 256 KiB: ' .. #json) + check(#ctx.blocks < 20 and #ctx.blocks >= 15, 'oldest blocks dropped: ' .. #ctx.blocks) + eq(ctx.blocks[#ctx.blocks].command, 'c20', 'the newest stays') + local s = core.context { pane = 1, screen = string.rep('line\n', 2000) } + local small = core.encode_context(s, 1000) + check(#small < 1000, 'screen cut: ' .. #small) + check(small:find('"screen"', 1, true) ~= nil, 'still a screen') +end) + +test('context files: names and age', function() + local name = core.context_file_name(1700000000, 'deadbeef') + eq(name, 'context-1700000000-deadbeef.json', 'name') + eq(core.context_file_age('/run/x/' .. name, 1700000100), 100, 'age from the name') + eq(core.is_stale_context_file(name, 1700000100), false, 'fresh') + eq(core.is_stale_context_file(name, 1700000601), true, 'stale after ten minutes') + eq(core.is_stale_context_file('gui-sock-123', 1700009999), false, 'not ours: never') + eq(core.is_stale_context_file('context-1-zz.json', 1700009999), false, 'not hex: never') + eq(core.is_stale_context_file('xcontext-1-ab.json', 1700009999), false, 'prefix: never') + eq(core.hex '\0\255A', '00ff41', 'hex') +end) + +test('cwd_from and shell_name', function() + eq(core.cwd_from 'file://host/home/u/my%20dir', '/home/u/my dir', 'file URL') + eq(core.cwd_from 'file:///tmp', '/tmp', 'no host') + eq(core.cwd_from '/plain/path', '/plain/path', 'plain path') + eq(core.cwd_from 'ftp://x/y', nil, 'not a file') + eq(core.cwd_from(nil), nil, 'nil') + eq(core.cwd_from { scheme = 'file', file_path = '/a/b' }, '/a/b', 'Url object') + eq(core.cwd_from { scheme = 'sftp', file_path = '/a/b' }, nil, 'other scheme') + eq(core.shell_name '/usr/bin/zsh', 'zsh', 'zsh') + eq(core.shell_name '-bash', 'bash', 'login shell') + eq(core.shell_name 'C:\\Program Files\\PowerShell\\pwsh.EXE', 'pwsh', 'windows') + eq(core.shell_name '/usr/bin/vim', nil, 'not a shell') + eq(core.shell_name(nil), nil, 'nil') +end) + +test('question_text and context_command', function() + eq(core.question_text ' why did it fail? ', 'why did it fail?', 'trimmed') + eq(core.question_text 'a\27[201~b\3', 'a[201~b', 'cannot end a paste or press a key') + eq(core.question_text 'two\nlines', 'two\nlines', 'a line break stays') + eq(core.question_text '', '', 'empty') + check(#core.question_text(string.rep('q', 100000)) <= 16384, 'capped') + eq( + core.context_command '/run/user/1000/llembas-tty/context-1-ab.json', + '/terminal-context /run/user/1000/llembas-tty/context-1-ab.json', + 'command' + ) + eq(core.context_command '/x\r/quit', '/terminal-context /x /quit', 'no line break can be smuggled in') +end) + +---------------------------------------------------------------------------- +-- core: the CLI + +test('versions', function() + eq(core.compare_versions('2.0.0', '2.0.0'), 0, 'equal') + eq(core.compare_versions('2.0.1', '2.0.0'), 1, 'patch') + eq(core.compare_versions('1.9.9', '2.0.0'), -1, 'major') + eq(core.compare_versions('2.0.0-beta.17', '2.0.0'), -1, 'pre-release before release') + eq(core.compare_versions('2.0.0-beta.17', '2.0.0-beta.18'), -1, 'beta numbers') + eq(core.compare_versions('2.0.0-beta.9', '2.0.0-beta.10'), -1, 'numeric, not text') + eq(core.compare_versions('2.0.0-beta.2', '2.0.0-alpha.9'), 1, 'names') + eq(core.compare_versions('2.0.0-beta', '2.0.0-beta.1'), -1, 'shorter first') + eq(core.compare_versions('lembas 2.1.0\n', 'v2.0.0'), 1, 'found in text') + eq(core.compare_versions('nonsense', '2.0.0'), nil, 'not a version') + eq(core.cli_supports_terminal(core.MIN_CLI_VERSION), true, 'the minimum itself') + eq(core.cli_supports_terminal '2.0.0-beta.17', false, 'older') + eq(core.cli_supports_terminal '2.0.0', true, 'release') + eq(core.cli_supports_terminal '3.1.4', true, 'newer') + eq(core.cli_supports_terminal '', nil, 'cannot tell') +end) + +test('cli_candidates', function() + local c = core.cli_candidates(nil, '/opt/tty/bin', '/usr/local/bin:/usr/bin', false) + eq(c[1], '/opt/tty/bin/lembas', 'beside the terminal first') + eq(c[2], '/usr/local/bin/lembas', 'then PATH, in order') + eq(c[3], '/usr/bin/lembas', 'PATH 2') + eq(#core.cli_candidates('/x/lembas', '/opt', '/usr/bin', false), 1, 'explicit: only that') + eq(core.cli_candidates(nil, 'C:\\tty', 'C:\\a;C:\\b', true)[3], 'C:\\b\\lembas.exe', 'windows') + eq(#core.cli_candidates(nil, nil, nil, false), 0, 'nothing known') +end) + +test('session_choices and is_session_id', function() + eq(core.session_choices 'text', nil, 'not decoded') + eq(core.session_choices {}, nil, 'empty') + local c = core.session_choices { + { id = 'ses_1', title = 'Fix the\27[2J build', model = 'm', root = '/p' }, + { id = 'bad id; rm -rf /', title = 'x' }, + { id = 'ses_2' }, + 'junk', + } + eq(#c, 2, 'only real ids') + eq(c[1].id, 'ses_1', 'id') + eq(c[1].label, 'Fix the[2J build · m · /p', 'label cleaned') + eq(c[2].label, 'ses_2', 'no title: the id') + eq(#core.session_choices { sessions = { { id = 'a' } } }, 1, 'wrapped in an object') + eq(core.is_session_id 'abc-123_x.y:z', true, 'id') + eq(core.is_session_id '-s', false, 'cannot look like an option') + eq(core.is_session_id '', false, 'empty') + eq(core.is_session_id(nil), false, 'nil') + eq(core.is_session_id(string.rep('a', 200)), false, 'too long') +end) + +---------------------------------------------------------------------------- +-- The layer against a stand-in terminal + +local env = {} +os.getenv = function(name) + if env[name] ~= nil then + return env[name] or nil + end + return real_getenv(name) +end + +local function layer(wez_env, options, config) + local w = fake.new(wez_env) + package.loaded['wezterm'] = w + package.loaded['llembas'] = nil + local l = require 'llembas' + config = config or w.config_builder() + l.apply(config, options) + return l, w, config +end + +-- A directory of its own for one test, inside the scratch directory. +local function fresh_dir(name) + local dir = TMP .. '/' .. name + sh(string.format("mkdir -p '%s/bin' '%s/run'", dir, dir)) + return dir +end + +local function with_cli(dir) + write_file(dir .. '/bin/lembas', '#!/bin/sh\n') + env.PATH = dir .. '/bin' + env.LLEMBAS_TTY_UNIX_SOCKET = dir .. '/run/gui-sock-1' + env.XDG_RUNTIME_DIR = false +end + +local NEW = { ['--version'] = { true, '2.0.0-beta.18\n' } } +local OLD = { ['--version'] = { true, '2.0.0-beta.17\n' } } + +local function context_files(dir) + local out = sh(string.format("ls -1 '%s/run' | grep '^context-' || true", dir)) + local list = {} + for name in out:gmatch '[^\n]+' do + list[#list + 1] = dir .. '/run/' .. name + end + return list +end + +local ZONES = { + zone('Prompt', 0, '$ '), + zone('Input', 0, 'cargo test'), + zone('Output', 1, 'error: it broke\n'), + zone('Prompt', 2, '$ '), +} + +test('apply: keys, events, and nothing else happens', function() + local dir = fresh_dir 'apply' + with_cli(dir) + local _, w, config = layer({ child_results = NEW }, nil, { keys = { { key = 'x', mods = 'CTRL', action = 'mine' } } }) + eq(#config.keys, 6, 'five keys added to the one that was there') + eq(config.keys[1].action, 'mine', "the person's own key stays first") + eq(config.keys[2].key, 'A', 'ask') + eq(config.keys[2].action.action, 'EmitEvent', 'an event') + eq(config.keys[2].action.args, 'llembas-ask', 'ask event') + eq(#w.spawned, 0, 'no process started by applying') + eq(config.set_environment_variables, nil, 'no environment touched') + for _, name in ipairs { 'update-status', 'format-tab-title', 'augment-command-palette', 'user-var-changed', 'llembas-ask' } do + check(w.handlers[name] ~= nil, 'handler for ' .. name) + end + local entries = w.emit 'augment-command-palette' + eq(#entries, 7, 'seven palette entries') + eq(entries[1].brief, 'LLeMbas: ask the model', 'first entry') + eq(#context_files(dir), 0, 'no file written') +end) + +test('apply: everything can be turned off', function() + local _, w, config = layer({}, { enabled = false }) + eq(config.keys, nil, 'disabled: no keys') + eq(next(w.handlers), nil, 'disabled: no handlers') + local _, w2, config2 = layer({}, { + session_bar = false, + tab_marker = false, + palette = false, + keys = { ask = false, new_session = false, resume = false, toggle_display = false, private = false }, + }) + eq(#config2.keys, 0, 'no keys') + eq(w2.handlers['update-status'], nil, 'no bar') + eq(w2.handlers['format-tab-title'], nil, 'no tab mark') + eq(w2.handlers['augment-command-palette'], nil, 'no palette') + local _, w3 = layer({}, { nonsense = 1 }) + check(#w3.logs == 1 and w3.logs[1]:find('nonsense', 1, true), 'a typo is logged') +end) + +test('apply: autocomplete exports the switch', function() + local dir = fresh_dir 'ac' + with_cli(dir) + local _, w, config = layer({}, { autocomplete = true }, { set_environment_variables = { MINE = '1' } }) + eq(config.set_environment_variables.LLEMBAS_TTY_AUTOCOMPLETE, '1', 'switch') + eq(config.set_environment_variables.LLEMBAS_TTY_CLI, dir .. '/bin/lembas', 'the CLI found') + eq(config.set_environment_variables.MINE, '1', "the person's own stays") + eq(#w.spawned, 0, 'still no process') +end) + +test('ask key opens a one-line prompt', function() + local _, w = layer {} + local win, pane = fake.window(), fake.pane {} + w.emit('llembas-ask', win, pane) + eq(#win.actions, 1, 'one action') + eq(win.actions[1].action.action, 'PromptInputLine', 'prompt') + eq(win.actions[1].action.args.action.args, 'llembas-ask-line', 'answer goes to ask-line') + eq(pane.read_zones, nil, 'nothing read yet') +end) + +test('ask: new session beside the pane, with a context file', function() + local dir = fresh_dir 'ask' + with_cli(dir) + local _, w = layer { child_results = NEW } + local win = fake.window() + local pane = fake.pane { id = 7, zones = ZONES, cwd = 'file:///home/u/proj', program = '/bin/zsh', screen = 'SCREEN' } + fake.tab { pane } + w.emit('llembas-ask-line', win, pane, 'why did it fail?') + local files = context_files(dir) + eq(#files, 1, 'one context file') + eq(sh(string.format("stat -c %%a '%s'", files[1])), '600', 'mode 0600') + eq( + read_file(files[1]), + '{"version":1,"pane":7,"cwd":"/home/u/proj","shell":"zsh","blocks":[{"command":"cargo test","exit":null,"output":"error: it broke"}]}', + 'content' + ) + eq(pane.read_screen, nil, 'screen not read when there are blocks') + eq(#pane.splits, 1, 'split') + local s = pane.splits[1] + eq(s.direction, 'Right', 'to the right') + eq(s.size, 0.45, '45 %') + eq(s.cwd, '/home/u/proj', "in the pane's directory") + eq(s.args[1], dir .. '/bin/lembas', 'the CLI') + eq(s.args[2], '--terminal-context', 'flag') + eq(s.args[3], files[1], 'file') + eq(#s.args, 3, 'the question is not on the command line') + local new = pane.last_split + eq(#new.sent, 0, 'nothing sent before the session is ready') + w.emit('user-var-changed', win, new, 'lembas_state', 'thinking') + eq(#new.sent, 0, 'not on another state') + w.emit('user-var-changed', win, new, 'lembas_state', 'idle') + eq(#new.sent, 2, 'sent when idle') + eq(new.sent[1][1], 'paste', 'pasted') + eq(new.sent[1][2], 'why did it fail?', 'the question') + eq(new.sent[2][2], '\r', 'then Enter') + w.emit('user-var-changed', win, new, 'lembas_state', 'idle') + eq(#new.sent, 2, 'only once') +end) + +test('ask: into the LLeMbas pane the tab already has', function() + local dir = fresh_dir 'ask2' + with_cli(dir) + local _, w = layer { child_results = NEW } + local win = fake.window() + local pane = fake.pane { id = 1, zones = ZONES, cwd = '/p' } + local agent = fake.pane { id = 2, vars = { lembas_session = 's1', lembas_state = 'idle' } } + fake.tab { pane, agent } + w.emit('llembas-ask-line', win, pane, 'explain\27[201~ this') + local files = context_files(dir) + eq(#files, 1, 'context file') + eq(#pane.splits, 0, 'no new pane') + eq(#agent.sent, 3, 'three sends') + eq(agent.sent[1][2], '/terminal-context ' .. files[1] .. '\r', 'context first') + eq(agent.sent[2][1], 'paste', 'question pasted') + eq(agent.sent[2][2], 'explain[201~ this', 'and cannot end the paste') + eq(agent.sent[3][2], '\r', 'Enter') + eq(agent.read_zones, nil, 'the LLeMbas pane itself is not read') +end) + +test('ask: from inside the LLeMbas pane it is just typed', function() + local dir = fresh_dir 'ask3' + with_cli(dir) + local _, w = layer { child_results = NEW } + local agent = fake.pane { id = 2, vars = { lembas_session = 's1' }, zones = ZONES } + fake.tab { agent } + w.emit('llembas-ask-line', fake.window(), agent, 'hello') + eq(#context_files(dir), 0, 'no context') + eq(agent.sent[1][2], 'hello', 'typed') + eq(agent.read_zones, nil, 'not read') +end) + +test('ask: no shell integration gives the screen', function() + local dir = fresh_dir 'ask4' + with_cli(dir) + local _, w = layer { child_results = NEW } + local pane = fake.pane { id = 3, screen = 'l1\nl2\27[0m\n', cwd = '/p', program = '/usr/bin/python3' } + fake.tab { pane } + w.emit('llembas-ask-line', fake.window(), pane, 'q') + eq(read_file(context_files(dir)[1]), '{"version":1,"pane":3,"cwd":"/p","blocks":[],"screen":"l1\\nl2[0m\\n"}', 'screen, no shell') +end) + +test('ask: a private pane gives nothing, and the question still goes', function() + local dir = fresh_dir 'priv' + with_cli(dir) + local l, w = layer { child_results = NEW } + local win = fake.window() + local pane = fake.pane { id = 4, zones = ZONES, screen = 'secret' } + fake.tab { pane } + w.emit('llembas-private', win, pane) + eq(l.is_private(4), true, 'private') + eq(l.is_private(5), false, 'only that pane') + w.emit('update-status', win, pane) + eq(win.status, ' private ', 'shown in the bar') + w.emit('llembas-ask-line', win, pane, 'q') + eq(#context_files(dir), 0, 'no file') + eq(pane.read_zones, nil, 'zones not read') + eq(pane.read_screen, nil, 'screen not read') + eq(#pane.splits, 1, 'session still starts') + eq(#pane.splits[1].args, 1, 'with no context') + eq(win.actions[#win.actions].action.action, 'InputSelector', 'and the person is told') + w.emit('llembas-private', win, pane) + eq(l.is_private(4), false, 'off again') + w.emit('update-status', win, pane) + eq(win.status, '', 'bar cleared') +end) + +test('ask: a password prompt gives nothing', function() + local dir = fresh_dir 'pw' + with_cli(dir) + local _, w = layer { child_results = NEW } + local pane = fake.pane { id = 5, zones = ZONES, metadata = { password_input = true } } + fake.tab { pane } + w.emit('llembas-ask-line', fake.window(), pane, 'q') + eq(#context_files(dir), 0, 'no file') + eq(pane.read_zones, nil, 'not read') +end) + +test('ask: a full-screen program gives nothing unless asked', function() + local dir = fresh_dir 'alt' + with_cli(dir) + local _, w = layer { child_results = NEW } + local pane = fake.pane { id = 6, alt = true, zones = ZONES, screen = 'vim buffer', cwd = '/p' } + fake.tab { pane } + w.emit('llembas-ask-line', fake.window(), pane, 'q') + eq(read_file(context_files(dir)[1]), '{"version":1,"pane":6,"cwd":"/p","blocks":[]}', 'only where it is') + eq(pane.read_screen, nil, 'screen not read') + local dir2 = fresh_dir 'alt2' + with_cli(dir2) + w.emit('llembas-ask-screen-line', fake.window(), pane, 'q') + eq(read_file(context_files(dir2)[1]), '{"version":1,"pane":6,"cwd":"/p","blocks":[],"screen":"vim buffer"}', 'asked about the screen') +end) + +test('ask: context off, empty line, remote pane', function() + local dir = fresh_dir 'off' + with_cli(dir) + local _, w = layer({ child_results = NEW }, { context = { enabled = false } }) + local pane = fake.pane { id = 8, zones = ZONES } + fake.tab { pane } + w.emit('llembas-ask-line', fake.window(), pane, 'q') + eq(#context_files(dir), 0, 'context off: no file') + eq(pane.read_zones, nil, 'not read') + eq(#pane.splits, 1, 'question still goes') + local p2 = fake.pane { id = 9, zones = ZONES } + w.emit('llembas-ask-line', fake.window(), p2, nil) + w.emit('llembas-ask-line', fake.window(), p2, ' ') + eq(#p2.splits, 0, 'cancelled or empty: nothing') + local win = fake.window() + local far = fake.pane { id = 10, zones = ZONES, domain = 'SSH:server' } + w.emit('llembas-ask-line', win, far, 'q') + eq(#far.splits, 0, 'remote pane: nothing started') + eq(far.read_zones, nil, 'remote pane: not read') + check(win.actions[1].action.args.description:find('local panes', 1, true), 'told in words') +end) + +test('missing CLI: said clearly, never silently', function() + local dir = fresh_dir 'nocli' + env.PATH = dir .. '/bin' + env.LLEMBAS_TTY_UNIX_SOCKET = dir .. '/run/gui-sock-1' + local _, w = layer {} + local win, pane = fake.window(), fake.pane { zones = ZONES } + w.emit('llembas-ask-line', win, pane, 'q') + eq(#win.actions, 1, 'a message') + check(win.actions[1].action.args.description:find('get.sh | bash', 1, true), 'names the install command') + eq(pane.read_zones, nil, 'nothing read') + eq(#w.spawned, 0, 'nothing started') + w.emit('llembas-new-session', win, pane) + eq(#win.actions, 2, 'a message again') + check(win.actions[2].action.args.description:find('still not found', 1, true), 'the short one') + eq(#pane.splits, 0, 'no pane') +end) + +test('a CLI that is too old: said in words', function() + local dir = fresh_dir 'old' + with_cli(dir) + local _, w = layer { child_results = OLD } + local win, pane = fake.window(), fake.pane { zones = ZONES } + fake.tab { pane } + w.emit('llembas-ask-line', win, pane, 'q') + eq(#pane.splits, 0, 'nothing started') + eq(pane.read_zones, nil, 'nothing read') + local d = win.actions[1].action.args.description + check(d:find('2.0.0-beta.17', 1, true) and d:find('lembas update', 1, true), 'says which and what to do') + w.emit('llembas-ask-line', win, pane, 'q') + eq(#w.spawned, 1, 'version asked once') + w.emit('llembas-new-session', win, pane) + eq(#pane.splits, 1, 'a session still starts with an old CLI') +end) + +test('a CLI whose version cannot be read is tried', function() + local dir = fresh_dir 'unk' + with_cli(dir) + local _, w = layer {} + local pane = fake.pane { zones = ZONES } + fake.tab { pane } + w.emit('llembas-ask-line', fake.window(), pane, 'q') + eq(#pane.splits, 1, 'tried') +end) + +test('stale context files are swept, other files never', function() + local dir = fresh_dir 'sweep' + with_cli(dir) + local now = os.time() + local old = dir .. '/run/' .. core.context_file_name(now - 601, 'aa') + local young = dir .. '/run/' .. core.context_file_name(now - 30, 'bb') + local other = dir .. '/run/gui-sock-1' + write_file(old) + write_file(young) + write_file(other) + local _, w = layer { child_results = NEW } + local pane = fake.pane { zones = ZONES } + fake.tab { pane } + w.emit('llembas-ask-line', fake.window(), pane, 'q') + eq(read_file(old), nil, 'old one gone') + check(read_file(young) ~= nil, 'young one stays') + check(read_file(other) ~= nil, 'the socket stays') + eq(#context_files(dir), 2, 'young + new') +end) + +test('sessions: new, in a split or a tab', function() + local dir = fresh_dir 'new' + with_cli(dir) + local _, w = layer {} + local win, pane = fake.window(), fake.pane { cwd = '/p' } + w.emit('llembas-new-session', win, pane) + eq(#pane.splits, 1, 'split') + eq(#pane.splits[1].args, 1, 'just lembas') + eq(pane.splits[1].cwd, '/p', 'cwd') + eq(#w.spawned, 0, 'no version probe needed') + local _, w2 = layer({}, { new_session_in = 'tab' }) + local win2, pane2 = fake.window(), fake.pane { cwd = '/p' } + w2.emit('llembas-new-session', win2, pane2) + eq(#pane2.splits, 0, 'no split') + eq(win2.actions[1].action.action, 'SpawnCommandInNewTab', 'a tab') + eq(win2.actions[1].action.args.cwd, '/p', 'cwd') +end) + +test('sessions: resume from a JSON list', function() + local dir = fresh_dir 'resume' + with_cli(dir) + local _, w = layer { + child_results = { ['sessions --json'] = { true, 'JSON' } }, + json_parse = function(text) + assert(text == 'JSON') + return { { id = 'ses_9', title = 'T' }, { id = '--evil' } } + end, + } + local win, pane = fake.window(), fake.pane { cwd = '/p' } + w.emit('llembas-resume', win, pane) + local a = win.actions[1].action + eq(a.action, 'InputSelector', 'a list') + eq(#a.args.choices, 1, 'only real ids') + eq(a.args.action.args, 'llembas-resume-pick', 'pick event') + w.emit('llembas-resume-pick', win, pane, 'ses_9', 'T') + eq(pane.splits[1].args[2], '-s', 'resume flag') + eq(pane.splits[1].args[3], 'ses_9', 'id') + w.emit('llembas-resume-pick', win, pane, '--evil', 'x') + w.emit('llembas-resume-pick', win, pane, nil, nil) + eq(#pane.splits, 1, 'a bad or missing id starts nothing') +end) + +test('sessions: resume falls back to the list of the CLI itself', function() + local dir = fresh_dir 'resume2' + with_cli(dir) + local _, w = layer { child_results = { ['sessions --json'] = { true, 'ses_1 2026-01-01 10:00 m /p\n' } } } + local win, pane = fake.window(), fake.pane {} + w.emit('llembas-resume', win, pane) + eq(#win.actions, 0, 'no list of ours') + eq(#pane.splits, 1, 'the CLI in a pane') + local new = pane.last_split + w.emit('user-var-changed', win, new, 'lembas_state', 'idle') + eq(new.sent[1][2], '/sessions\r', 'its own session list') +end) + +test('sessions: display toggle, log in', function() + local dir = fresh_dir 'misc' + with_cli(dir) + local _, w = layer {} + local win = fake.window() + local pane = fake.pane { id = 1 } + local agent = fake.pane { id = 2, vars = { lembas_session = 's' } } + fake.tab { pane, agent } + w.emit('llembas-toggle-display', win, pane) + eq(win.actions[1].action.action, 'SendKey', 'a key') + eq(win.actions[1].action.args.key, 'd', 'd') + eq(win.actions[1].action.args.mods, 'ALT', 'Alt') + eq(win.actions[1].pane, agent, 'to the LLeMbas pane') + local lone = fake.pane { id = 3 } + fake.tab { lone } + w.emit('llembas-toggle-display', win, lone) + eq(win.actions[2].action.action, 'InputSelector', 'no LLeMbas pane: said') + w.emit('llembas-login', win, pane) + eq(pane.splits[1].args[2], 'login', 'lembas login') +end) + +test('session bar: shows the session, and nothing of ours otherwise', function() + local _, w = layer {} + local win = fake.window() + local shell = fake.pane { id = 1, zones = ZONES, screen = 'x' } + w.emit('update-status', win, shell) + eq(win.status, nil, 'no LLeMbas pane: the status is not touched') + local agent = fake.pane { + id = 2, + vars = { lembas_session = 's', lembas_model = 'm\27[5m', lembas_mode = 'auto', lembas_state = 'tools', lembas_context = '9' }, + } + w.emit('update-status', win, agent) + eq(win.status, ' m[5m · auto · tools · ctx 9% ', 'the bar') + w.emit('update-status', win, shell) + eq(win.status, '', 'cleared when the person leaves the pane') + win.status = 'theirs' + w.emit('update-status', win, shell) + eq(win.status, 'theirs', 'and then left alone') + eq(shell.read_zones, nil, 'the bar never reads zones') + eq(shell.read_screen, nil, 'the bar never reads the screen') +end) + +test('tab mark', function() + local _, w = layer {} + local tab = { + tab_index = 0, + tab_title = '', + active_pane = { title = 'lembas' }, + panes = { { pane_id = 1, user_vars = { lembas_session = 's', lembas_state = 'asking' } } }, + } + local items = w.emit('format-tab-title', tab, {}, {}, { show_tab_index_in_tab_bar = true }, false, 20) + check(type(items) == 'table', 'marked') + tab.panes[1].user_vars.lembas_state = 'idle' + eq(w.emit('format-tab-title', tab, {}, {}, {}, false, 20), nil, 'not marked: left to the terminal') +end) + +---------------------------------------------------------------------------- + +os.getenv = real_getenv +io.stdout:write(string.format('llembas layer: %d checks passed, %d failed\n', passed, failed)) +os.exit(failed == 0 and 0 or 1) diff --git a/scripts/test-lua.sh b/scripts/test-lua.sh new file mode 100755 index 000000000..7125ffe87 --- /dev/null +++ b/scripts/test-lua.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +# Run the tests of the LLeMbas layer (llembas/tests/run.lua) with whatever Lua is here: +# lua5.4 (what the terminal embeds), else lua, luajit, or Neovim's LuaJIT (`nvim -l`). +# +# scripts/test-lua.sh the first one found +# scripts/test-lua.sh --all every one found (CI: 5.4 and LuaJIT must agree) +# LUA="lua5.4" scripts/test-lua.sh +set -euo pipefail +cd "$(dirname "$0")/.." + +tmp="$(mktemp -d)" +trap 'rm -rf "${tmp:?}"' EXIT + +found=() +if [ -n "${LUA:-}" ]; then + found=("$LUA") +else + for c in lua5.4 lua luajit; do + command -v "$c" >/dev/null 2>&1 && found+=("$c") + done + command -v nvim >/dev/null 2>&1 && found+=("nvim -l") +fi +[ "${#found[@]}" -gt 0 ] || { echo "test-lua: no Lua here (lua5.4, lua, luajit or nvim)" >&2; exit 2; } +[ "${1:-}" = "--all" ] || found=("${found[0]}") + +n=0 +for lua in "${found[@]}"; do + echo "test-lua: $lua" + n=$((n + 1)) + mkdir "$tmp/run$n" + # shellcheck disable=SC2086 # "nvim -l" is two words on purpose + LLEMBAS_TEST_TMP="$tmp/run$n" $lua llembas/tests/run.lua +done