-- LLeMbas TERM: 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.ai, true, 'AI is on: nothing happens until a key') eq(o.autocomplete, false, 'AI autocomplete is off') eq(o.suggestions, true, 'suggestions from history are on') eq(o.highlight, true, 'highlighting is on') eq(o.history.enabled, true, 'history') eq(o.history.max, 5000, 'history size') eq(o.keys.ask.key, 'A', 'ask key') eq(o.keys.ask.mods, 'CTRL|SHIFT', 'ask mods') eq(o.keys.cli.key, 'S', 'the CLI key') eq(o.keys.ai.key, 'I', 'the switch') eq(o.keys.model.key .. ' ' .. o.keys.model.mods, 'M CTRL|SHIFT', 'the list of chat models') eq(o.keys.ac_model.key .. ' ' .. o.keys.ac_model.mods, 'm CTRL|ALT', 'the list of autocomplete models: a small letter, no Shift') eq(o.keys.mode_next.key .. ' ' .. o.keys.mode_next.mods, 'Tab CTRL', 'the other mode, on the ask line') eq(o.keys.palette.mods, 'CTRL|SHIFT|ALT', "the terminal's palette on a chord of its own") eq(o.keys.mode, false, 'the list of modes has no key until given one') eq(o.ide_keys, true, 'keys LLeMbas IDE has too are handed to it') eq(o.kitty_keyboard, true, 'a program that asks for the keyboard protocol gets it') eq(o.display.width, 0.33, 'a third') eq(o.mode, 'manual', 'mode') 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') eq(o.colors.ghost, 8, 'a colour of the scheme, not one of ours') eq(o.colors.option, false, 'options are left as the shell drew them') eq(o.colors.unknown, false, 'and so is a command the terminal does not find') end) test('options: merge, turn off, clamp', function() local o, w = core.options { ai = false, session_bar = false, keys = { ask = { key = 'Space', mods = 'CTRL' }, cli = false, model = { key = 'M', mods = 'CTRL|SHIFT' } }, context = { blocks = 99, output_lines = 0 }, display = { width = 0.25 }, mode = 'auto', autocomplete = true, history = { max = 10 }, colors = { ghost = '#808080', command = 10, unknown = false }, cli = '/opt/lembas', } eq(#w, 0, 'no warnings') eq(o.ai, false, 'AI off by default, as asked') 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.cli, false, 'key off') eq(o.keys.model.key, 'M', 'a key given to what had none') 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.display.width, 0.25, 'width') eq(o.mode, 'auto', 'mode') eq(o.history.max, 10, 'history size') eq(o.history.enabled, true, 'the rest of the table stays') eq(o.colors.ghost, '#808080', 'a colour by its value') eq(o.colors.command, 10, 'a colour of the palette') eq(o.colors.unknown, false, 'a colour turned off') eq(o.colors.string, 3, 'other colours stay') 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', ai = 'on', keys = { ask = 'A', nope = false }, display = { width = 7 }, split = { size = 0.3 }, mode = 'edit', bar = { fields = { 'model', 'nonsense' } }, context = 'all', colors = { ghost = 'grey', command = 256, nope = 1 }, cli = 7, } check(#w >= 11, 'a warning for each: got ' .. #w) eq(o.palette, true, 'wrong type keeps the default') eq(o.ai, true, 'ai') eq(o.keys.ask.key, 'A', 'bad key keeps the default') eq(o.display.width, 0.33, 'width') eq(o.mode, 'manual', 'there is no edit mode here') eq(#o.bar.fields, 1, 'unknown field dropped') eq(o.context.blocks, 5, 'context untouched') eq(o.colors.ghost, 8, 'a colour that is none keeps the default') eq(o.colors.command, 2, 'out of the palette') eq(o.colors.nope, nil, 'no such colour') eq(o.cli, nil, 'cli') local _, w2 = core.options 'nonsense' eq(#w2, 1, 'not a table') eq(core.options({ display = { width = 0.01 } }).display.width, 0.15, 'never too narrow to read') 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') eq(s.display, nil, "the CLI's own interface is not the display") local d = core.session_from_vars { lembas_state = 'asking', lembas_surface = 'display', lembas_ask = 'approval yn terminal_run: make\27[2J test' } eq(d.display, true, 'the display says that it is one') eq(d.ask.kind, 'approval', 'what it asks: the kind') eq(d.ask.keys, 'yn', 'the keys') eq(d.ask.text, 'terminal_run: make[2J test', 'the text, cleaned') eq(core.parse_ask '', nil, 'nothing asked') eq(core.parse_ask 'rm -rf /', nil, 'not an ask') eq(core.parse_ask(nil), nil, 'nil') 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') eq(core.bar_items(nil, fields, false, true)[2].Attribute.Intensity, 'Half', 'AI off is shown, quietly') eq(#core.bar_parts(core.session_from_vars { lembas_state = 'idle' }, fields, true, true), 1, 'AI off: that alone') eq(core.bar_parts(nil, fields, false, true)[1].text, 'AI off', 'in words') 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: only a pane the terminal knows to run LLeMbas marks a tab', 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 = {} } local function yes() return true end eq(core.tab_title({ tab_index = 0, panes = { shell, idle }, active_pane = { title = 'zsh' } }, { trusted = yes }), nil, 'nobody asks') eq(core.tab_title(nil), nil, 'nil') local tab = { tab_index = 1, tab_title = '', panes = { shell, asking }, active_pane = { title = 'zsh\27]0;evil\7' } } local items = core.tab_title(tab, { max_width = 16, trusted = yes }) 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.tab_title(tab, { max_width = 16 }), nil, 'nobody vouches for the pane: not marked') eq( core.tab_title(tab, { trusted = function(p) return p.pane_id ~= 2 end, }), nil, 'the pane that says it asks is not one of ours: not marked' ) end) test('the trust rule: which process is the engine, and whether a display still runs', function() local cli, real = '/home/u/.local/bin/lembas', '/opt/lembas/lembas' eq(core.is_engine(cli, cli, nil), true, 'the path the layer starts') eq(core.is_engine(real, cli, real), true, 'the same with its links followed') eq(core.is_engine(real .. ' (deleted)', cli, real), true, 'replaced on disk while it runs: still what was started') eq(core.is_engine('/usr/bin/zsh', cli, real), false, 'a shell') eq(core.is_engine('/tmp/lembas', cli, real), false, 'another program of the same name') eq(core.is_engine('lembas', cli, real), false, 'a bare name') eq(core.is_engine(nil, cli, real), false, 'nothing') eq(core.is_engine(cli, '', nil), false, 'no CLI') eq(core.is_engine('', '', ''), false, 'nothing at all') local started = core.held { cli, 'terminal', '--window', 't1-2' } local function copy(list) local out = {} for i, v in ipairs(list) do out[i] = v end return out end local engine = { executable = real, argv = { cli, 'terminal', '--window', 't1-2' }, children = {} } local held = { executable = '/usr/bin/dash', argv = copy(started), children = { [77] = engine } } eq(core.display_runs(held, started, cli, real), true, 'the shell that holds the pane, with the engine under it') eq(core.display_runs(engine, started, cli, real), true, 'the engine itself in front') eq(core.display_runs(nil, started, cli, real), nil, 'the terminal cannot say: not known') eq(core.display_runs('x', started, cli, real), nil, 'not a process at all: not known') eq( core.display_runs({ executable = '/usr/bin/dash', argv = copy(started), children = {} }, started, cli, real), false, 'the holding shell alone: the engine has ended' ) eq( core.display_runs({ executable = '/usr/bin/dash', argv = copy(started), children = { [78] = { executable = '/usr/bin/sleep', argv = { 'sleep', '3600' }, children = {} } }, }, started, cli, real), false, 'the holding shell asleep after the engine: ended' ) eq( core.display_runs({ executable = '/usr/bin/zsh', argv = { 'zsh' }, children = { [79] = engine } }, started, cli, real), false, 'another shell in front, whatever runs under it' ) local other = copy(started) other[#other] = 't1-3' eq( core.display_runs({ executable = '/usr/bin/dash', argv = other, children = { [77] = engine } }, started, cli, real), false, 'not the command that was started there' ) eq( core.display_runs({ executable = '/usr/bin/dash', argv = copy(started), children = { [80] = { executable = '/usr/bin/env', children = { [81] = engine } } } }, started, cli, real), true, 'the engine further down under the command that was started' ) eq(core.display_runs(held, {}, cli, real), false, 'nothing is remembered as started: nothing is trusted') eq(core.display_runs(held, started, '', ''), false, 'no CLI: nothing is the engine') local words = { '/bin/sh', '-c', 'a b\n"c"', '', 'x' } local back = core.split_args(core.join_args(words)) eq(#back, 5, 'a command kept as one string and read back') eq(back[3], 'a b\n"c"', 'word for word') eq(back[4], '', 'an empty word too') eq(#core.split_args(nil), 0, 'nothing kept: nothing') eq(core.last_line('one\n\27[31mnot logged in\27[0m: lembas login\n\n \n'), 'not logged in: lembas login', 'the last line that says something, cleaned') eq(core.last_line(''), '', 'none') end) test('what a pane says is shown without anything invisible in it', function() eq(core.one_line('safe\226\128\174txt.exe', 64), 'safetxt.exe', 'a right-to-left override') eq(core.one_line('a\226\128\139b\226\128\141c\239\187\191d', 64), 'abcd', 'zero-width characters') eq(core.one_line('x\239\184\143y\243\160\128\129z', 64), 'xyz', 'a variation selector and a tag') eq(core.one_line('a\226\128\168b', 64), 'ab', 'a line separator') eq(core.one_line('žltý kôň ✓ 🙂 中', 64), 'žltý kôň ✓ 🙂 中', 'everything that shows stays') eq(core.visible 'plain', 'plain', 'plain text is not touched') 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('looks_secret: the table of cases the Rust side is held to as well', function() local held, ordinary = 0, 0 for line in io.lines(here .. '/secrets.txt') do local sign, text = line:match('^([+-]) (.*)$') if sign == '+' then held = held + 1 check(core.looks_secret(text) == true, 'missed: ' .. text) elseif sign == '-' then ordinary = ordinary + 1 check(core.looks_secret(text) == false, 'taken for a secret: ' .. text) end end check(held >= 80 and ordinary >= 150, string.format('the table was read: %d and %d cases', held, ordinary)) eq(core.looks_secret(''), false, 'nothing') eq(core.looks_secret(nil), false, 'not text') eq(core.looks_secret('export HESLO_TOKEN=žltý'), true, 'a value that is not ASCII') eq(core.looks_secret('echo žltý kôň'), false, 'text that is not ASCII is only text') end) test('a context file: a command that looks as if it holds a secret is left out, with its output', function() local fetched = {} local function fetch(z) fetched[#fetched + 1] = z.text return z.text end local zones = { zone('Prompt', 0, '$ '), zone('Input', 0, 'export GITHUB_TOKEN=abc123'), zone('Prompt', 1, '$ '), zone('Input', 1, 'mysql -u root -pS3cretWord shop'), zone('Output', 2, 'ERROR 1045: Access denied for S3cretWord\n', 3), zone('Prompt', 4, '$ '), zone('Input', 4, 'env | sort'), zone('Output', 5, 'HOME=/home/u\nAPI_KEY=abc123\nLANG=C\n', 8), zone('Prompt', 9, '$ '), zone('Input', 9, 'git status'), zone('Output', 10, 'clean\n', 11), zone('Prompt', 12, '$ '), } local blocks = core.blocks_from_zones(zones, fetch, LIM) eq(#blocks, 4, 'four commands ran') eq(blocks[1].command, core.SECRET_COMMAND, 'the export is not there, only that there was one') eq(blocks[2].command, core.SECRET_COMMAND, 'nor the password on a command line') eq(blocks[2].output, '', 'and what it printed goes with it') eq(blocks[3].command, 'env | sort', 'an ordinary command stays') eq(blocks[3].output, 'HOME=/home/u\n' .. core.SECRET_LINE .. '\nLANG=C', 'a line of its output that holds one is left out') eq(blocks[4].command, 'git status', 'the next command is as it was') eq(blocks[4].output, 'clean', 'with its output') local json = core.encode_context(core.context { pane = 1, cwd = '/p', blocks = blocks }) check(not json:find('abc123', 1, true) and not json:find('S3cretWord', 1, true), 'nothing of a secret is in the file') for _, text in ipairs(fetched) do check(not text:find('Access denied', 1, true), 'the output of such a command is not even read') end -- a command over several lines, the secret on its second local multi = core.blocks_from_zones({ zone('Input', 0, 'curl https://example.org \\\n -H "Authorization: Bearer abc123def456"', 1), zone('Output', 2, 'ok\n'), zone('Prompt', 3, '$ '), }, text_of, LIM) eq(multi[1].command, core.SECRET_COMMAND, 'found on any of its lines') eq(core.scrub 'a\nPASSWORD=hunter2\nb\n', 'a\n' .. core.SECRET_LINE .. '\nb\n', 'the screen, a line at a time') eq(core.scrub 'plain', 'plain', 'nothing to leave out') eq(core.scrub '', '', 'empty') end) 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') local none, why = core.context { pane = 2 } eq(none, nil, 'nothing at all: no context (the engine refuses an empty one, M3)') eq(why, 'empty', 'and why') local blank, why2 = core.context { pane = 2, blocks = core.array {}, screen = '' } eq(blank, nil, 'an empty screen is nothing too') eq(why2, 'empty', 'and why') -- version 2: the shell's host and each block's seq, only when asked for local known = function(z) if z.start_y == 0 then return 17, 2 end end local b2 = core.blocks_from_zones({ zone('Input', 0, 'make'), zone('Output', 1, 'err') }, text_of, LIM, known) local v2 = core.context { version = 2, pane = 7, cwd = '/srv', shell = 'bash', host = 'buildhost', blocks = b2 } eq( core.encode_context(v2), '{"version":2,"pane":7,"cwd":"/srv","shell":"bash","host":"buildhost","blocks":[{"seq":17,"command":"make","exit":2,"output":"err"}]}', 'version 2: host, seq, the exit the shell said' ) local b1 = core.blocks_from_zones({ zone('Input', 0, 'make'), zone('Output', 1, 'err') }, text_of, LIM, known) local v1 = core.context { pane = 7, cwd = '/srv', host = 'buildhost', blocks = b1 } eq( core.encode_context(v1), '{"version":1,"pane":7,"cwd":"/srv","blocks":[{"command":"make","exit":2,"output":"err"}]}', 'version 1: no host and no seq' ) 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-term/context-1-ab.json', '/terminal-context /run/user/1000/llembas-term/context-1-ab.json', 'command' ) eq(core.context_command '/x\r/quit', '/terminal-context /x /quit', 'no line break can be smuggled in') eq(core.ask_bytes('hi', nil), '\27[200~hi\27[201~\r', 'question alone') eq(core.ask_bytes('a\nb', '/r/c.json'), '/terminal-context /r/c.json\r\27[200~a\nb\27[201~\r', 'context, then a two-line question') eq(core.ask_bytes('x\27[201~\ry', nil), '\27[200~x[201~y\27[201~\r', 'the paste cannot be ended from inside') 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') end) test('cli_candidates', function() local c = core.cli_candidates(nil, '/home/ana', '/usr/local/bin:/usr/bin', false) eq(c[1], '/usr/local/bin/lembas', 'PATH first, in order') eq(c[2], '/usr/bin/lembas', 'PATH 2') eq(c[3], '/home/ana/.local/bin/lembas', 'then where its installer puts it') eq(#c, 3, 'and nowhere else: no copy beside the terminal') eq(#core.cli_candidates('/x/lembas', '/home/ana', '/usr/bin', false), 1, 'explicit: only that') eq(core.cli_candidates(nil, nil, 'C:\\a;C:\\b', true)[2], 'C:\\b\\lembas.exe', 'windows') eq(#core.cli_candidates(nil, nil, nil, false), 0, 'nothing known') eq(#core.cli_candidates(nil, '/home/ana', '/home/ana/.local/bin', false), 1, 'a directory is tried once') eq(core.ide_candidates(nil, '/home/ana', '/usr/bin', false)[2], '/home/ana/.local/bin/llembas-ide', 'the IDE the same way') eq(core.ide_candidates('/opt/ide', '/home/ana', '/usr/bin', false)[1], '/opt/ide', 'the IDE, explicit') end) test('the CLI says what it can do', function() local info = core.cli_info { name = 'lembas', version = '2.0.0-beta.21', terminal_contract = 4, capabilities = { 'terminal_state', 'terminal_surface', 'terminal_models', 7 }, } eq(info.version, '2.0.0-beta.21', 'version') eq(core.cli_has(info, 'display'), true, 'the display') eq(core.cli_has(info, 'models'), true, 'the models') eq(core.cli_before_contract(info), false, 'of the contract that knows the terminal by its name') local old = core.cli_info { version = '2.0.0-beta.18', terminal_contract = 1, capabilities = { 'terminal_state' } } eq(core.cli_has(old, 'display'), false, 'a CLI of the first contract has no display') local half = core.cli_info { version = 'x', terminal_contract = 4, capabilities = { 'terminal_models' } } eq(core.cli_has(half, 'display'), false, 'decided by the part named, not by the contract alone') -- an engine from before the rename has every part and still is not it: it -- does not know the terminal by its new name local before = core.cli_info { version = '2.0.0-beta.20', terminal_contract = 3, capabilities = { 'terminal_surface', 'terminal_models', 'terminal_settings', 'terminal_placed' }, } for _, part in ipairs { 'display', 'models', 'settings', 'placed' } do eq(core.cli_has(before, part), false, 'contract 3 has no ' .. part .. ' for this terminal') end eq(core.cli_before_contract(before), true, 'and that is known to be why') eq(core.cli_before_contract(nil), false, 'a CLI that says nothing of itself is not that') eq(core.CONTRACT, 4, 'the contract of the names') eq(core.cli_info '2.0.0-beta.17', nil, 'plain text is not that') eq(core.cli_info { version = '2.0.0' }, nil, 'no list of parts') eq(core.cli_has(nil, 'display'), false, 'nil') eq(core.cli_has(false, 'display'), false, 'false') eq(table.concat(core.version_json_args '/x/lembas', ' '), '/x/lembas --version --json', 'how it is asked') end) test('the display: its command, its lines and keys', function() eq( table.concat(core.display_args('/x/lembas', { window = 't5-2', pane = 7, context = '/run/c.json', mode = 'auto', new = true }), ' '), '/x/lembas terminal --window t5-2 --pane 7 --terminal-context /run/c.json --mode auto --new', 'everything' ) eq(table.concat(core.display_args('lembas', { window = 't5-2' }), ' '), 'lembas terminal --window t5-2', 'the least') eq( table.concat(core.display_args('lembas', { window = 'w', mode = 'edit', model = '-x', pane = 'p' }), ' '), 'lembas terminal --window w', 'what is not a mode, a model or a pane is left out' ) eq(table.concat(core.display_args('lembas', { window = 'w', model = 'open/flash' }), ' '), 'lembas terminal --window w -m open/flash', 'a model') eq(core.window_id(1700000000, 3), 't1700000000-3', 'this run of the terminal and the tab') check(core.window_id(1700000000.7, 3):match('^[%w%._%-]+$') ~= nil, 'only characters an id may have') eq(core.display_line('model', 'open/flash'), '/model open/flash\r', 'model') eq(core.display_line('model', 'x\r/quit'), nil, 'a model that is more than a word is not sent') eq(core.display_line('model', '-m'), nil, 'nor one that looks like an option') eq(core.display_line('mode', 'auto'), '/mode auto\r', 'mode') eq(core.display_line('mode', 'edit'), nil, 'no edit mode') eq(core.display_line 'new', '/new\r', 'new') eq(core.display_line 'quit', nil, 'nothing else') eq(core.DISPLAY_KEYS.approve .. core.DISPLAY_KEYS.always .. core.DISPLAY_KEYS.deny, '/approve\r/always\r/deny\r', 'a line each') eq(core.DISPLAY_KEYS.stop, '/stop\r', 'what stops the turn') local held = core.held { 'lembas', 'terminal', '; rm -rf /' } eq(held[1] .. ' ' .. held[2], '/bin/sh -c', 'through a shell that holds the pane') eq(held[7], '; rm -rf /', 'the words as arguments, never as text of the script') eq(#held, 7, 'all of them') eq(core.is_model_ref 'provider/model-1.5:latest@x', true, 'a ref') eq(core.is_model_ref(string.rep('a', 200)), false, 'too long') eq(core.is_model_ref 'a b', false, 'two words') end) test('the terminal\'s two models', function() local m = core.terminal_models { models = { { id = 'open/flash', ref = 'open/flash', name = 'Flash', ['for'] = { 'main', 'autocomplete' } }, { id = 'srv-big', ref = 'big', name = 'big', ['for'] = { 'main' } }, { id = 'bad id', ref = 'also bad', name = 'x' }, 'nonsense', }, main = { start = 'open/flash' }, autocomplete = { model = 'open/flash', account = 'other' }, } eq(m.main, 'open/flash', 'what a display would start on') eq(m.autocomplete, 'open/flash', "the person's own choice before the account's") eq(#m.choices.main, 2, 'two to choose from') eq(m.choices.main[1].label, 'Flash · open/flash', 'name and ref') eq(m.choices.main[2].id, 'big', 'a display takes the ref') eq(m.choices.main[2].label, 'big', 'a name that says nothing more is not repeated') eq(#m.choices.autocomplete, 1, 'only what may complete') eq(core.terminal_models { models = {}, main = { model = 'mine', start = 'x' } }.main, 'mine', 'chosen before the start model') eq(core.terminal_models { models = {} }.main, nil, 'none known') eq(core.terminal_models 'text', nil, 'not that') eq(table.concat(core.models_args 'l', ' '), 'l terminal models --json', 'list') eq(table.concat(core.models_set_args('l', 'main', 'a/b'), ' '), 'l terminal models set --main a/b', 'set') eq(table.concat(core.models_set_args('l', 'autocomplete', nil), ' '), 'l terminal models set --autocomplete default', 'none of my own') eq(core.ask_prefix('open/flash', 'auto'), 'llembas open/flash · auto', 'the ask line names both') eq(core.ask_prefix(nil, 'manual', true), 'llembas manual · this screen', 'no model known') eq(core.ask_prefix(nil, nil), 'llembas', 'nothing known') eq(core.ask_prefix('m\27[31m', 'auto'), 'llembas m[31m · auto', 'cleaned') end) test('status records', function() local text = 'llembas-term-status=1\nid=4242.1700000000\nseq=17\nstate=prompt\nexit=2\nran=1\ntime=1700000123\nshell=zsh\npid=4242\ncwd=/home/user/my project\nfuture=x\n' local r = core.parse_status(text) eq(r.id, '4242.1700000000', 'id') eq(r.seq, 17, 'seq') eq(r.state, 'prompt', 'state') eq(r.exit, 2, 'exit') eq(r.ran, true, 'a command ran') eq(r.cwd, '/home/user/my project', 'cwd to the end of the line') eq(r.shell, 'zsh', 'shell') eq( core.status_text(r), 'llembas-term-status=1\nid=4242.1700000000\nseq=17\nstate=prompt\nexit=2\nran=1\ntime=1700000123\nshell=zsh\nvia=uservar\ncwd=/home/user/my project\n', 'as the terminal writes it: no process id, and said to have come by variable' ) eq(core.parse_status('llembas-term-status=1\nid=a\nseq=1\n').state, 'prompt', 'state absent: at the prompt') eq(core.parse_status('llembas-term-status=1\nid=a\nseq=1\n').exit, nil, 'no exit before the first command') eq(core.parse_status('llembas-term-status=1\nid=a\nseq=1\nstate=running\n').state, 'running', 'running') eq(core.parse_status('llembas-term-status=1\r\nid=a\r\nseq=1\r\n').id, 'a', 'line ends of another kind') eq(core.parse_status('llembas-term-status=1\nid=a\nseq=1\ncwd=/x\27]0;t\7y\n').cwd, '/x]0;ty', 'no escape sequence survives') eq(core.parse_status 'llembas-term-status=2\nid=a\nseq=1\n', nil, 'another version') eq(core.parse_status 'id=a\nseq=1\n', nil, 'no first line') eq(core.parse_status 'llembas-term-status=1\nseq=1\n', nil, 'no id') eq(core.parse_status 'llembas-term-status=1\nid=a b\nseq=1\n', nil, 'an id of two words') eq(core.parse_status 'llembas-term-status=1\nid=a\nseq=x\n', nil, 'seq not a number') eq(core.parse_status 'llembas-term-status=1\nid=a\nseq=1\nstate=odd\n', nil, 'a state there is none of') eq(core.parse_status 'llembas-term-status=1\nid=a\nseq=1\nnonsense\n', nil, 'a line with no =') eq(core.parse_status('llembas-term-status=1\nid=a\nseq=1\ncwd=' .. string.rep('x', 2000) .. '\n'), nil, 'a value too long') eq(core.parse_status(string.rep('x', 5000)), nil, 'too much') eq(core.parse_status(nil), nil, 'nil') eq(core.parse_status('llembas-term-status=1\nid=a\nseq=1\nexit=999\n').exit, nil, 'an exit status there is none of') eq(core.same_status(r, core.parse_status(text)), true, 'the same moment') eq(core.same_status(r, core.parse_status 'llembas-term-status=1\nid=4242.1700000000\nseq=18\n'), false, 'a later one') eq(core.same_status(nil, r), false, 'none') local function env(t) return function(name) return t[name] end end eq(core.status_dir(env { LLEMBAS_TERM_STATUS_DIR = '/s', XDG_RUNTIME_DIR = '/r' }, '/h'), '/s', 'named') eq(core.status_dir(env { LLEMBAS_TERM_STATUS_DIR = 'rel', XDG_RUNTIME_DIR = '/r' }, '/h'), '/r/llembas-term', 'runtime') eq(core.status_dir(env { XDG_STATE_HOME = '/st' }, '/h'), '/st/llembas-term', 'state') eq(core.status_dir(env {}, '/h'), '/h/.local/state/llembas-term', 'home') eq(core.status_dir(env {}, nil), nil, 'nowhere') eq(core.status_file_name(12), 'pane-12.status', 'the file') eq(core.status_dir_of('/r/llembas-term', 4242), '/r/llembas-term/status-4242', 'of one run of the terminal') eq(core.status_dir_of('/r/llembas-term', nil), '/r/llembas-term', 'when it does not know its own process') eq(core.status_dir_pid '/r/llembas-term/status-4242', 4242, 'whose it is') eq(core.status_dir_pid '/r/llembas-term/sock', nil, 'any other name is not one') eq(core.status_dir_pid '/r/llembas-term/status-x', nil, 'nor this') 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_TERM_UNIX_SOCKET = dir .. '/run/gui-sock-1' env.XDG_RUNTIME_DIR = false end -- What a CLI answers when asked what it can do: one that has the display, -- one from before it (which says its version and nothing else). local JSON = {} local function json_parse(text) local v = JSON[text] if v == nil then error 'not json' end return v end JSON['INFO2'] = { name = 'lembas', version = '2.0.0-beta.21', terminal_contract = 4, capabilities = { 'terminal_state', 'terminal_context', 'terminal_tools', 'complete_shell', 'terminal_surface', 'terminal_await', 'terminal_models' }, } JSON['MODELS'] = { models = { { id = 'open/flash', ref = 'open/flash', name = 'Flash', ['for'] = { 'main', 'autocomplete' } }, { id = 'open/big', ref = 'open/big', name = 'Big', ['for'] = { 'main' } }, }, main = { start = 'open/big' }, autocomplete = { account = 'open/flash' }, } local NEW = { ['--version --json'] = { true, 'INFO2' }, ['terminal models --json'] = { true, 'MODELS' }, ['terminal models set --main open/flash'] = { true, 'MODELS' }, ['terminal models set --autocomplete open/flash'] = { true, 'MODELS' }, } -- beta.18: knows terminals, has no display local B18 = { ['--version --json'] = { true, 'not json' }, ['--version'] = { true, '2.0.0-beta.18\n' } } local OLD = { ['--version'] = { true, '2.0.0-beta.17\n' } } local function new_layer(extra, options, config) local e = { child_results = NEW, json_parse = json_parse } for k, v in pairs(extra or {}) do e[k] = v end return layer(e, options, config) end 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 -- The display's own command in a split: what follows the shell that holds the pane open. local function dargs(split) local out = {} for i = 5, #split.args do out[#out + 1] = split.args[i] end return out end local function spawned(w, what) local n = 0 for _, args in ipairs(w.spawned) do if table.concat(args, ' ', 2) == what then n = n + 1 end end return n end local function described(win) local a = win.actions[#win.actions] return a and a.action.args and a.action.args.description or '' end local ZONES = { zone('Prompt', 0, '$ '), zone('Input', 0, 'cargo test'), zone('Output', 1, 'error: it broke\n'), zone('Prompt', 2, '$ '), } local DISPLAY_IDLE = { lembas_session = 's1', lembas_state = 'idle', lembas_surface = 'display', lembas_model = 'open/flash', lembas_mode = 'auto' } -- What the display asks has stood there for a while (S15: an answer is for -- what was read, not for what replaced it a moment before). local function settle(w, display) w.GLOBAL.llembas_ask_seen['d' .. tostring(display:pane_id())] = 0 end -- The display of a tab, as the only thing that makes one: the layer starts -- it beside the person's pane (the display key). The pane that comes of it -- then publishes what a display publishes. Needs a CLI (with_cli) and AI on. local function started(w, pane, vars) local focus = pane.activated w.emit('llembas-display', fake.window(), pane) local display = pane.last_split assert(display, 'the display did not start') pane.last_split = nil pane.activated = focus -- it has been up for a while: a display just started may not have its -- engine under it yet, and is given a moment before it counts as ended w.GLOBAL.llembas_display_started['d' .. tostring(display:pane_id())] = 0 display.vars = {} for k, v in pairs(vars or DISPLAY_IDLE) do display.vars[k] = v end return display end test('apply: keys, events, and nothing else happens', function() local dir = fresh_dir 'apply' with_cli(dir) local _, w, config = new_layer(nil, nil, { keys = { { key = 'x', mods = 'CTRL', action = 'mine' } } }) eq(#config.keys, 15, 'fourteen 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-key', 'ask: a chord LLeMbas IDE has too, so through the event that decides') eq(config.keys[3].action.args, 'llembas-cli', 'the CLI whole: the terminal\'s alone') eq(config.keys[4].action.args, 'llembas-ai-key', 'the switch') local bound = {} for _, k in ipairs(config.keys) do if type(k.action) == 'table' then bound[k.mods .. '+' .. k.key] = k.action.args end end eq(bound['CTRL|SHIFT+M'], 'llembas-model-key', 'the list of chat models') eq(bound['CTRL|ALT+m'], 'llembas-autocomplete-model-key', 'the list of autocomplete models') eq(bound['CTRL+Tab'], 'llembas-mode-next-key', 'the other mode') eq(bound['CTRL|SHIFT|ALT+P'], 'llembas-palette', "the terminal's palette, never handed on") eq(bound['CTRL|SHIFT+P'], 'llembas-pass-ctrl-shift-p', 'the palette chord is the IDE\'s while it is in front') eq(bound['CTRL|SHIFT+H'], 'llembas-private', 'a key the IDE does not have stays plain') eq(config.enable_kitty_keyboard, true, 'the keyboard protocol for a program that asks') for _, k in ipairs(config.keys) do if type(k.action) == 'table' then check(w.handlers[k.action.args] ~= nil, 'a handler behind ' .. k.action.args) end end eq(#w.spawned, 0, 'no process started by applying') check(config.set_environment_variables.LLEMBAS_TERM_STATUS_DIR:sub(1, 1) == '/', 'every pane is told where the status records are') eq(next(config.set_environment_variables, 'LLEMBAS_TERM_STATUS_DIR'), nil, 'and nothing else is put in its environment') for _, name in ipairs { 'update-status', 'format-tab-title', 'augment-command-palette', 'user-var-changed', 'llembas-ask', 'llembas-ai' } do check(w.handlers[name] ~= nil, 'handler for ' .. name) end local entries = w.emit 'augment-command-palette' eq(#entries, 32, 'the palette entries') local names = {} for _, e in ipairs(entries) do names[e.brief] = e.action.args end eq(names['LLeMbas: ask the model'], 'llembas-ask', 'ask') eq(names['LLeMbas: open the CLI'], 'llembas-cli', 'open the CLI') eq(names['LLeMbas: AI on/off — here'], 'llembas-ai-here', 'the switch, kept for this directory') eq(names['LLeMbas: AI on/off — everywhere'], 'llembas-ai-everywhere', 'the switch, kept for this machine') eq(names['LLeMbas: AI autocomplete on/off — here'], 'llembas-autocomplete-here', 'so is every switch') eq(names['LLeMbas: display width — everywhere'], 'llembas-width-everywhere', 'and every choice') eq(names['LLeMbas: settings'], 'llembas-settings', 'what applies, and from where') eq(names['LLeMbas: autocomplete status'], 'llembas-autocomplete-status', 'the autocomplete in words') eq(names['LLeMbas: approve (allow once)'], 'llembas-approve', 'approve') eq(names['LLeMbas: deny'], 'llembas-deny', 'deny') eq(names['LLeMbas: stop the turn'], 'llembas-stop', 'stop') eq(names['LLeMbas: new conversation'], 'llembas-new', 'new') eq(names['LLeMbas: show/hide the display'], 'llembas-display', 'display') eq(names['LLeMbas: choose chat model — here'], 'llembas-model-here', 'model') eq(names['LLeMbas: choose mode — everywhere'], 'llembas-mode-everywhere', 'mode') eq(names['LLeMbas: set up (CLI and IDE)'], 'llembas-setup', 'set-up') for _, e in ipairs(entries) do check(w.handlers[e.action.args] ~= nil, 'a handler behind ' .. e.brief) end 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, ide_keys = false, kitty_keyboard = false, keys = { ask = false, cli = false, ai = false, approve = false, deny = false, stop = false, display = false, private = false, ide = false, model = false, ac_model = false, mode_next = false, palette = false, }, }) eq(#config2.keys, 0, 'no keys') eq(config2.enable_kitty_keyboard, nil, 'and the keyboard protocol is left as the terminal has it') local win_off, pane_off = fake.window(), fake.pane { id = 1 } fake.tab { pane_off } w2.emit('update-status', win_off, pane_off) eq(win_off.status, nil, 'no bar') eq(#w2.spawned, 0, 'and looking at a pane starts nothing') 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: the terminal itself is told what is on', function() local dir = fresh_dir 'native' with_cli(dir) local native = fake.native() local _, w = new_layer({ native = native }, { autocomplete = true, highlight = false, colors = { ghost = 7 } }) local c = native.last 'configure'[2] eq(c.suggestions, true, 'suggestions') eq(c.highlight, false, 'highlighting off, as asked') eq(c.ai_complete, true, 'the AI completes: asked for, and AI is on') eq(c.cli, dir .. '/bin/lembas', 'with this CLI') eq(c.history, true, 'history') eq(c.history_max, 5000, 'its size') eq(c.colors.ghost, 7, 'the colours') eq(#w.spawned, 0, 'no process started') local _, _ = new_layer({ native = native }, { autocomplete = true, ai = false }) eq(native.last 'configure'[2].ai_complete, false, 'AI off: the AI completes nothing, whatever the option') end) test('apply: the shell has no autocomplete of its own any more (S11)', function() local dir = fresh_dir 'ac' with_cli(dir) local l, w, config = layer({}, { autocomplete = 'shell' }, { set_environment_variables = { MINE = '1' } }) eq(config.set_environment_variables.LLEMBAS_TERM_AUTOCOMPLETE, nil, 'nothing exported for the shell') eq(config.set_environment_variables.LLEMBAS_TERM_CLI, nil, 'nor the CLI') eq(config.set_environment_variables.MINE, '1', "the person's own stays") eq(l.is_on 'autocomplete', false, "'shell' turns nothing on") check(#w.logs >= 1 and w.logs[1]:find("'shell' is no longer", 1, true) ~= nil, 'and it is said') eq(#w.spawned, 0, 'still no process') local _, _, config3 = layer({}, { autocomplete = true }) eq(config3.set_environment_variables.LLEMBAS_TERM_AUTOCOMPLETE, nil, 'drawn by the terminal: the shell integration stays out of it') local _, w4 = layer({}, { autocomplete = 'zsh' }) check(#w4.logs == 1, 'anything else is a mistake, said') end) test('AI off: every key but the switch does nothing, and says so once', function() local dir = fresh_dir 'aioff' with_cli(dir) local l, w = new_layer(nil, { ai = false }) local win = fake.window() local pane = fake.pane { id = 7, zones = ZONES } fake.tab { pane } eq(l.is_on 'ai', false, 'off, as the option says') -- a display from a moment when AI was on w.emit('llembas-ai', fake.window(), pane) local display = started(w, pane, { lembas_state = 'asking', lembas_surface = 'display', lembas_ask = 'approval yn terminal_run: make test' }) w.emit('llembas-ai', fake.window(), pane) local spawned0 = #w.spawned w.emit('llembas-ask', win, pane) eq(#win.actions, 1, 'told') check(described(win):find('AI is off', 1, true), 'in words') check(described(win):find('Ctrl+Shift+I', 1, true), 'with the key that turns it on') for _, ev in ipairs { 'llembas-ask', 'llembas-ask-screen', 'llembas-cli', 'llembas-approve', 'llembas-always', 'llembas-deny', 'llembas-stop', 'llembas-new', 'llembas-display', 'llembas-model', 'llembas-mode', 'llembas-login' } do w.emit(ev, win, pane) end w.emit('llembas-ask-line', win, pane, 'q') eq(#win.actions, 1, 'said once, then quiet') eq(#w.spawned, spawned0, 'no process started') eq(#pane.splits, 1, 'no pane') eq(#display.sent, 0, 'nothing sent to a display') eq(pane.read_zones, nil, 'nothing read') eq(#context_files(dir), 0, 'nothing written') w.emit('update-status', win, pane) eq(win.status, ' AI off ', 'shown in the bar') -- the switch works at any moment w.emit('llembas-ai', win, pane) eq(l.is_on 'ai', true, 'on') w.emit('llembas-approve', win, pane) eq(display.sent[1][2], '/approve\r', 'and the keys work') w.emit('llembas-ai', win, pane) eq(l.is_on 'ai', false, 'off again') w.emit('llembas-deny', win, pane) eq(#display.sent, 1, 'quiet again') end) test('AI switch: said where the person is, and the terminal is told', function() local native = fake.native() local l, w = new_layer({ native = native }, { autocomplete = true }) local win, pane = fake.window(), fake.pane { id = 3 } w.emit('llembas-ai', win, pane) eq(l.is_on 'ai', false, 'off') eq(native.last 'notice'[3], 'AI off' .. core.messages.remember_needs_cli, 'said at the cursor, not in a dialog; and once, that it is not remembered') eq(#win.actions, 0, 'no overlay') eq(native.last 'configure'[2].ai_complete, false, 'the AI stops completing') eq(native.last 'configure'[2].suggestions, true, 'history suggestions are not AI') w.emit('llembas-ai', win, pane) eq(native.last 'notice'[3], 'AI on', 'on') eq(native.last 'configure'[2].ai_complete, true, 'again') -- the switch outlives a reload of the configuration w.emit('llembas-ai', win, pane) package.loaded['llembas'] = nil local l2 = require 'llembas' l2.apply({}, {}) eq(l2.is_on 'ai', false, 'still off after the configuration is read again') end) test('switches of what the terminal draws', function() local native = fake.native() local l, w = new_layer { native = native } local win, pane = fake.window(), fake.pane { id = 3 } w.emit('llembas-suggestions', win, pane) eq(l.is_on 'suggestions', false, 'off') eq(native.last 'configure'[2].suggestions, false, 'told') eq(native.last 'notice'[3], 'suggestions off' .. core.messages.remember_needs_cli, 'said') w.emit('llembas-highlight', win, pane) eq(native.last 'configure'[2].highlight, false, 'highlighting') w.emit('llembas-autocomplete', win, pane) eq(native.last 'configure'[2].ai_complete, true, 'AI autocomplete on') w.emit('llembas-clear-history', win, pane) eq(native.count 'clear_history', 1, 'history cleared') -- without that part of the terminal: said, never silent local _, w2 = new_layer {} local win2 = fake.window() w2.emit('llembas-suggestions', win2, pane) check(described(win2):find('needs the terminal window itself', 1, true), 'said') w2.emit('llembas-clear-history', win2, pane) eq(#win2.actions, 2, 'said again') end) test('ask key: the line in place, naming the model and the mode', function() local dir = fresh_dir 'askkey' with_cli(dir) local native = fake.native() local _, w = new_layer { native = native } local win, pane = fake.window(), fake.pane { id = 7, zones = ZONES } fake.tab { pane } w.emit('llembas-ask', win, pane) local call = native.last 'ask_line' eq(call[2], win, 'the window') eq(call[3], pane, 'the pane') eq(call[4].prefix, 'llembas manual', 'opened at once, from what is known without a process (S3)') eq(call[4].event, 'llembas-ask-line', 'the line comes back as this event') eq(spawned(w, 'terminal models --json'), 1, 'the models asked') eq(native.last 'ask_prefix'[4], 'llembas open/big · manual', 'then the model a display would start on, and the mode') eq(#win.actions, 0, 'no prompt bar') eq(pane.read_zones, nil, 'nothing read yet') w.emit('llembas-ask', win, pane) eq(spawned(w, 'terminal models --json'), 1, 'once') w.emit('llembas-ask-screen', win, pane) eq(native.last 'ask_line'[4].prefix, 'llembas open/big · manual · this screen', 'asking about the screen says so') eq(native.last 'ask_line'[4].event, 'llembas-ask-screen-line', 'and has its own event') -- with a display running, what it says of itself started(w, pane, DISPLAY_IDLE) w.emit('llembas-ask', win, pane) eq(native.last 'ask_line'[4].prefix, 'llembas open/flash · auto', "the running conversation's model and mode") end) test('ask key without the terminal\'s own part: a one-line prompt', function() local _, w = new_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: the display starts to the right, with a context file, and the focus stays', function() local dir = fresh_dir 'ask' with_cli(dir) local _, w = new_layer {} 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 }, 4) 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.33, 'a third') eq(s.cwd, '/home/u/proj', "in the pane's directory") eq(s.args[1] .. ' ' .. s.args[2], '/bin/sh -c', 'held open when the display cannot start, to say why') eq(s.args[3], core.DISPLAY_HOLD, 'by a script that is always the same') local a = dargs(s) eq(a[1], dir .. '/bin/lembas', 'the CLI') eq(a[2], 'terminal', 'as the display, never its own interface') eq(a[3], '--window', 'the conversation of this tab') check(a[4]:match('^t%d+%-4$') ~= nil, 'named by this run of the terminal and the tab: ' .. a[4]) eq(a[5] .. ' ' .. a[6], '--pane 7', "the person's pane") eq(a[7], '--terminal-context', 'flag') eq(a[8], files[1], 'file') eq(a[9] .. ' ' .. a[10], '--mode manual', 'the mode') eq(#a, 10, 'the question is not on the command line') eq(pane.activated, 1, 'the focus goes back to the person\'s pane') local new = pane.last_split eq(#new.sent, 0, 'nothing sent before the display 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, 1, 'sent when idle, in one write') eq(new.sent[1][1], 'text', 'written, not handed to the asynchronous paste') eq(new.sent[1][2], '\27[200~why did it fail?\27[201~\r', 'the question as a bracketed paste, then Enter') w.emit('user-var-changed', win, new, 'lembas_state', 'idle') eq(#new.sent, 1, 'only once') -- asked again before the display has said anything: no second display local pane2 = fake.pane { id = 9, zones = ZONES } fake.tab({ pane2 }, 5) w.emit('llembas-ask-line', win, pane2, 'first') w.emit('llembas-ask-line', win, pane2, 'second') eq(#pane2.splits, 1, 'one display for the tab') w.emit('user-var-changed', win, pane2.last_split, 'lembas_state', 'idle') local waited = pane2.last_split.sent[1][2] local named = waited:match('^/terminal%-context (%S+)\r\27%[200~second\27%[201~\r$') check(named ~= nil, 'the newest question is the one that waits, with its own context (S8)') check(named and read_file(named) ~= nil, 'and its file is there to be read') check(dargs(pane2.splits[1])[4] ~= a[4], 'another tab is another conversation') end) test('ask: into the display the tab already has', function() local dir = fresh_dir 'ask2' with_cli(dir) local _, w = new_layer {} local win = fake.window() local pane = fake.pane { id = 1, zones = ZONES, cwd = '/p' } fake.tab { pane } local display = started(w, pane, DISPLAY_IDLE) w.emit('llembas-ask-line', win, pane, 'explain\27[201~ this') local files = context_files(dir) eq(#files, 1, 'context file') eq(#pane.splits, 1, 'no new pane') eq(#display.sent, 1, 'one write') eq( display.sent[1][2], '/terminal-context ' .. files[1] .. '\r\27[200~explain[201~ this\27[201~\r', 'context line, Enter, the question pasted (and unable to end the paste), Enter' ) eq(display.read_zones, nil, 'the display itself is not read') eq(win.actions[1].action.action, 'SetPaneZoomState', 'a hidden display is shown again') eq(win.actions[1].action.args, false, 'by taking the zoom off') -- in the middle of a turn: words added to it display.vars.lembas_state = 'tools' w.emit('llembas-ask-line', win, pane, 'and then?') eq(#display.sent, 2, 'sent, as the display takes it') -- while it waits for an approval: said, not sent display.vars.lembas_state = 'asking' w.emit('llembas-ask-line', win, pane, 'hello?') eq(#display.sent, 2, 'not sent') check(described(win):find('allow or deny first', 1, true), 'said in words') check(described(win):find('Ctrl+Shift+Y', 1, true), 'with the keys') end) test('ask: a pane that runs the CLI itself is not the display, and is never written to', function() local dir = fresh_dir 'ask3' with_cli(dir) local native = fake.native() local _, w = new_layer { native = native } local pane = fake.pane { id = 1, zones = ZONES } local cli_pane = fake.pane { id = 2, vars = { lembas_session = 's1', lembas_state = 'idle' }, zones = ZONES, program = dir .. '/bin/lembas', } fake.tab { pane, cli_pane } w.emit('llembas-ask-line', fake.window(), pane, 'q') eq(#cli_pane.sent, 0, 'nothing is sent into the CLI beside') eq(#pane.splits, 1, 'the display is started') -- asked from inside it: it has its own line for questions; nothing is written w.emit('llembas-ask-line', fake.window(), cli_pane, 'hello') eq(#cli_pane.sent, 0, 'not typed there') eq(#cli_pane.splits, 0, 'and no display beside it') check(native.last 'notice'[3]:find('runs LLeMbas itself', 1, true), 'said') eq(cli_pane.read_zones, nil, 'not read') local asked = native.count 'ask_line' w.emit('llembas-ask', fake.window(), cli_pane) eq(native.count 'ask_line', asked, 'the ask key opens no ask line over it either') end) ---------------------------------------------------------------------------- -- The trust rule: the terminal trusts only what it started. Each of these -- failed before the rule: a pane that set the right variables was written to. local EVERY_VAR = { lembas_session = 's1', lembas_title = 'a title', lembas_model = 'open/flash', lembas_mode = 'auto', lembas_effort = 'high', lembas_state = 'idle', lembas_context = '9', lembas_tokens = '1k', lembas_spend = '0.01', lembas_surface = 'display', lembas_ask = 'approval yn terminal_run: rm -rf /', } local function every_key(w, win, pane) for _, ev in ipairs { 'llembas-approve', 'llembas-always', 'llembas-deny', 'llembas-stop', 'llembas-new', } do w.emit(ev, win, pane) end w.emit('llembas-model-pick', win, pane, 'open/flash') w.emit('llembas-mode-pick', win, pane, 'auto') end test('trust: a pane that forges any variable, or all of them, is never written to', function() local dir = fresh_dir 'forge1' with_cli(dir) local native = fake.native() local _, w = new_layer { native = native } local win = fake.window() local n = 0 local function forged(vars) n = n + 1 -- a shell in which something printed the sequences: a file shown with -- cat, a line of a log, a host reached over SSH local shell = fake.pane { id = n, vars = vars, zones = ZONES, program = '/bin/zsh' } fake.tab({ shell }, 100 + n) w.emit('llembas-ask-line', win, shell, 'why does `rm -rf build/` fail?') eq(#shell.sent, 0, 'the question is not typed into the pane that claims to be LLeMbas') eq(#shell.splits, 1, 'a display of our own is started for it instead') every_key(w, win, shell) eq(#shell.sent, 0, 'and no key of the layer writes to it') return shell end for name, value in pairs(EVERY_VAR) do forged { [name] = value } forged { [name] = value, lembas_state = 'asking' } end local all = forged(EVERY_VAR) -- and what it says is not shown as if LLeMbas said it w.emit('update-status', win, all) local hints = native.count 'hint' for name, value in pairs(EVERY_VAR) do w.emit('user-var-changed', win, all, name, value) end eq(native.count 'hint', hints, 'a forged "LLeMbas asks" is drawn nowhere') eq(#all.sent, 0, 'saying "idle" brings no question either') end) test('trust: a forged display beside the real one gets nothing', function() local dir = fresh_dir 'forge2' with_cli(dir) local native = fake.native() local _, w = new_layer { native = native } local win = fake.window() local pane = fake.pane { id = 1, zones = ZONES, cwd = '/p' } -- before the real one in the tab's list, and saying everything a display says local impostor = fake.pane { id = 2, vars = EVERY_VAR, program = '/usr/bin/ssh' } fake.tab { impostor, pane } w.emit('llembas-ask-line', win, pane, 'first') eq(#impostor.sent, 0, 'the question does not go to the pane that says it is the display') eq(#pane.splits, 1, 'the layer starts its own') local display = pane.last_split -- the impostor says it is ready: the question that waits is not for it w.emit('user-var-changed', win, impostor, 'lembas_state', 'idle') eq(#impostor.sent, 0, 'nothing on its word') eq(#display.sent, 0, 'and the real one has not spoken yet') display.vars = { lembas_state = 'idle', lembas_surface = 'display' } w.emit('user-var-changed', win, display, 'lembas_state', 'idle') eq(display.sent[1][2], '\27[200~first\27[201~\r', 'the real display gets it') w.emit('llembas-ask-line', win, pane, 'second') local named = display.sent[2][2]:match('^/terminal%-context (%S+)\r\27%[200~second\27%[201~\r$') check(named ~= nil and read_file(named) ~= nil, 'and the next, with its context file') -- the real one asks; the impostor asks something else display.vars = { lembas_state = 'asking', lembas_surface = 'display', lembas_ask = 'approval yn terminal_run: make test' } w.emit('user-var-changed', win, display, 'lembas_ask', display.vars.lembas_ask) check(native.last 'hint'[3]:find('make test', 1, true), 'the notice is the real display\'s') local hints = native.count 'hint' w.emit('user-var-changed', win, impostor, 'lembas_ask', EVERY_VAR.lembas_ask) eq(native.count 'hint', hints, 'the impostor\'s is not drawn') w.emit('llembas-approve', win, pane) eq(#display.sent, 2, 'what changed this very moment is not answered yet (S15)') check(native.last 'notice'[3]:find('something else now', 1, true), 'said') settle(w, display) w.emit('llembas-approve', win, pane) eq(display.sent[3][2], '/approve\r', 'the answer goes to the real one') every_key(w, win, pane) eq(#impostor.sent, 0, 'never a byte to the impostor') -- from inside the impostor: it is a pane like any other, with the tab's display display.vars = { lembas_state = 'idle', lembas_surface = 'display' } local before = #display.sent w.emit('llembas-ask-line', win, impostor, 'third') eq(#impostor.sent, 0, 'not typed into itself') eq(#display.sent, before + 1, 'it goes to the display the layer started') end) test('trust: the display\'s program replaced by a shell, and a display that was killed', function() local dir = fresh_dir 'forge3' with_cli(dir) local native = fake.native() local _, w = new_layer { native = native } local win = fake.window() local pane = fake.pane { id = 1, zones = ZONES } fake.tab({ pane }, 7) local display = started(w, pane, DISPLAY_IDLE) w.emit('llembas-ask-line', win, pane, 'one') eq(#display.sent, 1, 'while the engine runs there: sent') -- the engine was killed outright: its variables still stand (it could not -- empty them) and the shell that held the pane is all that is left display.process = { executable = '/usr/bin/dash', argv = display.spec.process.argv, children = {} } w.emit('llembas-ask-line', win, pane, 'how do I send output > ~/.bashrc') eq(#display.sent, 1, 'nothing is written to the pane the engine left') eq(#pane.splits, 2, 'a new display is started for the question') local second = pane.last_split w.GLOBAL.llembas_display_started['d' .. tostring(second:pane_id())] = 0 local found for _, a in ipairs(win.actions) do if a.action.action == 'CloseCurrentPane' and a.pane == display then found = a end end check(found ~= nil, 'the pane of the display that ended is closed by the layer') eq(found and found.action.args.confirm, false, 'without a question') second.vars = { lembas_state = 'idle', lembas_surface = 'display' } w.emit('user-var-changed', win, second, 'lembas_state', 'idle') eq(second.sent[1][2], '\27[200~how do I send output > ~/.bashrc\27[201~\r', 'which gets it when it is ready') -- the old pane speaking up again is nobody w.emit('user-var-changed', win, display, 'lembas_state', 'idle') eq(#display.sent, 1, 'the forgotten pane stays forgotten') -- a shell where the engine was: same pane, another program in front second.process = { executable = '/usr/bin/zsh', argv = { 'zsh' }, children = {} } every_key(w, win, pane) eq(#second.sent, 1, 'no key of the layer reaches a shell that took the display\'s pane') check(native.last 'notice'[3] ~= nil, 'and the person is told') -- a shell that starts the very engine again is still not what the layer started second.process = { executable = '/usr/bin/zsh', argv = { 'zsh' }, children = { [9] = { executable = dir .. '/bin/lembas', argv = { 'lembas', 'terminal' }, children = {} } }, } w.emit('llembas-approve', win, pane) eq(#second.sent, 1, 'nor one that runs the engine under another command') -- the terminal cannot say what runs there: nothing is sent, and that is said local pane3 = fake.pane { id = 3, zones = ZONES, next_id = 300 } fake.tab({ pane3 }, 8) local third = started(w, pane3, DISPLAY_IDLE) third.process = false local files_before = #context_files(dir) w.emit('llembas-ask-line', win, pane3, 'q') eq(#third.sent, 0, 'not known: not sent') check(native.last 'notice'[3]:find('Nothing was sent', 1, true), 'said in words') eq(#context_files(dir), files_before, 'and the context file written for it is removed') -- the display's pane was closed local pane4 = fake.pane { id = 4, zones = ZONES, next_id = 400 } local tab4 = fake.tab({ pane4 }, 9) local fourth = started(w, pane4, DISPLAY_IDLE) tab4.list = { pane4 } w.emit('llembas-stop', win, pane4) eq(#fourth.sent, 0, 'a pane that left its tab is no display') check(native.last 'notice'[3]:find('not running in this tab', 1, true), 'said') end) test('trust: a second window has its own display, and a claim there counts for nothing', function() local dir = fresh_dir 'forge4' with_cli(dir) local native = fake.native() local _, w = new_layer { native = native } local win1, win2 = fake.window(1), fake.window(2) local pane1 = fake.pane { id = 1, zones = ZONES } fake.tab({ pane1 }, 11) local display1 = started(w, pane1, DISPLAY_IDLE) -- another window: a pane of its own and one that claims to be a display local pane2 = fake.pane { id = 2, zones = ZONES, next_id = 200 } local claims = fake.pane { id = 3, vars = EVERY_VAR } fake.tab({ pane2, claims }, 12) w.emit('llembas-ask-line', win2, pane2, 'in the second window') eq(#claims.sent, 0, 'the claim in the second window gets nothing') eq(#display1.sent, 0, 'the first window\'s display is not this tab\'s') eq(#pane2.splits, 1, 'the second window gets a display of its own') w.emit('llembas-approve', win2, pane2) eq(#display1.sent, 0, 'a key in the second window does not answer the first one\'s') -- and the first window's display is still the first window's w.emit('llembas-ask-line', win1, pane1, 'in the first window') eq(#display1.sent, 1, 'its own question') check(dargs(pane1.splits[1])[4] ~= dargs(pane2.splits[1])[4], 'two conversations') end) test('the shell that holds a display reads nothing from the pane', function() check(not core.DISPLAY_HOLD:find('read', 1, true), 'no `read`: a shell that waited for Enter would take the next question') check(core.DISPLAY_HOLD:find('SetUserVar=%s=', 1, true), 'the variables a killed engine left are emptied') local dir = fresh_dir 'hold' write_file(dir .. '/fail', '#!/bin/sh\necho "not logged in: lembas login"\nexit 2\n') write_file(dir .. '/fine', '#!/bin/sh\necho "it ran: $1"\nexit 0\n') sh(string.format("chmod +x '%s/fail' '%s/fine'", dir, dir)) -- as the terminal runs it, with nothing to read from: a clean end ends it local held = core.held { dir .. '/fine', 'a b' } local out = sh(string.format("/bin/sh -c '%s' %s '%s' '%s' = 1, 'a value set in the configuration is said to be replaced') local native = fake.native() local _, w2 = new_layer { native = native } sh(string.format("mkdir -p '%s/llembas-term'", dir)) w2.emit('user-var-changed', fake.window(), fake.pane { id = 3 }, 'llembas_term_status', RECORD) check(read_file(dir .. '/llembas-term/pane-3.status') ~= nil, 'and the records are kept there') env.LLEMBAS_TERM_STATUS_DIR = false env.XDG_RUNTIME_DIR = false end) test('status records of terminals that are gone are removed when a display starts', function() local dir = fresh_dir 'statussweep' with_cli(dir) env.XDG_RUNTIME_DIR = dir .. '/rt' local base = dir .. '/rt/llembas-term' sh(string.format("mkdir -p '%s/status-999999999' '%s/status-1' '%s/other'", base, base, base)) write_file(base .. '/status-999999999/pane-3.status', 'x') write_file(base .. '/status-1/pane-0.status', 'x') write_file(base .. '/other/pane-0.status', 'x') local _, w = new_layer {} local pane = fake.pane { id = 1, zones = ZONES } fake.tab { pane } w.emit('llembas-ask-line', fake.window(), pane, 'q') eq(read_file(base .. '/status-999999999/pane-3.status'), nil, 'a terminal that is gone: its records go') eq(sh(string.format("ls -1 '%s' | tr '\\n' ' '", base)), 'other status-1', 'and its directory; a living process and other names stay') check(read_file(base .. '/status-1/pane-0.status') ~= nil, 'the records of a process that lives stay') env.XDG_RUNTIME_DIR = false end) test('status record: the terminal is told where a shell stands, for the input mark', function() local dir = fresh_dir 'mark' with_cli(dir) env.XDG_RUNTIME_DIR = dir local native = fake.native() local _, w = new_layer { native = native } local win, pane = fake.window(), fake.pane { id = 4 } fake.tab { pane } win.active = pane eq(native.last 'configure'[2].input_mark, true, 'with AI on the mark is written') eq(native.last 'configure'[2].ac_debug, false, 'the engine was not asked what it can do: nothing assumed') local rec = string.format('llembas-term-status=1\nid=9.1\nseq=6\nstate=prompt\npid=%d\n', LIVE_PID) sh(string.format("mkdir -p '%s/llembas-term'", dir)) write_file(dir .. '/llembas-term/pane-4.status', rec) w.emit('user-var-changed', win, pane, 'llembas_term_status', rec) local call = native.last 'status' eq(call[2], 4, 'the pane') eq(call[3] .. ' ' .. call[4] .. ' ' .. call[5], '9.1 6 prompt', "the record's id, seq and state") check(call[6]:sub(1, 1) == '/', 'and where the records are kept') -- a pane of another machine's domain, or AI off: no mark local far = fake.pane { id = 5, domain = 'SSH:far' } fake.tab { far } w.emit('user-var-changed', win, far, 'llembas_term_status', 'llembas-term-status=1\nid=9.2\nseq=1\nstate=prompt\n') eq(native.last 'status'[2], 5, 'said of that pane') eq(native.last 'status'[6], nil, 'no input mark there') w.emit('llembas-ai', win, pane) eq(native.last 'configure_pane'[3].input_mark, false, 'AI off: no mark') local rec7 = string.format('llembas-term-status=1\nid=9.1\nseq=7\nstate=prompt\npid=%d\n', LIVE_PID) write_file(dir .. '/llembas-term/pane-4.status', rec7) w.emit('user-var-changed', win, pane, 'llembas_term_status', rec7) eq(native.last 'status'[6], nil, 'and none is written') eq(native.last 'status'[5], 'prompt', 'where the shell stands is still known') env.XDG_RUNTIME_DIR = false end) test('status record: private panes, AI off, history off', function() local dir = fresh_dir 'status2' env.XDG_RUNTIME_DIR = dir local path = dir .. '/llembas-term/pane-12.status' sh(string.format("mkdir -p '%s/llembas-term'", dir)) local native = fake.native() local _, w = new_layer({ native = native }, { ai = false }) local win, pane = fake.window(), fake.pane { id = 12 } w.emit('user-var-changed', win, pane, 'llembas_term_status', RECORD) eq(read_file(path), nil, 'AI off: no file, its one reader is LLeMbas') eq(#w.spawned, 0, 'and nothing started') local ended = own_record(5, 'prompt', 'exit=2\nran=1\n') write_file(path, ended) w.emit('user-var-changed', win, pane, 'llembas_term_status', ended) eq(native.count 'command_ended', 1, 'the history of commands is the terminal\'s own, not AI') w.emit('llembas-private', win, pane) w.emit('user-var-changed', win, pane, 'llembas_term_status', ended) eq(native.count 'command_ended', 1, 'a private pane is not remembered') local native2 = fake.native() local _, w2 = new_layer({ native = native2 }, { history = { enabled = false } }) local p14 = fake.pane { id = 14 } write_file(dir .. '/llembas-term/pane-14.status', ended) w2.emit('user-var-changed', win, p14, 'llembas_term_status', ended) eq(native2.count 'command_ended', 0, 'history off: nothing remembered') env.XDG_RUNTIME_DIR = false end) test('session bar: the conversation of the tab, and nothing of ours otherwise', function() local dir = fresh_dir 'bar' with_cli(dir) local _, w = new_layer {} local win = fake.window() local shell = fake.pane { id = 1, zones = ZONES, screen = 'x' } fake.tab({ shell }, 1) w.emit('update-status', win, shell) eq(win.status, nil, 'no LLeMbas in the tab: the status is not touched') local mine = fake.pane { id = 3 } fake.tab({ mine }, 2) started( w, mine, { lembas_surface = 'display', lembas_model = 'm\27[5m\226\128\174', lembas_mode = 'auto', lembas_state = 'tools', lembas_context = '9' } ) w.emit('update-status', win, mine) eq(win.status, ' m[5m · auto · tools · ctx 9% ', "from the person's own pane: the display's conversation") w.emit('update-status', win, shell) eq(win.status, '', 'cleared in a tab without one') 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') local cli_pane = fake.pane { id = 4, vars = { lembas_session = 's', lembas_model = 'big', lembas_state = 'idle' }, program = dir .. '/bin/lembas', } fake.tab({ cli_pane }, 3) w.emit('update-status', win, cli_pane) eq(win.status, ' big · idle ', 'inside the CLI opened whole: its own') -- the same variables in a pane whose program is not LLeMbas say nothing: -- anything can print them, and they stay when the CLI was killed cli_pane.program = '/bin/zsh' w.emit('update-status', win, cli_pane) eq(win.status, '', 'a shell that only says it is LLeMbas: no bar') end) test('tab mark', function() local dir = fresh_dir 'mark' with_cli(dir) local _, w = new_layer {} local tab = { tab_index = 0, tab_title = '', active_pane = { title = 'lembas' }, panes = { { pane_id = 1, user_vars = { lembas_session = 's', lembas_state = 'asking' }, foreground_process_name = dir .. '/bin/lembas' }, }, } 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') -- a pane that only says it asks (any program can) marks nothing tab.panes[1].user_vars.lembas_state = 'asking' tab.panes[1].foreground_process_name = '/bin/bash' eq(w.emit('format-tab-title', tab, {}, {}, {}, false, 20), nil, 'a forged "asking" marks no tab') -- the display the layer started does local pane = fake.pane { id = 5 } fake.tab({ pane }, 9) local display = started(w, pane, { lembas_state = 'asking', lembas_surface = 'display' }) tab.panes[1] = { pane_id = display:pane_id(), user_vars = display.vars, foreground_process_name = '/usr/bin/dash' } check(type(w.emit('format-tab-title', tab, {}, {}, {}, false, 20)) == 'table', 'the display asks: marked') end) test('keys of the family: chords and the bytes a program knows them by', function() eq(core.mod_bits 'CTRL|SHIFT', 5, 'ctrl 4, shift 1') eq(core.mod_bits 'SHIFT | CTRL', 5, 'in any order, with spaces') eq(core.mod_bits 'ALT', 2, 'alt') eq(core.mod_bits 'CTRL|ALT|SHIFT', 7, 'three') eq(core.mod_bits 'NONE', 0, 'none') eq(core.mod_bits 'HYPER', nil, 'not a modifier') eq(core.mod_bits(nil), nil, 'nil') eq(core.chord('a', 'SHIFT|CTRL'), 'CTRL|SHIFT+A', 'one spelling') eq(core.chord('Tab', 'CTRL'), 'CTRL+TAB', 'a named key') eq(core.chord('', 'CTRL'), nil, 'no key') eq(core.pass_event 'CTRL|SHIFT+P', 'llembas-pass-ctrl-shift-p', 'the event behind a chord') eq(#core.IDE_KEYS, 15, 'fifteen chords') check(core.is_ide_chord('A', 'CTRL|SHIFT'), 'the ask key') check(core.is_ide_chord('a', 'SHIFT|CTRL'), 'however it is spelled') check(core.is_ide_chord('Tab', 'CTRL'), 'the mode key') check(core.is_ide_chord('RightArrow', 'ALT'), 'a word to the right') check(not core.is_ide_chord('S', 'CTRL|SHIFT'), "the CLI key is the terminal's alone") check(not core.is_ide_chord('T', 'CTRL|SHIFT'), 'and so is a new tab') check(not core.is_ide_chord('A', 'CTRL'), 'Ctrl+A is not Ctrl+Shift+A') -- the kitty keyboard protocol: CSI ; <1 + modifiers> u local want = { { 'A', 'CTRL|SHIFT', '\27[97;6u' }, { 'Tab', 'CTRL', '\27[9;5u' }, { 'M', 'CTRL|SHIFT', '\27[109;6u' }, { 'M', 'CTRL|ALT', '\27[109;7u' }, { 'A', 'CTRL|ALT', '\27[97;7u' }, { 'I', 'CTRL|SHIFT', '\27[105;6u' }, { 'G', 'CTRL|SHIFT', '\27[103;6u' }, { 'Y', 'CTRL|SHIFT', '\27[121;6u' }, { 'J', 'CTRL|SHIFT', '\27[106;6u' }, { 'Y', 'CTRL|ALT|SHIFT', '\27[121;8u' }, { 'E', 'CTRL|SHIFT', '\27[101;6u' }, { 'D', 'CTRL|SHIFT', '\27[100;6u' }, { 'P', 'CTRL|SHIFT', '\27[112;6u' }, { 'LeftArrow', 'ALT', '\27[1;3D' }, { 'RightArrow', 'ALT', '\27[1;3C' }, } for i, k in ipairs(want) do eq(core.IDE_KEYS[i][1] .. ' ' .. core.IDE_KEYS[i][2], k[1] .. ' ' .. k[2], 'the chord, in the order of the list') eq(core.ide_key_bytes(k[1], k[2]), k[3], k[2] .. '+' .. k[1]) end eq(core.ide_key_bytes('a', 'CTRL|SHIFT'), '\27[97;6u', 'the key without Shift, however it is written') eq(core.ide_key_bytes('A', 'CTRL'), '\27[97;5u', 'Ctrl+A is another sequence than Ctrl+Shift+A') eq(core.ide_key_bytes('A', 'NONE'), nil, 'no modifier: not a chord') eq(core.ide_key_bytes('F9', 'CTRL'), nil, 'a key this does not know is not sent') eq(core.ide_key_bytes('\27', 'CTRL'), nil, 'nor anything that is not a letter or a digit') check(core.ide_in_front { llembas_ide = '1' }, 'the editor says it is in front') check(not core.ide_in_front { llembas_ide = '' }, 'emptied when it leaves') check(not core.ide_in_front { llembas_ide = 'yes' }, 'only that word') check(not core.ide_in_front {}, 'not said') check(not core.ide_in_front(nil), 'nil') end) test('LLeMbas IDE in front: the keys both have go to it', function() local dir = fresh_dir 'idekeys' with_cli(dir) local native = fake.native() local l, w = new_layer { native = native } local win = fake.window() local shell = fake.pane { id = 1, zones = ZONES } local ide = fake.pane { id = 2, vars = { llembas_ide = '1' } } fake.tab { shell, ide } check(l.ide_pane(ide), 'the pane that says so') check(not l.ide_pane(shell), 'no other') -- the ask key w.emit('llembas-ask-key', win, ide) eq(#ide.sent, 1, 'sent on to the pane') eq(ide.sent[1][1], 'text', 'as it is, not as a paste') eq(ide.sent[1][2], '\27[97;6u', 'Ctrl+Shift+A, told apart from Ctrl+A') eq(native.count 'ask_line', 0, 'and the terminal does nothing of its own') eq(#win.actions, 0, 'nothing at all') eq(#w.spawned, 0, 'nothing started') w.emit('llembas-ask-key', win, shell) eq(#shell.sent, 0, 'any other pane: nothing is sent to it') eq(native.count 'ask_line', 1, "and the layer's own action: the ask line") -- every key of the layer that the IDE has too, with the right key and modifiers local keys = { { 'llembas-ai-key', '\27[105;6u' }, { 'llembas-approve-key', '\27[121;6u' }, { 'llembas-deny-key', '\27[106;6u' }, { 'llembas-stop-key', '\27[103;6u' }, { 'llembas-display-key', '\27[100;6u' }, { 'llembas-ide-key', '\27[101;6u' }, { 'llembas-model-key', '\27[109;6u' }, { 'llembas-autocomplete-model-key', '\27[109;7u' }, { 'llembas-mode-next-key', '\27[9;5u' }, { 'llembas-pass-ctrl-shift-p', '\27[112;6u' }, } for _, k in ipairs(keys) do local before = #ide.sent w.emit(k[1], win, ide) eq(#ide.sent, before + 1, k[1] .. ': sent on') eq(ide.sent[#ide.sent][2], k[2], k[1] .. ': the bytes') end eq(#win.actions, 0, 'the terminal acted on none of them') check(l.is_on 'ai', 'the AI switch did not move') -- the terminal's own keys that are not LLeMbas's are not touched: they have -- no event of ours at all; and the keys of the layer the IDE does not have -- do what they do w.emit('llembas-cli', win, ide) eq(win.actions[#win.actions].action.action, 'SpawnCommandInNewTab', 'the CLI key works in an IDE pane') -- the palette: the chord is the IDE's, the second chord and any other pane are the terminal's w.emit('llembas-palette', win, ide) eq(win.actions[#win.actions].action.action, 'ActivateCommandPalette', "the terminal's palette stays reachable") w.emit('llembas-pass-ctrl-shift-p', win, shell) eq(win.actions[#win.actions].action.action, 'ActivateCommandPalette', 'Ctrl+Shift+P in any other pane') eq(#shell.sent, 0, 'and nothing is sent there') -- an entry picked from the terminal's palette is the terminal's, in an IDE pane too local sent = #ide.sent w.emit('llembas-ai', win, ide) eq(#ide.sent, sent, 'not sent on') check(not l.is_on 'ai', 'the switch moved') w.emit('llembas-ai', win, ide) -- the editor went without saying so: the shell's prompt takes the keys back local record = 'llembas-term-status=1\nid=77.5\nseq=3\nstate=prompt\n' ide.vars.llembas_term_status = record w.emit('user-var-changed', win, ide, 'llembas_term_status', record) check(not l.ide_pane(ide), 'a shell at its prompt is in front') w.emit('llembas-pass-ctrl-shift-p', win, ide) eq(win.actions[#win.actions].action.action, 'ActivateCommandPalette', 'the keys are the terminal\'s again') -- and it says so again when it starts again w.emit('user-var-changed', win, ide, 'llembas_ide', '1') check(l.ide_pane(ide), 'by its own word, again') -- it leaves: the variable is emptied ide.vars.llembas_ide = '' w.emit('user-var-changed', win, ide, 'llembas_ide', '') check(not l.ide_pane(ide), 'gone') local n = native.count 'ask_line' w.emit('llembas-ask-key', win, ide) eq(native.count 'ask_line', n + 1, 'the ask key is the terminal\'s there again') end) test('LLeMbas IDE in front: AI off, a key of one\'s own, and the switch for all of it', function() local dir = fresh_dir 'idekeys2' with_cli(dir) -- AI off in the terminal: any other pane is as it was (said once, then quiet); -- the IDE has its own switch, and its keys still reach it local native = fake.native() local _, w = new_layer({ native = native }, { ai = false }) local win = fake.window() local shell = fake.pane { id = 1 } local ide = fake.pane { id = 2, vars = { llembas_ide = '1' } } fake.tab { shell, ide } w.emit('llembas-ask-key', win, shell) eq(#win.actions, 1, 'AI off: said') check(described(win):find('AI is off', 1, true), 'in these words') w.emit('llembas-ask-key', win, shell) eq(#win.actions, 1, 'once') eq(native.count 'ask_line', 0, 'and no ask line') w.emit('llembas-ask-key', win, ide) eq(ide.sent[1][2], '\27[97;6u', 'the IDE gets its key') eq(#win.actions, 1, 'and nothing is said') -- a key moved to a chord the IDE does not have is the terminal's everywhere local _, w2, config2 = new_layer({ native = fake.native() }, { keys = { ask = { key = 'F9', mods = 'NONE' } } }) eq(config2.keys[1].action.args, 'llembas-ask', 'bound to the plain event') check(w2.handlers['llembas-ask'] ~= nil, 'which is there') -- ide_keys = false: the terminal keeps its keys local _, w3, config3 = new_layer({ native = fake.native() }, { ide_keys = false }) for _, k in ipairs(config3.keys) do check(not k.action.args:find('-key$') or k.action.args == 'llembas-mode-next-key', 'plain: ' .. k.action.args) check(not k.action.args:find('llembas-pass-', 1, true), 'no chord bound only to hand it on') end local ide3 = fake.pane { id = 2, vars = { llembas_ide = '1' } } fake.tab { ide3 } local win3 = fake.window() w3.emit('llembas-mode-next-key', win3, ide3) eq(#ide3.sent, 0, 'nothing is handed on') eq(win3.actions[1].action.action, 'ActivateTabRelative', 'Ctrl+Tab is the next tab') end) test('the mode key: the other mode on the ask line, the next tab anywhere else', function() local dir = fresh_dir 'modenext' with_cli(dir) local native = fake.native() local _, w = new_layer { native = native } local win = fake.window() local pane = fake.pane { id = 1, zones = ZONES } fake.tab { pane } local display = started(w, pane, { lembas_session = 's1', lembas_state = 'idle', lembas_surface = 'display', lembas_model = 'open/flash', lembas_mode = 'manual' }) local spawned0 = #w.spawned w.emit('llembas-mode-next-key', win, pane) eq(#win.actions, 1, 'not on the ask line: one action') eq(win.actions[1].action.action, 'ActivateTabRelative', 'the next tab, as before') eq(win.actions[1].action.args, 1, 'forward') eq(#display.sent, 0, 'the mode is not touched') eq(#w.spawned, spawned0, 'and nothing of LLeMbas wakes') w.emit('llembas-ask', win, pane) eq(native.last 'ask_line'[4].prefix, 'llembas open/flash · manual', 'the ask line, in manual') native.asking = true w.emit('llembas-mode-next-key', win, pane) eq(#win.actions, 1, 'on the ask line: no tab is changed') eq(display.sent[1][2], '/mode auto\r', 'the display is told') local call = native.last 'ask_prefix' eq(call[2], win, 'the window') eq(call[3], pane, 'the pane') eq(call[4], 'llembas open/flash · auto', 'the prefix changes at once, before the display has said so') eq(native.count 'notice', 0, 'the line itself says it: no other word') -- again: back, though the display still says manual of itself w.emit('llembas-mode-next-key', win, pane) eq(display.sent[2][2], '/mode manual\r', 'and back') eq(native.last 'ask_prefix'[4], 'llembas open/flash · manual', 'said') w.emit('llembas-mode-next-key', win, pane) eq(display.sent[3][2], '/mode auto\r', 'and again') -- it is remembered like a mode picked from the list: the next display starts in it native.asking = false local alone = fake.pane { id = 5, zones = ZONES } fake.tab({ alone }, 7) w.emit('llembas-ask-line', win, alone, 'q') local args = table.concat(alone.splits[1].args, ' ') check(args:find('--mode auto', 1, true), 'the mode chosen: ' .. args) -- asking about the screen keeps saying so native.asking = false w.emit('llembas-ask-screen', win, pane) native.asking = true w.emit('llembas-mode-next-key', win, pane) check(native.last 'ask_prefix'[4]:find('· this screen', 1, true), 'this screen: ' .. native.last 'ask_prefix'[4]) -- AI off: the key is the terminal's local native2 = fake.native() native2.asking = true local _, w2 = new_layer({ native = native2 }, { ai = false }) local win2 = fake.window() w2.emit('llembas-mode-next-key', win2, pane) eq(win2.actions[1].action.action, 'ActivateTabRelative', 'AI off: the next tab') -- moved to another chord, it has nothing to fall back to local native3 = fake.native() local _, w3 = new_layer({ native = native3 }, { keys = { mode_next = { key = 'F8', mods = 'NONE' } } }) local win3 = fake.window() w3.emit('llembas-mode-next-key', win3, pane) eq(#win3.actions, 0, 'a chord the terminal had nothing on: nothing') end) test('the model keys from the ask line: the list opens, and the line says the choice', function() local dir = fresh_dir 'modelask' with_cli(dir) local native = fake.native() local _, w = new_layer { native = native } local win = fake.window() local pane = fake.pane { id = 1 } fake.tab { pane } local display = started(w, pane, { lembas_session = 's1', lembas_state = 'idle', lembas_surface = 'display', lembas_model = 'open/big', lembas_mode = 'manual' }) w.emit('llembas-ask', win, pane) native.asking = true w.emit('llembas-model-key', win, pane) eq(win.actions[1].action.action, 'InputSelector', 'the list of chat models, by its key') eq(win.actions[1].action.args.action.args, 'llembas-model-pick', 'the pick comes back') w.emit('llembas-model-pick', win, pane, 'open/flash', 'Flash') eq(display.sent[1][2], '/model open/flash\r', 'the running conversation is switched') eq(native.last 'ask_prefix'[4], 'llembas open/flash · manual', 'and the ask line says the model at once') eq(native.count 'notice', 0, 'no other word') w.emit('llembas-model-pick', win, pane, nil, nil) eq(native.count 'ask_prefix', 1, 'cancelled: the line is as it was') w.emit('llembas-autocomplete-model-key', win, pane) eq(win.actions[2].action.args.action.args, 'llembas-autocomplete-model-pick', 'the list of autocomplete models, by its key') w.emit('llembas-autocomplete-model-pick', win, pane, 'open/flash') eq(native.count 'ask_prefix', 1, 'the autocomplete model is not on the ask line') eq(native.last 'notice'[3], 'model: open/flash', 'so it is said') end) test('a CLI from before the rename: said in words, with how to update', function() local dir = fresh_dir 'contract3' with_cli(dir) JSON['INFO_B20'] = { name = 'lembas', version = '2.0.0-beta.20', terminal_contract = 3, capabilities = { 'terminal_surface', 'terminal_models', 'terminal_settings', 'terminal_placed' }, } local native = fake.native() local _, w = layer { child_results = { ['--version --json'] = { true, 'INFO_B20' } }, json_parse = json_parse, native = native } local win, pane = fake.window(), fake.pane { id = 1, zones = ZONES } fake.tab { pane } w.emit('llembas-ask-line', win, pane, 'q') eq(#pane.splits, 0, 'no display: it would not know this terminal') eq(pane.read_zones, nil, 'nothing read') local said = described(win) check(said:find('2.0.0-beta.20', 1, true), 'names the version: ' .. said) check(said:find('renamed LLeMbas TERM', 1, true), 'says why') check(said:find('terminal contract 3', 1, true) and said:find('needs 4', 1, true), 'the contract it speaks and the one needed') check(said:find('lembas update', 1, true), 'and how to update') w.emit('llembas-model', win, pane) check(described(win):find('lembas update', 1, true), 'the model list says to update too') w.emit('llembas-autocomplete-status', win, pane) check(described(win):find('from before the terminal was renamed', 1, true), 'and so does the autocomplete in words') end) test('a configuration file still under the old name: said once', function() eq(core.is_old_config '/home/ana/.config/llembas-tty/llembas-tty.lua', true, 'in the old directory') eq(core.is_old_config '/home/ana/.llembas-tty.lua', true, 'in the home directory') eq(core.is_old_config '/home/ana/.config/llembas-term/llembas-term.lua', false, 'the new name') eq(core.is_old_config(nil), false, 'no file') eq(core.new_config_path '/home/ana/.config/llembas-tty/llembas-tty.lua', '/home/ana/.config/llembas-term/llembas-term.lua', 'where it belongs') eq(core.new_config_path '/home/ana/.llembas-tty.lua', '/home/ana/.llembas-term.lua', 'in the home directory') local native = fake.native() local _, w = layer { native = native, config_file = '/home/ana/.config/llembas-tty/llembas-tty.lua' } check(w.logs[1] and w.logs[1]:find('/home/ana/.config/llembas-term/llembas-term.lua', 1, true), 'logged, with where to move it') local win, pane = fake.window(), fake.pane { id = 1 } fake.tab { pane } w.emit('update-status', win, pane) eq(native.count 'notice', 1, 'said where the person is') check(native.last 'notice'[3]:find('llembas-tty.lua', 1, true), 'naming the file') w.emit('update-status', win, pane) eq(native.count 'notice', 1, 'once') local native2 = fake.native() local _, w2 = layer { native = native2, config_file = '/home/ana/.config/llembas-term/llembas-term.lua' } w2.emit('update-status', win, pane) eq(native2.count 'notice', 0, 'a file of the new name: nothing is said') eq(#w2.logs, 0, 'nor logged') end) test('the keyboard protocol: on for a program that asks, unless the person said', function() local _, _, c1 = layer({}, nil, { enable_kitty_keyboard = false }) eq(c1.enable_kitty_keyboard, false, "the person's own word stands") local _, _, c2 = layer({}, { kitty_keyboard = false }) eq(c2.enable_kitty_keyboard, nil, 'the option turns it off') local _, _, c3 = layer {} eq(c3.enable_kitty_keyboard, true, 'on by default') eq(c3.enable_csi_u_key_encoding, nil, 'the encoding every program would get is not touched') end) test('M3: a display that ends before it took the question gives it back', function() local dir = fresh_dir 'giveback' with_cli(dir) local native = fake.native() local _, w = new_layer { native = native } local win = fake.window() local pane = fake.pane { id = 1, zones = ZONES } fake.tab { pane } win.active = pane w.emit('llembas-ask-line', win, pane, 'how do I quit this?') local display = pane.last_split check(display ~= nil, 'a display starts') local files = context_files(dir) eq(#files, 1, 'with its context') -- it says why and ends: the shell that held it is in front, the engine is gone w.GLOBAL.llembas_display_started['d' .. tostring(display:pane_id())] = 0 display.process = { pid = 4000, executable = '/usr/bin/dash', argv = display.spec.process.argv, children = {} } display.spec.screen = 'You are not logged in: lembas login \n\n[LLeMbas ended (status 1). Ctrl+C closes this pane; so does your next question.]\n' w.emit('update-status', win, pane) local call = native.last 'ask_line' check(call ~= nil, 'the ask line opens again') eq(call[4].text, 'how do I quit this?', 'with the question on it') check(call[4].prefix:find('You are not logged in', 1, true) ~= nil, 'and what the display said last') check(call[4].prefix:find('LLeMbas ended', 1, true) == nil, "not the holding shell's own line") eq(#context_files(dir), 0, 'the context file it never read is gone') local n = native.count 'ask_line' w.emit('update-status', win, pane) eq(native.count 'ask_line', n, 'given back once') -- the next question starts a new display, and the old pane is closed w.emit('llembas-ask-line', win, pane, 'how do I quit this?') eq(#pane.splits, 2, 'a new display') -- a display that cannot be split at all: given back at once local pane2 = fake.pane { id = 7, zones = ZONES } fake.tab({ pane2 }, 4) function pane2:split() return nil end w.emit('llembas-ask-line', win, pane2, 'still here?') eq(native.last 'ask_line'[4].text, 'still here?', 'given back') check(native.last 'ask_line'[4].prefix:find(core.messages.given_back, 1, true) ~= nil, 'saying why') -- the person is on the ask line already: it waits, and comes back after local pane3 = fake.pane { id = 8, zones = ZONES } fake.tab({ pane3 }, 5) w.emit('llembas-ask-line', win, pane3, 'third') local d3 = pane3.last_split w.GLOBAL.llembas_display_started['d' .. tostring(d3:pane_id())] = 0 d3.process = false d3.process = { pid = 1, executable = '/usr/bin/dash', argv = {}, children = {} } native.asking = true n = native.count 'ask_line' w.emit('update-status', win, pane3) eq(native.count 'ask_line', n, 'not over the ask line that is open') native.asking = false w.emit('update-status', win, pane3) eq(native.last 'ask_line'[4].text, 'third', 'then given back') end) test('S7: a question card is answered on the ask line', function() with_cli(fresh_dir 'question') local native = fake.native() local _, w = new_layer { native = native } local win, pane = fake.window(), fake.pane { id = 1, zones = ZONES } fake.tab { pane } win.active = pane local display = started(w, pane, { lembas_state = 'asking', lembas_surface = 'display', lembas_session = 's' }) display.vars.lembas_ask = 'question 1-3n Which branch? 1 main, 2 dev, 3 other' w.emit('user-var-changed', win, display, 'lembas_ask', display.vars.lembas_ask) check(native.last 'hint'[3]:find('answer on the ask line', 1, true) ~= nil, 'the notice says where it is answered') check(native.last 'hint'[3]:find('allow', 1, true) == nil, 'not with the approval keys') w.emit('llembas-ask-line', win, pane, '2') eq(#display.sent, 0, 'what changed this moment is not answered yet') settle(w, display) w.emit('llembas-ask-line', win, pane, '2') eq(display.sent[1][2], '/answer 2\r', 'a number answers it') w.emit('llembas-ask-line', win, pane, 'n') eq(display.sent[2][2], '/answer n\r', 'n skips it') w.emit('llembas-ask-line', win, pane, 'the one I made yesterday') eq(display.sent[3][2], '\27[200~the one I made yesterday\27[201~\r', 'words are pasted: the display takes them as the answer') eq(#context_files(fresh_dir 'question'), 0, 'and no context goes with an answer') w.emit('llembas-approve', win, pane) eq(#display.sent, 3, 'the approve key does not answer a question') check(native.last 'notice'[3]:find('ask line', 1, true) ~= nil, 'and says where to answer') end) test('S15: an answer goes with the number of what was asked', function() with_cli(fresh_dir 'askid') local native = fake.native() local _, w = new_layer { native = native } local win, pane = fake.window(), fake.pane { id = 1 } fake.tab { pane } win.active = pane local display = started(w, pane, { lembas_state = 'asking', lembas_surface = 'display', lembas_session = 's' }) display.vars.lembas_ask = 'approval#7 yn terminal_run: make test' w.emit('user-var-changed', win, display, 'lembas_ask', display.vars.lembas_ask) check(native.last 'hint'[3]:find('make test', 1, true) ~= nil, 'shown without its number') w.emit('llembas-approve', win, pane) eq(display.sent[1][2], '/approve 7\r', 'numbered: sent at once, with the number (the engine refuses it for anything else)') w.emit('llembas-deny', win, pane) eq(display.sent[2][2], '/deny 7\r', 'deny too') eq(core.parse_ask('approval#12345678901 yn x').id, nil, 'a number that long is none') eq(core.answer_line('stop', '7'), nil, 'stop answers nothing') -- a value the engine cut says so display.vars.lembas_ask = 'approval yn terminal_run: ' .. string.rep('x', 20) .. '\226\128\166' w.emit('user-var-changed', win, display, 'lembas_ask', display.vars.lembas_ask) check(native.last 'hint'[3]:find('more in the display', 1, true) ~= nil, 'a cut value: the rest is in the display') end) test('S5: with AI off nothing of the engine is started, but the switch', function() local dir = fresh_dir 'aioffstore' with_cli(dir) local native = fake.native() native.dir = dir .. '/run' write_file(dir .. '/run/settings-chosen', '') local l, w = store_layer({ ['terminal settings --cwd /home/u/proj --json'] = { true, 'S_PROJ' }, ['terminal settings set --global --json ai=true'] = { true, '{}' }, }, native, { ai = false }) local win, pane = fake.window(), fake.pane { id = 1, cwd = 'file://workstation/home/u/proj' } fake.tab { pane } win.active = pane w.emit('update-status', win, pane) w.emit('user-var-changed', win, pane, 'llembas_term_status', 'llembas-term-status=1\nid=1.1\nseq=1\nstate=prompt\n') w.emit('llembas-suggestions', win, pane) eq(l.is_on('suggestions', pane), false, 'a switch still works') eq(select(2, l.value('suggestions', pane)), 'session', 'held until the terminal closes') check(native.last 'notice'[3]:find('until the terminal closes', 1, true) ~= nil, 'and that is said') for _, ev in ipairs { 'llembas-highlight', 'llembas-width', 'llembas-settings', 'llembas-private', 'llembas-autocomplete-status', 'llembas-mode' } do w.emit(ev, win, pane) end eq(#w.spawned, 0, 'no process: not for a prompt, the bar, a switch of the drawing, the settings list, the status') -- the AI switch itself is kept by the engine, and then the settings are read w.emit('llembas-ai', win, pane) eq(spawned(w, 'terminal settings set --global --json ai=true'), 1, 'the switch: the engine keeps it') eq(spawned(w, 'terminal settings --cwd /home/u/proj --json'), 1, 'and the settings of the directory are read') end) test('S6: what the CLI can do is asked again after an update', function() local dir = fresh_dir 'recheck' with_cli(dir) local results = { ['--version --json'] = { true, 'not json' }, ['--version'] = { true, '2.0.0-beta.18\n' } } local _, w = layer { child_results = results, json_parse = json_parse } local win, pane = fake.window(), fake.pane { id = 1, zones = ZONES } fake.tab { pane } w.emit('llembas-ask-line', win, pane, 'q') eq(#pane.splits, 0, 'too old: nothing') -- `lembas update`; a few seconds later the key again results['--version --json'] = { true, 'INFO2' } for k, v in pairs(w.GLOBAL.llembas_cli_info) do v.at = v.at - 10 w.GLOBAL.llembas_cli_info[k] = v end w.emit('llembas-ask-line', win, pane, 'q') eq(#pane.splits, 1, 'the new one is seen without restarting the terminal') end) test('S4: the settings that apply are the pane\'s own directory\'s', function() local dir = fresh_dir 'perpane' with_cli(dir) local native = fake.native() native.dir = dir .. '/run' write_file(dir .. '/run/settings-chosen', '') local l, w = store_layer({ ['terminal settings --cwd /home/u/proj --json'] = { true, 'S_PROJ' }, ['terminal settings --cwd /home/u/other --json'] = { true, 'S_OTHER' }, }, native) local w1, w2 = fake.window(1), fake.window(2) local a = fake.pane { id = 1, cwd = 'file://workstation/home/u/proj' } local b = fake.pane { id = 2, cwd = 'file://workstation/home/u/other' } fake.tab({ a }, 1) fake.tab({ b }, 2) w1.active, w2.active = a, b w.emit('update-status', w1, a) w.emit('update-status', w2, b) eq(l.is_on('suggestions', a), false, "one window: its directory's own") eq(l.is_on('suggestions', b), true, 'the other: the other directory') local seen = {} for _, c in ipairs(native.calls) do if c[1] == 'configure_pane' then seen[c[2]] = c[3].suggestions end end eq(seen[1], false, 'each pane is told its own') eq(seen[2], true, 'and the other its own') local configured = native.count 'configure' w.emit('update-status', w1, a) w.emit('update-status', w2, b) eq(native.count 'configure', configured, 'nothing flips back and forth for every pane') end) test('S17: a shell on another machine inside a local pane is named as such', function() local dir = fresh_dir 'remotectx' with_cli(dir) local _, w = new_layer {} w.hostname = function() return 'workstation' end local pane = fake.pane { id = 1, zones = ZONES, cwd = 'file://faraway/srv/app' } fake.tab { pane } w.emit('llembas-ask-line', fake.window(), pane, 'why?') local split = pane.splits[1] check(split.cwd ~= '/srv/app', 'the display starts in a directory of this machine') local text = read_file(context_files(dir)[1]) check(text:find('"version":1', 1, true) ~= nil, 'an engine that reads only version 1 gets version 1') 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)