rpc-client-process-exit.test.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, describe, expect, test } from "vitest";
  5. import { RpcClient } from "../src/modes/rpc/rpc-client.ts";
  6. const tempDirs: string[] = [];
  7. function writeChildScript(contents: string): string {
  8. const dir = mkdtempSync(join(tmpdir(), "pi-rpc-client-exit-"));
  9. tempDirs.push(dir);
  10. const path = join(dir, "child.mjs");
  11. writeFileSync(path, contents);
  12. return path;
  13. }
  14. afterEach(() => {
  15. for (const dir of tempDirs.splice(0)) {
  16. rmSync(dir, { recursive: true, force: true });
  17. }
  18. });
  19. describe("RpcClient child process failures", () => {
  20. test("rejects an in-flight request when the child process exits", async () => {
  21. const client = new RpcClient({
  22. cliPath: writeChildScript(`
  23. process.stdin.once("data", () => {
  24. process.exit(43);
  25. });
  26. process.stdin.resume();
  27. `),
  28. });
  29. await client.start();
  30. await expect(client.getCommands()).rejects.toThrow(/Agent process exited \(code=43 signal=null\)/);
  31. });
  32. });