webRemoteControlDeviceListPage.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it } from "vitest";
  4. import type { WebRemoteControlRelayV2Device } from "@zcode/shared/web-remote-relay";
  5. import type {
  6. WebRemoteControlDeviceGroup,
  7. WebRemoteControlDeviceListView,
  8. } from "#src/web-remote-control/index.js";
  9. import {
  10. WebRemoteControlDeviceListPage,
  11. WebRemoteControlConnectionList,
  12. } from "#src/web-remote-control/views.js";
  13. const actions = { onSelect: () => {}, onRefresh: () => {}, onSwitchAccount: () => {} };
  14. const connection = (
  15. sid: string,
  16. mid: string,
  17. meta: WebRemoteControlRelayV2Device["meta"],
  18. createdAt = 1,
  19. ): WebRemoteControlRelayV2Device => ({
  20. device_sid: sid,
  21. device_mid: mid,
  22. online: true,
  23. created_at: createdAt,
  24. meta,
  25. });
  26. const mac: WebRemoteControlDeviceGroup = {
  27. deviceMid: "abcdefgh-long",
  28. name: "MacBook",
  29. platform: "darwin",
  30. onlineConnections: [
  31. connection("on", "abcdefgh-long", { name: "MacBook", platform: "darwin", version: "3.15.0" }),
  32. ],
  33. };
  34. const homePc: WebRemoteControlDeviceGroup = {
  35. deviceMid: "12345678-long",
  36. name: "Home PC",
  37. platform: "linux",
  38. onlineConnections: [],
  39. };
  40. const unnamed: WebRemoteControlDeviceGroup = { deviceMid: "87654321-long", onlineConnections: [] };
  41. function render(
  42. locale: "zh-CN" | "en-US",
  43. view: Partial<WebRemoteControlDeviceListView> & Pick<WebRemoteControlDeviceListView, "devices">,
  44. ) {
  45. return renderToStaticMarkup(
  46. createElement(WebRemoteControlDeviceListPage, {
  47. ...actions,
  48. locale,
  49. view: { kind: "devices", loading: false, error: false, ...view },
  50. }),
  51. );
  52. }
  53. describe("连接前设备列表", () => {
  54. it.each(["zh-CN", "en-US"] as const)(
  55. "%s 按在线/离线分组,显示平台与版本,离线禁用且默认不显示设备 ID",
  56. (locale) => {
  57. const zh = locale === "zh-CN";
  58. const html = render(locale, { devices: [mac, homePc, unnamed] });
  59. expect(html).toContain(zh ? "选择电脑" : "Choose a computer");
  60. expect(html).toMatch(new RegExp(`${zh ? "在线" : "Online"}<span[^>]*>1</span>`));
  61. expect(html).toMatch(new RegExp(`${zh ? "离线" : "Offline"}<span[^>]*>2</span>`));
  62. expect(html).toContain("macOS · ZCode 3.15.0");
  63. expect(html).toContain(zh ? "Linux · 离线" : "Linux · Offline");
  64. expect(html).toContain(zh ? "未命名设备" : "Unnamed device");
  65. expect(html).toContain('data-device-platform-icon="darwin"');
  66. expect(html).toContain('data-device-platform-icon="linux"');
  67. expect(html).toContain('data-device-platform-icon="unknown"');
  68. expect(html).not.toContain("abcdefgh");
  69. expect(html).not.toContain("12345678");
  70. // 两个离线行原生禁用;在线单连接行可点
  71. expect(html.match(/<button type="button" disabled=""[^>]*class="flex w-full/g)).toHaveLength(
  72. 2,
  73. );
  74. expect(html).toContain(
  75. zh
  76. ? "没看到你的电脑?在电脑端 ZCode 登录同一账号并开启远程控制,然后刷新。"
  77. : "Don&#x27;t see your computer?",
  78. );
  79. },
  80. );
  81. it("同组重名时追加 MID 前 8 位区分,其他设备仍不显示 ID", () => {
  82. const twin = { ...homePc, deviceMid: "fedcba98-long", name: "Home PC" };
  83. const html = render("zh-CN", { devices: [mac, homePc, twin] });
  84. expect(html).toContain("Linux · 离线 · ID 12345678");
  85. expect(html).toContain("Linux · 离线 · ID fedcba98");
  86. expect(html).not.toContain("abcdefgh");
  87. });
  88. it("账号行显示显示名与登录平台;读不到账号时只保留切换账号按钮", () => {
  89. const withAccount = render("zh-CN", {
  90. devices: [mac],
  91. account: { displayName: "jyrong", provider: "bigmodel" },
  92. });
  93. expect(withAccount).toContain("jyrong · BigModel");
  94. expect(withAccount).toMatch(/>J<\/span>/);
  95. expect(withAccount).toContain(">切换</button>");
  96. const withoutAccount = render("en-US", { devices: [mac] });
  97. expect(withoutAccount).toContain(">Switch account</button>");
  98. expect(withoutAccount).not.toContain(" · Z.ai");
  99. });
  100. it.each(["zh-CN", "en-US"] as const)("%s 多个在线连接只占一行,显示连接数且可点开", (locale) => {
  101. const multi: WebRemoteControlDeviceGroup = {
  102. deviceMid: "abcdefgh-long",
  103. name: "Workstation",
  104. platform: "win32",
  105. onlineConnections: ["sid-one", "sid-two"].map((sid) =>
  106. connection(sid, "abcdefgh-long", { name: "Workstation", version: "3.14.1" }),
  107. ),
  108. };
  109. const html = render(locale, { devices: [multi] });
  110. expect(html.match(/<li>/g)).toHaveLength(1);
  111. expect(html).toContain(locale === "zh-CN" ? "2 个连接" : "2 connections");
  112. expect(html).toContain(">Windows</span>");
  113. expect(html).not.toContain("sid-one");
  114. expect(html).not.toContain('disabled=""');
  115. });
  116. it("多连接选择行显示版本、启动时间与 SID 末 6 位,不展示完整 SID;刷新中禁用", () => {
  117. const now = Date.UTC(2026, 8, 23, 12);
  118. const device: WebRemoteControlDeviceGroup = {
  119. deviceMid: "abcdefgh-long",
  120. name: "Workstation",
  121. platform: "win32",
  122. onlineConnections: [
  123. connection(
  124. "sid-alpha-a41f9c",
  125. "abcdefgh-long",
  126. { version: "3.15.0" },
  127. now / 1000 - 3 * 3600,
  128. ),
  129. connection(
  130. "e2e-device-alternative-with-a-long-connection-identifier",
  131. "abcdefgh-long",
  132. {},
  133. now / 1000 - 2 * 86_400,
  134. ),
  135. ],
  136. };
  137. const list = (locale: "zh-CN" | "en-US", disabled = false) =>
  138. renderToStaticMarkup(
  139. createElement(WebRemoteControlConnectionList, {
  140. device,
  141. locale,
  142. disabled,
  143. now,
  144. onSelect: () => {},
  145. }),
  146. );
  147. const zh = list("zh-CN");
  148. expect(zh).toContain("ZCode 3.15.0");
  149. expect(zh).toContain("3小时前启动 · ID a41f9c");
  150. expect(zh).toContain(">ZCode</span>");
  151. expect(zh).toContain("前天启动 · ID tifier");
  152. expect(zh).not.toContain("e2e-device-alternative");
  153. expect(list("en-US")).toContain("started 3 hours ago · ID a41f9c");
  154. expect(list("zh-CN", true).match(/disabled=""/g)).toHaveLength(2);
  155. });
  156. it("空列表给出同账号开启远控提示与刷新按钮,加载和失败不能冒充空态", () => {
  157. const empty = (loading: boolean, error: boolean) =>
  158. render("zh-CN", { devices: [], loading, error });
  159. expect(empty(false, false)).toContain("暂无设备");
  160. expect(empty(false, false)).toContain("在电脑端登录同一账号并开启 Web 远程控制");
  161. expect(empty(false, false)).toContain(">刷新</button>");
  162. expect(empty(true, false)).toContain("正在加载设备");
  163. expect(empty(true, false)).not.toContain("暂无设备");
  164. expect(empty(true, false)).toMatch(
  165. /aria-label="刷新"[^>]*disabled=""|disabled=""[^>]*aria-label="刷新"/,
  166. );
  167. expect(empty(false, true)).toContain("无法加载设备");
  168. expect(empty(false, true)).not.toContain("暂无设备");
  169. });
  170. it("刷新中或刷新失败时在线行暂不可选", () => {
  171. expect(render("zh-CN", { devices: [mac], loading: true }).match(/disabled=""/g)).toHaveLength(
  172. 2,
  173. );
  174. expect(render("zh-CN", { devices: [mac], error: true }).match(/disabled=""/g)).toHaveLength(1);
  175. });
  176. });