remote-device-groups.mjs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // MD08–MD10:设备按完整 MID 聚合,SID 保持独立选择和连接。
  2. // 启动及受控 OAuth/relay 边界同 remote-login-v2.mjs;本用例保持 pending,不操作线上设备。
  3. // 运行:node packages/web/test/e2e/remote-control/projects/manual-review/pending/remote-device-groups.mjs
  4. import assert from "node:assert/strict";
  5. import { mkdir, writeFile } from "node:fs/promises";
  6. import { resolve } from "node:path";
  7. import { chromium } from "playwright-core";
  8. import {
  9. setup,
  10. assertEventually,
  11. } from "../../../authentication/manual-review/pending/remote-login-v2.helpers.mjs";
  12. const origin = process.env.REMOTE_LOGIN_E2E_ORIGIN || "http://127.0.0.1:5178";
  13. const artifacts = resolve(".tmp/remote-device-groups-e2e");
  14. await mkdir(artifacts, { recursive: true });
  15. const browser = await chromium.launch({ channel: "chrome", headless: true });
  16. const results = [];
  17. const nowSeconds = Math.floor(Date.now() / 1000);
  18. const current = {
  19. device_sid: "e2e-device",
  20. device_mid: "e2e-mid-first",
  21. online: true,
  22. created_at: nowSeconds - 3 * 3600,
  23. meta: { name: "Alice’s MacBook", platform: "darwin", version: "3.14.1" },
  24. };
  25. const alternate = {
  26. ...current,
  27. device_sid: "e2e-device-alternative-with-a-long-connection-identifier",
  28. created_at: nowSeconds - 2 * 86_400,
  29. meta: { name: "Alice’s MacBook", platform: "darwin", version: "3.14.2" },
  30. };
  31. const otherMachine = {
  32. ...current,
  33. device_sid: "other-machine",
  34. device_mid: "e2e-mid-second",
  35. online: false,
  36. meta: {},
  37. };
  38. const history = Array.from({ length: 11 }, (_, i) => ({
  39. ...current,
  40. device_sid: `history-${i}`,
  41. online: false,
  42. meta: {},
  43. }));
  44. try {
  45. for (const width of [390, 1024])
  46. for (const theme of ["zai-light", "zai-dark"])
  47. for (const locale of ["zh-CN", "en-US"])
  48. for (const screen of ["single", "multiple", "offline"]) {
  49. const selected = screen === "multiple" ? alternate : current;
  50. const devices = [
  51. ...history,
  52. screen === "offline" ? { ...current, online: false, meta: {} } : current,
  53. ...(screen === "multiple" ? [alternate] : []),
  54. otherMachine,
  55. ];
  56. const h = await setup(browser, {
  57. width,
  58. theme,
  59. locale,
  60. signedIn: true,
  61. devices,
  62. expectedDeviceSid: selected.device_sid,
  63. expectedDeviceMid: selected.device_mid,
  64. });
  65. try {
  66. await h.page.goto(`${origin}/remote/v4?app_version=3.14.1`);
  67. const rows = h.page.locator("main li");
  68. await rows.first().waitFor();
  69. // 相同前缀但不同完整 MID 仍然是两个设备;历史 SID 不占用设备项。
  70. assert.equal(await rows.count(), 2);
  71. assert.equal(h.traffic.tokens, 0);
  72. // 多连接也只占一行按钮,点开底部弹层后再选具体连接。
  73. const row = rows.first().getByRole("button");
  74. assert.equal(await rows.first().getByRole("button").count(), 1);
  75. assert.equal(await rows.last().getByRole("button").isDisabled(), true);
  76. if (screen === "offline") assert.equal(await row.isDisabled(), true);
  77. else assert.match(await rows.first().innerText(), /Alice’s MacBook/);
  78. if (screen === "multiple")
  79. assert.match(await rows.first().innerText(), /2 个连接|2 connections/);
  80. assert.equal(
  81. await h.page.evaluate(() => document.documentElement.scrollWidth > innerWidth),
  82. false,
  83. );
  84. await h.page.evaluate(() => document.fonts.ready);
  85. const screenshot = `${screen}-${width}-${theme}-${locale}.png`;
  86. await h.page.screenshot({ path: resolve(artifacts, screenshot), fullPage: true });
  87. // 刷新时禁用设备行;失败仍禁用,成功后从响应重建,不追加历史记录。
  88. if (screen === "multiple") {
  89. let mode = "pending";
  90. const pending = Promise.withResolvers();
  91. await h.page.route("**/api/v1/remote/device", async (route) => {
  92. h.traffic.lists++;
  93. if (mode === "pending") {
  94. await pending.promise;
  95. await route.fulfill({ status: 503 });
  96. } else {
  97. await route.fulfill({ json: { code: 0, msg: "", data: [...devices].reverse() } });
  98. }
  99. });
  100. await h.page.getByRole("button", { name: /^(刷新|Refresh)$/ }).click();
  101. await assertEventually(() => h.traffic.lists === 2);
  102. assert.equal(await row.isDisabled(), true);
  103. pending.resolve();
  104. await h.page.getByRole("alert").waitFor();
  105. assert.equal(await row.isDisabled(), true);
  106. assert.equal(h.traffic.tokens, 0);
  107. mode = "success";
  108. await h.page.getByRole("button", { name: /^(刷新|Refresh)$/ }).click();
  109. await h.page.getByRole("alert").waitFor({ state: "hidden" });
  110. // loading 开始就会清除旧错误;必须等请求结束再测试键盘选择。
  111. await assertEventually(() => h.traffic.lists === 3);
  112. await h.page.getByRole("button", { name: /^(刷新|Refresh)$/ }).waitFor();
  113. for (let attempt = 0; attempt < 50 && (await row.isDisabled()); attempt++)
  114. await h.page.waitForTimeout(100);
  115. assert.equal(await row.isDisabled(), false);
  116. assert.equal(await rows.count(), 2);
  117. }
  118. if (screen !== "offline") {
  119. await row.focus();
  120. await h.page.keyboard.press("Enter");
  121. if (screen === "multiple") {
  122. const sheet = h.page.getByRole("dialog");
  123. await sheet.waitFor();
  124. // DESIGN 的弹窗表面/圆角在两种主题都必须生效,不能使用设备卡片表面。
  125. const dialogStyle = await sheet.evaluate((element) => {
  126. const probe = document.createElement("div");
  127. probe.className = "bg-popover border-popover-border rounded-t-2xl";
  128. document.body.append(probe);
  129. const actual = getComputedStyle(element);
  130. const expected = getComputedStyle(probe);
  131. const result = {
  132. actual: [
  133. actual.backgroundColor,
  134. actual.borderTopColor,
  135. actual.borderTopLeftRadius,
  136. ],
  137. expected: [
  138. expected.backgroundColor,
  139. expected.borderTopColor,
  140. expected.borderTopLeftRadius,
  141. ],
  142. };
  143. probe.remove();
  144. return result;
  145. });
  146. assert.deepEqual(dialogStyle.actual, dialogStyle.expected);
  147. const connections = sheet.locator("ul button");
  148. assert.equal(await connections.count(), 2);
  149. assert.match(await connections.nth(1).innerText(), /3\.14\.2/);
  150. // 完整 SID 不在界面展示,只显示末 6 位。
  151. assert.doesNotMatch(await sheet.innerText(), /e2e-device-alternative/);
  152. await h.page.evaluate(() => document.fonts.ready);
  153. await h.page.screenshot({
  154. path: resolve(artifacts, `sheet-${width}-${theme}-${locale}.png`),
  155. animations: "disabled",
  156. });
  157. await connections.nth(1).focus();
  158. await h.page.keyboard.press("Enter");
  159. }
  160. await assertEventually(() => h.traffic.sockets === 1);
  161. await h.page
  162. .getByRole("heading", { name: /该设备不属于当前账号|This device does not belong/ })
  163. .waitFor();
  164. const url = new URL(h.page.url());
  165. assert.equal(url.searchParams.get("sid"), selected.device_sid);
  166. assert.equal(url.searchParams.get("mid"), selected.device_mid);
  167. assert.equal(url.searchParams.get("name"), selected.meta.name);
  168. assert.equal(url.searchParams.get("app_version"), "3.14.1");
  169. assert.equal(
  170. await h.page.evaluate(() =>
  171. localStorage.getItem("zcode-web-remote-last-device-sid"),
  172. ),
  173. selected.device_sid,
  174. );
  175. assert.equal(
  176. await h.page.evaluate(() =>
  177. localStorage.getItem("zcode-web-remote-last-device-mid"),
  178. ),
  179. selected.device_mid,
  180. );
  181. assert.equal(h.traffic.tokens, 1);
  182. }
  183. assert.deepEqual(h.errors, []);
  184. results.push({ screen, width, theme, locale, screenshot, result: "passed" });
  185. } finally {
  186. await h.context.close();
  187. }
  188. }
  189. } finally {
  190. await browser.close();
  191. await writeFile(resolve(artifacts, "results.json"), JSON.stringify(results, null, 2));
  192. await writeFile(
  193. resolve(artifacts, "index.html"),
  194. `<!doctype html><meta charset="utf-8"><title>Remote device groups</title>${results.map((r) => `<figure><figcaption>${r.screen} · ${r.width} · ${r.theme} · ${r.locale}</figcaption><img src="${r.screenshot}" style="max-width:100%"></figure>`).join("\n")}`,
  195. );
  196. }
  197. console.log(JSON.stringify({ passed: results.length, artifacts }, null, 2));