import { readdir, readFile } from "node:fs/promises"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { expect, it } from "vitest"; import { createPiSession } from "../../../test/fixture/create-pi-session.ts"; it("emits only safe extension-local diagnostics and closes lifecycle", async () => { const box = await createPiSession({ extensionPaths: [fileURLToPath(new URL("../index.ts", import.meta.url))], env: { PI_MEMORY_DEBUG: "1", PI_MEMORY_DATABASE_PATH: undefined }, }); try { const runner = box.session.extensionRunner; await runner.emit({ type: "session_start", reason: "startup" }); const tool = box.extensions[0]?.tools.get("memory")?.definition; if (!tool) throw new Error("missing tool"); await tool.execute( "test", { sql: "SELECT 1 AS one", readonly: true, }, undefined, undefined, runner.createToolContext("test", undefined), ); await runner.emit({ type: "session_shutdown", reason: "quit" }); const dir = join( box.env["XDG_STATE_HOME"] as string, "pi-ext", "debug", "memory", ); const files = await readdir(dir); const logs = ( await Promise.all(files.map((file) => readFile(join(dir, file), "utf8"))) ).join("\n"); expect(logs).not.toContain("SELECT 1"); expect(logs).not.toContain(box.cwd); const events = logs .trim() .split("\n") .map((line) => JSON.parse(line)); expect(events.map((event) => event.event)).toEqual( expect.arrayContaining([ "session.start", "database.open", "tool.call", "session.shutdown", ]), ); const toolCall = events.find((event) => event.event === "tool.call"); expect(toolCall).toMatchObject({ outcome: "ok", readonly: true }); } finally { await box.dispose(); } });