import { readdirSync, readFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { afterEach, describe, expect, it } from "vitest"; import { createPiSession, type PiSession, } from "../../../test/fixture/create-pi-session.ts"; describe("usage pi-test-harness", () => { let session: PiSession | undefined; afterEach(async () => { await session?.dispose(); session = undefined; }); it("loads in real Pi extension runtime", async () => { session = await createPiSession({ extensionPaths: [fileURLToPath(new URL("../index.ts", import.meta.url))], }); expect(session.extensions).toHaveLength(1); }); it("writes debug lifecycle records in its sandbox when enabled", async () => { session = await createPiSession({ env: { PI_USAGE_DEBUG: "1" }, extensionPaths: [fileURLToPath(new URL("../index.ts", import.meta.url))], }); const extension = session.extensions[0]!; const context = session.session.extensionRunner.createCommandContext(); for (const handler of extension.handlers.get("session_start") ?? []) await handler({ type: "session_start" }, context); for (const handler of extension.handlers.get("session_shutdown") ?? []) await handler({ type: "session_shutdown" }, context); const directory = path.join( session.cwd, ".test-state", "pi-ext", "debug", "usage", ); const records = readFileSync( path.join(directory, readdirSync(directory)[0]!), "utf8", ) .trim() .split("\n") .map((line) => JSON.parse(line)); expect(records.map((record) => record.event)).toEqual( expect.arrayContaining(["session.start", "session.shutdown"]), ); }); it("loads the dashboard implementation on first command use", async () => { session = await createPiSession({ extensionPaths: [fileURLToPath(new URL("../index.ts", import.meta.url))], }); const command = session.extensions[0]!.commands.get("usage")!; await expect(command.handler("", { hasUI: false } as never)).resolves.toBe( undefined, ); }); });