-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmock-worker.test.ts
More file actions
145 lines (130 loc) · 3.73 KB
/
Copy pathmock-worker.test.ts
File metadata and controls
145 lines (130 loc) · 3.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
/**
* Simple suite of unit tests against the mock worker API
* Passes a job in and expects to get a simulated result back
*
* The mock is a real web worker which internally uses a mock runtime engine
* Inside the worker, everything apart from execute is the same as the real environment
*/
import path from 'node:path';
import test from 'ava';
import createPool from '../../src/worker/pool';
import { createPlan } from '../../src/test/util';
import * as e from '../../src/worker/events';
import { createMockLogger } from '@openfn/logger';
const logger = createMockLogger('', { level: 'debug' });
const workers = createPool(
path.resolve('dist/test/mock-run.js'),
{
capacity: 1,
},
logger
);
test('execute a mock plan inside a worker thread', async (t) => {
const plan = createPlan();
const result = await workers.exec('run', [plan, {}]);
t.deepEqual(result, { data: { answer: 42 } });
});
test('execute a mock plan with data', async (t) => {
const plan = createPlan({
id: 'j2',
data: { input: 44 },
});
const result = await workers.exec('run', [plan, {}]);
t.deepEqual(result, { data: { answer: 44 } });
});
test('execute a mock plan with an expression', async (t) => {
const plan = createPlan({
id: 'j2',
expression: '() => ({ data: { answer: 46 } })',
});
const result = await workers.exec('run', [plan, {}]);
t.deepEqual(result, { data: { answer: 46 } });
});
test('execute a mock plan with an expression which uses state', async (t) => {
const plan = createPlan({
id: 'j2',
data: { input: 2 },
expression: '(s) => ({ data: { answer: s.data.input * 2 } })',
});
const result = await workers.exec('run', [plan, {}]);
t.deepEqual(result, { data: { answer: 4 } });
});
test('execute a mock plan with a promise expression', async (t) => {
const plan = createPlan({
id: 'j2',
expression: `(s) =>
new Promise((resolve) => {
setTimeout(() => {
resolve({ data: { answer: 46 } })
}, 1);
})`,
});
const result = await workers.exec('run', [plan, {}]);
t.deepEqual(result, { data: { answer: 46 } });
});
test('expression state overrides data', async (t) => {
const plan = createPlan({
id: 'j2',
data: { answer: 44 },
expression: '() => ({ data: { agent: "007" } })',
});
const result = await workers.exec('run', [plan, {}]);
t.deepEqual(result, { data: { agent: '007' } });
});
test('execute a mock plan with delay', async (t) => {
const start = new Date().getTime();
const plan = createPlan({
id: 'j1',
_delay: 50,
});
await workers.exec('run', [plan, {}]);
const elapsed = new Date().getTime() - start;
t.log(elapsed);
t.assert(elapsed > 40);
});
test('Publish workflow-start event', async (t) => {
t.plan(1)
const plan = createPlan();
plan.id = 'xx';
await workers.exec('run', [plan, {}], {
on: ({ type }) => {
if (type === e.WORKFLOW_START) {
t.pass()
}
},
});
});
test('Publish workflow-complete event with state', async (t) => {
t.plan(1)
const plan = createPlan();
await workers.exec('run', [plan, {}], {
on: ({ type, ...args }) => {
if (type === e.WORKFLOW_COMPLETE) {
t.deepEqual(args.state, { data: { answer: 42 } });
}
},
});
});
test('Publish a job log event', async (t) => {
const plan = createPlan({
expression: `(s) => {
console.log('test')
return s;
}`,
});
let log: any;
let id;
await workers.exec('run', [plan, {}], {
on: ({ workflowId, type, log: _log }) => {
if (type === e.LOG) {
log = _log;
id = workflowId;
}
},
});
t.is(id, plan.id as any);
t.is(log.level, 'info');
t.is(log.name, 'JOB');
t.truthy(log.time);
t.deepEqual(log.message, JSON.stringify(['test']));
});