web-remote-control-startup-restore.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { readFile, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { type WebRemoteControlStatus } from "@zcode/shared";
  4. import {
  5. DEFAULT_WORKSPACE,
  6. getE2EAppDataPaths,
  7. waitForWorkspaceApp,
  8. } from "#e2e/helpers/desktop-app";
  9. import { reloadElectronSessionSafely } from "#e2e/helpers/e2e-electron-reload";
  10. async function remoteStatus() {
  11. const serialized = await browser.execute(async () => {
  12. const bridge = (
  13. window as Window & { zcode?: { getWebRemoteControlStatus(): Promise<{ status: string }> } }
  14. ).zcode;
  15. if (!bridge) throw new Error("缺少 preload 远控接口");
  16. // WebDriver 把返回对象的 error 字段当成协议错误;序列化后再在测试端还原真实状态。
  17. return JSON.stringify(await bridge.getWebRemoteControlStatus());
  18. });
  19. return JSON.parse(serialized) as WebRemoteControlStatus;
  20. }
  21. async function onlineDevices(origin: string) {
  22. const response = await fetch(`${origin}/api/v1/remote/device`, {
  23. headers: { Authorization: "Bearer mock-token" },
  24. });
  25. const devices = (await response.json()).data as Array<{ device_sid: string; online: boolean }>;
  26. return devices.filter((device) => device.online).map((device) => device.device_sid);
  27. }
  28. async function lastEnabledContext() {
  29. const { appDataDir } = getE2EAppDataPaths();
  30. const settings = JSON.parse(await readFile(join(appDataDir, "setting.json"), "utf8"));
  31. return settings.webRemoteControlLastEnabledContext as { workspacePath: string } | undefined;
  32. }
  33. async function waitForRemoteTrigger() {
  34. let trigger: WebdriverIO.Element | undefined;
  35. await browser.waitUntil(
  36. async () => {
  37. if (await $('[data-testid="onboarding-page"]').isDisplayed()) await browser.keys("Escape");
  38. for (const candidate of await $$(
  39. 'button[aria-label="Mobile remote control"], button[aria-label="移动端远程控制"]',
  40. )) {
  41. if (await candidate.isDisplayed()) {
  42. trigger = candidate;
  43. return true;
  44. }
  45. }
  46. return false;
  47. },
  48. { timeout: 60_000, timeoutMsg: "未出现远控入口" },
  49. );
  50. return trigger!;
  51. }
  52. describe("桌面重启后自动恢复远控", () => {
  53. it("AUTH-09: 上次启用目标不在窗口时仍恢复设备连接,用户停止后重启保持关闭", async function () {
  54. this.timeout(180_000);
  55. const origin = process.env.ZCODE_E2E_REMOTE_AUTH_GATE_URL;
  56. const artifacts = process.env.ZCODE_E2E_ARTIFACT_DIR;
  57. if (!origin || !artifacts) throw new Error("缺少远控启动恢复 fixture");
  58. // fixture 把上次启用目标写成未打开的 ZCodeProject-previous;窗口只恢复 ZCodeProject。
  59. await waitForRemoteTrigger();
  60. await browser.waitUntil(async () => (await remoteStatus()).status === "running", {
  61. timeout: 40_000,
  62. timeoutMsg: "上次启用目标不在窗口时未自动恢复远控",
  63. });
  64. const restored = await remoteStatus();
  65. expect(restored.workspacePath).toBe(DEFAULT_WORKSPACE);
  66. expect(restored.initialTaskId).toBeUndefined();
  67. await browser.waitUntil(async () => (await onlineDevices(origin)).length === 1, {
  68. timeout: 10_000,
  69. timeoutMsg: "自动恢复后 relay 上没有在线设备",
  70. });
  71. await browser.waitUntil(
  72. async () => (await lastEnabledContext())?.workspacePath === DEFAULT_WORKSPACE,
  73. { timeout: 10_000, timeoutMsg: "自动恢复成功后未更新上次启用目标" },
  74. );
  75. await (await waitForRemoteTrigger()).click();
  76. const stop = $('[data-testid="web-remote-control-connection-card"] button');
  77. await stop.waitForEnabled({ timeout: 10_000 });
  78. await stop.click();
  79. await browser.waitUntil(async () => (await remoteStatus()).status === "idle", {
  80. timeout: 10_000,
  81. });
  82. await browser.waitUntil(async () => (await onlineDevices(origin)).length === 0, {
  83. timeout: 10_000,
  84. });
  85. expect(await lastEnabledContext()).toBeUndefined();
  86. await browser.keys("Escape");
  87. await reloadElectronSessionSafely(browser);
  88. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 60_000);
  89. await waitForRemoteTrigger();
  90. // 恢复判定在窗口同步时完成;再观察一段时间,确认没有迟到的自动启动。
  91. await browser.pause(3000);
  92. const afterRestart = await remoteStatus();
  93. expect(afterRestart.status).toBe("idle");
  94. expect(await onlineDevices(origin)).toEqual([]);
  95. await writeFile(
  96. join(artifacts, "remote-startup-restore.json"),
  97. JSON.stringify(
  98. {
  99. restored: {
  100. status: restored.status,
  101. workspacePath: restored.workspacePath,
  102. initialTaskId: restored.initialTaskId ?? null,
  103. },
  104. afterRestart: { status: afterRestart.status },
  105. },
  106. null,
  107. 2,
  108. ),
  109. );
  110. });
  111. });