Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/horst/__tests__/debug.test.ts

Raw
import { readdirSync, readFileSync } from "node:fs";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
	createPiSession,
	type PiSession,
} from "../../../test/fixture/create-pi-session.ts";

const extensionPath = path.resolve(import.meta.dirname, "../index.ts");
let session: PiSession | undefined;

afterEach(async () => {
	await session?.dispose();
	session = undefined;
});

describe("horst debug interface", () => {
	it("writes its lifecycle records in the isolated sandbox", async () => {
		session = await createPiSession({
			env: { PI_HORST_DEBUG: "1" },
			extensionPaths: [extensionPath],
		});
		const runner = session.session.extensionRunner;
		await runner.emit({ type: "session_start", reason: "startup" });
		await runner.emit({ type: "session_shutdown", reason: "quit" });

		const directory = path.join(
			session.env["XDG_STATE_HOME"] as string,
			"pi-ext",
			"debug",
			"horst",
		);
		// Debug records flush asynchronously; poll until both events appear.
		await vi.waitFor(() => {
			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: "horst",
				}),
			);
			expect(records).toContainEqual(
				expect.objectContaining({
					event: "session.shutdown",
					extension: "horst",
				}),
			);
		});
	});
});