import { AbstractConnection, AbstractMethodMap, IPacket } from "../../src"; type MethodMap = { sendTest(value: number): void }; class MockRequestHandler extends AbstractMethodMap { methodMap = {}; } class MockConnection extends AbstractConnection { public disconnect = () => {}; public read = (rawPacket: any) => rawPacket; public write = jest.fn(); } class MockConnectionWithReply extends AbstractConnection { public disconnect = () => {}; public read = (rawPacket: any) => rawPacket; public write(packet: IPacket) { this.receive({ requestId: packet.requestId, result: 100 }); } } describe("Abstract IPC Connections", () => { it("Should write complete packet", () => { let connection = new MockConnection(new MockRequestHandler()); connection.send("sendTest", [10]); expect(connection.write).toBeCalledWith(expect.objectContaining({ method: "sendTest", args: [10] })); }); it("Request packet should timeout", async () => { let connection = new MockConnection(new MockRequestHandler()); expect(connection.request("sendTest", [25])).rejects.toMatch("timeout"); }); it("Request should be resolved", async () => { let connection = new MockConnectionWithReply(new MockRequestHandler()); expect(connection.request("sendTest", [25])).resolves.toEqual(100); }); });