Files
reverse-engineering-anything/tests/fixtures/buildComputerMcp.ts
T
morluto c7fa6af8a3 test: fail loudly on setup refusals and assert exact MCP refusal and linkage facts (#15368)
* test(mcp): assert exact refusal, identity and Evidence linkage facts

Replace isError-only, expect.any and oracle-mirror checks in MCP and CLI workflow tests with the exact code, reason, subject identity and Evidence links the producers report. The capability-unavailable unknowns test now opens its target, so it exercises the path it names instead of NoBinaryOpenError.

* test: fail loudly on setup refusals, unrecorded paths and zombie liveness

Three shared helpers remove weakness classes from the suite:

- callOk (tests/support/mcp/toolResult.ts) fails at the setup step when a
  tool used as fixture setup refuses. It replaces the build-computer copy
  and the unchecked open_binary calls. serverUnknownsMcp's
  capability-unavailable test passed only because open_binary failed: its
  fixture refused health, and NoBinaryOpenError has the same "unavailable"
  category. The fixture now answers health, and the test asserts
  capability_unavailable for procedure_pseudo_code.
- expectRemoved (tests/support/expectRemoved.ts) throws when no path was
  recorded; access(list[0] ?? "") passed because "" is also ENOENT.
- expectProcessRunning checks ps state instead of signal 0, which still
  reaches a zombie that REA wrongly signalled.

Also asserts the open target before and after an ordinary close, the
written overwrite snapshot, and the comparison's links, status and
supporting unknown.

* test(mcp): compare delivered recovery with its owning projection
2026-12-01 02:26:44 +00:00

126 lines
4.6 KiB
TypeScript

import type { BuildComputerDefaults } from "../../src/desktop/BuildComputerEnvironment.js";
import type { ComputerStartTiming } from "../../src/desktop/AgentEnvComputer.js";
import { closeVerifierResources } from "../../scripts/lib/verifier-cleanup.mjs";
import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { callOk } from "../support/mcp/toolResult.js";
import { expect, onTestFinished } from "vitest";
import type { BuildSettle } from "../../src/application/desktop/DesktopBuildSettle.js";
import { toolContract } from "../../src/contracts/toolContracts.js";
import type { SettleTiming } from "../../src/domain/desktop/appSettle.js";
import { createServer } from "../../src/server/createServer.js";
import { createTestBinarySession } from "./binarySession.js";
import {
fakeAgentEnvRunner,
isolatedBuildDefaults,
startFakeComputer,
type FakeAgentEnvRunner,
type FakeComputer,
type FakeComputerOptions,
} from "./fakeAgentEnvComputer.js";
import { createTestTempDirectory } from "./temporaryDirectory.js";
import { createMcpSchemaValidation } from "./mcpSchemaValidation.js";
/** The host's environment without the settings the opt-in real test exports, which would reach past the fakes. */
const hostEnvironment = Object.fromEntries(
Object.entries(process.env).filter(
([name]) =>
![
"REA_AGENT_ENV_COMMAND",
"REA_DOCKER_COMMAND",
"REA_BUILD_COMPUTER_REA_ARCHIVE",
].includes(name),
),
);
/** A check grades its first read, for tests whose app changes nothing while it is checked. */
const FIRST_READ: SettleTiming = {
intervalMs: 0,
windowMs: 0,
boundMs: 0,
};
/** An MCP client of a REA server whose build computers reach the fakes through `environment`, read apps with `settle` and start on `startTiming`. */
export const connectBuildServer = async (
environment: Readonly<Record<string, string>>,
settle: BuildSettle = FIRST_READ,
defaults?: BuildComputerDefaults,
startTiming?: ComputerStartTiming,
): Promise<Client> => {
const session = createTestBinarySession(() => {
throw new Error("build computers must not start a deep provider");
});
const server = createServer(
{ kind: "session", session },
{
environment: {
...hostEnvironment,
...environment,
},
desktopBuildSettle: settle,
desktopBuildDefaults: defaults ?? (await isolatedBuildDefaults()),
...(startTiming === undefined
? {}
: { desktopBuildStartTiming: startTiming }),
},
);
const client = new Client(
{ name: "build-computer-mcp", version: "1" },
{ jsonSchemaValidator: createMcpSchemaValidation() },
);
onTestFinished(async () => {
await closeVerifierResources([
() => client.close(),
() => server.close(),
async () => {
const closed = await session.close();
if (!closed.ok) throw closed.error;
},
]);
});
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
await server.connect(serverTransport);
await client.connect(clientTransport);
return client;
};
/** A fake computer and the fake runner that hands it out. */
export const buildComputerFakes = async (
options: FakeComputerOptions = {},
): Promise<{ computer: FakeComputer; runner: FakeAgentEnvRunner }> => {
const computer = await startFakeComputer(options);
onTestFinished(() => computer.close());
const runner = await fakeAgentEnvRunner(computer);
return { computer, runner };
};
/** An exported bundle's folder as far as the fake runner reads it; given a fake computer, the runner hands that one out for it. */
export const bundleFolder = async (
computer?: Pick<FakeComputer, "url">,
): Promise<string> => {
const bundle = await createTestTempDirectory("exported-bundle-");
await mkdir(join(bundle, "tasks"));
await writeFile(join(bundle, "tasks", "smoke.json"), "[]\n");
if (computer !== undefined)
await writeFile(join(bundle, "fake-computer-url"), computer.url);
return bundle;
};
/** Start a build computer and return its id. */
export const startBuildComputer = async (
client: Client,
source: Record<string, unknown>,
): Promise<string> =>
toolContract("start_build_computer").outputSchema.parse(
(await callOk(client, "start_build_computer", { source }))
.structuredContent,
).computer_id;
/** No delivered text, success or failure, may hold the control token. */
export const expectNoToken = (computer: FakeComputer, result: unknown) =>
expect(JSON.stringify(result)).not.toContain(computer.token);