webRemoteControlRelayV2.test.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. parseWebRemoteControlRelayV2OutgoingMessage,
  4. parseWebRemoteControlRelayV2IncomingMessage,
  5. } from "@zcode/shared/web-remote-relay";
  6. describe("Relay V2 wire contract", () => {
  7. it("accepts auth_init without a role and rejects V1 identity fields", () => {
  8. const auth = {
  9. type: "auth_init",
  10. device_sid: "d_1",
  11. meta: { name: "桌面" },
  12. client_ts: 1_600_000_000,
  13. };
  14. expect(parseWebRemoteControlRelayV2OutgoingMessage(auth)).toEqual(auth);
  15. expect(parseWebRemoteControlRelayV2OutgoingMessage({ ...auth, role: "device" })).toBeNull();
  16. expect(parseWebRemoteControlRelayV2OutgoingMessage({ ...auth, user_id: "other" })).toBeNull();
  17. });
  18. });
  19. it("validates message direction, seconds and opaque JSON payloads", () => {
  20. const samples = [
  21. { type: "pair_status_query", client_ts: 1_600_000_000 },
  22. { type: "data", payload: { device_sid: "opaque", nested: [null, true, 1] } },
  23. { type: "data", payload: null },
  24. ];
  25. for (const sample of samples)
  26. expect(parseWebRemoteControlRelayV2OutgoingMessage(sample)).toEqual(sample);
  27. for (const invalid of [
  28. { type: "pair_status_query", device_sid: "d_1" },
  29. { type: "pair_status_query", client_ts: 1_600_000_000_000 },
  30. { type: "pair_status_query", client_ts: 1.5 },
  31. { type: "pair_status_query", client_ts: -1 },
  32. { type: "auth_init", device_sid: " " },
  33. { type: "auth_init", device_sid: "中".repeat(43) },
  34. { type: "auth_init", device_sid: "d_1", meta: { extra: 1 } },
  35. { type: "data" },
  36. { type: "data", payload: undefined },
  37. { type: "data", payload: { nested: () => 1 } },
  38. { type: "data", payload: "hello", server_ts: 1 },
  39. { type: "device_register_init" },
  40. null,
  41. [],
  42. "not decoded JSON",
  43. ])
  44. expect(parseWebRemoteControlRelayV2OutgoingMessage(invalid)).toBeNull();
  45. });
  46. it("accepts server ACKs/errors and rejects wrong direction or invalid status", () => {
  47. for (const sample of [
  48. {
  49. type: "auth_ack",
  50. device_sid: "d_1",
  51. terminal_sid: "",
  52. pair_status: "waiting",
  53. server_ts: 1_600_000_000,
  54. },
  55. { type: "pair_status_ack", terminal_sid: "t_1", pair_status: "matched" },
  56. { type: "data", payload: ["unchanged", null], server_ts: 1_600_000_000 },
  57. ...["AUTH_FAILED", "WRONG_PARAM", "KICKED", "INTERNAL", "DEVICE_OFFLINE"].map((code) => ({
  58. type: "error",
  59. code,
  60. })),
  61. { type: "error", code: "INTERNAL", message: "unavailable" },
  62. ])
  63. expect(parseWebRemoteControlRelayV2IncomingMessage(sample)).toEqual(sample);
  64. for (const invalid of [
  65. { type: "auth_ack", device_sid: "d_1", pair_status: "kicked" },
  66. { type: "auth_ack", pair_status: "matched" },
  67. { type: "pair_status_ack", pair_status: "waiting", device_sid: "d_1" },
  68. { type: "data", payload: {}, client_ts: 1 },
  69. { type: "data" },
  70. { type: "error", code: "UNAUTHORIZED" },
  71. { type: "error", code: "INTERNAL", message: 123 },
  72. { type: "error", code: "INTERNAL", server_ts: Infinity },
  73. { type: "auth_challenge", nonce: "v1" },
  74. ])
  75. expect(parseWebRemoteControlRelayV2IncomingMessage(invalid)).toBeNull();
  76. });