e2eSpecAdmission.test.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import { readFileSync } from "node:fs";
  2. import { copyFile, mkdtemp, mkdir, writeFile, rm } from "node:fs/promises";
  3. import { pathToFileURL } from "node:url";
  4. import { tmpdir } from "node:os";
  5. import { describe, expect, it } from "vitest";
  6. import { resolve, join } from "node:path";
  7. import { resolveE2EScheduledSpecFiles } from "#e2e/helpers/e2e-spec-resolution";
  8. import {
  9. hasRequiredSshE2EEnvironment,
  10. isFormalSpecAdmittedByEnvironment,
  11. isSshFormalSpec,
  12. isExplicitE2ESpecSelection,
  13. MANUAL_REVIEW_SPEC_GLOB,
  14. resolveEnvironmentGatedFormalSpecExcludes,
  15. SSH_FORMAL_SPEC_GLOB,
  16. } from "../scripts/e2e-formal-spec-selection.mjs";
  17. describe("desktop E2E environment-gated formal spec admission", () => {
  18. it("loads public admission rules without the internal remote harness", async () => {
  19. const fixture = await mkdtemp(join(tmpdir(), "e2e-public-admission-"));
  20. try {
  21. const scripts = join(fixture, "packages/desktop/scripts");
  22. await mkdir(scripts, { recursive: true });
  23. for (const file of [
  24. "e2e-formal-spec-selection.mjs",
  25. "e2e-remote-target-specs.json",
  26. "r3-remote-spec-inventory.mjs",
  27. ]) {
  28. await copyFile(resolve("packages/desktop/scripts", file), join(scripts, file));
  29. }
  30. const admission = await import(
  31. pathToFileURL(join(scripts, "e2e-formal-spec-selection.mjs")).href
  32. );
  33. expect(
  34. admission.resolveEnvironmentGatedFormalSpecExcludes({ env: {}, targeted: false }),
  35. ).toContain(
  36. "./test/e2e/conversation-session/remote/configuration/conversation-session-r3-model-switch.test.ts",
  37. );
  38. expect(
  39. admission.isSshFormalSpec(
  40. "./test/e2e/conversation-session/remote/connection/conversation-session-remote-offline-icons.test.ts",
  41. ),
  42. ).toBe(false);
  43. } finally {
  44. await rm(fixture, { recursive: true, force: true });
  45. }
  46. });
  47. it("excludes every manual-review domain from the default recursive glob", () => {
  48. const scheduled = resolveE2EScheduledSpecFiles({
  49. baseDir: resolve("packages/desktop"),
  50. specs: ["./test/e2e/**/*.test.ts"],
  51. exclude: [MANUAL_REVIEW_SPEC_GLOB],
  52. });
  53. expect(scheduled).toContain("test/e2e/infrastructure/smoke/smoke.test.ts");
  54. expect(scheduled.some((spec) => spec.includes("/manual-review/"))).toBe(false);
  55. });
  56. it.each(["bots", "settings", "feedback"])(
  57. "keeps explicit %s candidates discoverable",
  58. async (domain) => {
  59. const fixture = await mkdtemp(join(tmpdir(), "e2e-spec-discovery-"));
  60. try {
  61. // 转正后领域可以没有候选;发现规则必须使用独立 fixture,不能依赖仓库一直留有 pending。
  62. const directory = join(fixture, "test/e2e", domain, "manual-review/pending");
  63. await mkdir(directory, { recursive: true });
  64. await writeFile(join(directory, "example.test.ts"), "");
  65. const scheduled = resolveE2EScheduledSpecFiles({
  66. baseDir: fixture,
  67. specs: [`./test/e2e/${domain}/manual-review/**/*.test.ts`],
  68. exclude: [],
  69. });
  70. expect(scheduled).toEqual([`test/e2e/${domain}/manual-review/pending/example.test.ts`]);
  71. } finally {
  72. await rm(fixture, { recursive: true, force: true });
  73. }
  74. },
  75. );
  76. it("applies environment gates to formal globs but keeps exact-file runs fail-closed", () => {
  77. const glob = ["./test/e2e/conversation-session/*.test.ts"];
  78. // 此处验证 canonical 正式目标;实际候选可以暂留 pending,不能随目录回退改掉门禁输入。
  79. const file = [
  80. "./test/e2e/conversation-session/remote/configuration/conversation-session-r3-model-switch.test.ts",
  81. ];
  82. expect(isExplicitE2ESpecSelection(glob)).toBe(false);
  83. expect(isExplicitE2ESpecSelection([])).toBe(false);
  84. expect(isExplicitE2ESpecSelection(file)).toBe(true);
  85. expect(
  86. resolveEnvironmentGatedFormalSpecExcludes({
  87. env: {},
  88. targeted: isExplicitE2ESpecSelection(glob),
  89. }),
  90. ).toContain(file[0]);
  91. expect(
  92. resolveEnvironmentGatedFormalSpecExcludes({
  93. env: {},
  94. targeted: isExplicitE2ESpecSelection(file),
  95. }),
  96. ).toEqual([]);
  97. });
  98. it("gates promoted remote targets while retaining local offline coverage", () => {
  99. expect(
  100. isSshFormalSpec(
  101. "./test/e2e/conversation-session/remote/configuration/conversation-session-r3-model-switch.test.ts",
  102. ),
  103. ).toBe(true);
  104. expect(
  105. isSshFormalSpec(
  106. "./test/e2e/conversation-session/remote/navigation/conversation-session-chat-remote-folder.test.ts",
  107. ),
  108. ).toBe(true);
  109. expect(
  110. isSshFormalSpec(
  111. "./test/e2e/conversation-session/remote/connection/conversation-session-remote-offline-icons.test.ts",
  112. ),
  113. ).toBe(false);
  114. expect(isSshFormalSpec("./test/e2e/bots/runtime/bot-r3-workspace-identity.test.ts")).toBe(
  115. false,
  116. );
  117. expect(resolveEnvironmentGatedFormalSpecExcludes({ env: {}, targeted: false })).toContain(
  118. "./test/e2e/conversation-session/remote/configuration/conversation-session-r3-model-switch.test.ts",
  119. );
  120. });
  121. it("excludes real SSH specs from default runs without complete credentials", () => {
  122. expect(hasRequiredSshE2EEnvironment({})).toBe(false);
  123. expect(resolveEnvironmentGatedFormalSpecExcludes({ env: {}, targeted: false })).toContain(
  124. "./test/e2e/conversation-session/**/conversation-session-ssh-remote*.test.ts",
  125. );
  126. });
  127. it("keeps explicit SSH runs fail-closed and admits configured default runs", () => {
  128. const env = {
  129. ZCODE_E2E_SSH_HOST: "ssh.example.test",
  130. ZCODE_E2E_SSH_USERNAME: "root",
  131. ZCODE_E2E_SSH_PASSWORD: "secret-from-test-env",
  132. ZCODE_E2E_SSH_CONTAINER: "zcode-r3-server-example",
  133. };
  134. expect(hasRequiredSshE2EEnvironment(env)).toBe(true);
  135. expect(resolveEnvironmentGatedFormalSpecExcludes({ env, targeted: false })).toEqual([]);
  136. expect(resolveEnvironmentGatedFormalSpecExcludes({ env: {}, targeted: true })).toEqual([]);
  137. });
  138. it("requires the harness container for queued remote targets", () => {
  139. // 远程队列按 case 准备干净目标;只有共享 SSH 凭据的 runner 不能运行这些用例。
  140. const credentials = {
  141. ZCODE_E2E_SSH_HOST: "ssh.example.test",
  142. ZCODE_E2E_SSH_USERNAME: "root",
  143. ZCODE_E2E_SSH_PASSWORD: "secret-from-test-env",
  144. };
  145. const queued =
  146. "./test/e2e/conversation-session/remote/configuration/conversation-session-r3-model-switch.test.ts";
  147. const queuedSshRemote =
  148. "./test/e2e/conversation-session/remote/connection/conversation-session-ssh-remote-development-p0.test.ts";
  149. // 尚未排进队列的 ssh-remote 用例只依赖真实服务器凭据。
  150. const unqueuedSshRemote =
  151. "./test/e2e/conversation-session/conversation-session-ssh-remote-example.test.ts";
  152. const excludes = resolveEnvironmentGatedFormalSpecExcludes({
  153. env: credentials,
  154. targeted: false,
  155. });
  156. expect(excludes).toContain(queued);
  157. expect(excludes).toContain(queuedSshRemote);
  158. expect(excludes).not.toContain(SSH_FORMAL_SPEC_GLOB);
  159. expect(isFormalSpecAdmittedByEnvironment(queued, credentials)).toBe(false);
  160. expect(isFormalSpecAdmittedByEnvironment(queuedSshRemote, credentials)).toBe(false);
  161. expect(isFormalSpecAdmittedByEnvironment(unqueuedSshRemote, credentials)).toBe(true);
  162. expect(isFormalSpecAdmittedByEnvironment(unqueuedSshRemote, {})).toBe(false);
  163. expect(
  164. isFormalSpecAdmittedByEnvironment(queued, {
  165. ...credentials,
  166. ZCODE_E2E_SSH_CONTAINER: "zcode-r3-server-example",
  167. }),
  168. ).toBe(true);
  169. expect(
  170. isFormalSpecAdmittedByEnvironment(
  171. "./test/e2e/conversation-session/remote/connection/conversation-session-remote-offline-icons.test.ts",
  172. {},
  173. ),
  174. ).toBe(true);
  175. });
  176. it("keeps nested formal scenarios discoverable and nested candidates excluded", () => {
  177. const formal = resolveE2EScheduledSpecFiles({
  178. baseDir: resolve("packages/desktop"),
  179. specs: ["./test/e2e/conversation-session/**/*.test.ts"],
  180. exclude: [MANUAL_REVIEW_SPEC_GLOB],
  181. });
  182. expect(formal).toContain(
  183. "test/e2e/conversation-session/composer/conversation-session-first-send.test.ts",
  184. );
  185. expect(formal).toContain(
  186. "test/e2e/conversation-session/remote/connection/conversation-session-ssh-remote-banner-reconnect.test.ts",
  187. );
  188. expect(formal.some((spec) => spec.includes("/manual-review/"))).toBe(false);
  189. expect(
  190. isSshFormalSpec(
  191. "./test/e2e/conversation-session/remote/connection/conversation-session-ssh-remote-example.test.ts",
  192. ),
  193. ).toBe(true);
  194. });
  195. it("uses the shared admission helper in the WDIO exclude list", () => {
  196. const source = readFileSync(new URL("../wdio.conf.ts", import.meta.url), "utf8");
  197. expect(source).toContain("resolveEnvironmentGatedFormalSpecExcludes({");
  198. expect(source).toContain("hasRequiredSshE2EEnvironment(process.env)");
  199. });
  200. });