import { readdirSync, readFileSync } from "node:fs"; import path from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; import { createPiSession, type PiSession, } from "../../../test/fixture/create-pi-session.ts"; const extensionPath = path.resolve(import.meta.dirname, "../index.ts"); describe("angel debug", () => { let session: PiSession | undefined; afterEach(async () => { await session?.dispose(); session = undefined; }); it("writes its lifecycle JSONL through the standalone factory", async () => { session = await createPiSession({ extensionPaths: [extensionPath], env: { PI_ANGEL_DEBUG: "1" }, }); await session.session.extensionRunner.emit({ type: "session_start", reason: "startup", }); await session.session.extensionRunner.emit({ type: "session_shutdown", reason: "quit", }); const stateHome = session.env["XDG_STATE_HOME"]; if (!stateHome) throw new Error("missing sandbox state home"); const directory = path.join(stateHome, "pi-ext", "debug", "angel"); // Debug records flush asynchronously; poll until both events appear. const events = await vi.waitFor( () => { const [file] = readdirSync(directory); if (!file) throw new Error("no debug file yet"); const parsed = readFileSync(path.join(directory, file), "utf8") .trim() .split("\n") .map((line) => JSON.parse(line).event); if ( !parsed.includes("session.start") || !parsed.includes("session.shutdown") ) throw new Error("lifecycle events not flushed yet"); return parsed; }, { timeout: 2_000, interval: 20 }, ); expect(events).toEqual( expect.arrayContaining(["session.start", "session.shutdown"]), ); }); });