webRemoteControlRelayPayloadSerializer.test.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS,
  4. encodeWebRemoteControlRpcTransportMessage,
  5. type WebRemoteControlAppPayload,
  6. } from "@zcode/shared";
  7. import { WebRemoteControlRelayPayloadSerializer } from "../src/webRemoteControlRelayPayloadSerializer.js";
  8. describe("WebRemoteControlRelayPayloadSerializer", () => {
  9. it("reuses the identical final JSON and byte count for one immutable raw payload", () => {
  10. const serializer = new WebRemoteControlRelayPayloadSerializer();
  11. const [payload] = encodeWebRemoteControlRpcTransportMessage(new Uint8Array([1, 2, 3]), {
  12. bridgeSessionId: "bridge-1",
  13. firstPhysicalSeq: 1,
  14. messageSeq: 1,
  15. });
  16. const first = serializer.prepare(payload, 100);
  17. const replay = serializer.prepare(payload, 200);
  18. expect(replay).toBe(first);
  19. expect(replay.json).toBe(first.json);
  20. expect(replay.message.client_ts).toBe(100);
  21. expect(replay.bytes).toBe(new TextEncoder().encode(replay.json).byteLength);
  22. });
  23. it("distinguishes a permanent oversize envelope from a bounded payload", () => {
  24. const serializer = new WebRemoteControlRelayPayloadSerializer();
  25. const bounded: WebRemoteControlAppPayload = {
  26. zcode_type: "app-error",
  27. reason: "unexpected-error",
  28. error: "中文🙂bounded",
  29. };
  30. const oversize: WebRemoteControlAppPayload = {
  31. zcode_type: "app-error",
  32. reason: "unexpected-error",
  33. error: "x".repeat(WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS.maxPhysicalFrameBytes),
  34. };
  35. const preparedBounded = serializer.prepare(bounded, 1);
  36. expect(serializer.isOversize(preparedBounded)).toBe(false);
  37. expect(preparedBounded.bytes).toBe(new TextEncoder().encode(preparedBounded.json).byteLength);
  38. const preparedOversize = serializer.prepare(oversize, 1);
  39. expect(serializer.isOversize(preparedOversize)).toBe(true);
  40. expect(preparedOversize.bytes).toBeGreaterThan(
  41. WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS.maxPhysicalFrameBytes,
  42. );
  43. });
  44. });