-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.ts
More file actions
130 lines (112 loc) · 3.6 KB
/
Copy pathrun.ts
File metadata and controls
130 lines (112 loc) · 3.6 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
import {
ExecutionResult,
MessageType,
Neuron,
Synapse,
type ActionOptions,
type ExecutionContext,
type ParameterInterface,
} from "../../dist/esm/index.js";
import expectedOutput from "./expected-output.json" with { type: "json" };
import input from "./input.json" with { type: "json" };
import script from "./rules.json" with { type: "json" };
function readStatePath(context: ExecutionContext, path: string): unknown {
return path.split(".").reduce<unknown>((current, segment) => {
if (current && typeof current === "object" && segment in current) {
return (current as Record<string, unknown>)[segment];
}
return undefined;
}, context.state);
}
class StateNumberParameter {
static readonly TYPE = "state_number";
readonly id: string;
readonly type: string;
readonly name: string;
readonly value: string;
readonly options: Record<string, unknown>;
constructor(
id: string,
type: string,
name: string,
value: string,
options: Record<string, unknown>,
) {
this.id = id;
this.type = type;
this.name = name;
this.value = value;
this.options = options;
}
getValue(context: ExecutionContext): number | null {
const value = readStatePath(context, this.value);
return typeof value === "number" ? value : null;
}
}
class SetRouteAction {
static readonly TYPE = "set_route";
readonly id: string;
readonly type: string;
private readonly params: ParameterInterface[];
readonly options: ActionOptions;
private readonly neuron: Neuron;
constructor(
id: string,
type: string,
params: ParameterInterface[],
options: ActionOptions,
neuron: Neuron,
) {
this.id = id;
this.type = type;
this.params = params;
this.options = options;
this.neuron = neuron;
}
private resolveParam(context: ExecutionContext, name: string): unknown {
const param = this.params.find((item) => item.name === name);
if (!param) return null;
const ParamCtor = this.neuron.getParameter(param.type);
return ParamCtor
? new ParamCtor(param.id, param.type, param.name, param.value, param.options, param.defaultValue).getValue(context)
: null;
}
execute(context: ExecutionContext): ExecutionResult<string | null> {
const route = this.resolveParam(context, "route");
const slaHours = this.resolveParam(context, "slaHours");
if (typeof route !== "string" || typeof slaHours !== "number") {
return new ExecutionResult(false, context, null, ["Invalid route input"]);
}
const nextContext: ExecutionContext = {
...context,
messages: [
...context.messages,
{ type: MessageType.INFO, text: `Workflow route: ${route} within ${slaHours}h` },
],
state: {
...context.state,
workflow: { route, slaHours },
},
};
return new ExecutionResult(true, nextContext, route);
}
}
const neuron = new Neuron();
neuron.registerParameter(StateNumberParameter.TYPE, StateNumberParameter);
neuron.registerAction(SetRouteAction.TYPE, SetRouteAction);
const result = new Synapse(neuron).execute(script, input as ExecutionContext);
const workflow = result.context.state.workflow as
| { route?: string; slaHours?: number }
| undefined;
const actual = {
ok: result.isSuccessful(),
rulesExecuted: result.value,
route: workflow?.route,
slaHours: workflow?.slaHours,
messages: result.context.messages.map((message) => message.text),
};
if (JSON.stringify(actual) !== JSON.stringify(expectedOutput)) {
console.error(JSON.stringify({ expected: expectedOutput, actual }, null, 2));
process.exit(1);
}
console.log(JSON.stringify(actual, null, 2));