remote-device-groups.mjs 9.3 KB

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