repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/ultra/__tests__/example.test.ts

Raw
File outline
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
import { resolveSelector } from "../interp.ts";
import { discoverWorkflows, parseWorkflow } from "../spec.ts";

// The shipped example lives beside the runtime's bundled-workflows lookup.
const here = path.dirname(fileURLToPath(import.meta.url));
const EXAMPLE_PATH = path.join(here, "..", "workflows", "review.json");

// Read-only tool allowlist for the v1 read-only-`fanout` isolation rule. Every
// `fanout` step in the shipped example must stay within this set (this example
// IS the rule's reference implementation).
const READ_ONLY_TOOLS = new Set(["read", "grep", "find", "ls", "bash"]);

function loadRaw(): string {
	return fs.readFileSync(EXAMPLE_PATH, "utf8");
}

describe("review shipped example", () => {
	it("parses through parseWorkflow without error (validates the actual shipped file)", () => {
		// Pass the raw string so JSON.parse runs too — a malformed file fails loudly.
		const spec = parseWorkflow(loadRaw());
		expect(spec.name).toBe("review");
		expect(spec.phases).toHaveLength(2);
		expect(spec.phases[0].id).toBe("review");
		expect(spec.phases[1].id).toBe("verify");
	});

	it("declares a general review target positional and consumes {args.target}", () => {
		const spec = parseWorkflow(loadRaw());
		// A single positional param soaks the whole tail: `/ultra review PR 123`,
		// `/ultra review src/foo.ts`, `/ultra review last 3 commits`, etc.
		expect(spec.args?.params).toEqual(["target"]);
		const review = spec.phases.find((p) => p.id === "review");
		expect(review?.step.prompt).toContain("{args.target}");
		expect(review?.step.prompt).toMatch(/PR|branch|folder|file|recent/i);
	});

	it("Verdict schema includes required `file` and the verify prompt echoes the finding's file (correction #6b)", () => {
		const spec = parseWorkflow(loadRaw());

		// Producer + consumer side of the join key both present.
		const verdict = spec.schemas?.Verdict as {
			required?: string[];
			properties?: Record<string, unknown>;
		};
		expect(verdict.required).toContain("file");
		expect(verdict.properties).toHaveProperty("file");

		const verify = spec.phases.find((p) => p.id === "verify");
		// The echo instruction ties filtered verdicts back to their findings.
		expect(verify?.step.prompt).toMatch(/echo/i);
		expect(verify?.step.prompt).toMatch(/file/);
	});

	it("every fanout step uses only read-only tools (the v1 read-only-fanout isolation rule)", () => {
		const spec = parseWorkflow(loadRaw());
		for (const phase of spec.phases) {
			if (phase.kind !== "fanout") continue;
			const tools = phase.step.tools ?? []; // omitted tools => one-shot complete()
			for (const t of tools) {
				expect(READ_ONLY_TOOLS.has(t)).toBe(true);
			}
		}
	});

	it("the `return` selector correlates: it keeps ONLY real verdicts, each retaining its full join row", () => {
		const spec = parseWorkflow(loadRaw());
		// Lockstep: drive the test with the file's actual `return` expression.
		expect(spec.return).toBe("{verify.results | where real}");

		// Hand-built fanout-result fixture: some real, some not, each echoing `file`.
		const ctx = {
			results: {
				verify: [
					{
						title: "off-by-one in loop",
						real: true,
						why: "confirmed",
						file: "src/a.ts",
					},
					{
						title: "spurious nullcheck",
						real: false,
						why: "refuted",
						file: "src/b.ts",
					},
					{
						title: "unsanitized input",
						real: true,
						why: "reproduced",
						file: "src/c.ts",
					},
				],
			},
		};

		const out = resolveSelector(spec.return ?? "", ctx) as Array<
			Record<string, unknown>
		>;

		// ONLY the real rows survive...
		expect(out).toHaveLength(2);
		// ...and each retains its WHOLE row (title+why+file together) so the verdict
		// still ties back to the finding it judged — not just a bare `file`.
		expect(out).toEqual([
			{
				title: "off-by-one in loop",
				real: true,
				why: "confirmed",
				file: "src/a.ts",
			},
			{
				title: "unsanitized input",
				real: true,
				why: "reproduced",
				file: "src/c.ts",
			},
		]);
	});

	it("is discoverable as a bundled workflow named `review`", () => {
		// Empty project/global dirs => only the bundled dir (the real shipped file)
		// contributes. `discoverWorkflows` keys on the `name` field.
		const emptyCwd = fs.mkdtempSync(path.join(os.tmpdir(), "ultra-cwd-"));
		const emptyHome = fs.mkdtempSync(path.join(os.tmpdir(), "ultra-home-"));
		try {
			const found = discoverWorkflows(emptyCwd, { home: emptyHome });
			const review = found.find((w) => w.name === "review");
			expect(review).toBeDefined();
			expect(review?.source).toBe("bundled");
		} finally {
			fs.rmSync(emptyCwd, { recursive: true, force: true });
			fs.rmSync(emptyHome, { recursive: true, force: true });
		}
	});
});