Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/fast/__tests__/debug.test.ts

Raw
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("fast debug harness", () => {
	it("writes its lifecycle records in the isolated sandbox", async () => {
		root = mkdtempSync(path.join(tmpdir(), "pi-ext-fast-debug-"));
		t = await createTestSession({
			cwd: root,
			env: { PI_FAST_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", "fast");
		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: "fast" }),
		);
		expect(records).toContainEqual(
			expect.objectContaining({ event: "session.shutdown", extension: "fast" }),
		);
	});
});