repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
opencode/plugins/chef/test/builder.test.ts
Rawimport { describe, expect, test } from "bun:test";
import { z } from "zod";
import { fragment, pipeline, PipelineBuilder, step } from "../pipeline/builder";
describe("pipeline()", () => {
test("creates a PipelineBuilder instance", () => {
const builder = pipeline("test");
expect(builder).toBeInstanceOf(PipelineBuilder);
});
test("creates a builder with generic context type", () => {
const builder = pipeline<{ foo: string }>("test");
expect(builder).toBeInstanceOf(PipelineBuilder);
// The type system ensures the context type is preserved
});
});
describe("PipelineBuilder.step()", () => {
test("adds a step phase to the pipeline", () => {
const mockOutput = z.object({ result: z.string() });
const builder = pipeline("test").step("s", {
agent: "test-agent",
prompt: "Hello",
output: mockOutput,
});
const result = builder.build();
expect(result.phases[0]).toMatchObject({
name: "s",
type: "step",
agent: "test-agent",
prompt: "Hello",
output: mockOutput,
});
});
test("accepts function prompt", () => {
const mockOutput = z.object({ result: z.string() });
const builder = pipeline("test").step("s", {
agent: "test-agent",
prompt: (ctx: Record<string, any>) => `Hello ${ctx.foo}`,
output: mockOutput,
});
const result = builder.build();
expect(result.phases[0]).toMatchObject({
name: "s",
type: "step",
});
// Verify prompt is stored as a function
const stepPhase = result.phases[0] as { prompt?: unknown };
expect(typeof stepPhase.prompt).toBe("function");
});
test("chains multiple steps", () => {
const mockOutput = z.object({ result: z.string() });
const builder = pipeline("test")
.step("step1", { agent: "a1", prompt: "p1", output: mockOutput })
.step("step2", { agent: "a2", prompt: "p2", output: mockOutput });
const result = builder.build();
expect(result.phases).toHaveLength(2);
expect(result.phases[0]!.name).toBe("step1");
expect(result.phases[1]!.name).toBe("step2");
});
});
describe("PipelineBuilder.gate()", () => {
test("adds a gate phase to the pipeline", () => {
const mockExpect = z.object({ status: z.string() });
const builder = pipeline("test").gate("g", {
agent: "gate-agent",
expect: mockExpect,
});
const result = builder.build();
expect(result.phases[0]).toMatchObject({
name: "g",
type: "gate",
agent: "gate-agent",
expect: mockExpect,
});
});
test("accepts optional agent", () => {
const mockExpect = z.object({ status: z.string() });
const builder = pipeline("test").gate("g", { expect: mockExpect });
const result = builder.build();
expect(result.phases[0]).toMatchObject({
name: "g",
type: "gate",
expect: mockExpect,
});
// Agent is optional, so it should not be present when not provided
const gatePhase = result.phases[0] as { agent?: string };
expect(gatePhase.agent).toBeUndefined();
});
test("accepts onFail target", () => {
const mockExpect = z.object({ status: z.string() });
const mockOutput = z.object({ result: z.string() });
const builder = pipeline("test")
.step("s", { agent: "a", prompt: "p", output: mockOutput })
.gate("g", { expect: mockExpect, onFail: "s" });
const result = builder.build();
expect(result.phases[1]).toMatchObject({
name: "g",
type: "gate",
onFail: "s",
});
});
});
describe("PipelineBuilder.parallel()", () => {
test("adds a parallel phase", () => {
const mockOutput = z.object({ result: z.string() });
const builder = pipeline("test").parallel([
{ name: "p1", agent: "a1", prompt: "p1", output: mockOutput },
{ name: "p2", agent: "a2", prompt: "p2", output: mockOutput },
]);
const result = builder.build();
const parallelPhase = result.phases[0] as {
name: string;
type: string;
steps: any[];
};
expect(parallelPhase).toMatchObject({
name: "parallel_0",
type: "parallel",
});
expect(parallelPhase.steps).toHaveLength(2);
expect(parallelPhase.steps[0]).toMatchObject({
name: "p1",
type: "step",
agent: "a1",
});
expect(parallelPhase.steps[1]).toMatchObject({
name: "p2",
type: "step",
agent: "a2",
});
});
test("names parallel phase with index", () => {
const mockOutput = z.object({ result: z.string() });
const builder = pipeline("test")
.parallel([{ name: "p1", agent: "a", prompt: "p", output: mockOutput }])
.parallel([{ name: "p2", agent: "a", prompt: "p", output: mockOutput }]);
const result = builder.build();
expect(result.phases[0]!.name).toBe("parallel_0");
expect(result.phases[1]!.name).toBe("parallel_1");
});
});
describe("PipelineBuilder.when()", () => {
test("adds a when phase", () => {
const builder = pipeline("test").when((ctx) => ctx.condition === true);
const result = builder.build();
const whenPhase = result.phases[0] as {
name: string;
type: string;
condition: Function;
};
expect(whenPhase).toMatchObject({
name: "when_0",
type: "when",
});
expect(typeof whenPhase.condition).toBe("function");
});
test("names when phase with index", () => {
const builder = pipeline("test")
.when((ctx) => ctx.a)
.when((ctx) => ctx.b);
const result = builder.build();
expect(result.phases[0]!.name).toBe("when_0");
expect(result.phases[1]!.name).toBe("when_1");
});
});
describe("PipelineBuilder.loop()", () => {
test("sets loopTarget on preceding when phase", () => {
const mockOutput = z.object({ result: z.string() });
const builder = pipeline("test")
.step("target", { agent: "a", prompt: "p", output: mockOutput })
.when((ctx) => ctx.continue)
.loop("target");
const result = builder.build();
const whenPhase = result.phases[1]!;
expect(whenPhase.type).toBe("when");
if (whenPhase.type === "when") {
expect(whenPhase.loopTarget).toBe("target");
}
});
test("does nothing when last phase is not when", () => {
const mockOutput = z.object({ result: z.string() });
const builder = pipeline("test")
.step("s", { agent: "a", prompt: "p", output: mockOutput })
.loop("target");
const result = builder.build();
expect(result.phases).toHaveLength(1);
expect(result.phases[0]!.type).toBe("step");
});
});
describe("PipelineBuilder.use()", () => {
test("adds fragment steps as phases", () => {
const mockOutput = z.object({ result: z.string() });
const myFragment = fragment("my-fragment", [
{ name: "f1", agent: "a1", prompt: "p1", output: mockOutput },
{ name: "f2", agent: "a2", prompt: "p2", output: mockOutput },
]);
const builder = pipeline("test").use(myFragment);
const result = builder.build();
expect(result.phases).toHaveLength(2);
expect(result.phases[0]!.name).toBe("f1");
expect(result.phases[0]!.type).toBe("step");
expect(result.phases[1]!.name).toBe("f2");
expect(result.phases[1]!.type).toBe("step");
});
test("preserves step order from fragment", () => {
const mockOutput = z.object({ result: z.string() });
const myFragment = fragment("my-fragment", [
{ name: "first", agent: "a", prompt: "p", output: mockOutput },
{ name: "second", agent: "a", prompt: "p", output: mockOutput },
{ name: "third", agent: "a", prompt: "p", output: mockOutput },
]);
const builder = pipeline("test").use(myFragment);
const result = builder.build();
expect(result.phases[0]!.name).toBe("first");
expect(result.phases[1]!.name).toBe("second");
expect(result.phases[2]!.name).toBe("third");
});
});
describe("PipelineBuilder.validate()", () => {
test("returns valid for a correct pipeline", () => {
const mockOutput = z.object({ result: z.string() });
const mockExpect = z.object({ status: z.string() });
const builder = pipeline("test")
.step("s", { agent: "a", prompt: "p", output: mockOutput })
.gate("g", { agent: "gate-agent", expect: mockExpect });
const result = builder.validate();
expect(result.valid).toBe(true);
expect(result.errors).toHaveLength(0);
});
test("catches invalid output schema", () => {
const builder = pipeline("test").step("s", {
agent: "a",
prompt: "p",
output: {} as any,
});
const result = builder.validate();
expect(result.valid).toBe(false);
expect(result.errors).toContain(
'Step "s" has invalid output schema (expected Zod type)',
);
});
test("catches empty agent", () => {
const mockOutput = z.object({ result: z.string() });
const builder = pipeline("test").step("s", {
agent: "",
prompt: "p",
output: mockOutput,
});
const result = builder.validate();
expect(result.valid).toBe(false);
expect(result.errors).toContain(
'Step "s" has invalid agent (expected string)',
);
});
test("catches invalid gate expect", () => {
const builder = pipeline("test").gate("g", { expect: {} as any });
const result = builder.validate();
expect(result.valid).toBe(false);
expect(result.errors).toContain(
'Gate "g" has invalid expect schema (expected Zod type)',
);
});
test("catches missing onFail target", () => {
const mockExpect = z.object({ status: z.string() });
const builder = pipeline("test").gate("g", {
expect: mockExpect,
onFail: "nonexistent",
});
const result = builder.validate();
expect(result.valid).toBe(false);
expect(result.errors).toContain(
'Gate "g" onFail target "nonexistent" not found in pipeline',
);
});
test("catches missing loop target", () => {
const builder = pipeline("test").when((ctx) => ctx.continue).loop(
"nonexistent",
);
const result = builder.validate();
expect(result.valid).toBe(false);
expect(result.errors).toContain(
'Loop target "nonexistent" not found in pipeline',
);
});
test("validates when loop target exists in later phase", () => {
const mockOutput = z.object({ result: z.string() });
// When is added first, then step - the step doesn't exist yet when validating
// So this should fail
const builder = pipeline("test")
.when((ctx) => ctx.continue)
.loop("target")
.step("target", { agent: "a", prompt: "p", output: mockOutput });
const result = builder.validate();
// The loop target "target" is not yet seen when we validate the when phase
expect(result.valid).toBe(false);
expect(result.errors).toContain(
'Loop target "target" not found in pipeline',
);
});
test("catches invalid parallel step schema", () => {
const builder = pipeline("test").parallel([
{ name: "p1", agent: "a", prompt: "p", output: {} as any },
]);
const result = builder.validate();
expect(result.valid).toBe(false);
expect(result.errors).toContain(
'Parallel step "p1" has invalid output schema',
);
});
test("catches invalid parallel step agent", () => {
const mockOutput = z.object({ result: z.string() });
const builder = pipeline("test").parallel([
{ name: "p1", agent: "", prompt: "p", output: mockOutput },
]);
const result = builder.validate();
expect(result.valid).toBe(false);
expect(result.errors).toContain('Parallel step "p1" has invalid agent');
});
});
describe("PipelineBuilder.build()", () => {
test("returns PipelineDefinition with correct name", () => {
const builder = pipeline("my-pipeline");
const result = builder.build();
expect(result.name).toBe("my-pipeline");
});
test("returns all phases", () => {
const mockOutput = z.object({ result: z.string() });
const mockExpect = z.object({ status: z.string() });
const builder = pipeline("test")
.step("s1", { agent: "a1", prompt: "p1", output: mockOutput })
.gate("g", { expect: mockExpect })
.when((ctx) => ctx.continue);
const result = builder.build();
expect(result.phases).toHaveLength(3);
expect(result.phases[0]!.name).toBe("s1");
expect(result.phases[1]!.name).toBe("g");
// when is named using phases.length, so after 2 phases it's "when_2"
expect(result.phases[2]!.name).toBe("when_2");
});
});
describe("step() helper", () => {
test("creates step config with name", () => {
const mockOutput = z.object({ result: z.string() });
const result = step("my-step", {
agent: "test-agent",
prompt: "test prompt",
output: mockOutput,
});
expect(result).toMatchObject({
name: "my-step",
agent: "test-agent",
prompt: "test prompt",
output: mockOutput,
});
// Should NOT have type field
expect(result).not.toHaveProperty("type");
});
});
describe("fragment() helper", () => {
test("creates fragment definition", () => {
const mockOutput = z.object({ result: z.string() });
const step1 = step("f1", { agent: "a1", prompt: "p1", output: mockOutput });
const step2 = step("f2", { agent: "a2", prompt: "p2", output: mockOutput });
const result = fragment("my-fragment", [step1, step2]);
expect(result).toMatchObject({
name: "my-fragment",
steps: [step1, step2],
});
expect(result.steps).toHaveLength(2);
});
});
describe("chaining", () => {
test("each method returns this for chaining", () => {
const mockOutput = z.object({ result: z.string() });
const mockExpect = z.object({ status: z.string() });
const myFragment = fragment("frag", [
{ name: "frag1", agent: "a", prompt: "p", output: mockOutput },
]);
// Test step()
const builder1 = pipeline("test");
const result1 = builder1.step("s", {
agent: "a",
prompt: "p",
output: mockOutput,
});
expect(result1).toBe(builder1);
// Test gate()
const builder2 = pipeline("test");
const result2 = builder2.gate("g", { expect: mockExpect });
expect(result2).toBe(builder2);
// Test parallel()
const builder3 = pipeline("test");
const result3 = builder3.parallel([
{ name: "p", agent: "a", prompt: "p", output: mockOutput },
]);
expect(result3).toBe(builder3);
// Test when()
const builder4 = pipeline("test");
const result4 = builder4.when((ctx) => ctx.cond);
expect(result4).toBe(builder4);
// Test loop()
const builder5 = pipeline("test");
builder5.step("s", { agent: "a", prompt: "p", output: mockOutput });
builder5.when((ctx) => ctx.cond);
const result5 = builder5.loop("s");
expect(result5).toBe(builder5);
// Test use()
const builder6 = pipeline("test");
const result6 = builder6.use(myFragment);
expect(result6).toBe(builder6);
});
});