mobileRpcFrameGate.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { describe, expect, it } from "vitest";
  2. import { encodeWebRemoteControlRpcTransportMessage } from "@zcode/shared";
  3. import { MobileRpcFrameGate } from "#e2e/helpers/mobile-rpc-frame-gate";
  4. const frame = (seq: number, bridge = "bridge-a", generation = 1, recoveryId = "recovery-a") =>
  5. JSON.stringify({
  6. payload: encodeWebRemoteControlRpcTransportMessage(Buffer.from(`config-${seq}`), {
  7. bridgeSessionId: bridge,
  8. bridgeGeneration: generation,
  9. recoveryId,
  10. firstPhysicalSeq: seq,
  11. messageSeq: seq,
  12. })[0],
  13. });
  14. describe("mobile RPC fixture gate", () => {
  15. it("holds later frames in order, deduplicates replay and still forwards released replays", () => {
  16. const sent: string[] = [];
  17. let commands = 0;
  18. const gate = new MobileRpcFrameGate((message) => sent.push(String(message)));
  19. const accept = (message: string, hold: boolean) =>
  20. gate.receive(message, (payload) => {
  21. if (gate.firstMessage(payload)) commands++;
  22. return hold;
  23. });
  24. accept(frame(1), false);
  25. accept(frame(2), true);
  26. accept(frame(3), false);
  27. accept(frame(2), true);
  28. expect(commands).toBe(3);
  29. expect(gate.heldCount).toBe(2);
  30. expect(gate.replayedHeldFrames).toBe(1);
  31. expect(sent).toEqual([frame(1)]);
  32. gate.release();
  33. expect(sent).toEqual([frame(1), frame(2), frame(3)]);
  34. expect(gate.heldCount).toBe(0);
  35. accept(frame(2), false);
  36. expect(commands).toBe(3);
  37. expect(sent.at(-1)).toBe(frame(2));
  38. expect(sent).toHaveLength(4);
  39. });
  40. it("isolates bridge generations, recovery identities, directions and control frames", () => {
  41. const sent: string[] = [];
  42. const outbound = new MobileRpcFrameGate((message) => sent.push(String(message)));
  43. const inbound = new MobileRpcFrameGate((message) => sent.push(String(message)));
  44. outbound.receive(frame(1), () => true);
  45. for (const message of [
  46. frame(1, "bridge-b"),
  47. frame(1, "bridge-a", 2),
  48. frame(1, "bridge-a", 1, "recovery-b"),
  49. ]) {
  50. outbound.receive(message, (payload) => {
  51. expect(outbound.firstMessage(payload)).toBe(true);
  52. return false;
  53. });
  54. }
  55. inbound.receive(frame(1), () => false);
  56. const ack = JSON.stringify({
  57. payload: { zcode_type: "rpc-frame-ack", bridgeSessionId: "bridge-a", ackMessageSeq: 1 },
  58. });
  59. outbound.receive(ack, () => {
  60. throw new Error("control must not enter frame gate");
  61. });
  62. expect(sent).toHaveLength(5);
  63. expect(outbound.heldCount).toBe(1);
  64. expect(inbound.heldCount).toBe(0);
  65. outbound.release();
  66. expect(sent.at(-1)).toBe(frame(1));
  67. });
  68. it("shares logical counts across reconnects and never releases a closed connection", () => {
  69. const messages = new Set<string>();
  70. const oldSent: string[] = [];
  71. const newSent: string[] = [];
  72. const oldGate = new MobileRpcFrameGate((message) => oldSent.push(String(message)), messages);
  73. const newGate = new MobileRpcFrameGate((message) => newSent.push(String(message)), messages);
  74. let commands = 0;
  75. const accept = (gate: MobileRpcFrameGate, message: string) =>
  76. gate.receive(message, (payload) => {
  77. if (gate.firstMessage(payload)) commands++;
  78. return true;
  79. });
  80. accept(oldGate, frame(1));
  81. accept(oldGate, frame(2));
  82. oldGate.dispose();
  83. oldGate.dispose();
  84. expect(oldGate.heldCount).toBe(0);
  85. accept(oldGate, frame(3));
  86. accept(newGate, frame(1));
  87. accept(newGate, frame(2));
  88. expect(commands).toBe(2);
  89. oldGate.release();
  90. newGate.release();
  91. expect(oldSent).toEqual([]);
  92. expect(newSent).toEqual([frame(1), frame(2)]);
  93. accept(newGate, frame(1, "bridge-a", 2));
  94. accept(newGate, frame(1, "bridge-a", 1, "recovery-b"));
  95. expect(commands).toBe(4);
  96. });
  97. });