Repository navigation
Expand file tree
/
Copy pathworkflows-native.ffi.test.ts
More file actions
353 lines (297 loc) · 10.1 KB
/
Copy pathworkflows-native.ffi.test.ts
File metadata and controls
353 lines (297 loc) · 10.1 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
/**
* One TypeScript workflow runs a TypeScript function, a native function and a
* Go workflow, on the real native library (ADR 0011, TASK-393).
*
* The TypeScript engine runs the steps. `native-function` reaches the Go
* validator through the bridge's `functionCall`, and `go-workflow` hands a
* whole manifest workflow to the Go engine through `workflowRun`. Both read
* the same tree, so they find the same problem.
*/
import * as nodeTest from "node:test";
import * as assert from "@eserstack/test-kit/assert";
import { runtime } from "@eserstack/standards/cross-runtime";
import * as ffi from "@eserstack/ajan/ffi";
import * as nativeFunctions from "@eserstack/ajan/ffi/functions";
import * as functions from "@eserstack/functions";
import * as results from "@eserstack/primitives/results";
import * as workflows from "@eserstack/workflows/mod";
import type * as workflowTypes from "@eserstack/workflows/native-functions";
let library: ffi.FFILibrary | undefined;
nodeTest.before(async () => {
library = await ffi.loadEserAjan({ wasm: false });
});
nodeTest.after(() => {
library?.close();
});
const lib = (): ffi.FFILibrary => {
if (library === undefined) {
throw new Error("the native library did not load");
}
return library;
};
// The ports an application wires: the caller of fromNative and the runner of
// the Go engine, both on the loaded library.
const caller: workflows.NativeCaller = (call) =>
nativeFunctions.callFunction(lib(), call);
const runner: workflows.GoWorkflowRunner = async (request, signal) =>
(await lib().call("workflowRun", request, {
signal,
})) as workflows.GoWorkflowAnswer;
const GO_MANIFEST = [
"workflows:",
" - id: inner",
" on: [inner]",
" steps:",
" - validate-eof",
"",
].join("\n");
const withTree = async (
files: Readonly<Record<string, string>>,
body: (root: string) => Promise<void>,
): Promise<void> => {
const root = await runtime.fs.makeTempDir({ prefix: "workflows-native-" });
try {
for (const [name, content] of Object.entries(files)) {
// Files are written one after another into the same tree.
// oxlint-disable-next-line no-await-in-loop
await runtime.fs.writeTextFile(runtime.path.join(root, name), content);
}
await body(root);
} finally {
await runtime.fs.remove(root, { recursive: true });
}
};
nodeTest.test(
"a workflow runs a TypeScript function, a native function and a Go workflow",
async () => {
await withTree(
{ "a.txt": "no newline at the end", "b.txt": "fine\n" },
async (root) => {
const tools = workflows.createRegistry();
tools.register(
workflows.createHandlerTool("ts-count", "Counts in TypeScript", () =>
functions.task.succeed({ passed: true, stats: { counted: 3 } }),
),
);
tools.register(workflows.createNativeFunctionTool(caller));
tools.register(workflows.createGoWorkflowTool(runner));
const workflow = workflows
.createWorkflow("outer")
.on("check")
.step("ts-count")
.step("native-function", { function: "validate-eof" })
.step("go-workflow", { workflow: "inner", manifest: GO_MANIFEST })
.build();
const run = await functions.task.runTask(
workflows.runWorkflow(workflow, tools, { root }),
);
assert.assertEquals(results.isOk(run), true);
const outcome = results.isOk(run) ? run.value : undefined;
const [tsStep, nativeStep, goStep] = outcome?.steps ?? [];
assert.assertEquals(outcome?.passed, false);
assert.assertEquals(
outcome?.steps.map((step) => step.name),
["ts-count", "validate-eof", "go-workflow"],
);
assert.assertEquals(tsStep?.passed, true);
assert.assertEquals(tsStep?.stats, { counted: 3 });
assert.assertEquals(nativeStep?.passed, false);
assert.assertEquals(nativeStep?.issues.length, 1);
assert.assertEquals(
nativeStep?.issues[0]?.message.endsWith(
"a.txt: file does not end with a newline",
),
true,
);
// The Go engine found the same problem and the step brought it back.
assert.assertEquals(goStep?.passed, false);
assert.assertEquals(goStep?.stats["steps"], 1);
assert.assertEquals(goStep?.stats["failedSteps"], 1);
assert.assertEquals(
goStep?.issues[0]?.message,
`[validate-eof] ${nativeStep?.issues[0]?.message ?? ""}`,
);
},
);
},
);
nodeTest.test(
"a native function runs under its own name through createNativeFunctionTools",
async () => {
await withTree({ "ok.txt": "fine\n" }, async (root) => {
const tools = workflows.createRegistry();
tools.registerAll(
workflows.createNativeFunctionTools(
caller,
nativeFunctions
.listFunctions(lib())
.filter((descriptor) => descriptor.name === "validate-eof"),
),
);
const workflow = workflows
.createWorkflow("named")
.on("check")
.step("validate-eof")
.build();
const run = await functions.task.runTask(
workflows.runWorkflow(workflow, tools, { root }),
);
assert.assertEquals(
results.isOk(run) ? run.value.passed : undefined,
true,
);
assert.assertEquals(
results.isOk(run) ? run.value.steps[0]?.name : undefined,
"validate-eof",
);
});
},
);
nodeTest.test(
"a Go workflow that the Go engine cannot find fails the step",
async () => {
const tools = workflows.createRegistry();
tools.register(workflows.createGoWorkflowTool(runner));
const workflow = workflows
.createWorkflow("outer")
.on("check")
.step("go-workflow", { workflow: "missing", manifest: GO_MANIFEST })
.build();
const run = await functions.task.runTask(
workflows.runWorkflow(workflow, tools, {}),
);
assert.assertEquals(results.isFail(run), true);
},
);
// --- workflow-steps: the first function that streams ---
// The port of fromNativeStream. That this assigns is the check that
// FunctionStreamCaller of @eserstack/ajan and NativeStreamCaller of
// @eserstack/functions have the same shape.
const streamCaller: functions.native.NativeStreamCaller = (call) =>
nativeFunctions.streamFunction(lib(), call);
const live = (): functions.task.Cancellable => ({
signal: new AbortController().signal,
});
const watchSteps =
functions.native.fromNativeStreamOf<workflowTypes.NativeFunctions>(
streamCaller,
)("workflow-steps");
const TWO_SHELL_STEPS = [
"workflows:",
" - id: check",
" on: [precommit]",
" steps:",
" - shell:",
" name: first",
" command: echo one",
" - shell:",
" name: second",
" command: echo two",
"",
].join("\n");
nodeTest.test(
"workflow-steps answers each step of a Go workflow as it ends",
async () => {
await withTree({ "a.txt": "no newline at the end" }, async (root) => {
const items: Array<
results.Result<workflowTypes.WorkflowStepsOutput, unknown>
> = [];
for await (const item of watchSteps(
{ manifest: GO_MANIFEST, workflow: "inner", root },
live(),
)) {
items.push(item);
}
assert.assertEquals(items.length, 1);
const step = results.isOk(items[0]!) ? items[0].value : undefined;
assert.assertEquals(step?.name, "validate-eof");
assert.assertEquals(step?.passed, false);
assert.assertEquals(
step?.issues?.[0]?.message.endsWith(
"a.txt: file does not end with a newline",
),
true,
);
});
},
);
nodeTest.test(
"workflow-steps answers the steps in the order they run",
async () => {
const names: string[] = [];
for await (const item of watchSteps(
{ manifest: TWO_SHELL_STEPS, workflow: "check", root: "." },
live(),
)) {
names.push(results.isOk(item) ? item.value.name : "error");
}
assert.assertEquals(names, ["first", "second"]);
},
);
nodeTest.test(
"a reader that stops after one step stops the Go run",
async () => {
await withTree({}, async (root) => {
const manifest = TWO_SHELL_STEPS.replace(
"command: echo two",
"command: touch second-ran",
);
let seen = 0;
for await (const item of watchSteps(
{ manifest, workflow: "check", root },
live(),
)) {
assert.assertEquals(results.isOk(item), true);
seen += 1;
break;
}
// Leaving the loop closes the stream, and the close returns once the Go
// run is over, so the file says whether the second step ever started.
assert.assertEquals(seen, 1);
assert.assertEquals(
await runtime.fs.exists(runtime.path.join(root, "second-ran")),
false,
);
});
},
);
nodeTest.test(
"a workflow the manifest lacks is one ValidationError",
async () => {
const items = [];
for await (const item of watchSteps(
{ manifest: TWO_SHELL_STEPS, workflow: "missing", root: "." },
live(),
)) {
items.push(item);
}
assert.assertEquals(items.length, 1);
assert.assertEquals(
results.isFail(items[0]!) ? items[0].error._tag : undefined,
"ValidationError",
);
},
);
nodeTest.test(
"a signal that aborts ends a long step with an AbortError",
async () => {
const controller = new AbortController();
const manifest = TWO_SHELL_STEPS.replace("echo one", "sleep 30");
const started = Date.now();
const items = [];
setTimeout(() => controller.abort(new Error("stop")), 300);
for await (const item of watchSteps(
{ manifest, workflow: "check", root: "." },
{ signal: controller.signal },
)) {
items.push(item);
}
assert.assertEquals(Date.now() - started < 10_000, true);
const last = items.at(-1);
assert.assertEquals(
last !== undefined && results.isFail(last) ? last.error._tag : undefined,
"AbortError",
);
},
);