-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexecutor.test.ts
More file actions
261 lines (241 loc) · 7.01 KB
/
executor.test.ts
File metadata and controls
261 lines (241 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import { describe, expect, it } from "vitest";
import { dryRun } from "./executor.js";
import type { LogicSpec, Step } from "./types.js";
// =============================================================================
// Helpers
// =============================================================================
/** Build a minimal LogicSpec with given steps and optional reasoning */
function makeSpec(steps: Record<string, Step>, reasoning?: LogicSpec["reasoning"]): LogicSpec {
return {
spec_version: "1.0",
name: "test-spec",
steps,
reasoning,
};
}
// =============================================================================
// Basic Functionality
// =============================================================================
describe("dryRun", () => {
it("returns ok:true for empty spec", () => {
const spec: LogicSpec = {
spec_version: "1.0",
name: "empty",
};
const result = dryRun(spec);
expect(result.ok).toBe(true);
expect(result.totalSteps).toBe(0);
expect(result.totalLevels).toBe(0);
});
it("processes single step correctly", () => {
const spec = makeSpec({
step1: {
description: "First step",
instructions: "Do something",
},
});
const result = dryRun(spec);
expect(result.ok).toBe(true);
expect(result.totalSteps).toBe(1);
expect(result.totalLevels).toBe(1);
expect(result.executionOrder).toEqual(["step1"]);
expect(result.steps).toHaveLength(1);
expect(result.steps[0]?.stepName).toBe("step1");
expect(result.steps[0]?.dagLevel).toBe(0);
});
it("respects DAG ordering for dependent steps", () => {
const spec = makeSpec({
step1: {
description: "First step",
},
step2: {
description: "Second step",
needs: ["step1"],
},
step3: {
description: "Third step",
needs: ["step2"],
},
});
const result = dryRun(spec);
expect(result.ok).toBe(true);
expect(result.totalSteps).toBe(3);
expect(result.totalLevels).toBe(3);
expect(result.executionOrder).toEqual(["step1", "step2", "step3"]);
});
it("groups parallel steps by DAG level", () => {
const spec = makeSpec({
step1: { description: "Root" },
step2a: { description: "Parallel A", needs: ["step1"] },
step2b: { description: "Parallel B", needs: ["step1"] },
step3: { description: "Final", needs: ["step2a", "step2b"] },
});
const result = dryRun(spec);
expect(result.ok).toBe(true);
expect(result.dagLevels).toHaveLength(3);
expect(result.dagLevels[0]).toEqual(["step1"]);
expect(result.dagLevels[1]?.sort()).toEqual(["step2a", "step2b"]);
expect(result.dagLevels[2]).toEqual(["step3"]);
});
it("records step metadata correctly", () => {
const spec = makeSpec({
test_step: {
description: "Test description",
instructions: "Test instructions",
output_schema: {
type: "object",
properties: { result: { type: "string" } },
required: ["result"],
},
retry: {
max_attempts: 3,
initial_interval: "1s",
},
},
});
const result = dryRun(spec);
const trace = result.steps[0];
expect(trace).toBeDefined();
expect(trace?.stepName).toBe("test_step");
expect(trace?.outputSchema).toBeDefined();
expect(trace?.retryPolicy).toBeDefined();
expect(trace?.retryPolicy?.maxAttempts).toBe(3);
});
it("marks step as executed when mock output is provided", () => {
const spec = makeSpec({
step1: {
description: "Step",
output_schema: { type: "object" },
},
});
const result = dryRun(spec, {
mockOutputs: { step1: { result: "test" } },
});
expect(result.steps[0]?.status).toBe("executed");
});
it("marks step as skipped when mock output is missing", () => {
const spec = makeSpec({
step1: {
description: "Step",
},
});
const result = dryRun(spec, {
mockOutputs: {},
});
expect(result.steps[0]?.status).toBe("skipped");
});
it("validates schema contract when validateGates is enabled", () => {
const spec = makeSpec({
step1: {
description: "Step",
output_schema: {
type: "object",
required: ["name"],
properties: { name: { type: "string" } },
},
},
});
const result = dryRun(spec, {
validateGates: true,
mockOutputs: { step1: {} }, // missing required field
});
expect(result.steps[0]?.contractViolations).toContain('Missing required field: "name"');
expect(result.steps[0]?.status).toBe("failed");
expect(result.ok).toBe(false);
});
it("passes schema validation with correct mock output", () => {
const spec = makeSpec({
step1: {
description: "Step",
output_schema: {
type: "object",
required: ["name"],
properties: { name: { type: "string" } },
},
},
});
const result = dryRun(spec, {
validateGates: true,
mockOutputs: { step1: { name: "test" } },
});
expect(result.steps[0]?.contractViolations).toHaveLength(0);
});
it("estimates token count in prompt segment", () => {
const spec = makeSpec({
step1: {
description: "A".repeat(100),
instructions: "B".repeat(100),
},
});
const result = dryRun(spec);
const trace = result.steps[0];
expect(trace?.tokenEstimate).toBeGreaterThan(0);
expect(trace?.promptSegmentLength).toBeGreaterThan(0);
});
it("warns when token estimate exceeds 2000", () => {
const spec = makeSpec({
step1: {
description: "X".repeat(10000),
instructions: "Y".repeat(10000),
},
});
const result = dryRun(spec);
const warnings = result.warnings.filter((w) => w.includes("step1"));
expect(warnings.length).toBeGreaterThan(0);
});
it("handles missing mock output without validateGates", () => {
const spec = makeSpec({
step1: { description: "Step" },
});
const result = dryRun(spec, {
mockOutputs: {},
validateGates: false,
});
expect(result.ok).toBe(true);
expect(result.steps[0]?.status).toBe("skipped");
});
it("detects DAG cycles and reports errors", () => {
const spec = makeSpec({
step1: { needs: ["step2"] },
step2: { needs: ["step1"] },
});
const result = dryRun(spec);
expect(result.ok).toBe(false);
expect(result.errors.length).toBeGreaterThan(0);
expect(result.errors[0]).toMatch(/cycle|Circular/i);
});
it("reports error when step has missing dependency", () => {
const spec = makeSpec({
step1: { needs: ["nonexistent"] },
});
const result = dryRun(spec);
expect(result.ok).toBe(false);
expect(result.errors[0]).toMatch(/nonexistent/);
});
it("includes spec name in result", () => {
const spec = makeSpec({});
spec.name = "my-workflow";
const result = dryRun(spec);
expect(result.specName).toBe("my-workflow");
});
it("populates dagLevels in result", () => {
const spec = makeSpec({
a: {},
b: { needs: ["a"] },
c: { needs: ["b"] },
});
const result = dryRun(spec);
expect(result.dagLevels).toEqual([["a"], ["b"], ["c"]]);
});
it("handles multiple independent root steps", () => {
const spec = makeSpec({
root1: {},
root2: {},
join: { needs: ["root1", "root2"] },
});
const result = dryRun(spec);
expect(result.ok).toBe(true);
expect(result.dagLevels[0]?.sort()).toEqual(["root1", "root2"]);
expect(result.dagLevels[1]).toEqual(["join"]);
});
});