|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { test } from "node:test"; |
| 3 | + |
| 4 | +import type { ApprovalWorkflow as TWorkflow } from "@/lib/approval-workflow"; |
| 5 | +import type { WorkflowEditOp } from "@/lib/workflow-edit"; |
| 6 | +import { runEditAgent, type PlanModel } from "@/lib/workflow-edit-agent"; |
| 7 | + |
| 8 | +/** |
| 9 | + * The agent's ORCHESTRATION is what's tested here (the model is faked): it applies a |
| 10 | + * planned op list in order, validates with the validator, and on errors feeds them |
| 11 | + * back for a correction pass. The model itself (op selection) is exercised live in |
| 12 | + * the eval. |
| 13 | + */ |
| 14 | + |
| 15 | +const base: TWorkflow = { |
| 16 | + name: "base", |
| 17 | + roots: ["manager"], |
| 18 | + steps: [ |
| 19 | + { |
| 20 | + id: "manager", |
| 21 | + kind: "approval", |
| 22 | + label: "Manager review", |
| 23 | + when: { kind: "always" }, |
| 24 | + approverTitle: "Manager", |
| 25 | + approverName: "Riley Carter", |
| 26 | + next: ["post"], |
| 27 | + }, |
| 28 | + { |
| 29 | + id: "post", |
| 30 | + kind: "integration", |
| 31 | + label: "Post to NetSuite", |
| 32 | + when: { kind: "always" }, |
| 33 | + integration: "netsuite", |
| 34 | + next: [], |
| 35 | + }, |
| 36 | + ], |
| 37 | +}; |
| 38 | + |
| 39 | +test("dispatches a multi-op plan in order (one round)", async () => { |
| 40 | + const model: PlanModel = { |
| 41 | + planOps: () => |
| 42 | + Promise.resolve<WorkflowEditOp[]>([ |
| 43 | + { |
| 44 | + op: "add-approval", |
| 45 | + label: "CFO review", |
| 46 | + approverTitle: "CFO", |
| 47 | + amountOver: 50000, |
| 48 | + department: null, |
| 49 | + }, |
| 50 | + { |
| 51 | + op: "add-integration", |
| 52 | + label: "Notify on Slack", |
| 53 | + integration: "slack", |
| 54 | + }, |
| 55 | + ]), |
| 56 | + }; |
| 57 | + const { proposed, ops, changes } = await runEditAgent( |
| 58 | + model, |
| 59 | + base, |
| 60 | + "add a CFO gate over 50k and a Slack notice", |
| 61 | + ); |
| 62 | + assert.equal(ops.length, 2, "both ops applied"); |
| 63 | + assert.ok(proposed.steps.some((s) => s.label === "CFO review")); |
| 64 | + assert.ok(proposed.steps.some((s) => s.label === "Notify on Slack")); |
| 65 | + assert.ok(changes.some((c) => c.kind === "added")); |
| 66 | +}); |
| 67 | + |
| 68 | +test("on an erroring plan, it re-plans with the validator's errors as feedback", async () => { |
| 69 | + // The defining agentic behaviour: if the first plan leaves the workflow with a |
| 70 | + // validation ERROR, the agent calls the planner AGAIN and hands it those errors so |
| 71 | + // it can correct. (We assert the feedback contract — the loop's correction round.) |
| 72 | + let sawFeedbackError = false; |
| 73 | + let call = 0; |
| 74 | + const model: PlanModel = { |
| 75 | + planOps: ({ feedback }) => { |
| 76 | + call++; |
| 77 | + if (feedback?.issues.some((i) => i.severity === "error")) |
| 78 | + sawFeedbackError = true; |
| 79 | + if (call === 1) { |
| 80 | + // remove the post → "no-post" / "post-not-reached" error |
| 81 | + return Promise.resolve<WorkflowEditOp[]>([ |
| 82 | + { op: "remove-step", stepId: "post" }, |
| 83 | + ]); |
| 84 | + } |
| 85 | + return Promise.resolve<WorkflowEditOp[]>([]); // give up on the correction |
| 86 | + }, |
| 87 | + }; |
| 88 | + await runEditAgent(model, base, "tidy up"); |
| 89 | + assert.equal(call >= 2, true, "it ran a correction round"); |
| 90 | + assert.equal( |
| 91 | + sawFeedbackError, |
| 92 | + true, |
| 93 | + "the errors were fed back to the planner", |
| 94 | + ); |
| 95 | +}); |
| 96 | + |
| 97 | +test("returns a reason when the plan is all no-ops (no change)", async () => { |
| 98 | + const model: PlanModel = { |
| 99 | + planOps: () => |
| 100 | + Promise.resolve<WorkflowEditOp[]>([ |
| 101 | + { op: "none", reason: "already does that" }, |
| 102 | + ]), |
| 103 | + }; |
| 104 | + const { changes, reason } = await runEditAgent( |
| 105 | + model, |
| 106 | + base, |
| 107 | + "do nothing useful", |
| 108 | + ); |
| 109 | + assert.equal( |
| 110 | + changes.filter((c) => c.kind !== "unchanged").length, |
| 111 | + 0, |
| 112 | + "no real change", |
| 113 | + ); |
| 114 | + assert.match(reason ?? "", /already does that/); |
| 115 | +}); |
| 116 | + |
| 117 | +test("stops at the step budget on a stubborn error (doesn't hang)", async () => { |
| 118 | + // The model keeps removing the post (always leaving a no-post error) — the loop |
| 119 | + // must terminate at the budget rather than spin forever. |
| 120 | + let calls = 0; |
| 121 | + const model: PlanModel = { |
| 122 | + planOps: () => { |
| 123 | + calls++; |
| 124 | + return Promise.resolve<WorkflowEditOp[]>([ |
| 125 | + { op: "remove-step", stepId: "post" }, |
| 126 | + ]); |
| 127 | + }, |
| 128 | + }; |
| 129 | + const { issues } = await runEditAgent(model, base, "break it"); |
| 130 | + assert.ok(calls <= 4, "bounded to the step budget"); |
| 131 | + // It returns (doesn't hang); the remaining error is surfaced. |
| 132 | + assert.ok(issues.some((i) => i.severity === "error")); |
| 133 | +}); |
0 commit comments