desktopE2EContainerScript.test.ts 7.0 KB

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