admit-conversation-e2e-docker.mjs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #!/usr/bin/env node
  2. import { existsSync, readFileSync, writeFileSync } from "node:fs";
  3. import { basename, isAbsolute, relative, resolve } from "node:path";
  4. import { insertVerifiedDockerSpecRow } from "./conversation-docker-plan.mjs";
  5. const desktopDir = resolve(import.meta.dirname, "..");
  6. const repoRoot = resolve(desktopDir, "../..");
  7. const conversationDir = resolve(desktopDir, "test/e2e/conversation-session");
  8. const providerFixtureDir = resolve(desktopDir, "test/e2e/fixtures/upstream/conversation-session");
  9. const caseManifestDir = resolve(desktopDir, "test/e2e/fixtures/cases/conversation-session");
  10. const containerScriptPath = resolve(repoRoot, "scripts/test-desktop-e2e-container.sh");
  11. const dockerPlanPath = resolve(
  12. repoRoot,
  13. "docs/testing/conversation-session-docker-automation-plan.md",
  14. );
  15. const suiteAssignmentPattern = /^(CONVERSATION_SESSION_VERIFIED_E2E_SPEC=")([^"]*)(")$/m;
  16. const options = parseArgs(process.argv.slice(2));
  17. if (options.help || !options.spec) {
  18. printUsage();
  19. process.exit(options.help ? 0 : 1);
  20. }
  21. const result = admit(options);
  22. printResult(result);
  23. process.exit(result.errors.length > 0 ? 1 : 0);
  24. function parseArgs(args) {
  25. const parsed = {
  26. apply: false,
  27. artifact: "",
  28. help: false,
  29. spec: "",
  30. verified: false,
  31. };
  32. for (let index = 0; index < args.length; index += 1) {
  33. const arg = args[index];
  34. if (arg === "--" || arg.trim().length === 0) {
  35. continue;
  36. }
  37. if (arg === "--help" || arg === "-h") {
  38. parsed.help = true;
  39. continue;
  40. }
  41. if (arg === "--apply") {
  42. parsed.apply = true;
  43. continue;
  44. }
  45. if (arg === "--verified") {
  46. parsed.verified = true;
  47. continue;
  48. }
  49. if (arg === "--artifact") {
  50. parsed.artifact = args[index + 1] ?? "";
  51. index += 1;
  52. continue;
  53. }
  54. if (arg.startsWith("--artifact=")) {
  55. parsed.artifact = arg.slice("--artifact=".length);
  56. continue;
  57. }
  58. if (arg === "--spec") {
  59. parsed.spec = args[index + 1] ?? "";
  60. index += 1;
  61. continue;
  62. }
  63. if (arg.startsWith("--spec=")) {
  64. parsed.spec = arg.slice("--spec=".length);
  65. continue;
  66. }
  67. if (!parsed.spec) {
  68. parsed.spec = arg;
  69. continue;
  70. }
  71. }
  72. return parsed;
  73. }
  74. function printUsage() {
  75. console.log(`Usage:
  76. pnpm --filter @zcode/desktop e2e:docker:admit -- --spec ./test/e2e/conversation-session/<scenario>/<case>.test.ts --verified --artifact packages/desktop/.e2e-artifacts/<run-id> --apply
  77. Options:
  78. --apply Write changes. Without this flag the tool only prints a dry run.
  79. --verified Confirm the formal case already passed Docker replay-isolated.
  80. --artifact Optional artifact path from the passing Docker run.
  81. `);
  82. }
  83. function admit({ apply, artifact, spec, verified }) {
  84. const errors = [];
  85. const warnings = [];
  86. const actions = [];
  87. const specPath = resolveSpecPath(spec);
  88. const fileName = basename(specPath);
  89. const caseName = fileName.replace(/\.test\.ts$/, "");
  90. const specRelFromDesktop = `./${relative(desktopDir, specPath).replaceAll("\\", "/")}`;
  91. const providerFixturePath = resolve(providerFixtureDir, `${caseName}.json`);
  92. const caseManifestPath = resolve(caseManifestDir, `${caseName}.json`);
  93. const suite = readConversationDockerSuite({ errors });
  94. const alreadyInSuite = suite.specs.includes(specRelFromDesktop);
  95. const alreadyInPlan = dockerPlanIncludes(specRelFromDesktop);
  96. const relFromConversation = relative(conversationDir, specPath);
  97. if (relFromConversation.startsWith("..") || isAbsolute(relFromConversation)) {
  98. errors.push(
  99. `Spec must be under formal conversation-session dir: ${relative(repoRoot, specPath)}`,
  100. );
  101. }
  102. if (relFromConversation.includes("manual-review/")) {
  103. errors.push(
  104. `Manual review specs cannot enter Docker suite directly: ${relative(repoRoot, specPath)}`,
  105. );
  106. }
  107. if (!specPath.endsWith(".test.ts")) {
  108. errors.push(`Spec must be a .test.ts file: ${specPath}`);
  109. }
  110. if (!existsSync(specPath)) {
  111. errors.push(`Spec not found: ${specPath}`);
  112. }
  113. if (!alreadyInSuite && !existsSync(providerFixturePath)) {
  114. errors.push(`Provider fixture not found: ${relative(repoRoot, providerFixturePath)}`);
  115. }
  116. if (alreadyInSuite && !existsSync(providerFixturePath)) {
  117. warnings.push(
  118. `Existing Docker suite member has no case-local provider fixture: ${relative(repoRoot, providerFixturePath)}`,
  119. );
  120. }
  121. if (!alreadyInSuite && !existsSync(caseManifestPath)) {
  122. errors.push(`Case manifest not found: ${relative(repoRoot, caseManifestPath)}`);
  123. }
  124. if (alreadyInSuite && !existsSync(caseManifestPath)) {
  125. warnings.push(
  126. `Existing Docker suite member has no case manifest: ${relative(repoRoot, caseManifestPath)}`,
  127. );
  128. }
  129. if (apply && !verified) {
  130. errors.push("Use --verified when applying Docker admission after replay-isolated passes.");
  131. }
  132. if (artifact && !existsSync(resolveSpecPath(artifact))) {
  133. warnings.push(
  134. `Artifact path was not found locally; keep it in the PR/run record if it is external: ${artifact}`,
  135. );
  136. }
  137. if (!artifact) {
  138. warnings.push(
  139. "No --artifact path provided; record the passing Docker artifact path in the PR or run log.",
  140. );
  141. }
  142. if (errors.length === 0) {
  143. if (alreadyInSuite) {
  144. actions.push(`Docker suite already contains ${specRelFromDesktop}.`);
  145. } else {
  146. actions.push(`Add ${specRelFromDesktop} to conversation-session-verified Docker preset.`);
  147. }
  148. if (alreadyInPlan) {
  149. actions.push(`Docker plan already lists ${specRelFromDesktop}.`);
  150. } else {
  151. actions.push(`Add ${specRelFromDesktop} to ${relative(repoRoot, dockerPlanPath)}.`);
  152. }
  153. }
  154. if (apply && errors.length === 0) {
  155. if (!alreadyInSuite) {
  156. updateConversationDockerSuite({ specRelFromDesktop, suite });
  157. }
  158. if (!alreadyInPlan) {
  159. updateDockerPlan({ specRelFromDesktop, warnings });
  160. }
  161. }
  162. return {
  163. actions,
  164. alreadyInPlan,
  165. alreadyInSuite,
  166. apply,
  167. artifact,
  168. caseManifestPath,
  169. caseName,
  170. errors,
  171. providerFixturePath,
  172. specPath,
  173. specRelFromDesktop,
  174. warnings,
  175. };
  176. }
  177. function resolveSpecPath(pathText) {
  178. if (isAbsolute(pathText)) {
  179. return pathText;
  180. }
  181. const candidates = [
  182. resolve(process.cwd(), pathText),
  183. resolve(desktopDir, pathText),
  184. resolve(repoRoot, pathText),
  185. ];
  186. return candidates.find((candidate) => existsSync(candidate)) ?? candidates[0];
  187. }
  188. function readConversationDockerSuite({ errors }) {
  189. if (!existsSync(containerScriptPath)) {
  190. errors.push(`Container script not found: ${relative(repoRoot, containerScriptPath)}`);
  191. return { specs: [], text: "" };
  192. }
  193. const text = readFileSync(containerScriptPath, "utf8");
  194. const match = text.match(suiteAssignmentPattern);
  195. if (!match) {
  196. errors.push(
  197. `Cannot find CONVERSATION_SESSION_VERIFIED_E2E_SPEC in ${relative(repoRoot, containerScriptPath)}`,
  198. );
  199. return { specs: [], text };
  200. }
  201. return {
  202. specs: match[2].split(",").filter((entry) => entry.length > 0),
  203. text,
  204. };
  205. }
  206. function updateConversationDockerSuite({ specRelFromDesktop, suite }) {
  207. const nextSpecs = [...suite.specs, specRelFromDesktop];
  208. const nextLine = `CONVERSATION_SESSION_VERIFIED_E2E_SPEC="${nextSpecs.join(",")}"`;
  209. writeFileSync(containerScriptPath, suite.text.replace(suiteAssignmentPattern, nextLine), "utf8");
  210. }
  211. function updateDockerPlan({ specRelFromDesktop, warnings }) {
  212. if (!existsSync(dockerPlanPath)) {
  213. warnings.push(`Docker plan not found: ${relative(repoRoot, dockerPlanPath)}`);
  214. return;
  215. }
  216. const original = readFileSync(dockerPlanPath, "utf8");
  217. const updated = insertVerifiedDockerSpecRow(original, specRelFromDesktop);
  218. if (!updated.inserted && !updated.error) {
  219. return;
  220. }
  221. if (updated.error) {
  222. warnings.push(`${updated.error} in ${relative(repoRoot, dockerPlanPath)}`);
  223. return;
  224. }
  225. writeFileSync(dockerPlanPath, updated.text, "utf8");
  226. }
  227. function dockerPlanIncludes(specRelFromDesktop) {
  228. if (!existsSync(dockerPlanPath)) {
  229. return false;
  230. }
  231. const text = readFileSync(dockerPlanPath, "utf8");
  232. return text.includes(`| \`${specRelFromDesktop}\` |`);
  233. }
  234. function printResult(result) {
  235. console.log(`${result.apply ? "Apply" : "Dry run"} conversation Docker admission`);
  236. console.log(`Case: ${result.caseName}`);
  237. console.log(`Spec: ${relative(repoRoot, result.specPath)}`);
  238. console.log(`Provider fixture: ${relative(repoRoot, result.providerFixturePath)}`);
  239. console.log(`Case manifest: ${relative(repoRoot, result.caseManifestPath)}`);
  240. if (result.artifact) {
  241. console.log(`Artifact: ${result.artifact}`);
  242. }
  243. if (result.actions.length > 0) {
  244. console.log("\nActions:");
  245. for (const action of result.actions) {
  246. console.log(`- ${action}`);
  247. }
  248. }
  249. if (result.warnings.length > 0) {
  250. console.log("\nWarnings:");
  251. for (const warning of result.warnings) {
  252. console.log(`- ${warning}`);
  253. }
  254. }
  255. if (result.errors.length > 0) {
  256. console.log("\nErrors:");
  257. for (const error of result.errors) {
  258. console.log(`- ${error}`);
  259. }
  260. return;
  261. }
  262. const specArg = result.specRelFromDesktop;
  263. console.log("\nNext:");
  264. if (!result.alreadyInSuite) {
  265. console.log(`- E2E_SPEC=${specArg} pnpm run test:e2e:container`);
  266. console.log(
  267. `- pnpm --filter @zcode/desktop e2e:docker:admit -- --spec ${specArg} --verified --artifact packages/desktop/.e2e-artifacts/<run-id> --apply`,
  268. );
  269. } else if (!result.alreadyInPlan) {
  270. console.log(
  271. `- pnpm --filter @zcode/desktop e2e:docker:admit -- --spec ${specArg} --verified --artifact packages/desktop/.e2e-artifacts/<run-id> --apply`,
  272. );
  273. }
  274. console.log("- pnpm run test:e2e:container:conversation");
  275. }