-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathengine_test.ts
More file actions
270 lines (235 loc) · 7.73 KB
/
Copy pathengine_test.ts
File metadata and controls
270 lines (235 loc) · 7.73 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
262
263
264
265
266
267
268
269
270
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
import * as assert from "@std/assert";
import * as task from "@eserstack/functions/task";
import * as results from "@eserstack/primitives/results";
import type { WorkflowFileMutation, WorkflowTool } from "./types.ts";
import { resolveStep, runWorkflow } from "./engine.ts";
import { createRegistry } from "./registry.ts";
// ---------------------------------------------------------------------------
// Mock tools
// ---------------------------------------------------------------------------
const passingTool: WorkflowTool = {
name: "pass-tool",
description: "Always passes",
run: () =>
Promise.resolve({
name: "pass-tool",
passed: true,
issues: [],
mutations: [],
stats: { filesChecked: 1 },
}),
};
const passingTool2: WorkflowTool = {
name: "pass-tool-2",
description: "Also always passes",
run: () =>
Promise.resolve({
name: "pass-tool-2",
passed: true,
issues: [],
mutations: [],
stats: { filesChecked: 2 },
}),
};
const failingTool: WorkflowTool = {
name: "fail-tool",
description: "Returns issues",
run: () =>
Promise.resolve({
name: "fail-tool",
passed: false,
issues: [{ message: "something is wrong", path: "foo.ts", line: 10 }],
mutations: [],
stats: { filesChecked: 1 },
}),
};
const throwingTool: WorkflowTool = {
name: "throw-tool",
description: "Throws an error",
run: () => Promise.reject(new Error("tool exploded")),
};
const mutatingTool: WorkflowTool = {
name: "mutate-tool",
description: "Produces mutations",
run: () =>
Promise.resolve({
name: "mutate-tool",
passed: true,
issues: [],
mutations: [
{ path: "a.txt", oldContent: "old", newContent: "new" },
] as readonly WorkflowFileMutation[],
stats: { filesChecked: 1 },
}),
};
// ---------------------------------------------------------------------------
// resolveStep tests
// ---------------------------------------------------------------------------
Deno.test("resolveStep — string step returns name with empty options", () => {
const result = resolveStep("fix-eof");
assert.assertEquals(result, {
name: "fix-eof",
options: {},
continueOnError: false,
});
});
Deno.test("resolveStep — record step parses name and options", () => {
const result = resolveStep({ "check-json": { exclude: ["x"] } });
assert.assertEquals(result.name, "check-json");
assert.assertEquals(result.options, { exclude: ["x"] });
assert.assertEquals(result.continueOnError, false);
assert.assertEquals(result.timeout, undefined);
});
Deno.test("resolveStep — record with 2 keys throws", () => {
assert.assertThrows(
() => resolveStep({ a: {}, b: {} }),
Error,
"expected exactly one key, got 2",
);
});
Deno.test("resolveStep — continueOnError extracted and stripped from options", () => {
const result = resolveStep({
"my-tool": { continueOnError: true, strict: true },
});
assert.assertEquals(result.continueOnError, true);
assert.assertEquals(result.options, { strict: true });
});
Deno.test("resolveStep — timeout extracted as milliseconds and stripped from options", () => {
const result = resolveStep({
"my-tool": { timeout: 120, verbose: false },
});
assert.assertEquals(result.timeout, 120_000);
assert.assertEquals(result.options, { verbose: false });
assert.assertEquals(result.continueOnError, false);
});
// ---------------------------------------------------------------------------
// runWorkflow tests
// ---------------------------------------------------------------------------
Deno.test({
name:
"runWorkflow — 2 passing tools → result.passed === true, durationMs > 0",
sanitizeOps: false,
sanitizeResources: false,
fn: async () => {
const registry = createRegistry();
registry.registerAll([passingTool, passingTool2]);
const workflow = {
id: "test-wf",
on: ["precommit"],
steps: ["pass-tool", "pass-tool-2"],
} as const;
const taskResult = await task.runTask(runWorkflow(workflow, registry));
assert.assert(results.isOk(taskResult));
const result = taskResult.value;
assert.assertEquals(result.passed, true);
assert.assertEquals(result.workflowId, "test-wf");
assert.assertEquals(result.steps.length, 2);
assert.assert(result.totalDurationMs > 0);
assert.assert(result.steps[0]!.durationMs >= 0);
},
});
Deno.test({
name: "runWorkflow — tool that returns issues → result.passed === false",
sanitizeOps: false,
sanitizeResources: false,
fn: async () => {
const registry = createRegistry();
registry.register(failingTool);
const workflow = {
id: "fail-wf",
on: ["precommit"],
steps: ["fail-tool"],
} as const;
const taskResult = await task.runTask(runWorkflow(workflow, registry));
assert.assert(results.isOk(taskResult));
const result = taskResult.value;
assert.assertEquals(result.passed, false);
assert.assertEquals(result.steps.length, 1);
assert.assertEquals(result.steps[0]!.issues.length, 1);
assert.assertEquals(
result.steps[0]!.issues[0]!.message,
"something is wrong",
);
},
});
Deno.test({
name: "runWorkflow — unknown tool returns error result",
sanitizeOps: false,
sanitizeResources: false,
fn: async () => {
const registry = createRegistry();
registry.register(passingTool);
const workflow = {
id: "unknown-wf",
on: ["precommit"],
steps: ["nonexistent-tool"],
} as const;
const taskResult = await task.runTask(runWorkflow(workflow, registry));
assert.assert(results.isFail(taskResult));
assert.assertStringIncludes(
taskResult.error.message,
"Unknown tool 'nonexistent-tool'",
);
},
});
Deno.test({
name:
"runWorkflow — continueOnError:true + tool throws → continues, step marked failed",
sanitizeOps: false,
sanitizeResources: false,
fn: async () => {
const registry = createRegistry();
registry.registerAll([throwingTool, passingTool]);
const workflow = {
id: "continue-wf",
on: ["precommit"],
steps: [
{ "throw-tool": { continueOnError: true } },
"pass-tool",
],
} as const;
const taskResult = await task.runTask(runWorkflow(workflow, registry));
assert.assert(results.isOk(taskResult));
const result = taskResult.value;
assert.assertEquals(result.passed, false);
assert.assertEquals(result.steps.length, 2);
assert.assertEquals(result.steps[0]!.passed, false);
assert.assertEquals(
result.steps[0]!.issues[0]!.message,
"tool exploded",
);
assert.assertEquals(result.steps[1]!.passed, true);
},
});
Deno.test({
name:
"runWorkflow — onMutations callback invoked when tool returns mutations",
sanitizeOps: false,
sanitizeResources: false,
fn: async () => {
const registry = createRegistry();
registry.register(mutatingTool);
const workflow = {
id: "mutate-wf",
on: ["precommit"],
steps: ["mutate-tool"],
} as const;
const capturedMutations: WorkflowFileMutation[][] = [];
const taskResult = await task.runTask(
runWorkflow(workflow, registry, {
onMutations: (mutations) => {
capturedMutations.push([...mutations]);
return Promise.resolve();
},
}),
);
assert.assert(results.isOk(taskResult));
const result = taskResult.value;
assert.assertEquals(result.passed, true);
assert.assertEquals(capturedMutations.length, 1);
assert.assertEquals(capturedMutations[0]!.length, 1);
assert.assertEquals(capturedMutations[0]![0]!.path, "a.txt");
assert.assertEquals(capturedMutations[0]![0]!.newContent, "new");
},
});