repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/ultra/__tests__/example.test.ts
RawFile outline
- here line 10
- EXAMPLE_PATH line 11
- READ_ONLY_TOOLS line 16
- loadRaw line 18
- ultra-review shipped example line 22
- parses through parseWorkflow without error (validates the actual shipped file) line 23
- Verdict schema includes required `file` and the verify prompt echoes the finding's file (correction #6b) line 32
- every fanout step uses only read-only tools (the v1 read-only-fanout isolation rule) line 49
- the `return` selector correlates: it keeps ONLY real verdicts, each retaining its full join row line 60
- is discoverable as a bundled workflow named `ultra-review line 115
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", "ultra-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("ultra-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("ultra-review");
expect(spec.phases).toHaveLength(2);
expect(spec.phases[0].id).toBe("review");
expect(spec.phases[1].id).toBe("verify");
});
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 `ultra-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 === "ultra-review");
expect(review).toBeDefined();
expect(review?.source).toBe("bundled");
} finally {
fs.rmSync(emptyCwd, { recursive: true, force: true });
fs.rmSync(emptyHome, { recursive: true, force: true });
}
});
});