run-all-e2e-coverage.mjs 3.5 KB

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