| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { LineDecoder, OversizeFrameError, RpcClient, RpcError } from "./rpc.js"; |
| 4 | |
| 5 | function harness(limit?: number) { |
| 6 | const written: string[] = []; |
| 7 | const requests: Array<{ method: string; params: unknown }> = []; |
| 8 | const notifications: Array<{ method: string; params: unknown }> = []; |
| 9 | const errors: string[] = []; |
| 10 | const client = new RpcClient( |
| 11 | { write: (line) => written.push(line) }, |
| 12 | { |
| 13 | onRequest: async (method, params) => { |
| 14 | requests.push({ method, params }); |
| 15 | if (method === "host/fail") throw new RpcError(-32601, "nope"); |
| 16 | return { echoed: params }; |
| 17 | }, |
| 18 | onNotification: (method, params) => notifications.push({ method, params }), |
| 19 | onProtocolError: (kind) => errors.push(kind), |
| 20 | }, |
| 21 | limit, |
| 22 | ); |
| 23 | const feed = (text: string) => client.feed(Buffer.from(text)); |
| 24 | const lastFrame = () => JSON.parse(written[written.length - 1] as string) as Record<string, unknown>; |
| 25 | return { client, written, requests, notifications, errors, feed, lastFrame }; |
| 26 | } |
| 27 | |
| 28 | test("oversized outgoing requests never reach or disconnect the service", async () => { |
| 29 | const h = harness(256); |
| 30 | await assert.rejects(h.client.request("desktop/invoke", { data: "a".repeat(257) }), OversizeFrameError); |
| 31 | assert.equal(h.written.length, 0); |
| 32 | assert.equal(h.client.pendingCount, 0); |
| 33 | assert.equal(h.client.closed, false); |
| 34 | const pending = h.client.request("ping", {}); |
| 35 | const sent = h.lastFrame(); |
| 36 | h.feed(JSON.stringify({ jsonrpc: "2.0", id: sent.id, result: "pong" }) + "\n"); |
| 37 | assert.equal(await pending, "pong"); |
| 38 | }); |
| 39 | |
| 40 | test("LineDecoder reassembles partial lines and strips CR", () => { |
| 41 | const decoder = new LineDecoder(); |
| 42 | assert.deepEqual(decoder.push(Buffer.from('{"a":')), []); |
| 43 | assert.deepEqual(decoder.push(Buffer.from('1}\r\n{"b":2}\n{"c"')), ['{"a":1}', '{"b":2}']); |
| 44 | assert.deepEqual(decoder.push(Buffer.from(":3}\n")), ['{"c":3}']); |
| 45 | }); |
| 46 | |
| 47 | test("LineDecoder rejects a line above the limit, with and without a newline", () => { |
| 48 | const decoder = new LineDecoder(8); |
| 49 | assert.throws(() => decoder.push(Buffer.from("0123456789\n")), OversizeFrameError); |
| 50 | assert.deepEqual(decoder.push(Buffer.from("ok\n")), ["ok"], "the decoder resynchronises after the failure"); |
| 51 | assert.throws(() => decoder.push(Buffer.from("0123456789")), OversizeFrameError); |
| 52 | }); |
| 53 | |
| 54 | test("responses resolve their own request regardless of arrival order", async () => { |
| 55 | const h = harness(); |
| 56 | const first = h.client.request("desktop/invoke", { method: "A" }); |
| 57 | const second = h.client.request("desktop/invoke", { method: "B" }); |
| 58 | assert.equal(h.written.length, 2); |
| 59 | h.feed('{"jsonrpc":"2.0","id":2,"result":"b"}\n{"jsonrpc":"2.0","id":1,"result":"a"}\n'); |
| 60 | assert.equal(await second, "b"); |
| 61 | assert.equal(await first, "a"); |
| 62 | assert.equal(h.client.pendingCount, 0); |
| 63 | }); |
| 64 | |
| 65 | test("non-JSON and non-2.0 lines are counted and ignored", () => { |
| 66 | const h = harness(); |
| 67 | h.feed("plain log line\n{\"id\":1,\"result\":true}\n\n"); |
| 68 | assert.equal(h.client.stats.ignoredLines, 2); |
| 69 | assert.deepEqual(h.errors, ["non-json", "invalid"]); |
| 70 | }); |
| 71 | |
| 72 | test("a response arriving after the timeout is ignored", async () => { |
| 73 | const h = harness(); |
| 74 | await assert.rejects(h.client.request("desktop/start", {}, 5), (error: unknown) => error instanceof RpcError && /timed out/.test(error.message)); |
| 75 | h.feed('{"jsonrpc":"2.0","id":1,"result":{}}\n'); |
| 76 | assert.equal(h.client.stats.orphanResponses, 1); |
| 77 | assert.deepEqual(h.errors, ["orphan-response"]); |
| 78 | }); |
| 79 | |
| 80 | test("error responses reject with the service's code and message", async () => { |
| 81 | const h = harness(); |
| 82 | const pending = h.client.request("desktop/hello", {}); |
| 83 | h.feed('{"jsonrpc":"2.0","id":1,"error":{"code":-32003,"message":"contract digest differs","data":{"x":1}}}\n'); |
| 84 | await assert.rejects(pending, (error: unknown) => error instanceof RpcError && error.code === -32003 && error.message === "contract digest differs"); |
| 85 | }); |
| 86 | |
| 87 | test("reverse requests are answered and notifications dispatched", async () => { |
| 88 | const h = harness(); |
| 89 | h.feed('{"jsonrpc":"2.0","id":"r1","method":"host/window.show","params":{"reason":"tray"}}\n'); |
| 90 | h.feed('{"jsonrpc":"2.0","method":"desktop/event","params":{"seq":1}}\n'); |
| 91 | await new Promise((resolve) => setImmediate(resolve)); |
| 92 | assert.deepEqual(h.requests, [{ method: "host/window.show", params: { reason: "tray" } }]); |
| 93 | assert.deepEqual(h.lastFrame(), { jsonrpc: "2.0", id: "r1", result: { echoed: { reason: "tray" } } }); |
| 94 | assert.deepEqual(h.notifications, [{ method: "desktop/event", params: { seq: 1 } }]); |
| 95 | h.feed('{"jsonrpc":"2.0","id":7,"method":"host/fail","params":{}}\n'); |
| 96 | await new Promise((resolve) => setImmediate(resolve)); |
| 97 | assert.deepEqual(h.lastFrame(), { jsonrpc: "2.0", id: 7, error: { code: -32601, message: "nope" } }); |
| 98 | }); |
| 99 | |
| 100 | test("an oversize frame closes the client and rejects everything in flight", async () => { |
| 101 | const h = harness(32); |
| 102 | const pending = h.client.request("desktop/invoke", {}); |
| 103 | assert.throws(() => h.feed("x".repeat(64) + "\n"), OversizeFrameError); |
| 104 | await assert.rejects(pending, OversizeFrameError); |
| 105 | assert.equal(h.client.closed, true); |
| 106 | await assert.rejects(h.client.request("desktop/invoke", {}), OversizeFrameError); |
| 107 | }); |
| 108 |