-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathapi.test.ts
More file actions
145 lines (121 loc) · 3.75 KB
/
Copy pathapi.test.ts
File metadata and controls
145 lines (121 loc) · 3.75 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
import test from 'ava';
import { createMockLogger } from '@openfn/logger';
import type { ExecutionPlan } from '@openfn/lexicon';
import createAPI from '../src/api';
import type { RuntimeEngine } from '../src/types';
import loadVersions from '../src/util/load-versions';
// thes are tests on the public api functions generally
// so these are very high level tests and don't allow mock workers or anything
const logger = createMockLogger(undefined, { level: 'debug' });
let api: RuntimeEngine;
test.afterEach(async () => {
logger._reset();
await api?.destroy();
});
test.serial('create a default engine api without throwing', async (t) => {
api = await createAPI();
t.pass();
});
test.serial('create an engine api with options without throwing', async (t) => {
api = await createAPI({ logger });
// just a token test to see if the logger is accepted and used
t.true(logger._history.length > 0);
});
test.serial('create an engine api with a limited surface', async (t) => {
api = await createAPI({ logger });
const keys = Object.keys(api).sort();
// TODO the api will actually probably get a bit bigger than this
t.deepEqual(
keys,
['execute', 'listen', 'on', 'options', 'destroy', 'version'].sort()
);
});
test.serial('engine api includes a version number', async (t) => {
api = await createAPI({ logger });
t.is(api.version, loadVersions().engine);
});
test.serial('engine api uses default options', async (t) => {
api = await createAPI({ logger });
t.truthy(api.options);
t.deepEqual(api.options.statePropsToRemove, ['configuration', 'response']);
t.truthy(api.options.whitelist);
});
test.serial('engine api uses custom options', async (t) => {
const options = {
logger, // no test
repoDir: 'a/b/c',
whitelist: ['/@openfn/'],
maxWorkers: 29,
memoryLimitMb: 99,
runTimeoutMs: 33,
statePropsToRemove: ['z'],
};
api = await createAPI(options);
t.truthy(api.options);
t.is(api.options.repoDir, 'a/b/c');
t.true(api.options.whitelist![0] instanceof RegExp);
t.is(api.options.maxWorkers, 29);
t.is(api.options.memoryLimitMb, 99);
t.is(api.options.runTimeoutMs, 33);
t.deepEqual(api.options.statePropsToRemove, ['z']);
});
// Note that this runs with the actual runtime worker
// I won't want to do deep testing on execute here - I just want to make sure the basic
// execution functionality is working. It's more a test of the api surface than the inner
// workings of the job
test.serial(
'execute should return an event listener and receive workflow-complete',
async (t) => {
return new Promise(async (done) => {
api = await createAPI({
logger,
proxyStdout: true,
});
const plan: ExecutionPlan = {
id: 'a',
workflow: {
steps: [
{
expression: 'export default [s => s]',
// with no adaptor it shouldn't try to autoinstall
},
],
},
options: {},
};
const state = { x: 1 };
const listener = api.execute(plan, state);
listener.on('workflow-complete', () => {
t.pass('workflow completed');
done();
});
});
}
);
test.serial('should listen to workflow-complete', async (t) => {
return new Promise(async (done) => {
api = await createAPI({
logger,
});
const plan: ExecutionPlan = {
id: 'a',
workflow: {
steps: [
{
expression: 'export default [s => s]',
// with no adaptor it shouldn't try to autoinstall
},
],
},
options: {},
};
const state = { x: 1 };
api.execute(plan, state);
api.listen(plan.id!, {
'workflow-complete': () => {
t.pass('workflow completed');
done();
},
});
});
});