test: overhaul deterministic suite architecture
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
import { execFile, spawn, type ChildProcess } from "node:child_process";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
completeVerifierRun,
|
||||
createVerifierRun,
|
||||
} from "../../scripts/lib/verifier-run.mjs";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
describe.sequential("verifier run identity", () => {
|
||||
it("allocates, propagates, and reuses one process-local identity", async () => {
|
||||
const previousRunId = process.env.REA_PROCESS_RUN_ID;
|
||||
process.env.REA_PROCESS_RUN_ID = "11111111-1111-4111-8111-111111111111";
|
||||
try {
|
||||
const first = createVerifierRun();
|
||||
const nested = createVerifierRun();
|
||||
|
||||
expect(first).toEqual({
|
||||
schema_version: 1,
|
||||
run_id: expect.stringMatching(
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/u,
|
||||
),
|
||||
verifier_pid: process.pid,
|
||||
parent_pid: process.ppid,
|
||||
});
|
||||
expect(nested.run_id).toBe(first.run_id);
|
||||
expect(first.run_id).not.toBe("11111111-1111-4111-8111-111111111111");
|
||||
expect(process.env.REA_PROCESS_RUN_ID).toBe(first.run_id);
|
||||
const { stdout } = await execFileAsync(process.execPath, [
|
||||
"-e",
|
||||
"process.stdout.write(process.env.REA_PROCESS_RUN_ID ?? '')",
|
||||
]);
|
||||
expect(stdout).toBe(first.run_id);
|
||||
const { stdout: nestedProcessRunId } = await execFileAsync(
|
||||
process.execPath,
|
||||
[
|
||||
"--input-type=module",
|
||||
"-e",
|
||||
"import { createVerifierRun } from './scripts/lib/verifier-run.mjs'; process.stdout.write(createVerifierRun().run_id)",
|
||||
],
|
||||
);
|
||||
expect(nestedProcessRunId).not.toBe(first.run_id);
|
||||
} finally {
|
||||
restoreRunId(previousRunId);
|
||||
}
|
||||
});
|
||||
|
||||
it.runIf(process.platform !== "win32")(
|
||||
"reports token-verified live descendants",
|
||||
async () => {
|
||||
const previousRunId = process.env.REA_PROCESS_RUN_ID;
|
||||
delete process.env.REA_PROCESS_RUN_ID;
|
||||
const run = createVerifierRun();
|
||||
const child = spawnReadyChild(process.env);
|
||||
try {
|
||||
await childReady(child);
|
||||
const completed = await completeVerifierRun(run);
|
||||
expect(completed.process_lineage).toMatchObject({
|
||||
status: "verified",
|
||||
schema_version: 1,
|
||||
observed_at: expect.stringMatching(/Z$/u),
|
||||
launcher_pid: process.pid,
|
||||
launcher_parent_pid: process.ppid,
|
||||
process_group_id: expect.any(Number),
|
||||
descendants: expect.arrayContaining([
|
||||
{
|
||||
pid: child.pid,
|
||||
parent_pid: process.pid,
|
||||
process_group_id: expect.any(Number),
|
||||
},
|
||||
]),
|
||||
});
|
||||
} finally {
|
||||
await stopChild(child);
|
||||
restoreRunId(previousRunId);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
it.runIf(process.platform !== "win32")(
|
||||
"reports unavailable when a live descendant lacks the run token",
|
||||
async () => {
|
||||
const previousRunId = process.env.REA_PROCESS_RUN_ID;
|
||||
delete process.env.REA_PROCESS_RUN_ID;
|
||||
const run = createVerifierRun();
|
||||
const child = spawnReadyChild({ ...process.env, REA_PROCESS_RUN_ID: "" });
|
||||
try {
|
||||
await childReady(child);
|
||||
const completed = await completeVerifierRun(run);
|
||||
expect(completed.process_lineage).toMatchObject({
|
||||
status: "unavailable",
|
||||
observed_at: expect.stringMatching(/Z$/u),
|
||||
launcher_pid: process.pid,
|
||||
launcher_parent_pid: process.ppid,
|
||||
process_group_id: expect.any(Number),
|
||||
reason: `descendant ${String(child.pid)} run token did not match`,
|
||||
});
|
||||
} finally {
|
||||
await stopChild(child);
|
||||
restoreRunId(previousRunId);
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
const spawnReadyChild = (env: NodeJS.ProcessEnv): ChildProcess =>
|
||||
spawn(
|
||||
process.execPath,
|
||||
["-e", "process.stdout.write('ready'); setInterval(() => {}, 1000)"],
|
||||
{ env, stdio: ["ignore", "pipe", "ignore"] },
|
||||
);
|
||||
|
||||
const childReady = async (child: ChildProcess): Promise<void> => {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
child.once("error", reject);
|
||||
child.stdout?.once("data", () => resolve());
|
||||
child.once("exit", (code) =>
|
||||
reject(new Error(`verifier child exited early with ${String(code)}`)),
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
const stopChild = async (child: ChildProcess): Promise<void> => {
|
||||
if (child.exitCode !== null || child.signalCode !== null) return;
|
||||
const exited = new Promise<void>((resolve) =>
|
||||
child.once("exit", () => resolve()),
|
||||
);
|
||||
child.kill("SIGTERM");
|
||||
await exited;
|
||||
};
|
||||
|
||||
const restoreRunId = (runId: string | undefined): void => {
|
||||
if (runId === undefined) delete process.env.REA_PROCESS_RUN_ID;
|
||||
else process.env.REA_PROCESS_RUN_ID = runId;
|
||||
};
|
||||
@@ -0,0 +1,123 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { readdir, readFile } from "node:fs/promises";
|
||||
import { availableParallelism } from "node:os";
|
||||
import { join, relative, resolve } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import vitestConfiguration, {
|
||||
MAX_TEST_WORKERS,
|
||||
TEST_PROJECTS,
|
||||
} from "../../vitest.config.js";
|
||||
|
||||
const execute = promisify(execFile);
|
||||
const EXPECTED_PROJECTS = [
|
||||
"acceptance",
|
||||
"adapters",
|
||||
"boundary",
|
||||
"composition",
|
||||
"conformance",
|
||||
"domain",
|
||||
"evaluation",
|
||||
"process-global",
|
||||
"services",
|
||||
];
|
||||
const ALLOWED_DIRECT_TEMPORARY_ROOTS = new Set([
|
||||
"tests/support/workspace/workspaceFixture.ts",
|
||||
"tests/acceptance/setup/packageInstallWorkflow.test.ts",
|
||||
"tests/boundary/filesystem/temporaryDirectory.test.ts",
|
||||
"tests/boundary/filesystem/referenceSourceReader.test.ts",
|
||||
"tests/boundary/process/providerProcess.test.ts",
|
||||
]);
|
||||
|
||||
describe("Vitest project configuration", () => {
|
||||
it("keeps deterministic execution retry-free and locally quiet", () => {
|
||||
expect(vitestConfiguration.test?.coverage?.enabled).toBe(false);
|
||||
expect(vitestConfiguration.test?.reporters).toEqual(["default"]);
|
||||
expect(vitestConfiguration.test?.retry).toBe(0);
|
||||
expect(MAX_TEST_WORKERS).toBe(Math.min(4, availableParallelism()));
|
||||
expect(TEST_PROJECTS.map(({ name }) => name).sort()).toEqual(
|
||||
EXPECTED_PROJECTS,
|
||||
);
|
||||
});
|
||||
|
||||
it("classifies every deterministic test in exactly one project", async () => {
|
||||
const { stdout } = await execute(
|
||||
process.execPath,
|
||||
[
|
||||
resolve("node_modules/vitest/vitest.mjs"),
|
||||
"list",
|
||||
"--filesOnly",
|
||||
"--staticParse",
|
||||
],
|
||||
{ cwd: process.cwd(), maxBuffer: 4 * 1_024 * 1_024 },
|
||||
);
|
||||
const classified = parseProjects(stdout);
|
||||
const repositoryTests = [
|
||||
...(await testFiles("src")),
|
||||
...(await testFiles("tests")),
|
||||
].sort();
|
||||
|
||||
expect(
|
||||
[...classified.values()]
|
||||
.filter((owners) => owners.length > 1)
|
||||
.map((owners) => owners.join(", ")),
|
||||
).toEqual([]);
|
||||
expect([...classified.keys()].sort()).toEqual(repositoryTests);
|
||||
}, 20_000);
|
||||
|
||||
it("keeps direct temporary-root creation behind the workspace seam", async () => {
|
||||
const violations: string[] = [];
|
||||
for (const path of await repositoryTypeScriptFiles("tests")) {
|
||||
const source = await readFile(path, "utf8");
|
||||
if (
|
||||
/\b(?:mkdtemp|tmpdir)\s*\(/u.test(source) &&
|
||||
!ALLOWED_DIRECT_TEMPORARY_ROOTS.has(path)
|
||||
) {
|
||||
violations.push(path);
|
||||
}
|
||||
}
|
||||
expect(violations).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
const parseProjects = (output: string): Map<string, string[]> => {
|
||||
const classified = new Map<string, string[]>();
|
||||
for (const line of output.split("\n")) {
|
||||
const trimmed = line.trim();
|
||||
const separator = trimmed.indexOf("] ");
|
||||
if (!trimmed.startsWith("[") || separator < 2) continue;
|
||||
const project = trimmed.slice(1, separator);
|
||||
const path = trimmed.slice(separator + 2);
|
||||
if (path.length === 0) continue;
|
||||
const owners = classified.get(path) ?? [];
|
||||
owners.push(project);
|
||||
classified.set(path, owners);
|
||||
}
|
||||
return classified;
|
||||
};
|
||||
|
||||
const testFiles = async (root: string): Promise<string[]> => {
|
||||
const files: string[] = [];
|
||||
for (const entry of await readdir(root, { withFileTypes: true })) {
|
||||
const path = join(root, entry.name);
|
||||
if (entry.isDirectory()) files.push(...(await testFiles(path)));
|
||||
else if (entry.isFile() && entry.name.endsWith(".test.ts"))
|
||||
files.push(relative(process.cwd(), path));
|
||||
}
|
||||
return files;
|
||||
};
|
||||
|
||||
const repositoryTypeScriptFiles = async (root: string): Promise<string[]> => {
|
||||
const files: string[] = [];
|
||||
for (const entry of await readdir(root, { withFileTypes: true })) {
|
||||
const path = join(root, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
files.push(...(await repositoryTypeScriptFiles(path)));
|
||||
} else if (entry.isFile() && entry.name.endsWith(".ts")) {
|
||||
files.push(relative(process.cwd(), path));
|
||||
}
|
||||
}
|
||||
return files;
|
||||
};
|
||||
Reference in New Issue
Block a user