run-all-e2e-coverage.mjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env node
  2. import { spawn } from "node:child_process";
  3. import { existsSync, readFileSync } from "node:fs";
  4. import { resolve } from "node:path";
  5. import process from "node:process";
  6. import { fileURLToPath } from "node:url";
  7. import { parse as parseDotenv } from "dotenv";
  8. import { hasRequiredSshE2EEnvironment } from "./e2e-formal-spec-selection.mjs";
  9. const DESKTOP_DIR = resolve(fileURLToPath(new URL("..", import.meta.url)));
  10. const REPO_ROOT = resolve(DESKTOP_DIR, "../..");
  11. const UPSTREAM_PROVIDER_SPEC = "./test/e2e/upstream-provider.test.ts";
  12. const ALL_E2E_SPECS = [
  13. "./test/e2e/**/*.test.ts",
  14. "./test/e2e/conversation-session/manual-review/pending/conversation-session-turn-steer-probe.test.ts",
  15. ];
  16. const SSH_E2E_EXCLUDES = [
  17. "./test/e2e/conversation-session/conversation-session-ssh-remote*.test.ts",
  18. "./test/e2e/conversation-session/manual-review/**/conversation-session-ssh-remote*.test.ts",
  19. ];
  20. // 仓库不提供供应商地址与 key:两者只来自 shell 或本机 .env.e2e.local,示例文件值留空不参与判断。
  21. const PROVIDER_ENV_FILES = [
  22. resolve(DESKTOP_DIR, ".env.e2e.local"),
  23. resolve(REPO_ROOT, ".env.e2e.local"),
  24. ];
  25. // 真实 provider smoke 走 capture,需要环境变量同时给出上游地址与真实 key(回放占位 key 不算)。
  26. export function hasRealUpstreamProviderConfig(env = process.env, envFiles = PROVIDER_ENV_FILES) {
  27. const read = (key) =>
  28. env[key]?.trim() || envFiles.map((envFile) => readEnvValue(envFile, key)).find(Boolean) || "";
  29. const apiKey = read("E2E_PROVIDER_API_KEY");
  30. return Boolean(read("E2E_PROVIDER_BASE_URL") && apiKey && apiKey !== "e2e-fixture-key");
  31. }
  32. function readEnvValue(envFile, key) {
  33. if (!existsSync(envFile)) return "";
  34. return parseDotenv(readFileSync(envFile))[key]?.trim() || "";
  35. }
  36. export function createAllE2ECoverageRunPlan({
  37. args = [],
  38. env = process.env,
  39. upstreamReady = hasRealUpstreamProviderConfig(env),
  40. platform = process.platform,
  41. sshReady = hasRequiredSshE2EEnvironment(env),
  42. } = {}) {
  43. return {
  44. command: platform === "win32" ? "pnpm.cmd" : "pnpm",
  45. args: [
  46. "test:e2e",
  47. "--",
  48. ...ALL_E2E_SPECS.flatMap((spec) => ["--spec", spec]),
  49. ...(sshReady ? [] : SSH_E2E_EXCLUDES.flatMap((spec) => ["--exclude", spec])),
  50. ...(upstreamReady ? [] : ["--exclude", UPSTREAM_PROVIDER_SPEC]),
  51. ...args,
  52. ],
  53. env: {
  54. ...env,
  55. ZCODE_E2E_COVERAGE: "1",
  56. ZCODE_E2E_MANUAL_REVIEW: "1",
  57. },
  58. notes: upstreamReady
  59. ? []
  60. : [
  61. "skipping real provider smoke: configure E2E_PROVIDER_BASE_URL and E2E_PROVIDER_API_KEY to include it",
  62. ],
  63. };
  64. }
  65. export async function runAllE2ECoverage(options = {}) {
  66. const plan = createAllE2ECoverageRunPlan(options);
  67. for (const note of plan.notes) {
  68. console.warn(`[e2e-all-coverage] ${note}`);
  69. }
  70. const exitCode = await new Promise((resolveExit, reject) => {
  71. const child = spawn(plan.command, plan.args, {
  72. cwd: options.cwd ?? DESKTOP_DIR,
  73. env: plan.env,
  74. stdio: "inherit",
  75. });
  76. child.once("error", reject);
  77. child.once("exit", (code) => resolveExit(code ?? 1));
  78. });
  79. if (exitCode !== 0) {
  80. throw new Error(`all E2E coverage run exited with code ${exitCode}`);
  81. }
  82. }
  83. const isEntrypoint =
  84. typeof process.argv[1] === "string" &&
  85. resolve(process.argv[1]) === fileURLToPath(import.meta.url);
  86. if (isEntrypoint) {
  87. try {
  88. await runAllE2ECoverage({ args: process.argv.slice(2) });
  89. } catch (error) {
  90. console.error(`[e2e-all-coverage] ${error instanceof Error ? error.message : String(error)}`);
  91. process.exitCode = 1;
  92. }
  93. }