webRemoteControlVersion.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { WebRemoteControlWindowBootstrapResult } from "@zcode/shared";
  3. import { createWebRemoteControlVersionGuard } from "../src/webRemoteControlVersion.js";
  4. const bootstrap = (desktopAppVersion?: string): WebRemoteControlWindowBootstrapResult => ({
  5. windowControlSessionId: "device-1",
  6. workspaces: [],
  7. tasks: [],
  8. ...(desktopAppVersion === undefined ? {} : { desktopAppVersion }),
  9. });
  10. function harness(options: { url?: string; ready?: boolean; version?: string } = {}) {
  11. const requestBootstrap = vi.fn(async () => bootstrap(options.version));
  12. const navigate = vi.fn();
  13. const onMismatch = vi.fn();
  14. const url =
  15. options.url ?? "https://zcode.z.ai/remote/v4?sid=a&hash=x%2By&t=1&app_version=3.12.1#task";
  16. const guard = createWebRemoteControlVersionGuard({
  17. requestBootstrap,
  18. getUrl: () => url,
  19. isPageReady: () => options.ready ?? false,
  20. navigate,
  21. onMismatch,
  22. });
  23. return { guard, requestBootstrap, navigate, onMismatch, url };
  24. }
  25. describe("remote v4 desktop version guard", () => {
  26. it("corrects the first URL before allowing business bootstrap, preserving connection parameters", async () => {
  27. const h = harness({ version: "3.12.2" });
  28. expect(await h.guard.check()).toBeUndefined();
  29. expect(h.navigate).toHaveBeenCalledExactlyOnceWith(h.url.replace("3.12.1", "3.12.2"));
  30. expect(h.onMismatch).not.toHaveBeenCalled();
  31. expect(h.guard.isBlocked()).toBe(true);
  32. await h.guard.check();
  33. h.guard.confirm();
  34. expect(h.navigate).toHaveBeenCalledTimes(1);
  35. });
  36. it("fills a missing version and accepts the v4 trailing slash", async () => {
  37. const h = harness({ url: "https://zcode.z.ai/remote/v4/?sid=a", version: "3.12.2" });
  38. await h.guard.check();
  39. expect(h.navigate).toHaveBeenCalledWith(
  40. "https://zcode.z.ai/remote/v4/?sid=a&app_version=3.12.2",
  41. );
  42. });
  43. it.each([undefined, "3.12.1"])(
  44. "continues with an old desktop or equal version: %s",
  45. async (version) => {
  46. const h = harness({ version });
  47. expect(await h.guard.check()).toEqual(bootstrap(version));
  48. expect(h.navigate).not.toHaveBeenCalled();
  49. expect(h.guard.isBlocked()).toBe(false);
  50. },
  51. );
  52. it.each(["/remote", "/remote/v3", "/custom/remote"])(
  53. "does not redirect other entry points: %s",
  54. async (path) => {
  55. const h = harness({ url: `http://localhost:5173${path}?app_version=old`, version: "3.12.2" });
  56. expect(await h.guard.check()).toEqual(bootstrap("3.12.2"));
  57. expect(h.navigate).not.toHaveBeenCalled();
  58. },
  59. );
  60. it("keeps the active page and URL until confirmation, then navigates once", async () => {
  61. const h = harness({ ready: true, version: "3.12.2" });
  62. expect(await h.guard.check()).toBeUndefined();
  63. expect(h.onMismatch).toHaveBeenCalledExactlyOnceWith("3.12.2");
  64. expect(h.navigate).not.toHaveBeenCalled();
  65. await h.guard.check();
  66. h.guard.confirm();
  67. h.guard.confirm();
  68. expect(h.navigate).toHaveBeenCalledTimes(1);
  69. });
  70. it("coalesces concurrent checks and checks again after recovery", async () => {
  71. const h = harness({ version: "3.12.1" });
  72. await Promise.all([h.guard.check(), h.guard.check()]);
  73. expect(h.requestBootstrap).toHaveBeenCalledTimes(1);
  74. h.guard.invalidate();
  75. await h.guard.check();
  76. expect(h.requestBootstrap).toHaveBeenCalledTimes(2);
  77. });
  78. it("ignores a late version from an obsolete connection", async () => {
  79. const h = harness({ version: "3.12.1" });
  80. let resolveOld!: (value: WebRemoteControlWindowBootstrapResult) => void;
  81. h.requestBootstrap.mockImplementationOnce(
  82. () =>
  83. new Promise((resolve) => {
  84. resolveOld = resolve;
  85. }),
  86. );
  87. const old = h.guard.check();
  88. h.guard.invalidate();
  89. expect(await h.guard.check()).toEqual(bootstrap("3.12.1"));
  90. resolveOld(bootstrap("3.12.2"));
  91. expect(await old).toBeUndefined();
  92. expect(h.navigate).not.toHaveBeenCalled();
  93. });
  94. it("suppresses obsolete errors but propagates current failures", async () => {
  95. const h = harness();
  96. let rejectOld!: (error: Error) => void;
  97. h.requestBootstrap.mockImplementationOnce(
  98. () =>
  99. new Promise((_resolve, reject) => {
  100. rejectOld = reject;
  101. }),
  102. );
  103. const old = h.guard.check();
  104. await Promise.resolve();
  105. h.guard.invalidate();
  106. rejectOld(new Error("old connection"));
  107. expect(await old).toBeUndefined();
  108. h.requestBootstrap.mockRejectedValueOnce(new Error("current connection"));
  109. await expect(h.guard.check()).rejects.toThrow("current connection");
  110. });
  111. it("does not navigate after disposal or leak pending state to another device", async () => {
  112. const first = harness({ ready: true, version: "3.12.2" });
  113. await first.guard.check();
  114. first.guard.dispose();
  115. first.guard.confirm();
  116. expect(first.navigate).not.toHaveBeenCalled();
  117. const second = harness({ version: "3.12.1" });
  118. expect(await second.guard.check()).toEqual(bootstrap("3.12.1"));
  119. });
  120. });