import { afterEach, describe, expect, it } from "bun:test"; import { execFile } from "node:child_process"; import { writeFile } from "node:fs/promises"; import path from "node:path"; import { promisify } from "node:util"; import { createTestSession, type TestSession, } from "@marcfargas/pi-test-harness"; const execFileAsync = promisify(execFile); const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); describe("diff-review pi-test-harness", () => { let t: TestSession | undefined; afterEach(() => { t?.dispose(); t = undefined; }); it("loads in real Pi extension runtime", async () => { const extensionPath = path.resolve(import.meta.dir, "../index.ts"); t = await createTestSession({ extensions: [extensionPath] }); expect(t.session.extensionRunner.extensions).toHaveLength(1); expect(t.session.extensionRunner.extensions[0].path).toBe(extensionPath); }); it("does not auto-restore a persisted live review on session_start", async () => { const extensionPath = path.resolve(import.meta.dir, "../index.ts"); t = await createTestSession({ extensions: [extensionPath] }); (t.session.sessionManager as any).appendCustomEntry("diff-review", { version: 1, kind: "live", id: "live-1", token: "token-1", round: 1, args: { staged: false }, status: "open", createdAt: new Date().toISOString(), }); const statusCount = t.events.uiCallsFor("setStatus").length; const notifyCount = t.events.uiCallsFor("notify").length; await t.session.extensionRunner.emit({ type: "session_start", reason: "resume", }); await sleep(30); expect( t.events .uiCallsFor("setStatus") .slice(statusCount) .filter((call) => call.args[0] === "diff-review"), ).toEqual([]); expect(t.events.uiCallsFor("notify").slice(notifyCount)).toEqual([]); }); it("does not mutate UI after shutdown closes an active review server", async () => { const extensionPath = path.resolve(import.meta.dir, "../index.ts"); t = await createTestSession({ extensions: [extensionPath] }); await git(t.cwd, ["init", "-q"]); await git(t.cwd, ["config", "user.email", "review@example.invalid"]); await git(t.cwd, ["config", "user.name", "Review"]); await writeFile(path.join(t.cwd, "file.txt"), "old\n"); await git(t.cwd, ["add", "file.txt"]); await git(t.cwd, ["commit", "-qm", "initial"]); await writeFile(path.join(t.cwd, "file.txt"), "new\n"); const command = t.session.extensionRunner.extensions[0].commands.get("diff-review"); await command.handler("", t.session.extensionRunner.createCommandContext()); const notifyCount = t.events.uiCallsFor("notify").length; const statusCount = t.events.uiCallsFor("setStatus").length; await t.session.extensionRunner.emit({ type: "session_shutdown", reason: "quit", }); await sleep(30); expect( t.events .uiCallsFor("notify") .slice(notifyCount) .map((call) => call.args[0]), ).toEqual([]); expect( t.events .uiCallsFor("setStatus") .slice(statusCount) .filter((call) => call.args[0] === "diff-review"), ).toEqual([]); }); }); async function git(cwd: string, args: string[]) { await execFileAsync("git", args, { cwd, maxBuffer: 20 * 1024 * 1024 }); }