upstream-replay-fixture-resolution.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { existsSync } from "node:fs";
  2. import { join } from "node:path";
  3. export type UpstreamReplayFixtureMode = "compatible" | "strict-case-local";
  4. export type UpstreamE2EHttpMode = "capture" | "replay";
  5. export function resolveUpstreamE2EHttpMode({
  6. explicitReplayFixture,
  7. manualReview,
  8. requestedMode,
  9. }: {
  10. explicitReplayFixture: boolean;
  11. manualReview: boolean;
  12. requestedMode?: string;
  13. }): UpstreamE2EHttpMode {
  14. if (manualReview && explicitReplayFixture) {
  15. if (requestedMode === "capture") {
  16. throw new Error(
  17. "E2E_PROVIDER_REPLAY_FIXTURE_PATH cannot be combined with E2E_PROVIDER_HTTP_MODE=capture.",
  18. );
  19. }
  20. return "replay";
  21. }
  22. if (manualReview) {
  23. if (requestedMode && requestedMode !== "capture") {
  24. throw new Error(
  25. "Manual review defaults to capture; set E2E_PROVIDER_REPLAY_FIXTURE_PATH for pending replay.",
  26. );
  27. }
  28. return "capture";
  29. }
  30. return requestedMode === "capture" ? "capture" : "replay";
  31. }
  32. export function resolveUpstreamE2EHttpModeForWorker({
  33. caseLocalReplayFixtureAvailable = false,
  34. explicitReplayFixture,
  35. requestedMode,
  36. specs,
  37. }: {
  38. caseLocalReplayFixtureAvailable?: boolean;
  39. explicitReplayFixture: boolean;
  40. requestedMode?: string;
  41. specs: readonly string[];
  42. }): UpstreamE2EHttpMode {
  43. const captureFlags = new Set(
  44. specs.map((spec) => {
  45. const normalized = spec.replaceAll("\\", "/");
  46. return (
  47. (normalized.includes("/manual-review/") &&
  48. !caseLocalReplayFixtureAvailable) ||
  49. normalized.endsWith("/upstream-provider.test.ts")
  50. );
  51. }),
  52. );
  53. if (captureFlags.size > 1) {
  54. throw new Error(
  55. "A single E2E worker cannot mix replay and capture specs because their provider HTTP modes differ.",
  56. );
  57. }
  58. return resolveUpstreamE2EHttpMode({
  59. explicitReplayFixture,
  60. // Upstream provider smoke 与缺 fixture 的 manual-review 验证真实上游;已有确定性
  61. // case-local fixture 的 pending case 使用 replay,避免真实模型形态破坏精确断言。
  62. manualReview: captureFlags.has(true),
  63. requestedMode,
  64. });
  65. }
  66. export function hasUpstreamCaseLocalReplayFixtureForWorker({
  67. caseFixtureDir,
  68. pathExists = existsSync,
  69. specs,
  70. }: {
  71. caseFixtureDir: string;
  72. pathExists?: (path: string) => boolean;
  73. specs: readonly string[];
  74. }): boolean {
  75. const caseNames = specs
  76. .map(resolveConversationSessionCaseName)
  77. .filter((caseName): caseName is string => Boolean(caseName));
  78. return (
  79. caseNames.length > 0 &&
  80. caseNames.length === specs.length &&
  81. caseNames.every((caseName) => pathExists(join(caseFixtureDir, `${caseName}.json`)))
  82. );
  83. }
  84. interface ResolveUpstreamReplayFixturePathsOptions {
  85. caseFixtureDir: string;
  86. caseManifestDir: string;
  87. commonFixturePath: string;
  88. configuredFixturePaths: string[] | null;
  89. legacyFixturePaths: string[];
  90. mode: UpstreamReplayFixtureMode;
  91. pathExists?: (path: string) => boolean;
  92. pluginFixturePaths: string[];
  93. specs: string[];
  94. }
  95. export function resolveUpstreamReplayFixturePaths({
  96. caseFixtureDir,
  97. caseManifestDir,
  98. commonFixturePath,
  99. configuredFixturePaths,
  100. legacyFixturePaths,
  101. mode,
  102. pathExists = existsSync,
  103. pluginFixturePaths,
  104. specs,
  105. }: ResolveUpstreamReplayFixturePathsOptions): string[] {
  106. if (configuredFixturePaths) {
  107. if (mode === "strict-case-local") {
  108. // 修复原因:strict CI 若接受显式 fixture 覆盖,就能再次绕过 case-local 合同并加载 legacy 池。
  109. throw new Error(
  110. "E2E_PROVIDER_REPLAY_FIXTURE_PATH cannot be used with strict-case-local replay fixtures.",
  111. );
  112. }
  113. return uniquePaths(configuredFixturePaths);
  114. }
  115. const caseNames = uniquePaths(
  116. specs
  117. .map(resolveConversationSessionCaseName)
  118. .filter((caseName): caseName is string => Boolean(caseName)),
  119. );
  120. const caseFixturePaths = caseNames.map((caseName) => join(caseFixtureDir, `${caseName}.json`));
  121. if (mode === "strict-case-local") {
  122. const missingPaths = [
  123. ...(pathExists(commonFixturePath) ? [] : [`common fixture: ${commonFixturePath}`]),
  124. ...caseNames.flatMap((caseName) => {
  125. const manifestPath = join(caseManifestDir, `${caseName}.json`);
  126. const fixturePath = join(caseFixtureDir, `${caseName}.json`);
  127. return [
  128. ...(pathExists(manifestPath) ? [] : [`case manifest: ${manifestPath}`]),
  129. ...(pathExists(fixturePath) ? [] : [`case fixture: ${fixturePath}`]),
  130. ];
  131. }),
  132. ];
  133. if (missingPaths.length > 0) {
  134. throw new Error(
  135. `Strict case-local Upstream replay fixtures are incomplete:\n${missingPaths
  136. .map((path) => `- ${path}`)
  137. .join("\n")}`,
  138. );
  139. }
  140. }
  141. return uniquePaths([
  142. ...(pathExists(commonFixturePath) ? [commonFixturePath] : []),
  143. ...caseFixturePaths.filter(pathExists),
  144. ...(mode === "strict-case-local" ? [] : pluginFixturePaths),
  145. // 修复原因:formal CI 必须让缺失 matcher 直接失败,不能再由历史共享池替代 case-local fixture 假绿。
  146. ...(mode === "strict-case-local" ? [] : legacyFixturePaths),
  147. ]);
  148. }
  149. function resolveConversationSessionCaseName(spec: string): string | null {
  150. const normalized = spec.replaceAll("\\", "/");
  151. if (!normalized.includes("/test/e2e/conversation-session/")) {
  152. return null;
  153. }
  154. const fileName = normalized.split("/").at(-1) ?? "";
  155. if (!fileName.endsWith(".test.ts") || fileName.includes("*")) {
  156. return null;
  157. }
  158. return fileName.slice(0, -".test.ts".length);
  159. }
  160. function uniquePaths(paths: string[]): string[] {
  161. return [...new Set(paths)];
  162. }