'use strict'; // Against the LLeMbas CLI itself, when its source is at hand (LEMBAS_CLI_SOURCE, or a LLeMbas-CLI // checkout beside this repository) and bun is installed; skipped, saying why, when not. // The CLI runs with a home of its own (LEMBAS_HOME), so it is never signed in and never touches a // person's sessions: what is proved is the hello, the capabilities, what needs no sign-in, and // that "not signed in" ends in words that say what to do. const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const os = require('node:os'); const path = require('node:path'); const { Agent } = require('../src/agent'); const { Chat } = require('../src/chat'); const { Completer } = require('../src/completion'); const { MIN_CLI, compareVersions, say, notSignedIn } = require('../src/words'); const { tempDir, until, text } = require('./helpers'); function findCli() { const dirs = [process.env.LEMBAS_CLI_SOURCE]; let up = path.resolve(__dirname, '..', '..'); for (let i = 0; i < 4; i++) { up = path.dirname(up); dirs.push(path.join(up, 'LLeMbas-CLI')); } return dirs.find((d) => d && fs.existsSync(path.join(d, 'src', 'cli.ts')) && fs.existsSync(path.join(d, 'node_modules'))); } function findBun() { const names = String(process.env.PATH || '').split(path.delimiter).map((d) => path.join(d, 'bun')); names.push(path.join(os.homedir(), '.local', 'bin', 'bun'), path.join(os.homedir(), '.bun', 'bin', 'bun')); return names.find((f) => { try { fs.accessSync(f, fs.constants.X_OK); return true; } catch { return false; } }); } const cli = findCli(); const bun = findBun(); const skip = process.env.LEMBAS_SKIP_REAL_CLI === '1' ? 'LEMBAS_SKIP_REAL_CLI=1' : !cli ? "the CLI's source was not found (set LEMBAS_CLI_SOURCE to a LLeMbas-CLI checkout with its dependencies installed)" : !bun ? 'bun was not found on the PATH' : false; const SIGN_IN = 'The LLeMbas CLI is not signed in to a server. Sign in with the command "LLeMbas: Sign In" (or `lembas login ` in a terminal), then try again.'; test('the real LLeMbas CLI: the hello, what needs no sign-in, and "not signed in" in words', { skip, timeout: 120000 }, async (t) => { const home = tempDir(t); const project = tempDir(t); fs.mkdirSync(path.join(project, 'src')); fs.writeFileSync(path.join(project, 'src', 'oven.py'), "print('waybread')\n"); require('node:child_process').spawnSync('git', ['-C', project, 'init', '-q']); const agents = []; const chat = new Chat({ makeAgent: (m) => { const a = new Agent({ command: bun, args: ['run', path.join(cli, 'src', 'cli.ts'), 'serve', '--stdio'], cwd: m.cwd, env: { ...process.env, LEMBAS_HOME: home }, onRequest: m.onRequest, writeTextFile: true, clientInfo: { name: 'llembas-vscode', version: '0.0.0-test' }, startTimeoutMs: 60000, }); agents.push(a); return a; }, cwd: () => project, version: '0.0.0-test', }); t.after(() => chat.dispose()); // The hello. const agent = await chat.connect(false); assert.ok(agent, text(chat)); assert.equal(agent.info.protocolVersion, 1); assert.equal(agent.info.agentInfo.name, 'LLeMbas CLI'); assert.notEqual(compareVersions(agent.version, MIN_CLI), -1, `the CLI at hand (${agent.version}) is at least the oldest this works with (${MIN_CLI})`); assert.equal(agent.tooOld, ''); assert.equal(agent.info._meta.lembas.protocol, 3); assert.equal(agent.info._meta.lembas.limits, null, 'serve --stdio has no limits of a link'); for (const c of ['editor', 'media', 'show', 'files', 'commands', 'steer', 'sessions', 'compact', 'configure', 'ask', 'plan', 'model', 'turns']) { assert.ok(agent.has(c), 'the capability the extension uses: ' + c); } assert.ok(!agent.has('fs'), 'device files are the link\'s, never an editor\'s'); // What needs no sign-in, in the shapes the extension reads. assert.deepEqual(await chat.searchFiles('oven'), [{ path: 'src/oven.py', kind: 'file' }]); assert.deepEqual(await chat.searchFiles(''), [], 'nothing was used lately in a new project'); const commands = await agent.request('_lembas/commands', { cwd: project }); assert.deepEqual([commands.commands[0].name, commands.commands[0]._meta.lembas.kind], ['compact', 'builtin']); assert.deepEqual(await chat.listSessions(10), []); await assert.rejects(agent.request('_lembas/sessions/resume', { sessionId: 'ses_nope' }), (e) => e.code === -32602 && /no such session/.test(e.message)); await assert.rejects(agent.request('session/set_mode', { sessionId: 'ses_nope', modeId: 'plan' }), (e) => e.code === -32602); await assert.rejects(agent.request('no/such-method', {}), (e) => e.code === -32601); assert.deepEqual(await agent.request('session/cancel', { sessionId: 'ses_nope' }), { cancelled: 'none' }); // Not signed in: every way in says the same, and none hangs or throws. for (const [method, params] of [['_lembas/models', {}], ['_lembas/editor/settings', {}], ['_lembas/complete', { id: 'c1', prefix: 'def bake():', suffix: '' }], ['_lembas/edit', { id: 'e1', text: 'x = 1', instruction: 'make it 2', cwd: project }]]) { await assert.rejects(agent.request(method, params, 30000), (e) => e.code === -32602 && notSignedIn(e) && say(e) === SIGN_IN, method); } const refusals = []; const completer = new Completer({ request: (method, params) => agent.request(method, params, 30000), notify: (method, params) => agent.notify(method, params), debounceMs: () => 0, onRefusal: (reason, message) => refusals.push([reason, say({ message, data: { reason } })]), }); assert.equal(await completer.ask({ prefix: 'def bake():', suffix: '' }, { manual: true }), null); assert.deepEqual(refusals, [['not_logged_in', SIGN_IN]]); assert.equal(await completer.ask({ prefix: 'def bake():\n', suffix: '' }), null, 'and typing asks no more'); assert.equal(refusals.length, 1); // A message: the chat says how to sign in, once, and is not left working. assert.equal(await chat.send('hello, real CLI'), true); const errors = chat.items.filter((i) => i.kind === 'note' && i.level === 'error'); assert.deepEqual(errors.map((i) => [i.text, i.action]), [[SIGN_IN, 'signIn']], text(chat)); assert.equal(chat.busy, false); assert.equal(chat.session, null); assert.equal(chat.agent, null, 'an agent started before the sign-in is not kept'); // Nothing is left running. for (const a of agents) a.stop(); await until(() => agents.every((a) => a.gone), 15000); for (const a of agents) assert.throws(() => process.kill(a.proc.pid, 0), 'the CLI process has ended'); });