Files
reverse-engineering-anything/tests/boundary/cli/cutterCommands.test.ts
T
385e5d228f test: own temporary directories through createTestTempDirectory in remaining tests/ (#12727)
* test: own temporary directories through createTestTempDirectory in remaining tests/

Give per-test directories in the remaining tests/ suites to
createTestTempDirectory so onTestFinished removes them through
removeTestWorkspace. Keep suite-scoped roots and child-process directories
whose removal is the assertion.

* test: drop empty cleanup callbacks and keep the capture skip reason visible

Remove the no-op withVerifierCleanup steps left after directory removal
moved to createTestTempDirectory. Put the native capture skip explanation
in the Linux sample test name, because it.skipIf only tests truthiness.

Co-authored-by: morluto <morluto@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
2026-11-19 23:22:53 -05:00

303 lines
11 KiB
TypeScript

import { createServer } from "node:net";
import { createHmac, randomUUID } from "node:crypto";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { serveCli } from "./cliServeHarness.js";
import { cutterDiscoveryLimitations } from "../../../src/domain/cutterDiscovery.js";
import {
spawnOwnedProviderProcess,
ProviderProcessSupervisor,
} from "../../../src/process/ProviderProcess.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const SESSION_ID = "27d3e3f1-f1e5-49ae-91ec-95af1f343a5a";
const listen = async (
server: ReturnType<typeof createServer>,
): Promise<number> => {
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
const address = server.address();
if (address === null || typeof address === "string")
throw new Error("Expected an ephemeral TCP listener");
return address.port;
};
describe("Cutter CLI commands", () => {
let directory: string | undefined;
let server: ReturnType<typeof createServer> | undefined;
afterEach(async () => {
if (server !== undefined)
await new Promise<void>((resolve) => server?.close(() => resolve()));
server = undefined;
});
it("lists sessions from the selected bridge directory", async () => {
directory = await createTestTempDirectory("rea-cutter-cli-test-");
const result = await serveCli(["list-cutter-sessions", "--json"], {
REA_CUTTER_BRIDGE_DIR: directory,
});
expect(result.exitCode).toBe(0);
// The CLI projects the shared discovery owner's limitation unchanged.
const limitations = cutterDiscoveryLimitations("no_live_bridge_found");
expect(limitations).not.toEqual([]);
expect(JSON.parse(result.stdout)).toMatchObject({
sessions: [],
discovery_status: "no_live_bridge_found",
bridge_directory: directory,
limitations,
});
});
it.skipIf(process.platform === "win32")(
"awaits discovery cancellation on SIGINT and returns its typed outcome with exit130",
async () => {
directory = await createTestTempDirectory("rea-cutter-cli-cancel-");
const sockets = new Set<import("node:net").Socket>();
let accepted = 0;
server = createServer((socket) => {
accepted += 1;
sockets.add(socket);
socket.on("error", () => undefined);
socket.on("close", () => sockets.delete(socket));
socket.resume();
});
const port = await listen(server);
for (let index = 0; index < 9; index += 1) {
const id = randomUUID();
await writeFile(
join(directory, `cutter-${process.pid}-${id}.json`),
JSON.stringify({
session_id: id,
pid: process.pid,
host: "127.0.0.1",
port,
token: "cli-discovery-test-transport-token",
document_generation: 0,
current_file: null,
cutter_version: null,
identity_status: "partial",
}),
{ mode: 0o600 },
);
}
const launched = await spawnOwnedProviderProcess({
command: process.execPath,
arguments: ["scripts/rea.mjs", "list-cutter-sessions", "--json"],
runId: randomUUID(),
hostEnvironment: {
...process.env,
REA_CUTTER_BRIDGE_DIR: directory,
REA_LOG_LEVEL: "silent",
},
});
const supervisor = new ProviderProcessSupervisor({
...launched,
ownsProcessLifetime: true,
});
try {
// Probes that time out close their sockets, so only a full concurrent
// wave holds eight open at once.
await expect.poll(() => sockets.size).toBe(8);
launched.process.kill("SIGINT");
expect(await supervisor.waitForExit(5_000)).toBe(true);
expect(await supervisor.waitForOutputClose(1_000)).toBe(true);
expect(launched.process.exitCode).toBe(130);
expect(launched.process.signalCode).toBeNull();
expect(JSON.parse(supervisor.snapshot().stdout.text)).toMatchObject({
code: "cancelled",
});
await expect.poll(() => sockets.size).toBe(0);
expect(accepted).toBe(8);
} finally {
for (const socket of sockets) socket.destroy();
const stopped = await supervisor.stop();
expect(stopped.status).not.toBe("incomplete");
}
},
);
it("reports an unavailable selected session for a command", async () => {
directory = await createTestTempDirectory("rea-cutter-cli-test-");
const result = await serveCli(
["cutter-command", SESSION_ID, "0", "pdi", "--json"],
{ REA_CUTTER_BRIDGE_DIR: directory },
);
expect(result.exitCode).toBe(1);
expect(JSON.parse(result.stdout)).toMatchObject({
code: "invalid_request",
details: {
operation: "cutter_command",
issues: [
{
// Projected from its contract, so the issue names the CLI key.
path: ["sessionId"],
message:
"Cutter bridge session is unavailable or stale; list sessions again and select an available session.",
},
],
},
});
});
});
describe("Cutter CLI command output flags", () => {
let directory: string | undefined;
let server: ReturnType<typeof createServer> | undefined;
afterEach(async () => {
if (server !== undefined)
await new Promise<void>((resolve) => server?.close(() => resolve()));
server = undefined;
});
it("returns Evidence when the selected Cutter session completes a command", async () => {
directory = await createTestTempDirectory("rea-cutter-cli-test-");
const token = "synthetic-cutter-cli-token-for-test";
let nextConnectionOrdinal = 0;
const commandJsonModes: boolean[] = [];
server = createServer((connection) => {
const ordinal = ++nextConnectionOrdinal;
connection.setEncoding("utf8");
let input = "";
let clientNonce = "";
let serverNonce = "";
connection.on("data", (chunk: string) => {
input += chunk;
const newline = input.indexOf("\n");
if (newline < 0) return;
const frame = JSON.parse(input.slice(0, newline)) as Record<
string,
unknown
>;
input = input.slice(newline + 1);
if (frame.kind === "rea-auth-v2") {
clientNonce = String(frame.nonce);
serverNonce = `${ordinal.toString(16).padStart(16, "0")}${"b".repeat(48)}`;
connection.write(
`${JSON.stringify({ session_id: SESSION_ID, nonce: serverNonce, proof: createHmac("sha256", token).update(`rea-cutter-server-v2:${SESSION_ID}:${clientNonce}:${serverNonce}`).digest("hex") })}\n`,
);
return;
}
const requestBytes = String(frame.request);
const request = JSON.parse(requestBytes) as Record<string, unknown>;
if (request.kind === "command")
commandJsonModes.push(request.json === true);
if (
frame.proof !==
createHmac("sha256", token)
.update(
`rea-cutter-client-v2:${SESSION_ID}:${clientNonce}:${serverNonce}:${requestBytes}`,
)
.digest("hex")
) {
connection.destroy();
return;
}
const status = request.kind === "status";
const response = JSON.stringify({
ok: true,
session_id: request.session_id,
...(status
? {
current_file: null,
document_generation: 0,
cutter_version: "Cutter fixture",
identity_status: "partial",
completed_connection: (ordinal - 1)
.toString(16)
.padStart(16, "0"),
completed_commands: [],
}
: request.kind === "ack"
? { acknowledged: false }
: {
output: { answer: 42 },
current_file: null,
document_generation: 0,
cutter_version: "Cutter fixture",
identity_status: "partial",
execution_state: "complete",
output_truncated: false,
}),
});
const proof = createHmac("sha256", token)
.update(
`rea-cutter-response-v2:${SESSION_ID}:${clientNonce}:${serverNonce}:${response}`,
)
.digest("hex");
connection.end(`${JSON.stringify({ response, proof })}\n`);
});
});
const port = await listen(server);
await writeFile(
join(directory, `cutter-${process.pid}-${SESSION_ID}.json`),
JSON.stringify({
session_id: SESSION_ID,
pid: process.pid,
host: "127.0.0.1",
port,
token,
document_generation: 0,
current_file: null,
cutter_version: "Cutter fixture",
identity_status: "partial",
}),
{ mode: 0o600 },
);
const result = await serveCli(
["cutter-command", SESSION_ID, "000", "pdi", "--json-output"],
{ REA_CUTTER_BRIDGE_DIR: directory },
);
expect(result.exitCode).toBe(0);
expect(commandJsonModes).toEqual([true]);
expect(result.stdout).toContain("answer: 42");
const formatted = await serveCli(
["cutter-command", SESSION_ID, "0", "pdi", "--json"],
{ REA_CUTTER_BRIDGE_DIR: directory },
);
expect(formatted.exitCode).toBe(0);
expect(commandJsonModes).toEqual([true, false]);
expect(() => JSON.parse(formatted.stdout)).not.toThrow();
for (const [flags, expected] of [
[["--json-output=true"], true],
[["--json-output=false"], false],
[["--json", "--json-output=false"], false],
[["--json", "--json-output=true"], true],
[["--json-output=false", "--json"], false],
[["--json-output=true", "--json"], true],
] as const) {
const selected = await serveCli(
["cutter-command", SESSION_ID, "0", "pdi", ...flags],
{ REA_CUTTER_BRIDGE_DIR: directory },
);
expect(selected.exitCode).toBe(0);
expect(commandJsonModes.at(-1)).toBe(expected);
if (flags.some((flag) => flag === "--json"))
expect(() => JSON.parse(selected.stdout)).not.toThrow();
}
const commandsBeforeRejectedFlags = commandJsonModes.length;
for (const flag of ["--json=true", "--json=false"]) {
const rejected = await serveCli(
["cutter-command", SESSION_ID, "0", "pdi", flag],
{ REA_CUTTER_BRIDGE_DIR: directory },
);
expect(rejected.exitCode).toBe(1);
expect(rejected.stdout).toContain("Unknown flag: --json");
}
expect(commandJsonModes).toHaveLength(commandsBeforeRejectedFlags);
});
});