admit-conversation-e2e-docker.mjs 9.6 KB

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