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 `feature` workflow: research → plan → critique → refine → // implement → conditional review. It lives beside the bundled-workflows lookup. const here = path.dirname(fileURLToPath(import.meta.url)); const FEATURE_PATH = path.join(here, "..", "workflows", "feature.json"); // Read-only initial tool set for the v1 `fanout` rule. The single `implement` // phase is exempt (it writes via edit/write); only `fanout` steps are checked. const READ_ONLY_TOOLS = new Set(["read", "grep", "find", "ls", "bash"]); function loadRaw(): string { return fs.readFileSync(FEATURE_PATH, "utf8"); } describe("feature shipped workflow", () => { it("parses through parseWorkflow (validates the actual shipped file)", () => { const spec = parseWorkflow(loadRaw()); expect(spec.name).toBe("feature"); expect(spec.phases.map((p) => p.id)).toEqual([ "research", "plan", "critique", "refine", "implement", "review", ]); }); 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({ research: ["small", "medium"], plan: ["big", "high"], critique: ["big", "xhigh"], refine: ["big", "high"], implement: ["medium", "high"], review: ["big", "high"], }); }); it("takes a free-text task as a positional arg consumed by every phase", () => { const spec = parseWorkflow(loadRaw()); expect(spec.args?.params).toEqual(["task"]); // Every reasoning/writing phase remains scoped to the task. for (const id of ["research", "critique", "refine", "implement"]) { const p = spec.phases.find((ph) => ph.id === id); expect(p?.step.prompt).toContain("{args.task}"); } }); it("chains the phases: each consumes its predecessors' results", () => { const spec = parseWorkflow(loadRaw()); const get = (id: string) => spec.phases.find((p) => p.id === id)?.step.prompt ?? ""; expect(get("plan")).toContain("{research.results}"); expect(get("critique")).toContain("{plan.results}"); expect(get("refine")).toContain("{plan.results}"); expect(get("refine")).toContain("{critique.results}"); expect(get("implement")).toContain("{refine.results}"); expect(get("review")).toContain("{implement.results}"); }); it("only `implement` (a single phase) may write; the fanout review stays read-only", () => { const spec = parseWorkflow(loadRaw()); const implement = spec.phases.find((p) => p.id === "implement"); expect(implement?.kind).toBe("single"); expect(implement?.step.tools).toEqual( expect.arrayContaining(["edit", "write"]), ); for (const phase of spec.phases) { if (phase.kind !== "fanout") continue; // the rule only binds fanout steps for (const t of phase.step.tools ?? []) { expect(READ_ONLY_TOOLS.has(t)).toBe(true); } } }); it("strengthens the plan with critique feedback and reviews only completed implementations", () => { const spec = parseWorkflow(loadRaw()); const refine = spec.phases.find((p) => p.id === "refine"); const implement = spec.phases.find((p) => p.id === "implement"); const review = spec.phases.find((p) => p.id === "review"); expect(refine?.step.prompt).toContain("resolves EVERY blocking item"); expect(implement?.when).toBeUndefined(); expect(implement?.step.prompt).toContain("{refine.results}"); expect(review?.when).toBe("{implement.results | where implemented}"); const implementation = spec.schemas?.Implementation as { required?: string[]; }; expect(implementation.required).toContain("implemented"); }); it("reviews across security + quality, each echoing its dimension as the join key", () => { const spec = parseWorkflow(loadRaw()); const review = spec.phases.find((p) => p.id === "review"); expect(review?.kind).toBe("fanout"); expect(review?.over).toEqual(["security", "quality"]); // the verdict echoes `dimension` so a result ties back to its lens expect(review?.step.prompt).toMatch(/dimension/); const verdict = spec.schemas?.Verdict as { required?: string[] }; expect(verdict.required).toContain("dimension"); }); it("returns the review verdicts, and the selector keeps each full row", () => { const spec = parseWorkflow(loadRaw()); expect(spec.return).toBe("{review.results}"); const ctx = { results: { review: [ { dimension: "security", passed: true, findings: [] }, { dimension: "quality", passed: false, findings: ["no tests"], }, ], }, }; const out = resolveSelector(spec.return ?? "", ctx) as Array< Record >; expect(out).toHaveLength(2); expect(out[1]).toEqual({ dimension: "quality", passed: false, findings: ["no tests"], }); }); it("is discoverable as a bundled workflow named `feature`", () => { 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 feature = found.find((w) => w.name === "feature"); expect(feature).toBeDefined(); expect(feature?.source).toBe("bundled"); } finally { fs.rmSync(emptyCwd, { recursive: true, force: true }); fs.rmSync(emptyHome, { recursive: true, force: true }); } }); });