-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathhandler.ts
More file actions
74 lines (66 loc) · 2.05 KB
/
handler.ts
File metadata and controls
74 lines (66 loc) · 2.05 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
import type { ExecutionPlan } from '@openfn/lexicon';
import { TestOptions } from './command';
import { createNullLogger, Logger } from '../util/logger';
import loadState from '../util/load-state';
import compile from '../compile/compile';
import execute from '../execute/execute';
import { ExecuteOptions } from '../execute/command';
const testHandler = async (options: TestOptions, logger: Logger) => {
logger.log('Running test workflow...');
const opts: Partial<ExecuteOptions> = { ...options };
// Preconfigure some options
opts.compile = true;
opts.adaptors = [];
const plan = {
options: {
start: 'start',
},
workflow: {
steps: [
{
id: 'start',
state: { data: { defaultAnswer: 42 } },
expression:
"const fn = () => (state) => { console.log('Starting computer...'); return state; }; fn()",
next: {
calculate: '!state.error',
},
},
{
id: 'calculate',
expression:
"const fn = () => (state) => { console.log('Calculating life, the universe, and everything...'); return state }; fn()",
next: {
result: true,
},
},
{
id: 'result',
expression:
'const fn = () => (state) => ({ data: { answer: state.data.answer || state.data.defaultAnswer } }); fn()',
},
],
},
} as ExecutionPlan;
logger.break();
logger.info('Execution plan:');
logger.info(JSON.stringify(plan, null, 2));
logger.break();
if (!opts.stateStdin) {
logger.debug(
'No state provided: pass an object with state.data.answer to provide custom input'
);
logger.debug('eg: -S "{ "data": { "answer": 33 } }"');
}
const state = await loadState(plan, opts, createNullLogger());
const compiledPlan = await compile(plan, opts, logger);
const result = await execute(
compiledPlan,
state,
opts as ExecuteOptions,
logger
);
logger.success(`Result: ${result.data.answer}`);
return result;
};
export default testHandler;