remote-version.mjs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // RV01–RV03:真实 Web 入口与浏览器导航;模拟 relay/桌面响应,不验证外部资源路由服务。
  2. // 启动:pnpm --filter @zcode/web exec vite --host 127.0.0.1 --port 5178 --strictPort --base=/remote/v4/
  3. // 运行:node packages/web/test/e2e/manual-review/pending/remote-version.mjs
  4. import assert from "node:assert/strict";
  5. import { mkdir } from "node:fs/promises";
  6. import { resolve } from "node:path";
  7. import { chromium } from "playwright-core";
  8. const origin = process.env.REMOTE_VERSION_E2E_ORIGIN || "http://127.0.0.1:5178";
  9. const artifacts = resolve(".tmp/remote-version-e2e");
  10. await mkdir(artifacts, { recursive: true });
  11. const browser = await chromium.launch({ channel: "chrome", headless: true });
  12. async function scenario({ width, theme, locale, missingVersion = false, oldDesktop = false }) {
  13. const context = await browser.newContext({ viewport: { width, height: 844 }, locale });
  14. const page = await context.newPage();
  15. page.setDefaultTimeout(30_000);
  16. const errors = [];
  17. // 现有 home-only 空服务未实现动态会话订阅;在版本变化前已出现,不作为版本校验回归。
  18. // 保留显式输出,其他浏览器异常仍使本用例失败。
  19. const knownHomeOnlyError = "agentService.onDynamicSessionsIndexFrame(...) is not a function";
  20. page.on("pageerror", (error) => errors.push(error.message));
  21. await context.addInitScript((value) => localStorage.setItem("zcode-theme", value), theme);
  22. const waitForHome = async () => {
  23. const home = page.getByText("E2E_REMOTE_VERSION", { exact: true }).first();
  24. const onboarding = page.getByTestId("onboarding-page");
  25. await home.or(onboarding).first().waitFor();
  26. if (await onboarding.isVisible()) await page.keyboard.press("Escape");
  27. if (width < 768) {
  28. await home.waitFor();
  29. } else {
  30. // 宽屏的既有 home-only 服务还缺工作区事件订阅,业务区域可能落入 ErrorBoundary。
  31. // 本候选只断言入口校验/保留已挂载区域/提示/导航,不宣称覆盖宽屏任务操作。
  32. const baselineError = page
  33. .getByText(
  34. "shard.services.zcodeTaskService.onDynamicWorkspaceEvent(...) is not a function",
  35. { exact: true },
  36. )
  37. .first();
  38. await home.or(baselineError).first().waitFor();
  39. if (await baselineError.isVisible())
  40. console.warn("KNOWN wide home-only baseline: workspace event subscription unavailable");
  41. }
  42. };
  43. let desktopVersion = oldDesktop ? undefined : "3.12.2";
  44. let relay;
  45. let bootstrapCount = 0;
  46. let bridgeCount = 0;
  47. let navigationCount = 0;
  48. page.on("framenavigated", (frame) => {
  49. if (frame === page.mainFrame()) navigationCount += 1;
  50. });
  51. const workspace = {
  52. workspacePath: "/workspace/remote-version",
  53. workspaceIdentity: "remote:ssh:e2e:/workspace/remote-version",
  54. label: "E2E_REMOTE_VERSION",
  55. kind: "remote",
  56. connectionState: "disconnected",
  57. };
  58. const send = (message) => relay.send(JSON.stringify(message));
  59. await page.routeWebSocket(/\/ws(?:\?|$)/, (socket) => {
  60. relay = socket;
  61. socket.onMessage((raw) => {
  62. const message = JSON.parse(String(raw));
  63. if (message.type === "auth_init")
  64. send({ type: "auth_challenge", nonce: "e2e-version-nonce" });
  65. if (message.type === "auth_response") {
  66. send({ type: "auth_ack", device_sid: "e2e-device", pair_status: "matched" });
  67. }
  68. if (message.type === "pair_status_query")
  69. send({ type: "pair_status_ack", pair_status: "matched" });
  70. const payload = message.payload;
  71. if (payload?.zcode_type === "bootstrap-request") {
  72. bootstrapCount += 1;
  73. send({
  74. type: "data",
  75. payload: {
  76. zcode_type: "bootstrap-response",
  77. requestId: payload.requestId,
  78. success: true,
  79. result: {
  80. windowControlSessionId: "e2e-device",
  81. desktopAppVersion: desktopVersion,
  82. workspaces: [workspace],
  83. tasks: [],
  84. },
  85. },
  86. });
  87. }
  88. if (payload?.zcode_type === "workspace-list-request") {
  89. send({
  90. type: "data",
  91. payload: {
  92. zcode_type: "workspace-list-response",
  93. requestId: payload.requestId,
  94. success: true,
  95. result: { workspaces: [workspace], tasks: [] },
  96. },
  97. });
  98. }
  99. if (payload?.zcode_type === "workspace-bridge-open") bridgeCount += 1;
  100. });
  101. });
  102. const url = new URL("/remote/v4/", origin);
  103. url.search = "sid=e2e-device&hash=e2e-hash&t=1&mid=e2e-mid&name=E2E";
  104. if (!missingVersion) url.searchParams.set("app_version", "3.12.1");
  105. url.hash = "keep-fragment";
  106. try {
  107. await page.goto(url.toString());
  108. if (!oldDesktop)
  109. await page.waitForURL((target) => target.searchParams.get("app_version") === "3.12.2");
  110. await waitForHome();
  111. assert.deepEqual(
  112. errors.filter((error) => error !== knownHomeOnlyError),
  113. [],
  114. );
  115. if (errors.includes(knownHomeOnlyError))
  116. console.warn("KNOWN home-only baseline:", knownHomeOnlyError);
  117. const readyUrl = page.url();
  118. const parsed = new URL(readyUrl);
  119. assert.equal(parsed.hash, "#keep-fragment");
  120. for (const [key, value] of url.searchParams) {
  121. if (key !== "app_version") assert.equal(parsed.searchParams.get(key), value);
  122. }
  123. assert.equal(bridgeCount, 0);
  124. assert.equal(navigationCount, oldDesktop ? 1 : 2);
  125. if (oldDesktop) {
  126. assert.equal(readyUrl, url.toString());
  127. assert.equal(await page.locator("dialog[aria-labelledby='remote-version-title']").count(), 0);
  128. return;
  129. }
  130. const beforeRecovery = navigationCount;
  131. const initialBootstraps = bootstrapCount;
  132. send({ type: "pair_status_ack", pair_status: "waiting" });
  133. send({ type: "pair_status_ack", pair_status: "matched" });
  134. await page.waitForFunction(() => document.querySelector("dialog") === null);
  135. await assertEventually(() => bootstrapCount > initialBootstraps);
  136. assert.equal(navigationCount, beforeRecovery);
  137. assert.equal(page.url(), readyUrl);
  138. await page.evaluate(() => {
  139. window.__remoteVersionRoot = document.getElementById("root").firstElementChild;
  140. window.__backgroundShortcutCount = 0;
  141. window.addEventListener(
  142. "keydown",
  143. (event) => {
  144. if (event.key === "F2") window.__backgroundShortcutCount += 1;
  145. },
  146. true,
  147. );
  148. });
  149. desktopVersion = "3.12.3";
  150. send({ type: "pair_status_ack", pair_status: "waiting" });
  151. send({ type: "pair_status_ack", pair_status: "matched" });
  152. const dialog = page.getByRole("dialog", {
  153. name: locale === "zh-CN" ? "桌面版本已更新" : "Desktop version changed",
  154. exact: true,
  155. });
  156. await dialog.waitFor();
  157. const bounds = await dialog.boundingBox();
  158. assert.ok(bounds && bounds.x >= 15 && Math.abs(bounds.x - (width - bounds.width) / 2) < 2);
  159. assert.equal(page.url(), readyUrl);
  160. assert.equal(navigationCount, beforeRecovery);
  161. assert.equal(
  162. await page.evaluate(
  163. () => window.__remoteVersionRoot === document.getElementById("root").firstElementChild,
  164. ),
  165. true,
  166. );
  167. assert.equal(
  168. await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth),
  169. true,
  170. );
  171. await page.keyboard.press("Escape");
  172. assert.equal(await dialog.isVisible(), true);
  173. await page.keyboard.press("F2");
  174. assert.equal(await page.evaluate(() => window.__backgroundShortcutCount), 0);
  175. await page.screenshot({
  176. path: resolve(artifacts, `${width}-${theme}-${locale}.png`),
  177. fullPage: true,
  178. });
  179. await dialog
  180. .getByRole("button", { name: locale === "zh-CN" ? "重新加载" : "Reload", exact: true })
  181. .click();
  182. await page.waitForURL((target) => target.searchParams.get("app_version") === "3.12.3");
  183. await waitForHome();
  184. assert.equal(navigationCount, beforeRecovery + 1);
  185. assert.equal(await page.locator("dialog[aria-labelledby='remote-version-title']").count(), 0);
  186. assert.deepEqual(
  187. errors.filter((error) => error !== knownHomeOnlyError),
  188. [],
  189. );
  190. } catch (error) {
  191. await page.screenshot({ path: resolve(artifacts, "failure.png"), fullPage: true });
  192. console.error(await page.locator("body").innerText(), errors);
  193. throw error;
  194. } finally {
  195. await context.close();
  196. }
  197. }
  198. async function assertEventually(predicate) {
  199. for (let attempt = 0; attempt < 100; attempt += 1) {
  200. if (predicate()) return;
  201. await new Promise((done) => setTimeout(done, 50));
  202. }
  203. assert.ok(predicate(), "expected a new version bootstrap after recovery");
  204. }
  205. try {
  206. for (const options of [
  207. { width: 390, theme: "zai-light", locale: "zh-CN" },
  208. { width: 390, theme: "zai-dark", locale: "en-US" },
  209. { width: 1280, theme: "zai-light", locale: "en-US" },
  210. { width: 1280, theme: "zai-dark", locale: "zh-CN" },
  211. { width: 390, theme: "zai-dark", locale: "zh-CN", missingVersion: true },
  212. { width: 390, theme: "zai-dark", locale: "zh-CN", oldDesktop: true },
  213. ]) {
  214. await scenario(options);
  215. console.log("PASS RV01–RV03", options);
  216. }
  217. } finally {
  218. await browser.close();
  219. }