'use strict'; 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, resolveCommand } = require('../src/agent'); const { MIN_CLI, compareVersions } = require('../src/words'); const { tempDir, fakeAgent, until } = require('./helpers'); test('the hello says what this client is: an editor that shows media and has unsaved text', async (t) => { const agent = fakeAgent(t, tempDir(t)); await agent.connect(); const hello = agent.log().find((m) => m.method === 'initialize'); assert.equal(hello.params.protocolVersion, 1); assert.deepEqual(hello.params.clientCapabilities._meta.lembas, { protocol: 3, editor: true, media: true, show: true }); assert.deepEqual(hello.params.clientInfo, { name: 'test', version: '0' }); assert.equal(agent.version, '2.0.0-beta.17'); assert.equal(agent.tooOld, ''); assert.deepEqual(hello.params.clientCapabilities.fs, { readTextFile: true, writeTextFile: false }, 'writing is offered only by a client that can do it'); assert.ok(agent.ready && agent.has('editor') && agent.has('media') && !agent.has('fs')); }); test('a client that can put text into its documents says so', async (t) => { const agent = fakeAgent(t, tempDir(t), {}, { writeTextFile: true }); await agent.connect(); assert.deepEqual(agent.log().find((m) => m.method === 'initialize').params.clientCapabilities.fs, { readTextFile: true, writeTextFile: true }); }); test('every caller of connect gets the same start', async (t) => { const agent = fakeAgent(t, tempDir(t)); const [a, b] = await Promise.all([agent.connect(), agent.connect()]); assert.equal(a, b); assert.equal(agent.log().filter((m) => m.method === 'initialize').length, 1); }); test('an older CLI is known by its version, compared as numbers, and lacks what it lacks', async (t) => { const agent = fakeAgent(t, tempDir(t), { FAKE_AGENT_VERSION: '2.0.0-beta.9' }); await agent.connect(); assert.ok(!agent.has('editor') && agent.has('steer')); assert.equal(agent.tooOld, `LLeMbas CLI 2.0.0-beta.9 is older than this needs (${MIN_CLI} or newer), so parts of it will not work. Update the CLI: lembas update`); await assert.rejects(agent.request('_lembas/models', {}), (e) => e.code === -32601); }); test('versions are compared as numbers, and a release is newer than its betas', () => { assert.equal(compareVersions('2.0.0-beta.10', '2.0.0-beta.9'), 1); assert.equal(compareVersions('2.0.0-beta.9', '2.0.0-beta.16'), -1); assert.equal(compareVersions('2.0.0', '2.0.0-beta.99'), 1); assert.equal(compareVersions('2.0.0-beta.17+build', '2.0.0-beta.17'), 0); assert.equal(compareVersions('1.9.9', '2.0.0-beta.1'), -1); assert.equal(compareVersions('2.1.0-rc.1', '2.1.0-beta.9'), 1); assert.equal(compareVersions('nonsense', '1.0.0'), null); }); test('an agent that never answers the hello is given up on, said in words, and ended', async (t) => { const agent = fakeAgent(t, tempDir(t), { FAKE_AGENT_HANG: 'initialize' }, { startTimeoutMs: 300 }); let gone = false; agent.on('exit', () => (gone = true)); await assert.rejects(agent.connect(), /The LLeMbas CLI did not answer within 0 seconds\./); await until(() => gone); }); test('an agent that ignores a polite stop is killed, so none is left behind', async (t) => { const dir = tempDir(t); const script = path.join(dir, 'stubborn.py'); fs.writeFileSync(script, "import signal, sys, json\nsignal.signal(signal.SIGTERM, signal.SIG_IGN)\nfor line in sys.stdin:\n m = json.loads(line)\n print(json.dumps({'jsonrpc': '2.0', 'id': m['id'], 'result': {}}), flush=True)\nimport time\nwhile True: time.sleep(1)\n"); const agent = new Agent({ command: 'python3', args: [script], cwd: dir, killAfterMs: 200 }); await agent.connect(); const pid = agent.proc.pid; let exit = null; agent.on('exit', (e) => (exit = e)); agent.stop(); await until(() => exit !== null, 4000); assert.equal(exit.signal, 'SIGKILL'); assert.throws(() => process.kill(pid, 0)); }); test('a bare `lembas` that is not on the PATH is looked for where the installer puts it', (t) => { const home = tempDir(t); assert.equal(resolveCommand('lembas', { PATH: '/nonexistent-a:/nonexistent-b' }, home), 'lembas'); fs.mkdirSync(path.join(home, '.local', 'bin'), { recursive: true }); const at = path.join(home, '.local', 'bin', 'lembas'); fs.writeFileSync(at, '#!/bin/sh\n', { mode: 0o755 }); assert.equal(resolveCommand('lembas', { PATH: '/nonexistent-a' }, home), at); assert.equal(resolveCommand('lembas', { PATH: path.dirname(at) }, home), 'lembas', 'one on the PATH is used as it is'); assert.equal(resolveCommand('/opt/x/lembas', { PATH: '' }, home), '/opt/x/lembas', 'a set command is never replaced'); }); test('a command that does not exist is said in words, once', async (t) => { const agent = new Agent({ command: 'lembas-that-is-not-installed', cwd: tempDir(t) }); const exits = []; agent.on('exit', (e) => exits.push(e)); await assert.rejects(agent.connect(), /The LLeMbas CLI was not found: `lembas-that-is-not-installed` is not a program this editor can start\. Install the CLI, or set "LLeMbas: Command" \(llembas\.command\) to its full path\./); await assert.rejects(agent.request('x', {}), /not running/); assert.equal(exits.length, 1); assert.equal(exits[0].started, false); }); test('an agent that exits fails what was asked and says its last words', async (t) => { const agent = fakeAgent(t, tempDir(t)); await agent.connect(); const exits = []; agent.on('exit', (e) => exits.push(e)); const r = await agent.request('session/new', { cwd: os.tmpdir(), mcpServers: [] }); const turn = agent.request('session/prompt', { sessionId: r.sessionId, prompt: [{ type: 'text', text: 'CRASH' }] }); await assert.rejects(turn, /the agent stopped \(exit 3\): error: the session store could not be opened/); await until(() => exits.length === 1); assert.equal(exits[0].code, 3); assert.equal(exits[0].started, true); assert.match(exits[0].last, /session store/); }); test('stop ends the process', async (t) => { const agent = fakeAgent(t, tempDir(t)); await agent.connect(); let gone = false; agent.on('exit', () => (gone = true)); agent.stop(); await until(() => gone); await assert.rejects(agent.request('x', {}), /not running/); });