ipc.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { describe, it, expect } from "vitest";
  2. import { IPCServer, IPCClient, StaticRouter } from "../src/ipc.js";
  3. import { IServerChannel } from "../src/channels.js";
  4. import { createQueuePair } from "../src/protocol.js";
  5. import { Emitter, Event } from "../src/foundation.js";
  6. function connectClient(
  7. onConnect: Emitter<{ protocol: any; onDidClientDisconnect: Event<void> }>,
  8. ctx: string,
  9. ) {
  10. const [serverProtocol, clientProtocol] = createQueuePair();
  11. const disconnectEmitter = new Emitter<void>();
  12. onConnect.fire({
  13. protocol: serverProtocol,
  14. onDidClientDisconnect: disconnectEmitter.event,
  15. });
  16. const client = new IPCClient(clientProtocol, ctx);
  17. return { client, disconnect: () => disconnectEmitter.fire() };
  18. }
  19. describe("IPCServer + IPCClient", () => {
  20. it("should allow client to call server channel", async () => {
  21. const onConnect = new Emitter<{ protocol: any; onDidClientDisconnect: Event<void> }>();
  22. const server = new IPCServer(onConnect.event);
  23. const channel: IServerChannel = {
  24. call(_ctx, command, arg) {
  25. if (command === "echo") return Promise.resolve(arg);
  26. return Promise.reject(new Error("Unknown"));
  27. },
  28. listen() {
  29. return Event.None;
  30. },
  31. };
  32. server.registerChannel("echo", channel);
  33. const { client } = connectClient(onConnect, "client-1");
  34. // Wait for handshake
  35. await new Promise((r) => setTimeout(r, 50));
  36. const ch = client.getChannel("echo");
  37. const result = await ch.call("echo", "hello");
  38. expect(result).toBe("hello");
  39. client.dispose();
  40. server.dispose();
  41. });
  42. it("should track connections", async () => {
  43. const onConnect = new Emitter<{ protocol: any; onDidClientDisconnect: Event<void> }>();
  44. const server = new IPCServer(onConnect.event);
  45. expect(server.connections).toHaveLength(0);
  46. const { client: c1, disconnect: d1 } = connectClient(onConnect, "c1");
  47. // Wait for initial message
  48. await new Promise((r) => setTimeout(r, 50));
  49. expect(server.connections).toHaveLength(1);
  50. const { client: c2 } = connectClient(onConnect, "c2");
  51. await new Promise((r) => setTimeout(r, 50));
  52. expect(server.connections).toHaveLength(2);
  53. d1();
  54. await new Promise((r) => setTimeout(r, 10));
  55. expect(server.connections).toHaveLength(1);
  56. c1.dispose();
  57. c2.dispose();
  58. server.dispose();
  59. });
  60. it("should support client registering channels for server reverse call", async () => {
  61. const onConnect = new Emitter<{ protocol: any; onDidClientDisconnect: Event<void> }>();
  62. const server = new IPCServer(onConnect.event);
  63. const { client } = connectClient(onConnect, "worker");
  64. // Client registers a channel
  65. const clientChannel: IServerChannel = {
  66. call(_ctx, command) {
  67. if (command === "getStatus") return Promise.resolve("busy");
  68. return Promise.reject(new Error("Unknown"));
  69. },
  70. listen() {
  71. return Event.None;
  72. },
  73. };
  74. client.registerChannel("worker-status", clientChannel);
  75. await new Promise((r) => setTimeout(r, 50));
  76. // Server calls client's channel via filter
  77. const ch = server.getChannel("worker-status", (c) => c.ctx === "worker");
  78. const status = await ch.call("getStatus");
  79. expect(status).toBe("busy");
  80. client.dispose();
  81. server.dispose();
  82. });
  83. });
  84. describe("StaticRouter", () => {
  85. it("should route to matching client", async () => {
  86. const onConnect = new Emitter<{ protocol: any; onDidClientDisconnect: Event<void> }>();
  87. const server = new IPCServer(onConnect.event);
  88. const router = new StaticRouter((ctx: string) => ctx === "target");
  89. const { client: c1 } = connectClient(onConnect, "other");
  90. const { client: c2 } = connectClient(onConnect, "target");
  91. const targetChannel: IServerChannel = {
  92. call(_ctx, command) {
  93. if (command === "whoami") return Promise.resolve("I am target");
  94. return Promise.reject(new Error("Unknown"));
  95. },
  96. listen() {
  97. return Event.None;
  98. },
  99. };
  100. c2.registerChannel("identity", targetChannel);
  101. await new Promise((r) => setTimeout(r, 50));
  102. const ch = server.getChannel("identity", router);
  103. const result = await ch.call("whoami");
  104. expect(result).toBe("I am target");
  105. c1.dispose();
  106. c2.dispose();
  107. server.dispose();
  108. });
  109. });