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 initial tool set for the v1 `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(3); expect(spec.phases.map((phase) => phase.id)).toEqual([ "review", "verify", "report", ]); }); it("assigns compute deliberately by phase role and complexity", () => { const spec = parseWorkflow(loadRaw()); expect( Object.fromEntries( spec.phases.map((phase) => [ phase.id, [phase.step.model, phase.step.thinkingLevel], ]), ), ).toEqual({ review: ["small", "medium"], verify: ["big", "high"], report: ["big", "medium"], }); }); it("declares a general review target positional and consumes {args.target}", () => { const spec = parseWorkflow(loadRaw()); // A single positional param soaks the whole tail: `/ultra run review PR 123`, // `/ultra run review src/foo.ts`, `/ultra run 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; }; 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("returns the synthesized analysis, findings, and recommendations report", () => { const spec = parseWorkflow(loadRaw()); expect(spec.return).toBe("{report.results}"); const report = spec.phases.find((phase) => phase.id === "report"); expect(report?.step.prompt).toContain("{review.results}"); expect(report?.step.prompt).toContain("{verify.results}"); expect(report?.step.prompt).toMatch(/analysis/i); expect(report?.step.prompt).toMatch(/recommendations/i); expect(report?.step.schema).toBe("ReviewReport"); const value = { analysis: "The change is sound except for one confirmed defect.", findings: [ { title: "off-by-one in loop", file: "src/a.ts", why: "confirmed", severity: "high", }, ], recommendations: ["Fix the loop bound and rerun focused tests."], }; expect( resolveSelector(spec.return ?? "", { results: { report: [value] } }), ).toEqual([value]); }); 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 }); } }); });