|
| 1 | +import { ChildProcessWithoutNullStreams } from "child_process"; |
| 2 | +import { EventEmitter } from "events"; |
| 3 | +import { PassThrough, Writable } from "stream"; |
| 4 | +import { WindowsHostClient } from "./host-client"; |
| 5 | + |
| 6 | +jest.mock("fs", () => ({ existsSync: jest.fn(() => true) })); |
| 7 | + |
| 8 | +function peer() { |
| 9 | + const process = new EventEmitter(); |
| 10 | + const stdout = new PassThrough(); |
| 11 | + const stderr = new PassThrough(); |
| 12 | + const requests: { id: number; command: string; args: unknown }[] = []; |
| 13 | + const stdin = new Writable({ |
| 14 | + write(chunk, _encoding, callback) { |
| 15 | + const request = JSON.parse(chunk.toString()); |
| 16 | + requests.push(request); |
| 17 | + if (request.command === "hello") reply(request.id, { protocol: 1 }); |
| 18 | + callback(); |
| 19 | + } |
| 20 | + }); |
| 21 | + const child = Object.assign(process, { stdin, stdout, stderr }) as unknown as ChildProcessWithoutNullStreams; |
| 22 | + function reply(id: number, result: unknown, pixels = Buffer.alloc(0)) { |
| 23 | + stdout.write(Buffer.concat([Buffer.from(JSON.stringify({ id, result, byteLength: pixels.length }) + "\n"), pixels])); |
| 24 | + } |
| 25 | + return { child, requests, reply }; |
| 26 | +} |
| 27 | + |
| 28 | +describe("WindowsHostClient", () => { |
| 29 | + const clients: WindowsHostClient[] = []; |
| 30 | + afterEach(() => { for (const client of clients) client.close(); clients.length = 0; jest.useRealTimers(); }); |
| 31 | + |
| 32 | + function setup() { |
| 33 | + const remote = peer(); |
| 34 | + const spawn = jest.fn(() => remote.child); |
| 35 | + const client = new WindowsHostClient(() => "/helper.exe", spawn); |
| 36 | + clients.push(client); |
| 37 | + return { ...remote, client, spawn }; |
| 38 | + } |
| 39 | + |
| 40 | + it("starts once, handshakes and serializes commands", async () => { |
| 41 | + const remote = setup(); |
| 42 | + const first = remote.client.request("screenSize"); |
| 43 | + const second = remote.client.request("cursorPosition"); |
| 44 | + await Promise.resolve(); await Promise.resolve(); await Promise.resolve(); |
| 45 | + expect(remote.requests.map(request => request.command)).toEqual(["hello", "screenSize"]); |
| 46 | + remote.reply(2, { width: 100, height: 50 }); |
| 47 | + expect((await first).result).toEqual({ width: 100, height: 50 }); |
| 48 | + expect(remote.requests[2].command).toBe("cursorPosition"); |
| 49 | + remote.reply(3, { x: 4, y: 5 }); |
| 50 | + expect((await second).result).toEqual({ x: 4, y: 5 }); |
| 51 | + expect(remote.spawn).toHaveBeenCalledTimes(1); |
| 52 | + }); |
| 53 | + |
| 54 | + it("assembles fragmented UTF-8 headers and binary screenshot data", async () => { |
| 55 | + const remote = setup(); |
| 56 | + const response = remote.client.request("capture"); |
| 57 | + await Promise.resolve(); await Promise.resolve(); await Promise.resolve(); |
| 58 | + const pixels = Buffer.from([10, 0, 255, 255, 13, 10, 0, 255]); |
| 59 | + const packet = Buffer.concat([Buffer.from(JSON.stringify({ id: 2, result: "é", byteLength: pixels.length }) + "\n"), pixels]); |
| 60 | + for (const byte of packet) remote.child.stdout.emit("data", Buffer.from([byte])); |
| 61 | + expect(await response).toEqual({ result: "é", pixels }); |
| 62 | + }); |
| 63 | + |
| 64 | + it("reports command errors without repeating the command", async () => { |
| 65 | + const remote = setup(); |
| 66 | + const response = remote.client.request("click"); |
| 67 | + const rejected = expect(response).rejects.toThrow("input blocked"); |
| 68 | + await Promise.resolve(); await Promise.resolve(); await Promise.resolve(); |
| 69 | + remote.child.stdout.emit("data", Buffer.from('{"id":2,"error":"input blocked","byteLength":0}\n')); |
| 70 | + await rejected; |
| 71 | + expect(remote.requests.filter(request => request.command === "click")).toHaveLength(1); |
| 72 | + }); |
| 73 | + |
| 74 | + it.each([ |
| 75 | + '{"id":99,"result":null,"byteLength":0}\n', |
| 76 | + '{"id":2,"result":null,"byteLength":-1}\n', |
| 77 | + '{"id":2,"result":null,"byteLength":134217729}\n', |
| 78 | + '{"id":2,"byteLength":0}\n', |
| 79 | + 'not JSON\n' |
| 80 | + ])("fails the connection on malformed responses: %s", async packet => { |
| 81 | + const remote = setup(); |
| 82 | + const response = remote.client.request("capture"); |
| 83 | + const rejected = expect(response).rejects.toBeInstanceOf(Error); |
| 84 | + await Promise.resolve(); await Promise.resolve(); await Promise.resolve(); |
| 85 | + remote.child.stdout.emit("data", Buffer.from(packet)); |
| 86 | + await rejected; |
| 87 | + await expect(remote.client.request("capture")).rejects.toBeInstanceOf(Error); |
| 88 | + expect(remote.spawn).toHaveBeenCalledTimes(1); |
| 89 | + }); |
| 90 | + |
| 91 | + it("rejects active and queued requests when the host exits", async () => { |
| 92 | + const remote = setup(); |
| 93 | + const first = expect(remote.client.request("click")).rejects.toThrow("not retried"); |
| 94 | + const second = expect(remote.client.request("capture")).rejects.toThrow("not retried"); |
| 95 | + await Promise.resolve(); await Promise.resolve(); await Promise.resolve(); |
| 96 | + remote.child.emit("close", 1, null); |
| 97 | + await Promise.all([first, second]); |
| 98 | + expect(remote.requests.map(request => request.command)).toEqual(["hello", "click"]); |
| 99 | + }); |
| 100 | + |
| 101 | + it("ends helper input on close", async () => { |
| 102 | + const remote = setup(); |
| 103 | + const response = expect(remote.client.request("screenSize")).rejects.toThrow("closed"); |
| 104 | + await Promise.resolve(); await Promise.resolve(); await Promise.resolve(); |
| 105 | + remote.client.close(); |
| 106 | + await response; |
| 107 | + expect(remote.child.stdin.writableEnded).toBe(true); |
| 108 | + }); |
| 109 | + |
| 110 | + it("does not restart or retry after a timeout", async () => { |
| 111 | + jest.useFakeTimers(); |
| 112 | + const remote = setup(); |
| 113 | + const response = expect(remote.client.request("click", {}, 10)).rejects.toThrow("may already have occurred"); |
| 114 | + await Promise.resolve(); await Promise.resolve(); await Promise.resolve(); |
| 115 | + jest.advanceTimersByTime(10); |
| 116 | + await response; |
| 117 | + await expect(remote.client.request("click")).rejects.toThrow("may already have occurred"); |
| 118 | + expect(remote.spawn).toHaveBeenCalledTimes(1); |
| 119 | + }); |
| 120 | + |
| 121 | + it("rejects requests exceeding the protocol limit without writing them", async () => { |
| 122 | + const remote = setup(); |
| 123 | + await expect(remote.client.request("type", { text: "a".repeat(1024 * 1024) })).rejects.toThrow("exceeds 1 MiB"); |
| 124 | + expect(remote.requests.map(request => request.command)).toEqual(["hello"]); |
| 125 | + }); |
| 126 | +}); |
0 commit comments