Repository navigation
Expand file tree
/
Copy pathvalidators-native.ffi.test.ts
More file actions
296 lines (248 loc) · 8.62 KB
/
Copy pathvalidators-native.ffi.test.ts
File metadata and controls
296 lines (248 loc) · 8.62 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
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
/**
* A validator runs from TypeScript as a native function and gives the result
* the Go workflow engine gives (ADR 0011, TASK-395).
*
* The validators of `pkg/ajan/codebasefx/validatortools` are registered as
* functions on the bridge. `functions.native.fromNativeOf` turns one into a
* `Handler` whose input and output come from the generated `NativeFunctions`, with `callFunction` of `@eserstack/ajan/ffi/functions` as its
* port, and the workflow engine runs the same tool through `workflowRun`. Both
* read the same tree, so their issues must be the same.
*/
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 type * as validators from "./native-functions.ts";
type Issue = {
readonly path?: string;
readonly line?: number;
readonly message: string;
};
type ToolOutput = {
readonly passed: boolean;
readonly issues?: readonly Issue[];
readonly stats?: Record<string, unknown>;
};
type WorkflowAnswer = {
readonly error?: string;
readonly results?: ReadonlyArray<{
readonly steps: ReadonlyArray<ToolOutput & { readonly name: string }>;
}>;
};
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 port of fromNative, as an application writes it with the loaded library.
// That this assigns is the check that FunctionCaller of @eserstack/ajan and
// NativeCaller of @eserstack/functions have the same shape.
const caller: functions.native.NativeCaller = (call) =>
nativeFunctions.callFunction(lib(), call);
const live = (): functions.task.Cancellable => ({
signal: new AbortController().signal,
});
const withTree = async (
files: Readonly<Record<string, string>>,
body: (root: string) => Promise<void>,
): Promise<void> => {
const root = await runtime.fs.makeTempDir({ prefix: "validators-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 });
}
};
const MANIFEST = [
"workflows:",
" - id: check",
" on: [check]",
" steps:",
" - validate-eof",
"",
].join("\n");
nodeTest.test("the bridge lists the validators with their schemas", () => {
const answer = lib().call("functionList");
const descriptors = nativeFunctions.listFunctions(lib());
assert.assertEquals(answer.error, undefined);
const eof = descriptors.find(
(descriptor) => descriptor.name === "validate-eof",
);
assert.assertEquals(eof?.wasm, false);
assert.assertEquals(eof?.triggers, ["workflow"]);
assert.assertEquals(
(eof?.inputSchema as { type?: string } | undefined)?.type,
"object",
);
assert.assertEquals(
(eof?.outputSchema as { type?: string } | undefined)?.type,
"object",
);
});
nodeTest.test(
"a validator called with fromNative gives the issues of the Go engine",
async () => {
await withTree(
{ "a.txt": "no newline at the end", "b.txt": "fine\n" },
async (root) => {
const validateEof =
functions.native.fromNativeOf<validators.NativeFunctions>(caller)(
"validate-eof",
);
const result = await functions.task.runTask(
validateEof({ root }),
live(),
);
assert.assertEquals(results.isOk(result), true);
const fromFunction = results.isOk(result) ? result.value : undefined;
const run = (await lib().call("workflowRun", {
manifest: MANIFEST,
event: "check",
root,
})) as WorkflowAnswer;
assert.assertEquals(run.error, undefined);
const fromEngine = run.results?.[0]?.steps[0];
assert.assertEquals(fromFunction?.passed, false);
assert.assertEquals(fromFunction?.passed, fromEngine?.passed);
assert.assertEquals(fromFunction?.issues?.length, 1);
assert.assertEquals(
fromFunction?.issues?.map((issue) => issue.message),
fromEngine?.issues?.map((issue) => issue.message),
);
assert.assertEquals(
fromFunction?.stats?.["filesChecked"],
fromEngine?.stats?.["filesChecked"],
);
assert.assertEquals(
fromFunction?.issues?.[0]?.message.endsWith(
"a.txt: file does not end with a newline",
),
true,
);
},
);
},
);
nodeTest.test("a validator that finds nothing passes", async () => {
await withTree({ "ok.txt": "fine\n" }, async (root) => {
const validateEof =
functions.native.fromNativeOf<validators.NativeFunctions>(caller)(
"validate-eof",
);
const result = await functions.task.runTask(validateEof({ root }), live());
assert.assertEquals(
results.isOk(result) ? result.value.passed : undefined,
true,
);
});
});
nodeTest.test(
"an input the schema refuses fails with a ValidationError",
async () => {
const validateLargeFiles =
functions.native.fromNativeOf<validators.NativeFunctions>(caller)(
"validate-large-files",
);
// The generated type says maxKb is a number, so a caller of the types cannot
// write this. The cast reaches the check the schema still makes at run
// time, for a caller without the types.
const result = await functions.task.runTask(
validateLargeFiles({
maxKb: "big",
} as unknown as validators.ValidateLargeFilesInput),
live(),
);
assert.assertEquals(
results.isFail(result) ? result.error._tag : undefined,
"ValidationError",
);
},
);
nodeTest.test(
"a name the library does not serve fails with an UnknownFunction",
async () => {
const missing = functions.native.fromNative<
null,
validators.ValidateEofOutput
>(caller, "validate-nothing-of-the-kind");
const result = await functions.task.runTask(missing(null), live());
assert.assertEquals(
results.isFail(result) ? result.error._tag : undefined,
"UnknownFunction",
);
},
);
// The port of fromNativeStream, as an application writes it. 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 firstOf = async <T, E>(
items: AsyncGenerator<results.Result<T, E>>,
): Promise<results.Result<T, E> | undefined> => {
for await (const item of items) {
return item;
}
return undefined;
};
nodeTest.test(
"the only function the bridge lists as a stream is workflow-steps",
() => {
const streaming = nativeFunctions
.listFunctions(lib())
.filter((descriptor) => descriptor.stream === true)
.map((descriptor) => descriptor.name);
// The validators answer once. workflow-steps is the first stream
// (workflows-native.ffi.test.ts reads it end to end).
assert.assertEquals(streaming, ["workflow-steps"]);
},
);
nodeTest.test(
"fromNativeStream on a function that does not stream fails with the kind the bridge gives",
async () => {
const validateEof = functions.native.fromNativeStream<
validators.ValidateEofInput,
validators.ValidateEofOutput
>(streamCaller, "validate-eof");
const first = await firstOf(validateEof({}, live()));
assert.assertEquals(
first !== undefined && results.isFail(first)
? first.error.message.includes("does not stream")
: undefined,
true,
);
},
);
nodeTest.test(
"fromNativeStream on a name the library does not serve fails with an UnknownFunction",
async () => {
const missing = functions.native.fromNativeStream<null, unknown>(
streamCaller,
"no-such-stream",
);
const first = await firstOf(missing(null, live()));
assert.assertEquals(
first !== undefined && results.isFail(first)
? first.error._tag
: undefined,
"UnknownFunction",
);
},
);