Files
reverse-engineering-anything/tests/boundary/cli/webScriptSelectionCli.test.ts
T
morlutoandCursor Agent 2bf347dee6 fix(cli): reject invalid inline values and honor requested output on terminals (#14763)
* fix(cli): reject invalid inline values and honor requested output on terminals

- Reject inline boolean and empty number values the schema cannot accept
  instead of coercing --skill=yes to false or --limit= to 0.
- Give captured mcp add output the terminal presentation of every other
  command through one shared result destination.
- Show the unapproved setup plan whose remediation asks a terminal reader to
  review it, and open the setup rail once when a scoped run starts at approval.
- Treat --full-output and --token-count as requesting the result document, so
  setup and argument errors no longer add terminal presentation beside it.
- Answer --schema for an unknown command in the selected format, and set the
  exit code instead of exiting so piped stdout drains.

* fix(cli): keep rejected boolean and number literals across repeated flags

Binding stored one scalar value, so a later valid flag replaced an earlier
literal before the schema could reject it. Boolean and number literals the
field rejects now stay bound, including decimal unions and count options,
while repeated valid flags still use the last value. --dry-run keeps its
own error.

* test(cli): assert the terminal full-output failure envelope

The terminal --full-output regression only checked that usage text was
absent. Decode the default document and the --json envelope and require
VALIDATION_ERROR with the skill field's invalid_type error.

* test(cli): accept a counted setup approval prompt

A scoped terminal plan names every eligible action, so a Linux host that
can install Hopper asks "Apply these N changes?" rather than the singular
question. The consent helper now answers either prompt, and the rail test
finds that question before checking the intro was rendered once.

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
2026-11-26 12:15:17 -05:00

211 lines
6.9 KiB
TypeScript

import { createCommandCli } from "../../support/cli/commandCli.js";
import { cliCommand } from "../../../src/cli/commandRegistry.js";
import { access, mkdir, readdir, symlink, writeFile } from "node:fs/promises";
import { join } from "node:path";
import pino from "pino";
import { expect, it, vi } from "vitest";
import { registerWebScriptCommands } from "../../../src/cli/webScriptCommands.js";
import * as webScriptExport from "../../../src/application/WebScriptExportService.js";
import { cliCommandFor } from "../../../src/contracts/cliCommandNames.js";
import { AnalysisCancelledError } from "../../../src/domain/analysisErrorCore.js";
import { err, ok } from "../../../src/domain/result.js";
import { ArtifactResourceScope } from "../../../src/resources/ArtifactResourceScope.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import {
scriptCaptureEvidenceFixture,
scriptScenarioFixture,
} from "../../fixtures/webScriptCapture.js";
import { cliTest } from "../../support/cli/cliFixture.js";
const registeredExportCommand = () => {
const cli = createCommandCli();
registerWebScriptCommands(cli, pino({ enabled: false }));
return cliCommand(cli, cliCommandFor("export_web_scripts"))!.definition;
};
it("forwards an interrupt into the export workflow and reports cancellation", async () => {
const command = registeredExportCommand();
const exported = vi
.spyOn(webScriptExport, "exportWebScripts")
.mockImplementation(async (_input, _resources, options) => {
process.emit("SIGINT");
return err(
new AnalysisCancelledError("export_web_scripts", {
cause: options?.signal?.reason,
}),
);
});
const previousExitCode = process.exitCode;
try {
const output = await command.run({
structuredOutput: false,
options: {},
args: {
capturePath: "/unused/capture.json",
outputDirectory: "/unused/out",
},
});
expect(exported.mock.calls[0]?.[2]?.signal?.aborted).toBe(true);
expect(output).toMatchObject({ code: "cancelled" });
expect(process.exitCode).toBe(130);
} finally {
exported.mockRestore();
process.exitCode = previousExitCode;
}
});
it.each(["cleanup failure", "cancellation during cleanup"] as const)(
"retains exported Evidence on %s",
async (outcome) => {
const command = registeredExportCommand();
const evidence = scriptCaptureEvidenceFixture();
const exported = vi
.spyOn(webScriptExport, "exportWebScripts")
.mockResolvedValue(ok(evidence));
const close = vi
.spyOn(ArtifactResourceScope.prototype, "close")
.mockImplementation(async () => {
if (outcome === "cleanup failure") throw new Error("close denied");
process.emit("SIGINT");
});
const previousExitCode = process.exitCode;
try {
const output = await command.run({
structuredOutput: false,
options: {},
args: {
capturePath: "/unused/capture.json",
outputDirectory: "/unused/out",
},
});
expect(close).toHaveBeenCalledOnce();
expect(output).toMatchObject({
error: "Script export failed",
code:
outcome === "cleanup failure" ? "cleanup_incomplete" : "cancelled",
details: { partial_observation: evidence },
});
} finally {
exported.mockRestore();
close.mockRestore();
process.exitCode = previousExitCode;
}
},
);
it("closes export resources when the operation throws", async () => {
const command = registeredExportCommand();
const exported = vi
.spyOn(webScriptExport, "exportWebScripts")
.mockRejectedValue(new Error("unexpected export failure"));
const close = vi
.spyOn(ArtifactResourceScope.prototype, "close")
.mockResolvedValue();
try {
const output = await command.run({
structuredOutput: false,
options: {},
args: {
capturePath: "/unused/capture.json",
outputDirectory: "/unused/out",
},
});
expect(close).toHaveBeenCalledOnce();
expect(output).toMatchObject({
error: "Script export failed",
details: { operation: "export_web_scripts" },
});
} finally {
exported.mockRestore();
close.mockRestore();
}
});
cliTest.skipIf(process.platform === "win32")(
"exits with a capture_path invalid_request for a named pipe before publishing output",
async ({ cli, processes }) => {
const root = await createTestTempDirectory("rea-script-selection-cli-");
const capturePath = join(root, "capture.pipe");
const outputDirectory = join(root, "export");
const created = await processes.run("mkfifo", [capturePath]);
expect(created.exitCode).toBe(0);
const response = await cli.run({
arguments: ["export-web-scripts", capturePath, outputDirectory, "--json"],
environment: { REA_LOG_LEVEL: "silent" },
});
expect(response.exitCode).toBe(1);
expect(response.signal).toBeNull();
expect(response.json).toMatchObject({
error: "Script export failed",
code: "invalid_request",
details: {
operation: "export_web_scripts",
issues: [
{
path: ["capturePath"],
reason: "invalid_value",
message: expect.stringContaining("regular file"),
},
],
},
});
expect(response.stdout).toContain(capturePath);
await expect(access(outputDirectory)).rejects.toMatchObject({
code: "ENOENT",
});
},
);
cliTest(
"refuses an existing output directory or link as invalid output_directory input",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-script-export-existing-");
const capturePath = join(root, "capture.json");
await writeFile(capturePath, JSON.stringify(scriptScenarioFixture()));
const retained = join(root, "retained");
await mkdir(retained);
await writeFile(join(retained, "marker.txt"), "keep");
const link = join(root, "output-link");
await symlink(
retained,
link,
process.platform === "win32" ? "junction" : "dir",
);
for (const outputDirectory of [retained, link]) {
const response = await cli.run({
arguments: [
"export-web-scripts",
capturePath,
outputDirectory,
"--json",
],
environment: { REA_LOG_LEVEL: "silent" },
});
expect(response.exitCode).toBe(1);
expect(response.json).toMatchObject({
code: "invalid_request",
category: "invalid_input",
recovery: {
kind: "inspect_input_contract",
operation: "export_web_scripts",
},
details: {
operation: "export_web_scripts",
issues: [
{
path: ["outputDirectory"],
reason: "invalid_value",
message: "Output root already exists",
},
],
},
});
}
expect(await readdir(retained)).toEqual(["marker.txt"]);
},
);