Files
reverse-engineering-anything/tests/boundary/cli/cutterCommands.test.ts
T
gensokyo-devandgensokyo-dev 07e4b700ac test: let the deadline policy own test waits
Vitest's expect.poll and vi.waitFor stop after one second by default, a
host-speed guess that the project's deadline policy does not see. Under
hosted-CI load, kunaProjectConcurrency failed that way twice on unrelated
PRs. Another 256 waits relied on the default, and 97 passed literals
picked to fit some host.

Waits now take as long as their test may run, less the reporting reserve
that children already keep. Each Vitest project sets expect.poll's
default, and the new waitFor in tests/support/testWait.ts replaces
vi.waitFor. Fixture readiness waits (awaitAcknowledgement and the provider
process waits) default to the same deadline, and their host-speed literal
arguments are removed. One remainingTestDeadline owns the arithmetic for
both children and waits.

Ten waits whose short timeout was the real check now assert it directly
(Kuna version probe, Cutter discovery, doctor and Inspector cancellation,
apktool fixture teardown, JADX disconnect): settlement before the
production deadline that would otherwise also satisfy the wait, or a
deadline pinned beyond every test deadline.

The rea/project-wait-deadline lint rule rejects vi.waitFor and explicit
expect.poll timeouts. The testing guide and the CI tests audit skill now
document the policy.

Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-11-14 19:28:38 +08:00

302 lines
10 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 {
spawnOwnedProviderProcess,
ProviderProcessSupervisor,
} from "../../../src/process/ProviderProcess.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()));
if (directory !== undefined)
await rm(directory, { recursive: true, force: true });
directory = undefined;
server = undefined;
});
it("lists sessions from the selected bridge directory", async () => {
directory = await mkdtemp(join(tmpdir(), "rea-cutter-cli-test-"));
const result = await serveCli(["list-cutter-sessions", "--json"], {
REA_CUTTER_BRIDGE_DIR: directory,
});
expect(result.exitCode).toBe(0);
expect(JSON.parse(result.stdout)).toMatchObject({
sessions: [],
discovery_status: "no_live_bridge_found",
bridge_directory: directory,
});
});
it.skipIf(process.platform === "win32")(
"awaits discovery cancellation on SIGINT and returns its typed outcome with exit130",
async () => {
directory = await mkdtemp(join(tmpdir(), "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 mkdtemp(join(tmpdir(), "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: [
{
path: ["session_id"],
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()));
if (directory !== undefined)
await rm(directory, { recursive: true, force: true });
directory = undefined;
server = undefined;
});
it("returns Evidence when the selected Cutter session completes a command", async () => {
directory = await mkdtemp(join(tmpdir(), "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);
});
});