mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
Replace bare toThrow() across the MCP, CLI and other boundary tests, the Hopper and fixture conformance verifiers, the evaluation guards and the process-global tests with the SDK cancellation code and reason, schema issue paths, tagged error reasons, cleanup facts, errno codes and verifier messages each case is meant to refuse.
325 lines
10 KiB
TypeScript
325 lines
10 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { chmod, mkdir, readFile, realpath, writeFile } from "node:fs/promises";
|
|
import { delimiter, join } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
import { expect, it, onTestFinished } from "vitest";
|
|
|
|
import {
|
|
parseConformanceFixtureManifest,
|
|
splitNmSymbolRecords,
|
|
} from "../../../scripts/lib/conformance-fixtures.mjs";
|
|
import { createVerifierCommands } from "../../../scripts/lib/verify-commands.mjs";
|
|
import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js";
|
|
import { runningTestDeadline } from "../../support/runningTestDeadline.js";
|
|
import { waitFor } from "../../support/testWait.js";
|
|
import { removeTestWorkspace } from "../../support/workspace/workspaceFixture.js";
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
|
|
const digest = (bytes: string | Buffer) =>
|
|
createHash("sha256").update(bytes).digest("hex");
|
|
const verifier = fileURLToPath(
|
|
new URL("../../../scripts/verify-conformance-fixtures.mjs", import.meta.url),
|
|
);
|
|
|
|
const ownedFixture = async (prefix: string) => {
|
|
const root = await createTestTempDirectory(prefix);
|
|
const commands = await createVerifierCommands();
|
|
// Registered after the directory helper so stack order closes commands first.
|
|
onTestFinished(() => commands.close());
|
|
return { commands, root };
|
|
};
|
|
|
|
for (const [prefix, count] of [
|
|
["symbolPrefix", "symbolCount"],
|
|
["stringPrefix", "stringCount"],
|
|
] as const) {
|
|
const unpaired = `must carry ${prefix} and ${count} together`;
|
|
const invalid = `Invalid conformance fixture ${prefix}/${count} expectation`;
|
|
it.each([
|
|
{ name: "omitted", expectation: {} },
|
|
{
|
|
name: "zero",
|
|
expectation: { [prefix]: "fixture_", [count]: 0 },
|
|
},
|
|
{
|
|
name: "positive",
|
|
expectation: { [prefix]: "fixture_", [count]: 3 },
|
|
},
|
|
{
|
|
name: "empty prefix",
|
|
expectation: { [prefix]: "", [count]: 0 },
|
|
},
|
|
{
|
|
name: "prefix only",
|
|
expectation: { [prefix]: "fixture_" },
|
|
failure: unpaired,
|
|
},
|
|
{ name: "count only", expectation: { [count]: 0 }, failure: unpaired },
|
|
{
|
|
name: "null prefix",
|
|
expectation: { [prefix]: null, [count]: 0 },
|
|
failure: invalid,
|
|
},
|
|
{
|
|
name: "numeric prefix",
|
|
expectation: { [prefix]: 1, [count]: 0 },
|
|
failure: invalid,
|
|
},
|
|
{
|
|
name: "array prefix",
|
|
expectation: { [prefix]: [], [count]: 0 },
|
|
failure: invalid,
|
|
},
|
|
{
|
|
name: "null count",
|
|
expectation: { [prefix]: "fixture_", [count]: null },
|
|
failure: invalid,
|
|
},
|
|
{
|
|
name: "negative count",
|
|
expectation: { [prefix]: "fixture_", [count]: -1 },
|
|
failure: invalid,
|
|
},
|
|
{
|
|
name: "fractional count",
|
|
expectation: { [prefix]: "fixture_", [count]: 0.5 },
|
|
failure: invalid,
|
|
},
|
|
{
|
|
name: "string count",
|
|
expectation: { [prefix]: "fixture_", [count]: "0" },
|
|
failure: invalid,
|
|
},
|
|
{
|
|
name: "nonfinite count",
|
|
expectation: { [prefix]: "fixture_", [count]: Infinity },
|
|
failure: invalid,
|
|
},
|
|
{
|
|
name: "unsafe count",
|
|
expectation: {
|
|
[prefix]: "fixture_",
|
|
[count]: Number.MAX_SAFE_INTEGER + 1,
|
|
},
|
|
failure: invalid,
|
|
},
|
|
])(`validates ${prefix}/${count}: $name`, ({ expectation, failure }) => {
|
|
const manifest = {
|
|
schemaVersion: 2,
|
|
platform: "linux",
|
|
fixtures: [{ expectations: expectation }],
|
|
};
|
|
if (failure === undefined)
|
|
expect(parseConformanceFixtureManifest(manifest)).toBe(manifest);
|
|
else
|
|
expect(() => parseConformanceFixtureManifest(manifest)).toThrow(failure);
|
|
});
|
|
}
|
|
|
|
it.skipIf(process.platform !== "darwin" && process.platform !== "linux").each([
|
|
{ mode: "complete", marker: "REA_C_ENTRY" },
|
|
{ mode: "cancelled", marker: "REA_C_ENTRY" },
|
|
{ mode: "renamed", marker: "REA_C_ENTRY" },
|
|
{ mode: "wrong-prefix", marker: "REA_C_ENTRY" },
|
|
{ mode: "prefix-complete", marker: "REA_C_ENTRY" },
|
|
{ mode: "half-count", marker: "REA_C_ENTRY" },
|
|
{ mode: "marker", marker: "REA_C_ENTRY" },
|
|
{ mode: "fragment", marker: "performAction:error:" },
|
|
])(
|
|
"verifies compiler-produced fixture records: $mode / $marker",
|
|
{ tags: ["long-running"] },
|
|
async ({ mode, marker }) => {
|
|
const { commands, root } = await ownedFixture("rea-fixture-symbols-");
|
|
const sources = join(root, "sources");
|
|
const output = join(root, "output");
|
|
await mkdir(sources);
|
|
await mkdir(output);
|
|
const fixtures = [];
|
|
const prefix = process.platform === "darwin" ? "_" : "";
|
|
for (const version of [1, 2]) {
|
|
const name = `version-v${version}`;
|
|
const path = `${name}.c`;
|
|
const artifact = `${name}.o`;
|
|
const suffix = mode === "renamed" ? "_renamed" : "";
|
|
const literal =
|
|
mode === "marker"
|
|
? `${marker}_renamed`
|
|
: mode === "fragment"
|
|
? `embedded_${marker}_suffix`
|
|
: marker;
|
|
const counted = mode === "prefix-complete" ? "" : "unrelated_";
|
|
const content = `int rea_version_entry${suffix}(void) { return ${version}; }\nint ${counted}rea_inventory_0000(void) { return 0; }\nconst char *marker = "${literal}";\n`;
|
|
await writeFile(join(sources, path), content);
|
|
await commands.exec(
|
|
"cc",
|
|
["-c", join(sources, path), "-o", join(output, artifact)],
|
|
{ timeout: runningTestDeadline() },
|
|
);
|
|
const bytes = await readFile(join(output, artifact));
|
|
fixtures.push({
|
|
name,
|
|
artifact,
|
|
artifactSha256: digest(bytes),
|
|
sources: [{ path, sha256: digest(content) }],
|
|
sourceSha256: digest(`${path}\0${digest(content)}\n`),
|
|
expectations: {
|
|
symbols: [`${prefix}rea_version_entry`],
|
|
...(mode === "fragment"
|
|
? { stringFragments: [marker] }
|
|
: { stringMarkers: [marker] }),
|
|
...(mode === "wrong-prefix" ||
|
|
mode === "prefix-complete" ||
|
|
mode === "half-count"
|
|
? {
|
|
symbolPrefix: `${prefix}rea_inventory_`,
|
|
// A half-declared pair must be refused before verification.
|
|
...(mode === "half-count" ? {} : { symbolCount: 1 }),
|
|
}
|
|
: {}),
|
|
},
|
|
});
|
|
}
|
|
const manifest = join(output, "manifest.json");
|
|
await writeFile(
|
|
manifest,
|
|
JSON.stringify({
|
|
schemaVersion: 2,
|
|
platform: process.platform,
|
|
fixtures,
|
|
}),
|
|
);
|
|
if (mode === "cancelled") {
|
|
const helper = join(root, "file");
|
|
const ready = join(root, "file-started");
|
|
await writeFile(
|
|
helper,
|
|
`#!/usr/bin/env node
|
|
const { writeFileSync } = require("node:fs");
|
|
writeFileSync(process.env.CONFORMANCE_FILE_STARTED, String(process.pid));
|
|
setTimeout(() => {}, 15_000);
|
|
`,
|
|
);
|
|
await chmod(helper, 0o755);
|
|
const controller = new AbortController();
|
|
const pending = commands.exec(
|
|
process.execPath,
|
|
[verifier, manifest, sources],
|
|
{
|
|
cwd: root,
|
|
env: {
|
|
...process.env,
|
|
PATH: [root, process.env.PATH ?? ""].join(delimiter),
|
|
CONFORMANCE_FILE_STARTED: ready,
|
|
},
|
|
signal: controller.signal,
|
|
timeout: runningTestDeadline(),
|
|
},
|
|
);
|
|
await awaitAcknowledgement(
|
|
waitFor(() => readFile(ready)),
|
|
pending,
|
|
"conformance file probe",
|
|
);
|
|
controller.abort();
|
|
await expect(pending).rejects.toMatchObject({
|
|
reason: "cancelled",
|
|
cleanupFailure: null,
|
|
});
|
|
return;
|
|
}
|
|
const result = commands.exec(
|
|
process.execPath,
|
|
[verifier, manifest, sources],
|
|
{
|
|
cwd: root,
|
|
timeout: runningTestDeadline(),
|
|
},
|
|
);
|
|
if (!["complete", "fragment", "prefix-complete"].includes(mode))
|
|
await expect(result).rejects.toMatchObject({
|
|
code: 1,
|
|
snapshot: {
|
|
stderr: {
|
|
text: expect.stringContaining(
|
|
mode === "renamed"
|
|
? `missing symbol ${prefix}rea_version_entry`
|
|
: mode === "wrong-prefix"
|
|
? `expected 1 ${prefix}rea_inventory_ records, received 0`
|
|
: mode === "half-count"
|
|
? "must carry symbolPrefix and symbolCount together"
|
|
: `missing string marker ${marker}`,
|
|
),
|
|
},
|
|
},
|
|
});
|
|
else {
|
|
const completed = await result;
|
|
expect(completed.stderr).toBe("");
|
|
expect(JSON.parse(completed.stdout)).toMatchObject({ verified: 2 });
|
|
}
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
{ schemaVersion: undefined, expectations: {} },
|
|
{ schemaVersion: 1, expectations: {} },
|
|
{ schemaVersion: 2, expectations: { strings: ["ambiguous"] } },
|
|
])("refuses retired manifest semantics: %j", async (legacy) => {
|
|
const { commands, root } = await ownedFixture(
|
|
"rea-fixture-manifest-version-",
|
|
);
|
|
const manifest = join(root, "manifest.json");
|
|
await writeFile(
|
|
manifest,
|
|
JSON.stringify({
|
|
schemaVersion: legacy.schemaVersion,
|
|
platform: "linux",
|
|
fixtures: [{ expectations: legacy.expectations }],
|
|
}),
|
|
);
|
|
await expect(
|
|
commands.exec(process.execPath, [verifier, manifest, root], {
|
|
cwd: root,
|
|
timeout: runningTestDeadline(),
|
|
}),
|
|
).rejects.toMatchObject({
|
|
code: 1,
|
|
snapshot: {
|
|
stderr: {
|
|
text: expect.stringContaining(
|
|
legacy.schemaVersion === 2
|
|
? "Invalid conformance fixture string expectations"
|
|
: "Unsupported conformance manifest",
|
|
),
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
it("preserves a carriage return that belongs to an nm symbol name", () => {
|
|
expect(splitNmSymbolRecords("rea_entry\r\nrea_inventory_0000\r\n")).toEqual([
|
|
"rea_entry\r",
|
|
"rea_inventory_0000\r",
|
|
]);
|
|
});
|
|
|
|
it("refuses a missing explicit source root even when a default exists", async () => {
|
|
const { commands, root } = await ownedFixture("rea-fixture-source-root-");
|
|
await mkdir(join(root, "tests", "conformance"), { recursive: true });
|
|
const manifest = join(root, "manifest.json");
|
|
await writeFile(manifest, "{}");
|
|
const selected = join(root, "selected-sources");
|
|
await expect(
|
|
commands.exec(process.execPath, [verifier, manifest, selected], {
|
|
cwd: root,
|
|
timeout: 10_000,
|
|
}),
|
|
).rejects.toMatchObject({
|
|
code: 1,
|
|
snapshot: {
|
|
stderr: { text: expect.stringContaining(selected) },
|
|
},
|
|
});
|
|
});
|