Files
Agent-Workbench/docs/reliability-1.25.2-evidence/source-bytes/tests/mcp-stdio.test.mjs.txt
T

68 lines
5.3 KiB
Plaintext

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { McpStdio } from '../src/core/tools/mcp-stdio.mjs';
import { ReaAdapter, resolveReaInstallation } from '../src/core/tools/rea-adapter.mjs';
import { fixture, workspace } from './helpers.mjs';
const script = path.join(workspace, 'tests', 'fixtures', 'research-mcp.cjs');
function clientFixture(t, mode, options = {}) {
const f = fixture(t), client = new McpStdio({ executable: process.execPath, args: [script, mode], cwd: f.root, env: { ...process.env } }, options);
f.onCleanup(() => client.close({ force: true })); return { ...f, client };
}
test('stdio preserves split UTF-8 frames and final replies drained after process exit', async t => {
for (const mode of ['echo', 'exit-result']) {
const { client } = clientFixture(t, mode), result = await client.request('echo', { source: '用户工件🙂' });
assert.equal(result.text, '原字节🙂'); assert.equal(result.received.source, '用户工件🙂');
assert.equal(client.trace.length, 1); assert.equal(client.trace[0].status, 'completed');
assert.match(client.trace[0].resultSha256, /^[a-f0-9]{64}$/);
assert.equal((await client.close({ force: true })).rootExited, true);
}
});
test('server-initiated model requests are refused instead of inheriting provider privileges', async t => {
const { client } = clientFixture(t, 'server-request'); assert.deepEqual(await client.request('echo'), { denied: true });
assert.equal(client.trace.length, 1);
});
for (const [mode, code] of [['invalid', 'MCP_INVALID_FRAME'], ['oversized', 'MCP_FRAME_LIMIT']]) {
test('invalid or over-budget protocol ' + mode + ' closes only its owned process and is not retried', async t => {
const { client } = clientFixture(t, mode, { maximumFrameBytes: 1024 });
await assert.rejects(client.request('echo'), error => error.code === code && error.dispatched === true);
assert.equal(client.trace.length, 1); assert.equal(client.trace[0].status, 'failed');
assert.equal((await client.close({ force: true })).rootExited, true);
});
}
test('cancel and timeout do not resend a dispatched request and pre-cancel has no request trace', async t => {
const { client } = clientFixture(t, 'hang', { timeoutMs: 100 });
await assert.rejects(client.request('echo'), error => error.code === 'MCP_TIMEOUT' && error.dispatched);
assert.equal(client.trace.length, 1);
const controller = new AbortController(), pending = client.request('echo', {}, { signal: controller.signal, timeoutMs: 3000 }); controller.abort();
await assert.rejects(pending, error => error.code === 'MCP_CANCELLED' && error.dispatched);
await assert.rejects(client.request('echo', {}, { signal: controller.signal }), error => error.code === 'MCP_CANCELLED' && !error.dispatched);
assert.equal(client.trace.length, 2); assert.equal((await client.close({ force: true })).rootExited, true);
});
test('startup failure remains undispatched and has no process PID to terminate', async t => {
const f = fixture(t), client = new McpStdio({ executable: path.join(f.root, 'missing-node.exe'), args: [], cwd: f.root });
f.onCleanup(() => client.close()); await assert.rejects(client.request('initialize'), error => error.code === 'MCP_START_FAILED' && !error.dispatched);
assert.equal(client.trace[0].dispatched, false); const closed = await client.close(); assert.equal(closed.pid, null); assert.equal(closed.rootExited, true);
});
test('REA launch filters a fixture credential canary and rejects changed entry identity before a new connection', async t => {
const f = fixture(t), packageRoot = path.join(f.root, 'package'); fs.mkdirSync(path.join(packageRoot, 'scripts'), { recursive: true });
fs.writeFileSync(path.join(packageRoot, 'package.json'), JSON.stringify({ name: 'rea-agents', version: '6.8.0' }));
fs.copyFileSync(script, path.join(packageRoot, 'scripts', 'rea.mjs'));
// The fixture entry uses CommonJS; its local declaration intentionally selects
// CommonJS so the same protocol fixture can be launched through REA's profile.
fs.writeFileSync(path.join(packageRoot, 'package.json'), JSON.stringify({ name: 'rea-agents', version: '6.8.0', type: 'commonjs' }));
fs.copyFileSync(script, path.join(packageRoot, 'scripts', 'rea.cjs'));
fs.writeFileSync(path.join(packageRoot, 'scripts', 'rea.mjs'), "import './rea.cjs';\n");
const installation = resolveReaInstallation(f.root, { packageRoot });
const previous = process.env.AGENT_WORKBENCH_RESEARCH_TEST_CANARY; process.env.AGENT_WORKBENCH_RESEARCH_TEST_CANARY = 'fixture-only-canary';
const adapter = new ReaAdapter(installation, { cwd: f.root }); f.onCleanup(() => adapter.close({ force: true }));
try {
const contract = await adapter.connect(); assert.equal(contract.initialize.fixtureCanaryForwarded, false);
assert.equal(contract.advertisedTools, 0); assert.equal((await adapter.close({ force: true })).rootExited, true);
fs.appendFileSync(installation.entry.path, '// changed entry\n');
const changed = new ReaAdapter(installation, { cwd: f.root }); await assert.rejects(changed.connect(), /入口身份/); assert.equal(changed.client, undefined);
} finally { if (previous === undefined) delete process.env.AGENT_WORKBENCH_RESEARCH_TEST_CANARY; else process.env.AGENT_WORKBENCH_RESEARCH_TEST_CANARY = previous; }
});