-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathparallel-utils.test.ts
More file actions
186 lines (166 loc) · 5.39 KB
/
parallel-utils.test.ts
File metadata and controls
186 lines (166 loc) · 5.39 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
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import {
isParallelGroup,
flattenSteps,
mapConcurrent,
aggregateParallelOutputs,
MAX_PARALLEL_CONCURRENCY,
type RunnerSubagentStep,
type ParallelStepGroup,
type RunnerStep,
} from "./parallel-utils.ts";
describe("isParallelGroup", () => {
it("returns true for a parallel step group", () => {
const step: ParallelStepGroup = {
parallel: [
{ agent: "a", task: "do stuff" },
{ agent: "b", task: "do other stuff" },
],
};
assert.equal(isParallelGroup(step), true);
});
it("returns false for a sequential step", () => {
const step: RunnerSubagentStep = { agent: "a", task: "do stuff" };
assert.equal(isParallelGroup(step), false);
});
it("returns false when parallel is not an array", () => {
const step = { parallel: "not-an-array", agent: "a", task: "x" } as unknown as RunnerStep;
assert.equal(isParallelGroup(step), false);
});
});
describe("flattenSteps", () => {
it("returns sequential steps unchanged", () => {
const steps: RunnerStep[] = [
{ agent: "a", task: "t1" },
{ agent: "b", task: "t2" },
];
const flat = flattenSteps(steps);
assert.equal(flat.length, 2);
assert.equal(flat[0].agent, "a");
assert.equal(flat[1].agent, "b");
});
it("expands parallel groups into individual steps", () => {
const steps: RunnerStep[] = [
{ agent: "scout", task: "find info" },
{
parallel: [
{ agent: "reviewer-a", task: "review part 1" },
{ agent: "reviewer-b", task: "review part 2" },
],
},
{ agent: "summarizer", task: "combine" },
];
const flat = flattenSteps(steps);
assert.equal(flat.length, 4);
assert.deepEqual(
flat.map((s) => s.agent),
["scout", "reviewer-a", "reviewer-b", "summarizer"],
);
});
it("handles empty steps array", () => {
assert.deepEqual(flattenSteps([]), []);
});
it("handles empty parallel group", () => {
const steps: RunnerStep[] = [
{ agent: "before", task: "x" },
{ parallel: [] },
{ agent: "after", task: "y" },
];
const flat = flattenSteps(steps);
assert.equal(flat.length, 2);
assert.deepEqual(flat.map((s) => s.agent), ["before", "after"]);
});
});
describe("mapConcurrent", () => {
it("processes all items and preserves order", async () => {
const items = [10, 20, 30, 40];
const results = await mapConcurrent(items, 2, async (item) => item * 2);
assert.deepEqual(results, [20, 40, 60, 80]);
});
it("respects concurrency limit", async () => {
let running = 0;
let maxRunning = 0;
const items = [1, 2, 3, 4, 5, 6];
await mapConcurrent(items, 2, async () => {
running++;
maxRunning = Math.max(maxRunning, running);
await new Promise((r) => setTimeout(r, 10));
running--;
});
assert.ok(maxRunning <= 2, `max concurrent was ${maxRunning}, expected <= 2`);
});
it("handles empty input", async () => {
const results = await mapConcurrent([], 4, async (item: number) => item);
assert.deepEqual(results, []);
});
it("clamps limit=0 to 1 (sequential execution)", async () => {
let running = 0;
let maxRunning = 0;
const items = [1, 2, 3];
await mapConcurrent(items, 0, async (item) => {
running++;
maxRunning = Math.max(maxRunning, running);
await new Promise((r) => setTimeout(r, 10));
running--;
return item * 10;
});
assert.equal(maxRunning, 1, "should run sequentially with limit=0");
});
it("clamps limit=-1 to 1 (sequential execution)", async () => {
let running = 0;
let maxRunning = 0;
const items = [1, 2, 3];
await mapConcurrent(items, -1, async (item) => {
running++;
maxRunning = Math.max(maxRunning, running);
await new Promise((r) => setTimeout(r, 10));
running--;
return item * 10;
});
assert.equal(maxRunning, 1, "should run sequentially with limit=-1");
});
});
describe("aggregateParallelOutputs", () => {
it("aggregates successful outputs with headers", () => {
const result = aggregateParallelOutputs([
{ agent: "reviewer-a", output: "Looks good", exitCode: 0 },
{ agent: "reviewer-b", output: "Needs fixes", exitCode: 0 },
]);
assert.ok(result.includes("=== Parallel Task 1 (reviewer-a) ==="));
assert.ok(result.includes("Looks good"));
assert.ok(result.includes("=== Parallel Task 2 (reviewer-b) ==="));
assert.ok(result.includes("Needs fixes"));
});
it("marks failed tasks", () => {
const result = aggregateParallelOutputs([
{ agent: "agent-a", output: "partial output", exitCode: 1 },
]);
assert.ok(result.includes("⚠️ FAILED (exit code 1)"));
});
it("marks empty output", () => {
const result = aggregateParallelOutputs([
{ agent: "agent-a", output: "", exitCode: 0 },
]);
assert.ok(result.includes("⚠️ EMPTY OUTPUT"));
});
it("treats whitespace-only output as empty", () => {
const result = aggregateParallelOutputs([
{ agent: "agent-a", output: " \n ", exitCode: 0 },
]);
assert.ok(result.includes("⚠️ EMPTY OUTPUT"));
});
it("marks skipped tasks (exitCode=-1) distinctly from failed", () => {
const result = aggregateParallelOutputs([
{ agent: "agent-a", output: "done", exitCode: 0 },
{ agent: "agent-b", output: "(skipped — fail-fast)", exitCode: -1 },
]);
assert.ok(result.includes("⏭️ SKIPPED"), "skipped task should show SKIPPED");
assert.ok(!result.includes("FAILED"), "skipped task should not show FAILED");
});
});
describe("MAX_PARALLEL_CONCURRENCY", () => {
it("is 4", () => {
assert.equal(MAX_PARALLEL_CONCURRENCY, 4);
});
});