import { afterEach, describe, expect, test } from "bun:test"; import { mkdtempSync, readFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { LLMITERATE_WORKER_ENV, PiRpcWorker, workerArgs } from "../rpc"; import type { RpcEvent } from "../types"; const tempDirs: string[] = []; const workers: PiRpcWorker[] = []; const fakeRpcScript = new URL("./fake-rpc-child.mjs", import.meta.url).pathname; type FakeRpcMode = "ok" | "fail-new-session" | "malformed" | "env" | "hang"; afterEach(() => { while (workers.length) workers.pop()!.dispose(); while (tempDirs.length) rmSync(tempDirs.pop()!, { recursive: true, force: true }); }); describe("PiRpcWorker args", () => { test("builds llmiterate worker invocation", () => { expect( workerArgs({ sessionDir: "/state/sessions", model: "zai/glm-5.1", thinking: "high", }), ).toEqual([ "--mode", "rpc", "--offline", "--session-dir", "/state/sessions", "--append-system-prompt", expect.stringContaining("llmiterate"), "--model", "zai/glm-5.1", "--thinking", "high", ]); }); test("does not use no-extensions and sets worker env", async () => { const root = tempDir(); const worker = fakeWorker(root, "env"); workers.push(worker); await worker.runPrompt("do thing", "job name"); expect( JSON.parse(readFileSync(join(root, "env.json"), "utf-8")), ).toMatchObject({ [LLMITERATE_WORKER_ENV]: "1", }); }); }); describe("PiRpcWorker integration", () => { test("runs prompt through JSON-RPC worker and forwards events", async () => { const root = tempDir(); const events: RpcEvent[] = []; const worker = fakeWorker(root, "ok", (event) => events.push(event)); workers.push(worker); await worker.runPrompt("do thing", "job name"); expect(events.map((e) => e.type)).toEqual([ "message_update", "tool_execution_start", "tool_execution_end", "agent_end", ]); const first = events[0]; expect(first.type).toBe("message_update"); if (first.type === "message_update") { const content = first.message?.content; expect(Array.isArray(content) ? content[0]?.text : undefined).toBe( "streaming", ); } }); test("rejects concurrent prompts instead of corrupting current prompt", async () => { const worker = fakeWorker(tempDir(), "ok"); workers.push(worker); const first = worker.runPrompt("first", "first job"); await expect(worker.runPrompt("second", "second job")).rejects.toThrow( "already has an active prompt", ); await first; }); test("rejects failed RPC command", async () => { const worker = fakeWorker(tempDir(), "fail-new-session"); workers.push(worker); await expect(worker.runPrompt("do thing", "job name")).rejects.toThrow( "new_session denied", ); }); test("times out hung prompts", async () => { const worker = fakeWorker(tempDir(), "hang", () => {}, 20); workers.push(worker); await expect(worker.runPrompt("do thing", "job name")).rejects.toThrow( "timed out", ); }); test("rejects malformed RPC stdout", async () => { const worker = fakeWorker(tempDir(), "malformed"); workers.push(worker); await expect(worker.runPrompt("do thing", "job name")).rejects.toThrow(); }); }); function fakeWorker( root: string, mode: FakeRpcMode, onEvent: (event: RpcEvent) => void = () => {}, timeoutMs?: number, ): PiRpcWorker { return new PiRpcWorker({ cwd: root, sessionDir: join(root, "sessions"), invocation: { command: process.execPath, args: [fakeRpcScript, mode, root], }, timeoutMs, onEvent, }); } function tempDir(): string { const dir = mkdtempSync(join(tmpdir(), "llmiterate-rpc-")); tempDirs.push(dir); return dir; }