desktopE2EContainerScript.test.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { spawnSync } from "node:child_process";
  2. import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
  3. import { readFile } from "node:fs/promises";
  4. import { tmpdir } from "node:os";
  5. import { join, resolve } from "node:path";
  6. import { describe, expect, it } from "vitest";
  7. import { insertVerifiedDockerSpecRow } from "../scripts/conversation-docker-plan.mjs";
  8. const repoRoot = process.cwd();
  9. const containerScriptPath = resolve(repoRoot, "scripts/test-desktop-e2e-container.sh");
  10. describe("desktop E2E container presets", () => {
  11. it("keeps the default smoke preset free of stale conversation specs", () => {
  12. const script = readFileSync(containerScriptPath, "utf-8");
  13. const defaultAssignment = script.match(/^DEFAULT_E2E_SPEC="([^"]*)"$/m);
  14. expect(defaultAssignment).not.toBeNull();
  15. expect(defaultAssignment?.[1]).not.toContain("./test/e2e/conversation-session/");
  16. });
  17. it("makes the V4 verified conversation preset explicitly unavailable until Docker evidence exists", () => {
  18. const script = readFileSync(containerScriptPath, "utf-8");
  19. expect(script).toContain('CONVERSATION_SESSION_VERIFIED_E2E_SPEC=""');
  20. expect(script).toContain(
  21. "No V4 conversation-session spec has Docker replay-isolated admission evidence",
  22. );
  23. expect(script).not.toContain("conversation-session-admission)");
  24. expect(script).not.toContain("conversation-session-needs-docker-fix)");
  25. expect(script).not.toContain("conversation-session-tool-cross-product)");
  26. });
  27. it("retires package commands for stale conversation diagnostic presets", () => {
  28. const packageJson = JSON.parse(readFileSync(resolve(repoRoot, "package.json"), "utf-8")) as {
  29. scripts: Record<string, string>;
  30. };
  31. expect(packageJson.scripts["test:e2e:container:conversation"]).toContain(
  32. "conversation-session-verified",
  33. );
  34. expect(packageJson.scripts["test:e2e:container:conversation:admission"]).toBeUndefined();
  35. expect(packageJson.scripts["test:e2e:container:conversation:needs-docker-fix"]).toBeUndefined();
  36. expect(
  37. packageJson.scripts["test:e2e:container:conversation:tool-cross-product"],
  38. ).toBeUndefined();
  39. expect(packageJson.scripts["test:e2e:container:conversation:all"]).toBeUndefined();
  40. });
  41. it("keeps plugin Docker admission review-gated while exposing the verified preset command", () => {
  42. const script = readFileSync(containerScriptPath, "utf-8");
  43. const packageJson = JSON.parse(readFileSync(resolve(repoRoot, "package.json"), "utf-8")) as {
  44. scripts: Record<string, string>;
  45. };
  46. expect(script).toContain('PLUGIN_MANAGEMENT_VERIFIED_E2E_SPEC=""');
  47. expect(script).toContain("plugins-verified)");
  48. expect(script).toContain("No plugin-management spec has completed human review");
  49. expect(packageJson.scripts["test:e2e:container:plugins"]).toContain("plugins-verified");
  50. });
  51. it("references only existing conversation spec paths", () => {
  52. const script = readFileSync(containerScriptPath, "utf-8");
  53. const conversationPaths = [
  54. ...script.matchAll(/\.\/test\/e2e\/conversation-session\/[^,"'\\s]+\.test\.ts/g),
  55. ].map(([path]) => path);
  56. const missingPaths = conversationPaths.filter(
  57. (path) => !existsSync(resolve(repoRoot, "packages/desktop", path)),
  58. );
  59. expect(missingPaths).toEqual([]);
  60. });
  61. it.each(["\n", "\r\n"])(
  62. "keeps the formatted Docker plan table writable with %j line endings",
  63. async (newline) => {
  64. const dockerPlan = (
  65. await readFile(
  66. resolve(repoRoot, "docs/testing/conversation-session-docker-automation-plan.md"),
  67. "utf-8",
  68. )
  69. )
  70. .replaceAll("\r\n", "\n")
  71. .replaceAll("\n", newline);
  72. const marker = "<!-- conversation-session-verified-table-end -->";
  73. const tablePrefix = dockerPlan.slice(0, dockerPlan.indexOf(marker)).trimEnd();
  74. // 格式化会调整横线宽度和标记前空行;检查表结构,并验证新增行没有被空行隔到表外。
  75. expect(tablePrefix).toMatch(
  76. /\|\s*Spec\s*\|\s*迁移状态\s*\|\r?\n\|[ \t]*:?-{3,}:?[ \t]*\|[ \t]*:?-{3,}:?[ \t]*\|$/u,
  77. );
  78. const spec = "./test/e2e/conversation-session/example.test.ts";
  79. const row = `| \`${spec}\` | verified |`;
  80. const updated = insertVerifiedDockerSpecRow(dockerPlan, spec);
  81. expect(updated.inserted).toBe(true);
  82. expect(updated.text).toContain(`${tablePrefix}${newline}${row}${newline}${marker}`);
  83. expect(insertVerifiedDockerSpecRow(updated.text, spec)).toEqual({
  84. inserted: false,
  85. text: updated.text,
  86. });
  87. const nextSpec = "./test/e2e/conversation-session/another.test.ts";
  88. const next = insertVerifiedDockerSpecRow(updated.text, nextSpec);
  89. expect(next.inserted).toBe(true);
  90. expect(next.text).toContain(
  91. `${row}${newline}| \`${nextSpec}\` | verified |${newline}${marker}`,
  92. );
  93. },
  94. );
  95. });
  96. // 用假的 docker 记录参数,验证容器脚本透传给容器的上游供应商配置;取最后一次 docker run(前面一次是驱动预取)。
  97. function runContainerScriptWithFakeDocker(env: Record<string, string>) {
  98. const dir = mkdtempSync(join(tmpdir(), "zcode-e2e-container-"));
  99. try {
  100. const binDir = join(dir, "bin");
  101. mkdirSync(binDir);
  102. const logPath = join(dir, "docker.log");
  103. // 只记录 run:容器脚本的后台采样会并发调用 docker inspect/stats,长的 run 参数行分多次写入时
  104. // 会与之交错(曾出现 "-e E2inspect ...");两次 docker run 本身串行,只记 run 就没有并发写。
  105. writeFileSync(
  106. join(binDir, "docker"),
  107. `#!/usr/bin/env bash\nif [ "$1" = run ]; then printf '%s\\n' "$*" >> "${logPath}"; if [[ "$*" == *"chromedriver-preflight.mjs path"* ]]; then printf '%s\\n' '/cache/chromedriver-fixture'; fi; fi\n`,
  108. {
  109. mode: 0o755,
  110. },
  111. );
  112. const result = spawnSync("bash", [containerScriptPath], {
  113. cwd: repoRoot,
  114. encoding: "utf8",
  115. env: {
  116. ...process.env,
  117. PATH: `${binDir}:${process.env.PATH ?? ""}`,
  118. SKIP_IMAGE_BUILD: "1",
  119. E2E_ARTIFACT_DIR: join(dir, "artifacts"),
  120. PERF_SAMPLE_INTERVAL_SECONDS: "1",
  121. ...env,
  122. },
  123. });
  124. const runArgs = existsSync(logPath)
  125. ? (readFileSync(logPath, "utf8")
  126. .split("\n")
  127. .findLast((line) => line.startsWith("run ")) ?? "")
  128. : "";
  129. return { status: result.status, stderr: result.stderr, runArgs };
  130. } finally {
  131. rmSync(dir, { recursive: true, force: true });
  132. }
  133. }
  134. describe.skipIf(process.platform === "win32")("desktop E2E container upstream provider env", () => {
  135. it("capture 模式把上游地址、模型与 preset 透传给容器", () => {
  136. const result = runContainerScriptWithFakeDocker({
  137. E2E_NETWORK_MODE: "capture",
  138. E2E_PROVIDER_API_KEY: "test-key",
  139. E2E_PROVIDER_BASE_URL: "https://upstream.example/v1",
  140. E2E_PROVIDER_MODEL: "model-a",
  141. E2E_PROVIDER_SECONDARY_MODEL: "model-b",
  142. E2E_PROVIDER_PRESET: "env",
  143. });
  144. expect(result.status).toBe(0);
  145. expect(result.runArgs).toContain("CHROMEDRIVER_PATH=/cache/chromedriver-fixture");
  146. expect(result.runArgs).toContain("E2E_PROVIDER_HTTP_MODE=capture");
  147. expect(result.runArgs).toContain("E2E_PROVIDER_BASE_URL=https://upstream.example/v1");
  148. expect(result.runArgs).toContain("E2E_PROVIDER_MODEL=model-a");
  149. expect(result.runArgs).toContain("E2E_PROVIDER_SECONDARY_MODEL=model-b");
  150. expect(result.runArgs).toContain("E2E_PROVIDER_PRESET=env");
  151. });
  152. it("capture 模式缺少上游地址时在启动容器前失败", () => {
  153. const result = runContainerScriptWithFakeDocker({
  154. E2E_NETWORK_MODE: "capture",
  155. E2E_PROVIDER_API_KEY: "test-key",
  156. E2E_PROVIDER_BASE_URL: "",
  157. });
  158. expect(result.status).toBe(1);
  159. expect(result.stderr).toContain("requires E2E_PROVIDER_BASE_URL");
  160. expect(result.runArgs).toBe("");
  161. });
  162. });