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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 { seedCredentials, seedSettings, 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. await seedSettings({ locale, localePreference: locale });
  50. await browser.execute((value) => localStorage.setItem("zcode-theme", value), theme);
  51. await browser.refresh();
  52. await enterWorkspace();
  53. expect(
  54. await browser.execute(
  55. (value) => document.documentElement.classList.contains(`theme-${value}`),
  56. theme,
  57. ),
  58. ).toBe(true);
  59. const label = locale === "zh-CN" ? "移动端远程控制" : "Mobile remote control";
  60. await openRemoteControlDialog(label);
  61. await waitForTestIdByDom("web-remote-control-auth-required");
  62. const card = $('[data-testid="web-remote-control-auth-required"]');
  63. expect(await card.getText()).toContain(
  64. locale === "zh-CN" ? "需要先登录" : "Sign in required",
  65. );
  66. expect(await $('[data-testid="web-remote-control-login"]').getText()).toBe(
  67. locale === "zh-CN" ? "登录" : "Sign in",
  68. );
  69. expect(await $('[data-testid="web-remote-control-copy-link-row"]').isExisting()).toBe(
  70. false,
  71. );
  72. expect(await $('[data-testid="web-remote-control-scan-card"] img').isExisting()).toBe(
  73. false,
  74. );
  75. expect(
  76. await browser.execute(() => {
  77. const bridge = (
  78. window as Window & {
  79. zcode?: { getWebRemoteControlStatus(): Promise<{ status: string }> };
  80. }
  81. ).zcode;
  82. if (!bridge) throw new Error("缺少 preload 远控平台接口");
  83. return bridge.getWebRemoteControlStatus();
  84. }),
  85. ).toEqual({
  86. status: "auth-required",
  87. });
  88. await $('[role="dialog"]').saveScreenshot(
  89. join(artifactDir, `auth-required-${locale}-${theme}.png`),
  90. );
  91. await browser.keys("Escape");
  92. }
  93. }
  94. // 最后一轮仍通过真实远控登录按钮进入现有 OAuth 渠道面板。
  95. await openRemoteControlDialog("Mobile remote control");
  96. await $('[data-testid="web-remote-control-login"]').click();
  97. await waitForTestIdByDom(TID_OAUTH_LOGIN_BUTTON);
  98. const evidence: unknown = await (await fetch(`${baseUrl}/__e2e/status`)).json();
  99. expect(evidence).toEqual({ remoteHttp: 0, wsUpgrades: 0, oauthProviders: [] });
  100. await writeFile(
  101. join(artifactDir, "auth-required-network.json"),
  102. JSON.stringify(evidence, null, 2),
  103. );
  104. });
  105. it("AUTH-08: 远控401保留全局用户时,登录按钮仍启动当前渠道OAuth并停留在等待页", async function () {
  106. this.timeout(90_000);
  107. const baseUrl = process.env.ZCODE_E2E_REMOTE_AUTH_GATE_URL;
  108. const artifactDir = process.env.ZCODE_E2E_ARTIFACT_DIR;
  109. if (!baseUrl || !artifactDir) throw new Error("缺少远控门禁 fixture 或产物目录");
  110. const openExternal = await browser.electron.mock("shell", "openExternal");
  111. await openExternal.mockResolvedValue(undefined);
  112. try {
  113. await seedCredentials();
  114. await seedSettings({ locale: "en-US", localePreference: "en-US" });
  115. await browser.refresh();
  116. await enterWorkspace();
  117. await browser.waitUntil(
  118. async () =>
  119. (await $(`[data-testid="${TID_LOGIN_TRIGGER}"]`).getText()).includes("ZCode E2E"),
  120. { timeout: 20_000, timeoutMsg: "缓存OAuth用户未恢复" },
  121. );
  122. await openRemoteControlDialog("Mobile remote control");
  123. await waitForTestIdByDom("web-remote-control-auth-required");
  124. expect(await $(`[data-testid="${TID_LOGIN_TRIGGER}"]`).getText()).toContain("ZCode E2E");
  125. const login = $('[data-testid="web-remote-control-login"]');
  126. await login.waitForEnabled({ timeout: 10_000 });
  127. await login.click();
  128. await waitForTestIdByDom(TID_OAUTH_CANCEL);
  129. await browser.waitUntil(
  130. async () => {
  131. await openExternal.update();
  132. return openExternal.mock.calls.length === 1;
  133. },
  134. { timeout: 20_000, timeoutMsg: "远控重新登录没有启动浏览器OAuth授权" },
  135. );
  136. expect(await $('[data-testid="web-remote-control-auth-required"]').isExisting()).toBe(false);
  137. const evidence = (await (await fetch(`${baseUrl}/__e2e/status`)).json()) as {
  138. remoteHttp: number;
  139. wsUpgrades: number;
  140. oauthProviders: string[];
  141. };
  142. expect(evidence.remoteHttp).toBeGreaterThan(0);
  143. expect(evidence.wsUpgrades).toBe(0);
  144. expect(evidence.oauthProviders).toEqual(["zai"]);
  145. expect(await $(`[data-testid="${TID_OAUTH_CANCEL}"]`).isDisplayed()).toBe(true);
  146. await browser.saveScreenshot(join(artifactDir, "remote-401-relogin.png"));
  147. await writeFile(
  148. join(artifactDir, "remote-401-relogin.json"),
  149. JSON.stringify(evidence, null, 2),
  150. );
  151. } finally {
  152. await browser.electron.restoreAllMocks();
  153. }
  154. });
  155. });