The extension's tests run on the stand-in both editors share. Fixed: a failed turn was silent; usage and progress were read from fields the CLI does not send; a file the agent had read through the editor could no longer be changed once its tab was closed; a write into a CRLF document made the next edit fail as 'changed since you last read it'; no request had a time limit and a line that never ended was kept for ever; an agent that ignored a stop was left running. The hello says protocol 3, the CLI's version is compared with a stated minimum, and not being signed in is said the same way everywhere. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
79 lines
2.9 KiB
JavaScript
79 lines
2.9 KiB
JavaScript
'use strict';
|
|
// Shared by the tests: the stand-in agent, a place for files, and reading what the agent was sent.
|
|
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');
|
|
|
|
// The stand-in both editors' tests share: it answers in the LLeMbas CLI's own shapes.
|
|
const FAKE = path.join(__dirname, '..', '..', 'tests', 'fake_agent.py');
|
|
|
|
function tempDir(t) {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'llembas-vscode-test-'));
|
|
// An agent that is ending may still write its log: tried again rather than failed.
|
|
t.after(() => fs.rmSync(dir, { recursive: true, force: true, maxRetries: 5, retryDelay: 50 }));
|
|
return dir;
|
|
}
|
|
|
|
/** An Agent on the stand-in. `env` adds to the environment; the log of what it received is `agent.log()`. */
|
|
function fakeAgent(t, dir, env, more) {
|
|
const logFile = path.join(dir, `agent-${Math.random().toString(36).slice(2)}.log`);
|
|
const agent = new Agent({
|
|
command: 'python3',
|
|
args: [FAKE],
|
|
cwd: dir,
|
|
env: { ...process.env, PYTHONDONTWRITEBYTECODE: '1', FAKE_AGENT_LOG: logFile, ...(env || {}) },
|
|
clientInfo: { name: 'test', version: '0' },
|
|
...(more || {}),
|
|
});
|
|
agent.log = () => (fs.existsSync(logFile) ? fs.readFileSync(logFile, 'utf8').split('\n').filter(Boolean).map((l) => JSON.parse(l)) : []);
|
|
t.after(() => agent.stop());
|
|
return agent;
|
|
}
|
|
|
|
/** A Chat on the stand-in. Returns `{ chat, agents, dir }`; `agents` fills as the chat starts them. */
|
|
function fakeChat(t, options) {
|
|
const o = options || {};
|
|
const dir = o.dir || tempDir(t);
|
|
const agents = [];
|
|
const chat = new Chat({
|
|
makeAgent: (m) => {
|
|
const a = fakeAgent(t, dir, o.env, { cwd: m.cwd, onRequest: m.onRequest, ...(o.agent || {}) });
|
|
agents.push(a);
|
|
return a;
|
|
},
|
|
cwd: () => dir,
|
|
options: () => o.options || {},
|
|
hooks: o.hooks || {},
|
|
});
|
|
t.after(() => chat.dispose());
|
|
return { chat, agents, dir };
|
|
}
|
|
|
|
/** Wait until `test()` is true (checked every few milliseconds). */
|
|
async function until(test, ms) {
|
|
const end = Date.now() + (ms || 8000);
|
|
for (;;) {
|
|
if (test()) return true;
|
|
if (Date.now() > end) throw new Error('waited too long for: ' + test);
|
|
await new Promise((r) => setTimeout(r, 10));
|
|
}
|
|
}
|
|
|
|
const text = (chat) =>
|
|
chat.items
|
|
.map((i) => {
|
|
if (i.kind === 'user') return `You: ${i.label ? '[' + i.label + '] ' : ''}${i.text}`;
|
|
if (i.kind === 'agent') return `LLeMbas: ${i.text}`;
|
|
if (i.kind === 'thought') return `Thinking: ${i.text}`;
|
|
if (i.kind === 'note') return `> ${i.text}`;
|
|
if (i.kind === 'tool') return `tool ${i.status}: ${i.title}`;
|
|
if (i.kind === 'card') return `card ${i.state}: ${i.title} (${i.answer})`;
|
|
if (i.kind === 'media') return `media: ${i.name}`;
|
|
return i.kind;
|
|
})
|
|
.join('\n');
|
|
|
|
module.exports = { FAKE, tempDir, fakeAgent, fakeChat, until, text };
|