e2ePromotionAuditTransaction.test.ts 6.8 KB

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