e2e-promotion-audit-transaction.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { spawn } from "node:child_process";
  2. import { chmod, mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
  3. import { dirname } from "node:path";
  4. export function createPromotionProviderFixture(caseName, specPath) {
  5. return {
  6. version: 1,
  7. caseName,
  8. spec: specPath,
  9. fixtures: [],
  10. };
  11. }
  12. export function createPromotionCaseManifest(caseName, specPath) {
  13. return {
  14. version: 1,
  15. caseName,
  16. spec: specPath,
  17. providerFixtures: [
  18. "./test/e2e/fixtures/upstream/common.json",
  19. `./test/e2e/fixtures/upstream/conversation-session/${caseName}.json`,
  20. ],
  21. fileFixtures: [],
  22. requests: [],
  23. };
  24. }
  25. export function runPromotionCoverageAudit({ auditPath, repoRoot }) {
  26. return new Promise((resolveAudit, rejectAudit) => {
  27. const child = spawn(process.execPath, [auditPath, "--check"], {
  28. cwd: repoRoot,
  29. env: process.env,
  30. stdio: ["ignore", "pipe", "pipe"],
  31. });
  32. let stdout = "";
  33. let stderr = "";
  34. child.stdout.setEncoding("utf8");
  35. child.stderr.setEncoding("utf8");
  36. child.stdout.on("data", (chunk) => {
  37. stdout += chunk;
  38. });
  39. child.stderr.on("data", (chunk) => {
  40. stderr += chunk;
  41. });
  42. child.on("error", rejectAudit);
  43. child.on("close", (exitCode, signal) => {
  44. resolveAudit({
  45. command: "pnpm audit:conversation-session-coverage",
  46. exitCode: exitCode ?? 1,
  47. signal,
  48. stderr,
  49. stdout,
  50. });
  51. });
  52. });
  53. }
  54. export async function runPromotionAuditTransaction({ affectedPaths, applyChanges, runAudit }) {
  55. const snapshots = await Promise.all(affectedPaths.map(captureFileSnapshot));
  56. try {
  57. await applyChanges();
  58. const audit = await runAudit();
  59. if (audit.exitCode === 0) {
  60. return { audit, committed: true, rolledBack: false };
  61. }
  62. // Bug 根因:promotion 过去会先留下 formal 文件,再由后续命令发现准入失败,
  63. // 导致 spec、fixture 与 matrix 处于半转正状态;audit 未通过时必须恢复完整快照。
  64. await restoreFileSnapshots(snapshots);
  65. return { audit, committed: false, rolledBack: true };
  66. } catch (error) {
  67. try {
  68. await restoreFileSnapshots(snapshots);
  69. } catch (rollbackError) {
  70. throw new AggregateError(
  71. [error, rollbackError],
  72. "E2E promotion failed and its filesystem rollback also failed.",
  73. );
  74. }
  75. throw error;
  76. }
  77. }
  78. async function captureFileSnapshot(filePath) {
  79. try {
  80. const [content, metadata] = await Promise.all([readFile(filePath), stat(filePath)]);
  81. return {
  82. content,
  83. exists: true,
  84. filePath,
  85. mode: metadata.mode,
  86. };
  87. } catch (error) {
  88. if (error?.code === "ENOENT") {
  89. return { content: null, exists: false, filePath, mode: null };
  90. }
  91. throw error;
  92. }
  93. }
  94. async function restoreFileSnapshots(snapshots) {
  95. for (const snapshot of snapshots) {
  96. if (!snapshot.exists) {
  97. await rm(snapshot.filePath, { force: true });
  98. continue;
  99. }
  100. await mkdir(dirname(snapshot.filePath), { recursive: true });
  101. await writeFile(snapshot.filePath, snapshot.content);
  102. await chmod(snapshot.filePath, snapshot.mode);
  103. }
  104. }