webRemoteControlV2Qr.test.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { expect, it } from "vitest";
  2. import {
  3. buildWebRemoteControlExternalQrUrl,
  4. parseWebRemoteControlExternalQrParams,
  5. } from "@zcode/shared";
  6. import {
  7. getWebRemoteControlHeartbeatDelayMs,
  8. WEB_REMOTE_CONTROL_V2_HEARTBEAT_INTERVAL_MS,
  9. WEB_REMOTE_CONTROL_V2_HEARTBEAT_JITTER_MS,
  10. } from "@zcode/shared/web-remote-relay";
  11. it("builds V2 links without secrets, including removal of stale base URL credentials", () => {
  12. const url = buildWebRemoteControlExternalQrUrl({
  13. baseUrl: "https://zcode.z.ai/remote/v4?hash=old&t=123",
  14. deviceSid: "d_1",
  15. deviceMid: "mid-1",
  16. deviceName: "我的桌面",
  17. appVersion: "3.13.0",
  18. });
  19. const params = new URL(url).searchParams;
  20. expect([...params.keys()]).toEqual(["sid", "mid", "name", "app_version"]);
  21. expect(parseWebRemoteControlExternalQrParams(params)).toEqual({
  22. deviceSid: "d_1",
  23. deviceMid: "mid-1",
  24. deviceName: "我的桌面",
  25. appVersion: "3.13.0",
  26. });
  27. });
  28. it("accepts every V2 routing hint independently or none and rejects legacy credentials", () => {
  29. for (const [query, result] of [
  30. ["", {}],
  31. ["sid=d_1", { deviceSid: "d_1" }],
  32. ["mid=m1", { deviceMid: "m1" }],
  33. ["name=Desk", { deviceName: "Desk" }],
  34. ["app_version=3.13.0", { appVersion: "3.13.0" }],
  35. ["sid=%20&name=%20", {}],
  36. ] as const)
  37. expect(parseWebRemoteControlExternalQrParams(new URLSearchParams(query))).toEqual(result);
  38. for (const query of ["sid=d_1&hash=secret&t=123", "hash=", "t=123"]) {
  39. expect(parseWebRemoteControlExternalQrParams(new URLSearchParams(query))).toBeNull();
  40. }
  41. });
  42. it("uses the V2 heartbeat bounds as the helper defaults", () => {
  43. expect(
  44. getWebRemoteControlHeartbeatDelayMs(
  45. WEB_REMOTE_CONTROL_V2_HEARTBEAT_INTERVAL_MS,
  46. WEB_REMOTE_CONTROL_V2_HEARTBEAT_JITTER_MS,
  47. () => 0,
  48. ),
  49. ).toBe(4_000);
  50. expect(
  51. getWebRemoteControlHeartbeatDelayMs(
  52. WEB_REMOTE_CONTROL_V2_HEARTBEAT_INTERVAL_MS,
  53. WEB_REMOTE_CONTROL_V2_HEARTBEAT_JITTER_MS,
  54. () => 1,
  55. ),
  56. ).toBe(6_000);
  57. expect(getWebRemoteControlHeartbeatDelayMs(undefined, undefined, () => 0.5)).toBe(5_000);
  58. });