webRemoteControlVersion.test.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { WebRemoteControlWindowBootstrapResult } from "@zcode/shared";
  3. import {
  4. createWebRemoteControlTaskViewPublisher,
  5. createWebRemoteControlVersionGuard,
  6. } from "#src/web-remote-control/index.js";
  7. const bootstrap = (desktopAppVersion?: string): WebRemoteControlWindowBootstrapResult => ({
  8. windowControlSessionId: "device-1",
  9. workspaces: [],
  10. tasks: [],
  11. ...(desktopAppVersion === undefined ? {} : { desktopAppVersion }),
  12. });
  13. function harness(options: { url?: string; ready?: boolean; version?: string } = {}) {
  14. const requestBootstrap = vi.fn(async () => bootstrap(options.version));
  15. const navigate = vi.fn();
  16. const onMismatch = vi.fn();
  17. const onAccepted = vi.fn();
  18. const url =
  19. options.url ?? "https://zcode.z.ai/remote/v4?sid=a&hash=x%2By&t=1&app_version=3.12.1#task";
  20. const guard = createWebRemoteControlVersionGuard({
  21. requestBootstrap,
  22. getUrl: () => url,
  23. isPageReady: () => options.ready ?? false,
  24. navigate,
  25. onMismatch,
  26. onAccepted,
  27. });
  28. return { guard, requestBootstrap, navigate, onMismatch, onAccepted, url };
  29. }
  30. describe("remote v4 desktop version guard", () => {
  31. it("corrects the first URL before allowing business bootstrap, preserving connection parameters", async () => {
  32. const h = harness({ version: "3.12.2" });
  33. expect(await h.guard.check()).toBeUndefined();
  34. expect(h.navigate).toHaveBeenCalledExactlyOnceWith(h.url.replace("3.12.1", "3.12.2"));
  35. expect(h.onMismatch).not.toHaveBeenCalled();
  36. expect(h.onAccepted).not.toHaveBeenCalled();
  37. expect(h.guard.isBlocked()).toBe(true);
  38. await h.guard.check();
  39. h.guard.confirm();
  40. expect(h.navigate).toHaveBeenCalledTimes(1);
  41. });
  42. it("fills a missing version and accepts the v4 trailing slash", async () => {
  43. const h = harness({ url: "https://zcode.z.ai/remote/v4/?sid=a", version: "3.12.2" });
  44. await h.guard.check();
  45. expect(h.navigate).toHaveBeenCalledWith(
  46. "https://zcode.z.ai/remote/v4/?sid=a&app_version=3.12.2",
  47. );
  48. });
  49. it.each([undefined, "3.12.1"])(
  50. "continues with an old desktop or equal version: %s",
  51. async (version) => {
  52. const h = harness({ version });
  53. expect(await h.guard.check()).toEqual(bootstrap(version));
  54. expect(h.navigate).not.toHaveBeenCalled();
  55. expect(h.guard.isBlocked()).toBe(false);
  56. expect(h.onAccepted).toHaveBeenCalledExactlyOnceWith(bootstrap(version));
  57. },
  58. );
  59. it.each(["/remote", "/remote/v3", "/custom/remote"])(
  60. "does not redirect other entry points: %s",
  61. async (path) => {
  62. const h = harness({ url: `http://localhost:5173${path}?app_version=old`, version: "3.12.2" });
  63. expect(await h.guard.check()).toEqual(bootstrap("3.12.2"));
  64. expect(h.navigate).not.toHaveBeenCalled();
  65. expect(h.onAccepted).toHaveBeenCalledExactlyOnceWith(bootstrap("3.12.2"));
  66. },
  67. );
  68. it("keeps the active page and URL until confirmation, then navigates once", async () => {
  69. const h = harness({ ready: true, version: "3.12.2" });
  70. expect(await h.guard.check()).toBeUndefined();
  71. expect(h.onMismatch).toHaveBeenCalledExactlyOnceWith("3.12.2");
  72. expect(h.navigate).not.toHaveBeenCalled();
  73. expect(h.onAccepted).not.toHaveBeenCalled();
  74. await h.guard.check();
  75. h.guard.confirm();
  76. h.guard.confirm();
  77. expect(h.navigate).toHaveBeenCalledTimes(1);
  78. });
  79. it("coalesces concurrent checks and checks again after recovery", async () => {
  80. const h = harness({ version: "3.12.1" });
  81. await Promise.all([h.guard.check(), h.guard.check()]);
  82. expect(h.requestBootstrap).toHaveBeenCalledTimes(1);
  83. expect(h.onAccepted).toHaveBeenCalledTimes(1);
  84. h.guard.invalidate();
  85. await h.guard.check();
  86. expect(h.requestBootstrap).toHaveBeenCalledTimes(2);
  87. expect(h.onAccepted).toHaveBeenCalledTimes(2);
  88. });
  89. it("ignores a late version from an obsolete connection", async () => {
  90. const h = harness({ version: "3.12.1" });
  91. let resolveOld!: (value: WebRemoteControlWindowBootstrapResult) => void;
  92. h.requestBootstrap.mockImplementationOnce(
  93. () =>
  94. new Promise((resolve) => {
  95. resolveOld = resolve;
  96. }),
  97. );
  98. const old = h.guard.check();
  99. h.guard.invalidate();
  100. expect(await h.guard.check()).toEqual(bootstrap("3.12.1"));
  101. resolveOld(bootstrap("3.12.2"));
  102. expect(await old).toBeUndefined();
  103. expect(h.navigate).not.toHaveBeenCalled();
  104. expect(h.onAccepted).toHaveBeenCalledExactlyOnceWith(bootstrap("3.12.1"));
  105. });
  106. it.each([false, true])(
  107. "MRO03:旧回包先到不能占用新配对的查看 epoch,重申先到=%s",
  108. async (reasserted) => {
  109. const h = harness({ version: "3.12.1" });
  110. const send = vi.fn(() => true);
  111. const publisher = createWebRemoteControlTaskViewPublisher(send);
  112. const bridge = {
  113. kind: "local" as const,
  114. bridgeSessionId: "bridge",
  115. workspaceKey: "/repo",
  116. workspacePath: "/repo",
  117. };
  118. publisher.setBridge(bridge);
  119. publisher.update(bridge, { workspacePath: "/repo", taskId: "task" });
  120. h.onAccepted.mockImplementation((result: WebRemoteControlWindowBootstrapResult) =>
  121. publisher.seed(result.mobileTaskViewEpoch),
  122. );
  123. const oldResult = { ...bootstrap("3.12.1"), mobileTaskViewEpoch: "E1" };
  124. const newResult = { ...bootstrap("3.12.1"), mobileTaskViewEpoch: "E2" };
  125. const oldResponse = Promise.withResolvers<WebRemoteControlWindowBootstrapResult>();
  126. h.requestBootstrap.mockReturnValueOnce(oldResponse.promise).mockResolvedValue(newResult);
  127. const old = h.guard.check();
  128. await Promise.resolve();
  129. // 同 socket 的 waiting -> paired 也撤销原请求;不能仅校验物理 socket。
  130. publisher.transportStateChanged("waiting", "paired");
  131. h.guard.invalidate();
  132. publisher.transportStateChanged("paired", "waiting");
  133. if (reasserted) publisher.request("E2");
  134. oldResponse.resolve(oldResult);
  135. expect(await old).toBeUndefined();
  136. expect(h.onAccepted).not.toHaveBeenCalled();
  137. expect(await h.guard.check()).toEqual(newResult);
  138. expect(h.onAccepted).toHaveBeenCalledExactlyOnceWith(newResult);
  139. expect(send).toHaveBeenCalledExactlyOnceWith(
  140. expect.objectContaining({
  141. epoch: "E2",
  142. seq: 1,
  143. view: { workspacePath: "/repo", taskId: "task" },
  144. }),
  145. );
  146. },
  147. );
  148. it("does not accept an in-flight bootstrap after disposal", async () => {
  149. const h = harness();
  150. const response = Promise.withResolvers<WebRemoteControlWindowBootstrapResult>();
  151. h.requestBootstrap.mockReturnValueOnce(response.promise);
  152. const pending = h.guard.check();
  153. await Promise.resolve();
  154. h.guard.dispose();
  155. response.resolve(bootstrap());
  156. expect(await pending).toBeUndefined();
  157. expect(h.onAccepted).not.toHaveBeenCalled();
  158. });
  159. it("suppresses obsolete errors but propagates current failures", async () => {
  160. const h = harness();
  161. let rejectOld!: (error: Error) => void;
  162. h.requestBootstrap.mockImplementationOnce(
  163. () =>
  164. new Promise((_resolve, reject) => {
  165. rejectOld = reject;
  166. }),
  167. );
  168. const old = h.guard.check();
  169. await Promise.resolve();
  170. h.guard.invalidate();
  171. rejectOld(new Error("old connection"));
  172. expect(await old).toBeUndefined();
  173. h.requestBootstrap.mockRejectedValueOnce(new Error("current connection"));
  174. await expect(h.guard.check()).rejects.toThrow("current connection");
  175. });
  176. it("does not navigate after disposal or leak pending state to another device", async () => {
  177. const first = harness({ ready: true, version: "3.12.2" });
  178. await first.guard.check();
  179. first.guard.dispose();
  180. first.guard.confirm();
  181. expect(first.navigate).not.toHaveBeenCalled();
  182. const second = harness({ version: "3.12.1" });
  183. expect(await second.guard.check()).toEqual(bootstrap("3.12.1"));
  184. });
  185. });