upstream-replay-fixture-resolution.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { existsSync } from "node:fs";
  2. import { readdir } from "node:fs/promises";
  3. import { dirname, join } from "node:path";
  4. export type UpstreamReplayFixtureMode = "compatible" | "strict-case-local";
  5. export type UpstreamE2EHttpMode = "capture" | "replay";
  6. export async function resolvePluginReplayFixturePaths({
  7. fixtureRoot,
  8. specs,
  9. }: {
  10. fixtureRoot: string;
  11. specs: readonly string[];
  12. }): Promise<string[]> {
  13. // 原因:旧兼容入口返回目录,replay loader 按 JSON 文件读取时抛 EISDIR。
  14. // 仅展开该领域的 JSON 文件;不递归加载子目录,也不改变 case-local 的优先顺序。
  15. const directories = ["plugin-mcp-skill", "plugin-management"]
  16. .filter((domain) => specs.some((spec) => spec.includes(`${domain}-`)))
  17. .map((domain) => join(fixtureRoot, domain));
  18. const files = await Promise.all(
  19. directories.map(async (directory) => {
  20. const entries = await readdir(directory, { withFileTypes: true }).catch(
  21. (error: NodeJS.ErrnoException) => {
  22. if (error.code === "ENOENT") return [];
  23. throw error;
  24. },
  25. );
  26. return entries
  27. .filter((entry) => entry.isFile() && entry.name.endsWith(".json"))
  28. .map((entry) => join(directory, entry.name))
  29. .sort();
  30. }),
  31. );
  32. return files.flat();
  33. }
  34. export function resolveUpstreamE2EHttpMode({
  35. explicitReplayFixture,
  36. manualReview,
  37. requestedMode,
  38. }: {
  39. explicitReplayFixture: boolean;
  40. manualReview: boolean;
  41. requestedMode?: string;
  42. }): UpstreamE2EHttpMode {
  43. if (manualReview && explicitReplayFixture) {
  44. if (requestedMode === "capture") {
  45. throw new Error(
  46. "E2E_PROVIDER_REPLAY_FIXTURE_PATH cannot be combined with E2E_PROVIDER_HTTP_MODE=capture.",
  47. );
  48. }
  49. return "replay";
  50. }
  51. if (manualReview) {
  52. if (requestedMode && requestedMode !== "capture") {
  53. throw new Error(
  54. "Manual review defaults to capture; set E2E_PROVIDER_REPLAY_FIXTURE_PATH for pending replay.",
  55. );
  56. }
  57. return "capture";
  58. }
  59. return requestedMode === "capture" ? "capture" : "replay";
  60. }
  61. export function resolveUpstreamE2EHttpModeForWorker({
  62. caseLocalReplayFixtureAvailable = false,
  63. explicitReplayFixture,
  64. requestedMode,
  65. specs,
  66. }: {
  67. caseLocalReplayFixtureAvailable?: boolean;
  68. explicitReplayFixture: boolean;
  69. requestedMode?: string;
  70. specs: readonly string[];
  71. }): UpstreamE2EHttpMode {
  72. const captureFlags = new Set(
  73. specs.map((spec) => {
  74. const normalized = spec.replaceAll("\\", "/");
  75. return (
  76. (normalized.includes("/manual-review/") && !caseLocalReplayFixtureAvailable) ||
  77. normalized.endsWith("/upstream-provider.test.ts")
  78. );
  79. }),
  80. );
  81. if (captureFlags.size > 1) {
  82. throw new Error(
  83. "A single E2E worker cannot mix replay and capture specs because their provider HTTP modes differ.",
  84. );
  85. }
  86. return resolveUpstreamE2EHttpMode({
  87. explicitReplayFixture,
  88. // Upstream provider smoke 与缺 fixture 的 manual-review 验证真实上游;已有确定性
  89. // case-local fixture 的 pending case 使用 replay,避免真实模型形态破坏精确断言。
  90. manualReview: captureFlags.has(true),
  91. requestedMode,
  92. });
  93. }
  94. export function hasUpstreamCaseLocalReplayFixtureForWorker({
  95. caseFixtureDir,
  96. pathExists = existsSync,
  97. specs,
  98. }: {
  99. caseFixtureDir: string;
  100. pathExists?: (path: string) => boolean;
  101. specs: readonly string[];
  102. }): boolean {
  103. const caseNames = specs
  104. .map(resolveConversationSessionCaseName)
  105. .filter((caseName): caseName is string => Boolean(caseName));
  106. return (
  107. caseNames.length > 0 &&
  108. caseNames.length === specs.length &&
  109. caseNames.every((caseName) => pathExists(join(caseFixtureDir, `${caseName}.json`)))
  110. );
  111. }
  112. interface ResolveUpstreamReplayFixturePathsOptions {
  113. caseFixtureDir: string;
  114. caseManifestDir: string;
  115. commonFixturePath: string;
  116. configuredFixturePaths: string[] | null;
  117. legacyFixturePaths: string[];
  118. mode: UpstreamReplayFixtureMode;
  119. pathExists?: (path: string) => boolean;
  120. pluginFixturePaths: string[];
  121. specs: string[];
  122. }
  123. export function resolveUpstreamReplayFixturePaths({
  124. caseFixtureDir,
  125. caseManifestDir,
  126. commonFixturePath,
  127. configuredFixturePaths,
  128. legacyFixturePaths,
  129. mode,
  130. pathExists = existsSync,
  131. pluginFixturePaths,
  132. specs,
  133. }: ResolveUpstreamReplayFixturePathsOptions): string[] {
  134. if (configuredFixturePaths) {
  135. if (mode === "strict-case-local") {
  136. // 修复原因:strict CI 若接受显式 fixture 覆盖,就能再次绕过 case-local 合同并加载 legacy 池。
  137. throw new Error(
  138. "E2E_PROVIDER_REPLAY_FIXTURE_PATH cannot be used with strict-case-local replay fixtures.",
  139. );
  140. }
  141. return uniquePaths(configuredFixturePaths);
  142. }
  143. const caseNames = uniquePaths(
  144. specs
  145. .map(resolveConversationSessionCaseName)
  146. .filter((caseName): caseName is string => Boolean(caseName)),
  147. );
  148. const caseFixturePaths = caseNames.map((caseName) => join(caseFixtureDir, `${caseName}.json`));
  149. // 转正后不再显式指定 fixture;原解析器只识别 conversation,导致插件和 UI 用例丢失本例响应。
  150. // 按已存在的领域 fixture 加载,避免把整个插件响应池带入单个用例。
  151. const additionalCases = specs.flatMap((spec) => {
  152. const relative = spec.replaceAll("\\", "/").split("/test/e2e/")[1];
  153. if (!relative) return [];
  154. const parts = relative.replace("manual-review/pending/", "").split("/");
  155. const name = parts.at(-1)?.replace(/\.test\.ts$/, "");
  156. const domain = parts.length === 1 ? "ui-shell" : parts[0];
  157. if (!name || name.includes("*") || !domain || domain === "conversation-session") return [];
  158. const fixturePath = join(
  159. dirname(caseFixtureDir),
  160. domain === "plugins" ? "plugin-management" : domain,
  161. `${name}.json`,
  162. );
  163. if (!pathExists(fixturePath)) return [];
  164. return [{ fixturePath, manifestPath: join(dirname(caseManifestDir), domain, `${name}.json`) }];
  165. });
  166. if (mode === "strict-case-local") {
  167. const missingPaths = [
  168. ...(pathExists(commonFixturePath) ? [] : [`common fixture: ${commonFixturePath}`]),
  169. ...additionalCases
  170. .filter(({ manifestPath }) => !pathExists(manifestPath))
  171. .map(({ manifestPath }) => `case manifest: ${manifestPath}`),
  172. ...caseNames.flatMap((caseName) => {
  173. const manifestPath = join(caseManifestDir, `${caseName}.json`);
  174. const fixturePath = join(caseFixtureDir, `${caseName}.json`);
  175. return [
  176. ...(pathExists(manifestPath) ? [] : [`case manifest: ${manifestPath}`]),
  177. ...(pathExists(fixturePath) ? [] : [`case fixture: ${fixturePath}`]),
  178. ];
  179. }),
  180. ];
  181. if (missingPaths.length > 0) {
  182. throw new Error(
  183. `Strict case-local Upstream replay fixtures are incomplete:\n${missingPaths
  184. .map((path) => `- ${path}`)
  185. .join("\n")}`,
  186. );
  187. }
  188. }
  189. return uniquePaths([
  190. ...(pathExists(commonFixturePath) ? [commonFixturePath] : []),
  191. ...caseFixturePaths.filter(pathExists),
  192. ...additionalCases.map(({ fixturePath }) => fixturePath),
  193. ...(mode === "strict-case-local" ? [] : pluginFixturePaths),
  194. // 修复原因:formal CI 必须让缺失 matcher 直接失败,不能再由历史共享池替代 case-local fixture 假绿。
  195. ...(mode === "strict-case-local" ? [] : legacyFixturePaths),
  196. ]);
  197. }
  198. function resolveConversationSessionCaseName(spec: string): string | null {
  199. const normalized = spec.replaceAll("\\", "/");
  200. if (!normalized.includes("/test/e2e/conversation-session/")) {
  201. return null;
  202. }
  203. const fileName = normalized.split("/").at(-1) ?? "";
  204. if (!fileName.endsWith(".test.ts") || fileName.includes("*")) {
  205. return null;
  206. }
  207. return fileName.slice(0, -".test.ts".length);
  208. }
  209. function uniquePaths(paths: string[]): string[] {
  210. return [...new Set(paths)];
  211. }