repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/split-fork/__tests__/harness.test.ts

Raw
File outline
import { copyFileSync, mkdtempSync, 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";
import splitForkExtension, { __test } from "../index";

let t: TestSession | undefined;
let fakeBin: string | undefined;

afterEach(() => {
	t?.dispose();
	if (fakeBin) rmSync(fakeBin, { recursive: true, force: true });
	t = undefined;
	fakeBin = undefined;
});

describe("split-fork extension", () => {
	it("loads in real Pi extension runtime", async () => {
		t = await createTestSession({ extensionFactories: [splitForkExtension] });

		expect(t.session.extensionRunner.extensions).toHaveLength(1);
	});

	it("finds osascript through native PATH access", () => {
		fakeBin = mkdtempSync(path.join(tmpdir(), "pi-split-fork-bin-"));
		copyFileSync(process.execPath, path.join(fakeBin, "osascript"));

		expect(__test.hasOsascript({ Path: fakeBin })).toBe(true);
		expect(__test.hasOsascript({ PATH: path.join(fakeBin, "missing") })).toBe(
			false,
		);
	});

	it("shell quotes prompt-safe command parts", () => {
		expect(__test.shellQuote("can't")).toBe("'can'\"'\"'t'");
		expect(__test.shellQuote("")).toBe("''");
	});

	it("builds startup input with session and prompt", () => {
		const startup = __test.buildPiStartupInput(
			"/tmp/session.jsonl",
			"review this",
		);

		expect(startup).toContain("'--session' '/tmp/session.jsonl'");
		expect(startup).toContain("'--' 'review this'");
		expect(startup.endsWith("\n")).toBe(true);
	});
});