import { mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { createTestSession, type TestSession } from "../../../test/harness"; const extensionPath = path.resolve(import.meta.dirname, "../index.ts"); let root: string | undefined; let t: TestSession | undefined; afterEach(() => { t?.dispose(); if (root) rmSync(root, { recursive: true, force: true }); t = undefined; root = undefined; }); describe("intellij debug harness", () => { it("writes its lifecycle records in the isolated sandbox", async () => { root = mkdtempSync(path.join(tmpdir(), "pi-ext-intellij-debug-")); t = await createTestSession({ cwd: root, env: { PI_INTELLIJ_DEBUG: "1" }, extensions: [extensionPath], }); await t.session.extensionRunner.emit({ type: "session_start", reason: "startup", }); await t.session.extensionRunner.emit({ type: "session_shutdown", reason: "quit", }); const directory = path.join( root, ".test-state", "pi-ext", "debug", "intellij", ); const records = readdirSync(directory).flatMap((file) => readFileSync(path.join(directory, file), "utf8") .trim() .split("\n") .filter(Boolean) .map( (line) => JSON.parse(line) as { event: string; extension: string }, ), ); expect(records).toContainEqual( expect.objectContaining({ event: "session.start", extension: "intellij", }), ); expect(records).toContainEqual( expect.objectContaining({ event: "session.shutdown", extension: "intellij", }), ); }); });