e2ePromotionAuditTransaction.test.ts 7.1 KB

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