web-remote-control-auth-gate.test.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import {
  4. TID_LOGIN_TRIGGER,
  5. TID_OAUTH_CANCEL,
  6. TID_OAUTH_LOGIN_BUTTON,
  7. TID_TASK_SETTINGS_BUTTON,
  8. } from "@zcode/shared/test-ids";
  9. import { pinE2ELocale, seedCredentials, waitForTestIdByDom } from "#e2e/helpers/desktop-app";
  10. async function enterWorkspace() {
  11. await browser.waitUntil(
  12. async () => {
  13. if (await $('[data-testid="onboarding-page"]').isDisplayed()) await browser.keys("Escape");
  14. return $(`[data-testid="${TID_TASK_SETTINGS_BUTTON}"]`).isExisting();
  15. },
  16. { timeout: 60_000, timeoutMsg: "启动后未进入工作区" },
  17. );
  18. }
  19. async function openRemoteControlDialog(label: string) {
  20. // 设置按钮先于 workspace 恢复出现;必须等待目标入口可见,不能把启动中间态当作缺少门禁。
  21. await browser.waitUntil(
  22. async () => {
  23. for (const trigger of await $$(`button[aria-label="${label}"]`)) {
  24. if (await trigger.isDisplayed()) {
  25. await trigger.click();
  26. return true;
  27. }
  28. }
  29. return false;
  30. },
  31. { timeout: 20_000, timeoutMsg: `未出现远控入口:${label}` },
  32. );
  33. }
  34. describe("桌面远控登录门禁", () => {
  35. it("AUTH-08: 未登录时只展示登录入口,四种主题语言组合无HTTP或WS连接", async function () {
  36. this.timeout(180_000);
  37. const baseUrl = process.env.ZCODE_E2E_REMOTE_AUTH_GATE_URL;
  38. const artifactDir = process.env.ZCODE_E2E_ARTIFACT_DIR;
  39. if (!baseUrl || !artifactDir) throw new Error("缺少远控门禁 fixture 或产物目录");
  40. const endpoint = await browser.electron.execute(() => ({
  41. origin: process.env.ZCODE_ENDPOINT_ORIGIN,
  42. relay: process.env.ZCODE_WEB_REMOTE_CONTROL_RELAY_WS_URL,
  43. }));
  44. expect(endpoint.origin).toBe(baseUrl);
  45. expect(endpoint.relay).toBe(`${baseUrl.replace("http:", "ws:")}/api/v1/remote/ws2`);
  46. await enterWorkspace();
  47. for (const locale of ["zh-CN", "en-US"] as const) {
  48. for (const theme of ["zai-light", "zai-dark"] as const) {
  49. // 运行中只改 setting.json 不会覆盖浏览器已保存的语言;走真实语言操作后再验证刷新。
  50. await pinE2ELocale(locale);
  51. await browser.execute((value) => localStorage.setItem("zcode-theme", value), theme);
  52. await browser.refresh();
  53. await enterWorkspace();
  54. expect(
  55. await browser.execute(
  56. (value) => document.documentElement.classList.contains(`theme-${value}`),
  57. theme,
  58. ),
  59. ).toBe(true);
  60. const label = locale === "zh-CN" ? "移动端远程控制" : "Mobile remote control";
  61. await openRemoteControlDialog(label);
  62. await waitForTestIdByDom("web-remote-control-auth-required");
  63. const card = $('[data-testid="web-remote-control-auth-required"]');
  64. expect(await card.getText()).toContain(
  65. locale === "zh-CN" ? "需要先登录" : "Sign in required",
  66. );
  67. expect(await $('[data-testid="web-remote-control-login"]').getText()).toBe(
  68. locale === "zh-CN" ? "登录" : "Sign in",
  69. );
  70. expect(await $('[data-testid="web-remote-control-copy-link-row"]').isExisting()).toBe(
  71. false,
  72. );
  73. expect(await $('[data-testid="web-remote-control-scan-card"] img').isExisting()).toBe(
  74. false,
  75. );
  76. expect(
  77. await browser.execute(() => {
  78. const bridge = (
  79. window as Window & {
  80. zcode?: { getWebRemoteControlStatus(): Promise<{ status: string }> };
  81. }
  82. ).zcode;
  83. if (!bridge) throw new Error("缺少 preload 远控平台接口");
  84. return bridge.getWebRemoteControlStatus();
  85. }),
  86. ).toEqual({
  87. status: "auth-required",
  88. });
  89. await $('[role="dialog"]').saveScreenshot(
  90. join(artifactDir, `auth-required-${locale}-${theme}.png`),
  91. );
  92. await browser.keys("Escape");
  93. }
  94. }
  95. // 最后一轮仍通过真实远控登录按钮进入现有 OAuth 渠道面板。
  96. await openRemoteControlDialog("Mobile remote control");
  97. await $('[data-testid="web-remote-control-login"]').click();
  98. await waitForTestIdByDom(TID_OAUTH_LOGIN_BUTTON);
  99. const evidence: unknown = await (await fetch(`${baseUrl}/__e2e/status`)).json();
  100. expect(evidence).toEqual({ remoteHttp: 0, wsUpgrades: 0, oauthProviders: [] });
  101. await writeFile(
  102. join(artifactDir, "auth-required-network.json"),
  103. JSON.stringify(evidence, null, 2),
  104. );
  105. });
  106. it("AUTH-08: 远控401保留全局用户时,登录按钮仍启动当前渠道OAuth并停留在等待页", async function () {
  107. this.timeout(90_000);
  108. const baseUrl = process.env.ZCODE_E2E_REMOTE_AUTH_GATE_URL;
  109. const artifactDir = process.env.ZCODE_E2E_ARTIFACT_DIR;
  110. if (!baseUrl || !artifactDir) throw new Error("缺少远控门禁 fixture 或产物目录");
  111. const openExternal = await browser.electron.mock("shell", "openExternal");
  112. await openExternal.mockResolvedValue(undefined);
  113. try {
  114. await seedCredentials();
  115. await browser.refresh();
  116. await enterWorkspace();
  117. // 上一条用例停在 OAuth 页面;先恢复工作区,语言操作入口才重新挂载。
  118. await pinE2ELocale("en-US");
  119. await browser.waitUntil(
  120. async () =>
  121. (await $(`[data-testid="${TID_LOGIN_TRIGGER}"]`).getText()).includes("ZCode E2E"),
  122. { timeout: 20_000, timeoutMsg: "缓存OAuth用户未恢复" },
  123. );
  124. await openRemoteControlDialog("Mobile remote control");
  125. await waitForTestIdByDom("web-remote-control-auth-required");
  126. expect(await $(`[data-testid="${TID_LOGIN_TRIGGER}"]`).getText()).toContain("ZCode E2E");
  127. const login = $('[data-testid="web-remote-control-login"]');
  128. await login.waitForEnabled({ timeout: 10_000 });
  129. await login.click();
  130. await waitForTestIdByDom(TID_OAUTH_CANCEL);
  131. await browser.waitUntil(
  132. async () => {
  133. await openExternal.update();
  134. return openExternal.mock.calls.length === 1;
  135. },
  136. { timeout: 20_000, timeoutMsg: "远控重新登录没有启动浏览器OAuth授权" },
  137. );
  138. expect(await $('[data-testid="web-remote-control-auth-required"]').isExisting()).toBe(false);
  139. const evidence = (await (await fetch(`${baseUrl}/__e2e/status`)).json()) as {
  140. remoteHttp: number;
  141. wsUpgrades: number;
  142. oauthProviders: string[];
  143. };
  144. expect(evidence.remoteHttp).toBeGreaterThan(0);
  145. expect(evidence.wsUpgrades).toBe(0);
  146. expect(evidence.oauthProviders).toEqual(["zai"]);
  147. expect(await $(`[data-testid="${TID_OAUTH_CANCEL}"]`).isDisplayed()).toBe(true);
  148. await browser.saveScreenshot(join(artifactDir, "remote-401-relogin.png"));
  149. await writeFile(
  150. join(artifactDir, "remote-401-relogin.json"),
  151. JSON.stringify(evidence, null, 2),
  152. );
  153. } finally {
  154. await browser.electron.restoreAllMocks();
  155. }
  156. });
  157. });