e2ePromotionAuditTransaction.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { mkdir, mkdtemp, readFile, rename, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { dirname, join } from "node:path";
  4. import { afterEach, describe, expect, it } from "vitest";
  5. import {
  6. runPromotionAuditTransaction,
  7. runPromotionCoverageAudit,
  8. } from "../scripts/lib/e2e-promotion-audit-transaction.mjs";
  9. import { rewriteFormalSpecImports } from "../scripts/lib/e2e-promotion-spec-imports.mjs";
  10. const temporaryRoots: string[] = [];
  11. afterEach(async () => {
  12. await Promise.all(
  13. temporaryRoots.splice(0).map((root) => rm(root, { force: true, recursive: true })),
  14. );
  15. });
  16. describe("conversation E2E promotion audit transaction", () => {
  17. it("rewrites pending spec imports for the formal spec location", async () => {
  18. const root = await mkdtemp(join(tmpdir(), "zcode-e2e-promotion-imports-"));
  19. temporaryRoots.push(root);
  20. const specPath = join(root, "case.test.ts");
  21. await writeFile(
  22. specPath,
  23. [
  24. 'import "../../../helpers/session.js";',
  25. 'import "../../../pages/conversation.js";',
  26. 'import "../../../bots/helpers/bots-service-harness.js";',
  27. 'import fixture from "../../../fixtures/conversation-session/sample.json";',
  28. 'import alreadyFormal from "../fixtures/unchanged.json";',
  29. "",
  30. ].join("\n"),
  31. );
  32. rewriteFormalSpecImports(specPath);
  33. await expect(readFile(specPath, "utf8")).resolves.toBe(
  34. [
  35. 'import "../helpers/session.js";',
  36. 'import "../pages/conversation.js";',
  37. 'import "../bots/helpers/bots-service-harness.js";',
  38. 'import fixture from "../fixtures/conversation-session/sample.json";',
  39. 'import alreadyFormal from "../fixtures/unchanged.json";',
  40. "",
  41. ].join("\n"),
  42. );
  43. });
  44. it("returns the real coverage audit process result to the promotion gate", async () => {
  45. const root = await mkdtemp(join(tmpdir(), "zcode-e2e-promotion-audit-command-"));
  46. temporaryRoots.push(root);
  47. const auditPath = join(root, "audit.mjs");
  48. await writeFile(
  49. auditPath,
  50. 'process.stdout.write("formal admission: rejected\\n"); process.exitCode = 1;\n',
  51. );
  52. const result = await runPromotionCoverageAudit({ auditPath, repoRoot: root });
  53. expect(result.exitCode).toBe(1);
  54. expect(result.stdout).toContain("formal admission: rejected");
  55. expect(result.command).toBe("pnpm audit:conversation-session-coverage");
  56. });
  57. it("restores every promotion file when coverage audit fails", async () => {
  58. const fixture = await createPromotionFixture();
  59. const result = await runPromotionAuditTransaction({
  60. affectedPaths: fixture.affectedPaths,
  61. applyChanges: () => applyProbePromotion(fixture),
  62. runAudit: async () => ({ exitCode: 1, stderr: "formal admission rejected", stdout: "" }),
  63. });
  64. expect(result.committed).toBe(false);
  65. expect(result.rolledBack).toBe(true);
  66. await expect(readFile(fixture.sourcePath, "utf8")).resolves.toBe(fixture.originalSpec);
  67. await expect(readFile(fixture.targetPath, "utf8")).rejects.toMatchObject({ code: "ENOENT" });
  68. await expect(readFile(fixture.providerFixturePath, "utf8")).resolves.toBe(
  69. fixture.originalProviderFixture,
  70. );
  71. await expect(readFile(fixture.caseManifestPath, "utf8")).rejects.toMatchObject({
  72. code: "ENOENT",
  73. });
  74. await expect(readFile(fixture.matrixPath, "utf8")).resolves.toBe(fixture.originalMatrix);
  75. });
  76. it("keeps the promoted files only when coverage audit passes", async () => {
  77. const fixture = await createPromotionFixture();
  78. const result = await runPromotionAuditTransaction({
  79. affectedPaths: fixture.affectedPaths,
  80. applyChanges: () => applyProbePromotion(fixture),
  81. runAudit: async () => ({ exitCode: 0, stderr: "", stdout: "audit passed" }),
  82. });
  83. expect(result.committed).toBe(true);
  84. expect(result.rolledBack).toBe(false);
  85. await expect(readFile(fixture.sourcePath, "utf8")).rejects.toMatchObject({ code: "ENOENT" });
  86. await expect(readFile(fixture.targetPath, "utf8")).resolves.toBe("formal spec\n");
  87. await expect(readFile(fixture.providerFixturePath, "utf8")).resolves.toBe(
  88. "promoted provider fixture\n",
  89. );
  90. await expect(readFile(fixture.caseManifestPath, "utf8")).resolves.toBe(
  91. "promoted case manifest\n",
  92. );
  93. await expect(readFile(fixture.matrixPath, "utf8")).resolves.toBe("formal matrix path\n");
  94. });
  95. it("restores every promotion file when the audit process cannot run", async () => {
  96. const fixture = await createPromotionFixture();
  97. await expect(
  98. runPromotionAuditTransaction({
  99. affectedPaths: fixture.affectedPaths,
  100. applyChanges: () => applyProbePromotion(fixture),
  101. runAudit: async () => {
  102. throw new Error("audit process unavailable");
  103. },
  104. }),
  105. ).rejects.toThrow("audit process unavailable");
  106. await expect(readFile(fixture.sourcePath, "utf8")).resolves.toBe(fixture.originalSpec);
  107. await expect(readFile(fixture.targetPath, "utf8")).rejects.toMatchObject({ code: "ENOENT" });
  108. await expect(readFile(fixture.providerFixturePath, "utf8")).resolves.toBe(
  109. fixture.originalProviderFixture,
  110. );
  111. await expect(readFile(fixture.caseManifestPath, "utf8")).rejects.toMatchObject({
  112. code: "ENOENT",
  113. });
  114. await expect(readFile(fixture.matrixPath, "utf8")).resolves.toBe(fixture.originalMatrix);
  115. });
  116. });
  117. async function createPromotionFixture() {
  118. const root = await mkdtemp(join(tmpdir(), "zcode-e2e-promotion-audit-"));
  119. temporaryRoots.push(root);
  120. const sourcePath = join(root, "pending", "case.test.ts");
  121. const targetPath = join(root, "formal", "case.test.ts");
  122. const providerFixturePath = join(root, "fixtures", "provider.json");
  123. const caseManifestPath = join(root, "fixtures", "manifest.json");
  124. const matrixPath = join(root, "coverage.md");
  125. const originalSpec = "pending spec\n";
  126. const originalProviderFixture = "pending provider fixture\n";
  127. const originalMatrix = "pending matrix path\n";
  128. await Promise.all([
  129. mkdir(join(root, "pending"), { recursive: true }),
  130. mkdir(join(root, "fixtures"), { recursive: true }),
  131. ]);
  132. await Promise.all([
  133. writeFile(sourcePath, originalSpec),
  134. writeFile(providerFixturePath, originalProviderFixture),
  135. writeFile(matrixPath, originalMatrix),
  136. ]);
  137. return {
  138. affectedPaths: [sourcePath, targetPath, providerFixturePath, caseManifestPath, matrixPath],
  139. caseManifestPath,
  140. matrixPath,
  141. originalMatrix,
  142. originalProviderFixture,
  143. originalSpec,
  144. providerFixturePath,
  145. sourcePath,
  146. targetPath,
  147. };
  148. }
  149. async function applyProbePromotion(fixture: Awaited<ReturnType<typeof createPromotionFixture>>) {
  150. await mkdir(dirname(fixture.targetPath), { recursive: true });
  151. await rename(fixture.sourcePath, fixture.targetPath);
  152. await Promise.all([
  153. writeFile(fixture.targetPath, "formal spec\n"),
  154. writeFile(fixture.providerFixturePath, "promoted provider fixture\n"),
  155. writeFile(fixture.caseManifestPath, "promoted case manifest\n"),
  156. writeFile(fixture.matrixPath, "formal matrix path\n"),
  157. ]);
  158. }