import { readdirSync, readFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { afterEach, describe, expect, it, vi } from "vitest"; import { createPiSession, type PiSession, } from "../../../test/fixture/create-pi-session.ts"; describe("cost 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: [fileURLToPath(new URL("../index.ts", import.meta.url))], env: { PI_COST_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", "cost"); let events: unknown[] = []; await vi.waitFor(() => { const [file] = readdirSync(directory); if (!file) throw new Error("no debug file yet"); events = readFileSync(path.join(directory, file), "utf8") .trim() .split("\n") .map((line) => JSON.parse(line).event); expect(events).toEqual( expect.arrayContaining(["session.start", "session.shutdown"]), ); }); expect(events).toEqual( expect.arrayContaining(["session.start", "session.shutdown"]), ); }); });