mirror of
https://github.com/morluto/rea.git
synced 2026-11-29 01:46:39 +00:00
* refactor(cli,server): break remaining module-layer cycles * fix(ci): follow relocated CLI entry points * refactor: remove retired internal reader exports
325 lines
10 KiB
TypeScript
325 lines
10 KiB
TypeScript
import { registerCliCommand } from "../../../src/cli/commandRegistry.js";
|
|
import { createCommandCli } from "../../support/cli/commandCli.js";
|
|
import { Writable } from "node:stream";
|
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import { parseCliOutputArguments } from "../../../src/cli/cliOutput.js";
|
|
import { createCliResultOutput } from "../../../src/cli/cliResultOutput.js";
|
|
import {
|
|
cliOperationFailureIdentity,
|
|
cliOperationFailurePayload,
|
|
logCliCommand,
|
|
} from "../../../src/cliLogging.js";
|
|
import { ArtifactOperationError } from "../../../src/domain/artifactOperationError.js";
|
|
import { silentLogger } from "../../../src/logger.js";
|
|
import {
|
|
cliEvidencePublicationReceipt,
|
|
recordCliEvidencePublication,
|
|
withCliEvidencePublication,
|
|
} from "../../../src/cli/cliEvidencePublication.js";
|
|
|
|
const metadata = { command: "fixture", duration: "1ms" };
|
|
|
|
/** Fail at the formatter boundary after the command has returned its result. */
|
|
const formattingFailureValue = (
|
|
cause = new RangeError("Invalid string length"),
|
|
) =>
|
|
new Proxy(
|
|
{},
|
|
{
|
|
ownKeys: () => {
|
|
throw cause;
|
|
},
|
|
},
|
|
);
|
|
|
|
it("keeps receipts isolated across concurrent invocations", async () => {
|
|
const results = await Promise.all(
|
|
["first", "second"].map((path) =>
|
|
withCliEvidencePublication(async () => {
|
|
recordCliEvidencePublication({ path, bytes: 1 });
|
|
await Promise.resolve();
|
|
return cliEvidencePublicationReceipt();
|
|
}),
|
|
),
|
|
);
|
|
expect(results).toEqual([
|
|
{ path: "first", bytes: 1 },
|
|
{ path: "second", bytes: 1 },
|
|
]);
|
|
expect(cliEvidencePublicationReceipt()).toBeUndefined();
|
|
});
|
|
|
|
it("retains a publication receipt after formatting recovery leaves the invocation context", async () => {
|
|
const chunks: Buffer[] = [];
|
|
const output = createCliResultOutput(
|
|
parseCliOutputArguments([]),
|
|
new Writable({
|
|
write(chunk: Buffer, _encoding, callback) {
|
|
chunks.push(chunk);
|
|
callback();
|
|
},
|
|
}),
|
|
);
|
|
if (output === undefined) throw new Error("Missing output owner");
|
|
await withCliEvidencePublication(async () => {
|
|
recordCliEvidencePublication({ path: "/tmp/result.json", bytes: 123 });
|
|
expect(await output.write(formattingFailureValue(), metadata)).toBe(true);
|
|
});
|
|
expect(cliEvidencePublicationReceipt()).toBeUndefined();
|
|
expect(
|
|
await output.recoverFormattingFailure(
|
|
new RangeError("Invalid string length"),
|
|
),
|
|
).toBe(false);
|
|
expect(output.error).toMatchObject({
|
|
code: "resource_constraint",
|
|
details: {
|
|
diagnostics: {
|
|
evidence_bundle: { path: "/tmp/result.json", bytes: 123 },
|
|
},
|
|
},
|
|
});
|
|
expect(Buffer.concat(chunks).toString("utf8")).toContain("/tmp/result.json");
|
|
});
|
|
|
|
it.each([
|
|
{ format: "md", value: "\u0000", window: false },
|
|
{ format: "toon", value: "\ud800", window: false },
|
|
{ format: "toon", value: "\ud800", window: true },
|
|
] as const)(
|
|
"retains the committed receipt when $format cannot encode output (token window: $window)",
|
|
async ({ format, value, window }) => {
|
|
const chunks: Buffer[] = [];
|
|
const receipt = { path: "/authored/retained.json", bytes: 123 };
|
|
const output = createCliResultOutput(
|
|
parseCliOutputArguments([
|
|
"--format",
|
|
format,
|
|
...(window ? ["--token-limit", "1000"] : []),
|
|
]),
|
|
new Writable({
|
|
write(chunk: Buffer, _encoding, callback) {
|
|
chunks.push(chunk);
|
|
callback();
|
|
},
|
|
}),
|
|
);
|
|
if (output === undefined) throw new Error("Missing output owner");
|
|
await withCliEvidencePublication(async () => {
|
|
recordCliEvidencePublication(receipt);
|
|
expect(await output.write({ value }, metadata)).toBe(true);
|
|
});
|
|
expect(output.error).toMatchObject({
|
|
code: "invalid_request",
|
|
details: { diagnostics: { evidence_bundle: receipt } },
|
|
});
|
|
expect(Buffer.concat(chunks).toString("utf8")).toContain(receipt.path);
|
|
},
|
|
);
|
|
|
|
describe("CLI formatting stage admission", () => {
|
|
it("captures thrown integrity failures without re-reading diagnostics during recovery", async () => {
|
|
const previousExitCode = process.exitCode;
|
|
try {
|
|
const value = await logCliCommand(silentLogger, "fixture", async () => {
|
|
throw new ArtifactOperationError("inspect_artifact", "integrity", {
|
|
logicalPath: "entry.js",
|
|
declaredSha256: "a".repeat(64),
|
|
calculatedSha256: "b".repeat(64),
|
|
unpacked: false,
|
|
});
|
|
});
|
|
// A second traversal would be observable here. The producer already
|
|
// classified this exact projection before the formatter sees it.
|
|
Object.defineProperty(value, "details", {
|
|
get() {
|
|
throw new Error("Diagnostics were interpreted again");
|
|
},
|
|
});
|
|
expect(cliOperationFailurePayload(value)).toBe(value);
|
|
expect(cliOperationFailureIdentity(value)).toEqual({
|
|
code: "artifact_integrity_mismatch",
|
|
category: "integrity_mismatch",
|
|
retryable: false,
|
|
});
|
|
} finally {
|
|
process.exitCode = previousExitCode;
|
|
}
|
|
});
|
|
|
|
it("declines errors before a result or from a different failure class", async () => {
|
|
const output = createCliResultOutput(
|
|
parseCliOutputArguments([]),
|
|
new Writable({
|
|
write(_chunk, _encoding, done) {
|
|
done();
|
|
},
|
|
}),
|
|
);
|
|
expect(output).toBeDefined();
|
|
expect(
|
|
await output?.recoverFormattingFailure(
|
|
new RangeError("Invalid string length"),
|
|
),
|
|
).toBe(false);
|
|
await output?.write({ result: "completed" }, metadata);
|
|
for (const cause of [
|
|
new Error("Invalid string length"),
|
|
new RangeError("Maximum call stack size exceeded"),
|
|
new Error("operation failed"),
|
|
])
|
|
expect(await output?.recoverFormattingFailure(cause)).toBe(false);
|
|
expect(output?.handled).toBe(true);
|
|
});
|
|
|
|
it("keeps command RangeErrors distinct from post-result formatting capacity failures", async () => {
|
|
const output = createCliResultOutput(
|
|
parseCliOutputArguments([]),
|
|
new Writable(),
|
|
);
|
|
if (output === undefined) throw new Error("Missing output owner");
|
|
const cli = createCommandCli(output);
|
|
registerCliCommand(cli, "fixture", {
|
|
run: () => {
|
|
throw new RangeError("Invalid string length");
|
|
},
|
|
});
|
|
let text = "";
|
|
let status = 0;
|
|
await cli.serve(["fixture"], {
|
|
stdout: (part) => {
|
|
text += part;
|
|
},
|
|
exit: (code) => {
|
|
status = code;
|
|
},
|
|
});
|
|
expect(status).toBe(1);
|
|
expect(text).toContain("UNKNOWN");
|
|
expect(text).not.toContain("resource_constraint");
|
|
expect(output.error).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("CLI formatting recovery outcomes", () => {
|
|
it("formats a recovered Markdown result through the CLI Markdown owner", async () => {
|
|
const cause = new RangeError("Invalid string length");
|
|
const chunks: Buffer[] = [];
|
|
const output = createCliResultOutput(
|
|
parseCliOutputArguments(["--format", "md"]),
|
|
new Writable({
|
|
write(chunk: Buffer, _encoding, callback) {
|
|
chunks.push(chunk);
|
|
callback();
|
|
},
|
|
}),
|
|
);
|
|
const value = new Proxy(
|
|
{},
|
|
{
|
|
ownKeys: () => {
|
|
throw cause;
|
|
},
|
|
},
|
|
);
|
|
|
|
expect(await output?.write(value, { ...metadata })).toBe(true);
|
|
expect(output?.error).toMatchObject({ code: "resource_constraint" });
|
|
const text = Buffer.concat(chunks).toString("utf8");
|
|
expect(text).toContain("resource_constraint");
|
|
expect(text).toContain("original result was not written");
|
|
expect(output?.failed).toBe(false);
|
|
});
|
|
|
|
it("retains ownership if the recovery document cannot be written", async () => {
|
|
const cause = new RangeError("Invalid string length");
|
|
const output = createCliResultOutput(
|
|
parseCliOutputArguments([]),
|
|
new Writable({
|
|
write(_chunk, _encoding, callback) {
|
|
callback(cause);
|
|
},
|
|
}),
|
|
);
|
|
await expect(
|
|
output?.write(formattingFailureValue(cause), metadata),
|
|
).rejects.toBe(cause);
|
|
expect(output?.handled).toBe(true);
|
|
expect(output?.failed).toBe(true);
|
|
expect(await output?.recoverFormattingFailure(cause)).toBe(false);
|
|
});
|
|
|
|
it("does not replace a failed destination, even for the same RangeError", async () => {
|
|
const cause = new RangeError("Invalid string length");
|
|
const output = createCliResultOutput(
|
|
parseCliOutputArguments(["--json"]),
|
|
new Writable({
|
|
write(_chunk, _encoding, callback) {
|
|
callback(cause);
|
|
},
|
|
}),
|
|
);
|
|
await expect(
|
|
output?.write({ result: "completed" }, { ...metadata }),
|
|
).rejects.toBe(cause);
|
|
expect(output?.handled).toBe(true);
|
|
expect(output?.failed).toBe(true);
|
|
expect(await output?.recoverFormattingFailure(cause)).toBe(false);
|
|
});
|
|
|
|
it("retains the original failure identity and ignores large details on recovery", async () => {
|
|
const chunks: Buffer[] = [];
|
|
const output = createCliResultOutput(
|
|
parseCliOutputArguments([]),
|
|
new Writable({
|
|
write(chunk: Buffer, _encoding, callback) {
|
|
chunks.push(chunk);
|
|
callback();
|
|
},
|
|
}),
|
|
);
|
|
const previousExitCode = process.exitCode;
|
|
try {
|
|
const value = await logCliCommand(silentLogger, "fixture", async () => ({
|
|
code: "target_unavailable",
|
|
category: "unavailable",
|
|
message: "The operation did not complete.",
|
|
retryable: false,
|
|
remediation: { action: "Check the selected path." },
|
|
details: { partial_observation: { text: "captured" } },
|
|
}));
|
|
Object.defineProperty(value, "details", {
|
|
get() {
|
|
throw new RangeError("Invalid string length");
|
|
},
|
|
});
|
|
expect(await output?.write(value, metadata)).toBe(true);
|
|
expect(output?.error).toMatchObject({
|
|
code: "resource_constraint",
|
|
details: {
|
|
resource: "memory",
|
|
reported_limits: {
|
|
formatted_characters: null,
|
|
original_failure: {
|
|
code: "target_unavailable",
|
|
category: "unavailable",
|
|
},
|
|
},
|
|
},
|
|
});
|
|
const text = Buffer.concat(chunks).toString("utf8");
|
|
expect(text).toContain("--format json");
|
|
expect(text).not.toContain("partial_observation");
|
|
expect(
|
|
await output?.recoverFormattingFailure(
|
|
new RangeError("Invalid string length"),
|
|
),
|
|
).toBe(false);
|
|
} finally {
|
|
process.exitCode = previousExitCode;
|
|
}
|
|
});
|
|
});
|