| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- #!/usr/bin/env node
- import { existsSync, readFileSync, writeFileSync } from "node:fs";
- import { basename, isAbsolute, relative, resolve } from "node:path";
- import { insertVerifiedDockerSpecRow } from "./conversation-docker-plan.mjs";
- const desktopDir = resolve(import.meta.dirname, "..");
- const repoRoot = resolve(desktopDir, "../..");
- const conversationDir = resolve(desktopDir, "test/e2e/conversation-session");
- const providerFixtureDir = resolve(
- desktopDir,
- "test/e2e/fixtures/upstream/conversation-session",
- );
- const caseManifestDir = resolve(
- desktopDir,
- "test/e2e/fixtures/cases/conversation-session",
- );
- const containerScriptPath = resolve(
- repoRoot,
- "scripts/test-desktop-e2e-container.sh",
- );
- const dockerPlanPath = resolve(
- repoRoot,
- "docs/testing/conversation-session-docker-automation-plan.md",
- );
- const suiteAssignmentPattern =
- /^(CONVERSATION_SESSION_VERIFIED_E2E_SPEC=")([^"]*)(")$/m;
- const options = parseArgs(process.argv.slice(2));
- if (options.help || !options.spec) {
- printUsage();
- process.exit(options.help ? 0 : 1);
- }
- const result = admit(options);
- printResult(result);
- process.exit(result.errors.length > 0 ? 1 : 0);
- function parseArgs(args) {
- const parsed = {
- apply: false,
- artifact: "",
- help: false,
- spec: "",
- verified: false,
- };
- for (let index = 0; index < args.length; index += 1) {
- const arg = args[index];
- if (arg === "--" || arg.trim().length === 0) {
- continue;
- }
- if (arg === "--help" || arg === "-h") {
- parsed.help = true;
- continue;
- }
- if (arg === "--apply") {
- parsed.apply = true;
- continue;
- }
- if (arg === "--verified") {
- parsed.verified = true;
- continue;
- }
- if (arg === "--artifact") {
- parsed.artifact = args[index + 1] ?? "";
- index += 1;
- continue;
- }
- if (arg.startsWith("--artifact=")) {
- parsed.artifact = arg.slice("--artifact=".length);
- continue;
- }
- if (arg === "--spec") {
- parsed.spec = args[index + 1] ?? "";
- index += 1;
- continue;
- }
- if (arg.startsWith("--spec=")) {
- parsed.spec = arg.slice("--spec=".length);
- continue;
- }
- if (!parsed.spec) {
- parsed.spec = arg;
- continue;
- }
- }
- return parsed;
- }
- function printUsage() {
- console.log(`Usage:
- pnpm --filter @zcode/desktop e2e:docker:admit -- --spec ./test/e2e/conversation-session/<case>.test.ts --verified --artifact packages/desktop/.e2e-artifacts/<run-id> --apply
- Options:
- --apply Write changes. Without this flag the tool only prints a dry run.
- --verified Confirm the formal case already passed Docker replay-isolated.
- --artifact Optional artifact path from the passing Docker run.
- `);
- }
- function admit({ apply, artifact, spec, verified }) {
- const errors = [];
- const warnings = [];
- const actions = [];
- const specPath = resolveSpecPath(spec);
- const fileName = basename(specPath);
- const caseName = fileName.replace(/\.test\.ts$/, "");
- const specRelFromDesktop = `./test/e2e/conversation-session/${fileName}`;
- const providerFixturePath = resolve(providerFixtureDir, `${caseName}.json`);
- const caseManifestPath = resolve(caseManifestDir, `${caseName}.json`);
- const suite = readConversationDockerSuite({ errors });
- const alreadyInSuite = suite.specs.includes(specRelFromDesktop);
- const alreadyInPlan = dockerPlanIncludes(specRelFromDesktop);
- const relFromConversation = relative(conversationDir, specPath);
- if (relFromConversation.startsWith("..") || isAbsolute(relFromConversation)) {
- errors.push(
- `Spec must be under formal conversation-session dir: ${relative(repoRoot, specPath)}`,
- );
- }
- if (relFromConversation.includes("manual-review/")) {
- errors.push(
- `Manual review specs cannot enter Docker suite directly: ${relative(repoRoot, specPath)}`,
- );
- }
- if (!specPath.endsWith(".test.ts")) {
- errors.push(`Spec must be a .test.ts file: ${specPath}`);
- }
- if (!existsSync(specPath)) {
- errors.push(`Spec not found: ${specPath}`);
- }
- if (!alreadyInSuite && !existsSync(providerFixturePath)) {
- errors.push(
- `Provider fixture not found: ${relative(repoRoot, providerFixturePath)}`,
- );
- }
- if (alreadyInSuite && !existsSync(providerFixturePath)) {
- warnings.push(
- `Existing Docker suite member has no case-local provider fixture: ${relative(repoRoot, providerFixturePath)}`,
- );
- }
- if (!alreadyInSuite && !existsSync(caseManifestPath)) {
- errors.push(
- `Case manifest not found: ${relative(repoRoot, caseManifestPath)}`,
- );
- }
- if (alreadyInSuite && !existsSync(caseManifestPath)) {
- warnings.push(
- `Existing Docker suite member has no case manifest: ${relative(repoRoot, caseManifestPath)}`,
- );
- }
- if (apply && !verified) {
- errors.push(
- "Use --verified when applying Docker admission after replay-isolated passes.",
- );
- }
- if (artifact && !existsSync(resolveSpecPath(artifact))) {
- warnings.push(
- `Artifact path was not found locally; keep it in the PR/run record if it is external: ${artifact}`,
- );
- }
- if (!artifact) {
- warnings.push(
- "No --artifact path provided; record the passing Docker artifact path in the PR or run log.",
- );
- }
- if (errors.length === 0) {
- if (alreadyInSuite) {
- actions.push(`Docker suite already contains ${specRelFromDesktop}.`);
- } else {
- actions.push(
- `Add ${specRelFromDesktop} to conversation-session-verified Docker preset.`,
- );
- }
- if (alreadyInPlan) {
- actions.push(`Docker plan already lists ${specRelFromDesktop}.`);
- } else {
- actions.push(
- `Add ${specRelFromDesktop} to ${relative(repoRoot, dockerPlanPath)}.`,
- );
- }
- }
- if (apply && errors.length === 0) {
- if (!alreadyInSuite) {
- updateConversationDockerSuite({ specRelFromDesktop, suite });
- }
- if (!alreadyInPlan) {
- updateDockerPlan({ specRelFromDesktop, warnings });
- }
- }
- return {
- actions,
- alreadyInPlan,
- alreadyInSuite,
- apply,
- artifact,
- caseManifestPath,
- caseName,
- errors,
- providerFixturePath,
- specPath,
- specRelFromDesktop,
- warnings,
- };
- }
- function resolveSpecPath(pathText) {
- if (isAbsolute(pathText)) {
- return pathText;
- }
- const candidates = [
- resolve(process.cwd(), pathText),
- resolve(desktopDir, pathText),
- resolve(repoRoot, pathText),
- ];
- return candidates.find((candidate) => existsSync(candidate)) ?? candidates[0];
- }
- function readConversationDockerSuite({ errors }) {
- if (!existsSync(containerScriptPath)) {
- errors.push(
- `Container script not found: ${relative(repoRoot, containerScriptPath)}`,
- );
- return { specs: [], text: "" };
- }
- const text = readFileSync(containerScriptPath, "utf8");
- const match = text.match(suiteAssignmentPattern);
- if (!match) {
- errors.push(
- `Cannot find CONVERSATION_SESSION_VERIFIED_E2E_SPEC in ${relative(repoRoot, containerScriptPath)}`,
- );
- return { specs: [], text };
- }
- return {
- specs: match[2].split(",").filter((entry) => entry.length > 0),
- text,
- };
- }
- function updateConversationDockerSuite({ specRelFromDesktop, suite }) {
- const nextSpecs = [...suite.specs, specRelFromDesktop];
- const nextLine = `CONVERSATION_SESSION_VERIFIED_E2E_SPEC="${nextSpecs.join(",")}"`;
- writeFileSync(
- containerScriptPath,
- suite.text.replace(suiteAssignmentPattern, nextLine),
- "utf8",
- );
- }
- function updateDockerPlan({ specRelFromDesktop, warnings }) {
- if (!existsSync(dockerPlanPath)) {
- warnings.push(`Docker plan not found: ${relative(repoRoot, dockerPlanPath)}`);
- return;
- }
- const original = readFileSync(dockerPlanPath, "utf8");
- const updated = insertVerifiedDockerSpecRow(original, specRelFromDesktop);
- if (!updated.inserted && !updated.error) {
- return;
- }
- if (updated.error) {
- warnings.push(
- `${updated.error} in ${relative(repoRoot, dockerPlanPath)}`,
- );
- return;
- }
- writeFileSync(dockerPlanPath, updated.text, "utf8");
- }
- function dockerPlanIncludes(specRelFromDesktop) {
- if (!existsSync(dockerPlanPath)) {
- return false;
- }
- const text = readFileSync(dockerPlanPath, "utf8");
- return text.includes(`| \`${specRelFromDesktop}\` |`);
- }
- function printResult(result) {
- console.log(`${result.apply ? "Apply" : "Dry run"} conversation Docker admission`);
- console.log(`Case: ${result.caseName}`);
- console.log(`Spec: ${relative(repoRoot, result.specPath)}`);
- console.log(`Provider fixture: ${relative(repoRoot, result.providerFixturePath)}`);
- console.log(`Case manifest: ${relative(repoRoot, result.caseManifestPath)}`);
- if (result.artifact) {
- console.log(`Artifact: ${result.artifact}`);
- }
- if (result.actions.length > 0) {
- console.log("\nActions:");
- for (const action of result.actions) {
- console.log(`- ${action}`);
- }
- }
- if (result.warnings.length > 0) {
- console.log("\nWarnings:");
- for (const warning of result.warnings) {
- console.log(`- ${warning}`);
- }
- }
- if (result.errors.length > 0) {
- console.log("\nErrors:");
- for (const error of result.errors) {
- console.log(`- ${error}`);
- }
- return;
- }
- const specArg = result.specRelFromDesktop;
- console.log("\nNext:");
- if (!result.alreadyInSuite) {
- console.log(`- E2E_SPEC=${specArg} pnpm run test:e2e:container`);
- console.log(`- pnpm --filter @zcode/desktop e2e:docker:admit -- --spec ${specArg} --verified --artifact packages/desktop/.e2e-artifacts/<run-id> --apply`);
- } else if (!result.alreadyInPlan) {
- console.log(`- pnpm --filter @zcode/desktop e2e:docker:admit -- --spec ${specArg} --verified --artifact packages/desktop/.e2e-artifacts/<run-id> --apply`);
- }
- console.log("- pnpm run test:e2e:container:conversation");
- }
|