test(process): use owned CLI and workspace fixtures

This commit is contained in:
morluto
2026-10-10 01:45:01 +08:00
parent 74be52611e
commit 4940a867e2
+25 -38
View File
@@ -1,13 +1,14 @@
import { readFile, rm, writeFile } from "node:fs/promises"; import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path"; import { join } from "node:path";
import { fileURLToPath } from "node:url"; import { fileURLToPath } from "node:url";
import { execFile } from "node:child_process"; import { execFile } from "node:child_process";
import { promisify } from "node:util"; import { promisify } from "node:util";
import { afterEach, describe, expect, it } from "vitest"; import { describe, expect, it } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { cliTest } from "../../support/cli/cliFixture.js";
import { import {
captureProcessScenarioFile, captureProcessScenarioFile,
@@ -19,27 +20,14 @@ import { ProcessCaptureError } from "../../../src/process/capture/ProcessCapture
import { PROCESS_PROVIDER } from "../../../src/domain/process/processEvidenceProvider.js"; import { PROCESS_PROVIDER } from "../../../src/domain/process/processEvidenceProvider.js";
import { INVESTIGATION_EXAMPLES } from "../../../src/contracts/investigationExamples.js"; import { INVESTIGATION_EXAMPLES } from "../../../src/contracts/investigationExamples.js";
const roots: string[] = [];
const execFileAsync = promisify(execFile); const execFileAsync = promisify(execFile);
const CLI_INTEGRATION_TIMEOUT_MS = 60_000; const CLI_INTEGRATION_TIMEOUT_MS = 60_000;
afterEach(async () => {
await Promise.all(
roots.splice(0).map((root) => rm(root, { recursive: true, force: true })),
);
});
const fixture = async (): Promise<string> => {
const root = await createTestTempDirectory("rea-process-cli-");
roots.push(root);
return root;
};
describe("documented process CLI workflow", () => { describe("documented process CLI workflow", () => {
it( cliTest(
"feeds the documented JSON capture into the comparison CLI", "feeds the documented JSON capture into the comparison CLI",
async () => { async ({ cli }) => {
const root = await fixture(); const root = await createTestTempDirectory("rea-process-cli-");
const guide = await readFile( const guide = await readFile(
new URL("../../../docs/process-capture.md", import.meta.url), new URL("../../../docs/process-capture.md", import.meta.url),
"utf8", "utf8",
@@ -62,24 +50,23 @@ describe("documented process CLI workflow", () => {
arguments: ["-e", "process.stdout.write('documented-capture')"], arguments: ["-e", "process.stdout.write('documented-capture')"],
}), }),
); );
const cli = fileURLToPath( const capture = await cli.run({
new URL("../../../scripts/rea.mjs", import.meta.url), arguments: ["capture-process", ...args],
); cwd: root,
const capture = await execFileAsync( });
process.execPath, expect(capture.exitCode, capture.stdout || capture.stderr).toBe(0);
[cli, "capture-process", ...args], expect(capture.json).toMatchObject({
{ cwd: root },
);
expect(JSON.parse(capture.stdout)).toMatchObject({
operation: "capture_process_scenario", operation: "capture_process_scenario",
}); });
await writeFile(join(root, output), capture.stdout); await writeFile(join(root, output), capture.stdout);
const comparison = await execFileAsync( const comparison = await cli.run({
process.execPath, arguments: ["compare-process-captures", output, output, "--json"],
[cli, "compare-process-captures", output, output, "--json"], cwd: root,
{ cwd: root }, });
expect(comparison.exitCode, comparison.stdout || comparison.stderr).toBe(
0,
); );
expect(JSON.parse(comparison.stdout)).toMatchObject({ expect(comparison.json).toMatchObject({
operation: "compare_process_captures", operation: "compare_process_captures",
}); });
}, },
@@ -106,7 +93,7 @@ describe("process CLI errors", () => {
); );
it("rejects NUL arguments before process launch", async () => { it("rejects NUL arguments before process launch", async () => {
const root = await fixture(); const root = await createTestTempDirectory("rea-process-cli-");
const scenario = join(root, "nul-argument.json"); const scenario = join(root, "nul-argument.json");
await writeFile( await writeFile(
scenario, scenario,
@@ -135,7 +122,7 @@ describe("process CLI errors", () => {
describe("process CLI environment key diagnostics", () => { describe("process CLI environment key diagnostics", () => {
it("reports the reserved process environment key constraint", async () => { it("reports the reserved process environment key constraint", async () => {
const root = await fixture(); const root = await createTestTempDirectory("rea-process-cli-");
const scenario = join(root, "reserved-environment.json"); const scenario = join(root, "reserved-environment.json");
await writeFile( await writeFile(
scenario, scenario,
@@ -190,7 +177,7 @@ describe("process CLI evidence validation", () => {
}); });
it("captures the minimal executable-and-arguments scenario", async () => { it("captures the minimal executable-and-arguments scenario", async () => {
const root = await fixture(); const root = await createTestTempDirectory("rea-process-cli-");
const scenario = join(root, "scenario.json"); const scenario = join(root, "scenario.json");
await writeFile( await writeFile(
scenario, scenario,
@@ -208,7 +195,7 @@ describe("process CLI evidence validation", () => {
}); });
it("rejects unrelated capture evidence", async () => { it("rejects unrelated capture evidence", async () => {
const root = await fixture(); const root = await createTestTempDirectory("rea-process-cli-");
const invalidCapture = join(root, "invalid-capture.json"); const invalidCapture = join(root, "invalid-capture.json");
const unrelated = join(root, "unrelated.json"); const unrelated = join(root, "unrelated.json");
await writeFile( await writeFile(
@@ -249,7 +236,7 @@ describe("process CLI evidence validation", () => {
}); });
it("classifies malformed capture evidence as invalid input", async () => { it("classifies malformed capture evidence as invalid input", async () => {
const root = await fixture(); const root = await createTestTempDirectory("rea-process-cli-");
const malformed = join(root, "malformed-evidence.json"); const malformed = join(root, "malformed-evidence.json");
await writeFile(malformed, "{}"); await writeFile(malformed, "{}");
@@ -275,7 +262,7 @@ describe("process CLI evidence validation", () => {
it( it(
"reports the JSON depth constraint through the compiled comparison CLI", "reports the JSON depth constraint through the compiled comparison CLI",
async () => { async () => {
const root = await fixture(); const root = await createTestTempDirectory("rea-process-cli-");
const left = join(root, "left.json"); const left = join(root, "left.json");
const right = join(root, "right.json"); const right = join(root, "right.json");
const input = INVESTIGATION_EXAMPLES.compare_process_captures.input; const input = INVESTIGATION_EXAMPLES.compare_process_captures.input;