128 lines
6.0 KiB
JavaScript
128 lines
6.0 KiB
JavaScript
import { createHash } from "node:crypto";
|
|
import { spawnSync } from "node:child_process";
|
|
import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
|
|
import { resolve } from "node:path";
|
|
|
|
const phaseIndex = process.argv.indexOf("--phase");
|
|
const phase = phaseIndex >= 0 ? process.argv[phaseIndex + 1] : "green";
|
|
if (!["red", "green"].includes(phase)) throw new Error(`Unsupported phase: ${phase}`);
|
|
const runId = process.env.DADA_TDD_RUN_ID ?? `wp1-04-${phase}-${new Date().toISOString().replace(/[^0-9]/g, "")}`;
|
|
const runDirectory = resolve("artifacts", "tdd", runId);
|
|
const adminCase = "TDD-WP1-ADM-001-admin-auth-boundary";
|
|
const configCase = "TDD-WP1-CFG-001-secure-revision";
|
|
const directories = {
|
|
[adminCase]: resolve(runDirectory, "cases", adminCase),
|
|
[configCase]: resolve(runDirectory, "cases", configCase),
|
|
};
|
|
const playwrightDirectory = resolve(runDirectory, "playwright");
|
|
if (existsSync(runDirectory)) throw new Error(`Evidence run already exists: ${runId}`);
|
|
for (const directory of Object.values(directories)) mkdirSync(directory, { recursive: true });
|
|
|
|
const commandsToRun = phase === "red"
|
|
? [
|
|
["admin-integration", ["exec", "vitest", "run", "tests/integration/wp1-04-admin-auth.test.ts"]],
|
|
["config-integration", ["exec", "vitest", "run", "tests/integration/wp1-04-secure-config.test.ts"]],
|
|
["admin-api", ["exec", "vitest", "run", "tests/api/wp1-04-admin-auth.test.ts"]],
|
|
["admin-e2e", ["exec", "playwright", "test", "tests/e2e/admin-auth.spec.ts", "--config", "playwright.config.ts"]],
|
|
]
|
|
: [
|
|
["integration", ["test:integration"]],
|
|
["api", ["test:api"]],
|
|
["e2e", ["test:e2e"]],
|
|
["security", ["test:security"]],
|
|
["package", ["test:package"]],
|
|
["supervisor", ["exec", "dotnet", "run", "--project", "supervisor/Dada.Supervisor.Tests/Dada.Supervisor.Tests.csproj", "--configuration", "Release"]],
|
|
["tdd-trace", ["validate:tdd-trace"]],
|
|
];
|
|
const environment = {
|
|
...process.env,
|
|
DADA_EVIDENCE_DIR_ADMIN: directories[adminCase],
|
|
DADA_EVIDENCE_DIR_CONFIG: directories[configCase],
|
|
DADA_PLAYWRIGHT_OUTPUT_DIR: playwrightDirectory,
|
|
};
|
|
const commandResults = [];
|
|
for (const [name, args] of commandsToRun) {
|
|
const command = `pnpm ${args.join(" ")}`;
|
|
const started_at = new Date().toISOString();
|
|
const executable = process.env.ComSpec ?? "cmd.exe";
|
|
const execution = spawnSync(executable, ["/d", "/s", "/c", command], { encoding: "utf8", env: environment });
|
|
if (execution.stdout) process.stdout.write(execution.stdout);
|
|
if (execution.stderr) process.stderr.write(execution.stderr);
|
|
commandResults.push({ command, exit_code: execution.status ?? 1, finished_at: new Date().toISOString(), name, started_at });
|
|
}
|
|
|
|
function find(root, name) {
|
|
if (!existsSync(root)) return [];
|
|
return readdirSync(root).flatMap((entry) => {
|
|
const child = resolve(root, entry);
|
|
return statSync(child).isDirectory() ? find(child, name) : entry === name ? [child] : [];
|
|
});
|
|
}
|
|
if (phase === "green") {
|
|
const adminTrace = find(playwrightDirectory, "trace.zip")
|
|
.find((path) => path.replaceAll("\\", "/").includes("admin-auth"));
|
|
if (adminTrace) copyFileSync(adminTrace, resolve(directories[adminCase], "trace.zip"));
|
|
}
|
|
|
|
for (const directory of Object.values(directories)) {
|
|
writeFileSync(resolve(directory, "commands.json"), `${JSON.stringify({ commands: commandResults, phase, run_id: runId }, null, 2)}\n`);
|
|
}
|
|
const expectedEvidence = phase === "red"
|
|
? { [adminCase]: ["red-observation.json"], [configCase]: ["red-observation.json"] }
|
|
: {
|
|
[adminCase]: ["response.json", "db-diff.json", "external-calls.json", "trace.zip"],
|
|
[configCase]: ["config-result.json", "db-diff.json", "redaction.json"],
|
|
};
|
|
const commandState = phase === "red"
|
|
? commandResults.every((result) => result.exit_code !== 0)
|
|
: commandResults.every((result) => result.exit_code === 0);
|
|
if (phase === "red") {
|
|
for (const [testId, directory] of Object.entries(directories)) {
|
|
writeFileSync(resolve(directory, "red-observation.json"), `${JSON.stringify({
|
|
expected_failure: testId === adminCase ? "administrator boundary is absent" : "secure revision application is absent",
|
|
status: commandState ? "red_confirmed" : "failed",
|
|
}, null, 2)}\n`);
|
|
}
|
|
}
|
|
const manifest = {
|
|
path: "tasks.manifest.json",
|
|
sha256: createHash("sha256").update(readFileSync("tasks.manifest.json")).digest("hex").toUpperCase(),
|
|
};
|
|
const commit = spawnSync("git", ["rev-parse", "--short", "HEAD"], { encoding: "utf8" }).stdout.trim();
|
|
const dirty = spawnSync("git", ["status", "--porcelain"], { encoding: "utf8" }).stdout.trim().length > 0;
|
|
const results = Object.entries(directories).map(([testId, directory]) => {
|
|
const evidence_refs = expectedEvidence[testId];
|
|
const missing_evidence = evidence_refs.filter((file) => !existsSync(resolve(directory, file)));
|
|
const status = commandState && missing_evidence.length === 0 ? (phase === "red" ? "red_confirmed" : "passed") : "failed";
|
|
const result = {
|
|
acceptance_criteria: ["AC-49", "AC-50"],
|
|
automation: ["automated"],
|
|
commit,
|
|
evidence_refs,
|
|
layer: testId === adminCase ? ["API", "E2E", "DB", "SUPERVISOR"] : ["API", "DB", "SECURITY"],
|
|
manifest,
|
|
missing_evidence,
|
|
phase,
|
|
requirements: ["ADMIN-09", "AUTH-07"],
|
|
run_id: runId,
|
|
status,
|
|
task_id: "TASK-WP1-04",
|
|
test_id: testId,
|
|
work_package: "WP-1",
|
|
worktree_under_test: dirty ? "uncommitted implementation" : "clean committed implementation",
|
|
};
|
|
writeFileSync(resolve(directory, "result.json"), `${JSON.stringify(result, null, 2)}\n`);
|
|
return result;
|
|
});
|
|
const targetStatus = phase === "red" ? "red_confirmed" : "passed";
|
|
const passed = results.every((result) => result.status === targetStatus);
|
|
const summary = {
|
|
cases: results.map(({ missing_evidence, status, test_id }) => ({ missing_evidence, status, test_id })),
|
|
phase,
|
|
run_id: runId,
|
|
status: passed ? targetStatus : "failed",
|
|
};
|
|
writeFileSync(resolve(runDirectory, "evidence.json"), `${JSON.stringify(summary, null, 2)}\n`);
|
|
console.log(JSON.stringify(summary, null, 2));
|
|
if (!passed) process.exit(1);
|