|
| 1 | +import { |
| 2 | + ExecutionResult, |
| 3 | + MessageType, |
| 4 | + Neuron, |
| 5 | + Synapse, |
| 6 | + type ActionOptions, |
| 7 | + type ExecutionContext, |
| 8 | + type ParameterInterface, |
| 9 | +} from "../../dist/esm/index.js"; |
| 10 | +import expectedOutput from "./expected-output.json" with { type: "json" }; |
| 11 | +import input from "./input.json" with { type: "json" }; |
| 12 | +import script from "./rules.json" with { type: "json" }; |
| 13 | + |
| 14 | +function readStatePath(context: ExecutionContext, path: string): unknown { |
| 15 | + return path.split(".").reduce<unknown>((current, segment) => { |
| 16 | + if (current && typeof current === "object" && segment in current) { |
| 17 | + return (current as Record<string, unknown>)[segment]; |
| 18 | + } |
| 19 | + return undefined; |
| 20 | + }, context.state); |
| 21 | +} |
| 22 | + |
| 23 | +class StateNumberParameter { |
| 24 | + static readonly TYPE = "state_number"; |
| 25 | + |
| 26 | + readonly id: string; |
| 27 | + readonly type: string; |
| 28 | + readonly name: string; |
| 29 | + readonly value: string; |
| 30 | + readonly options: Record<string, unknown>; |
| 31 | + |
| 32 | + constructor( |
| 33 | + id: string, |
| 34 | + type: string, |
| 35 | + name: string, |
| 36 | + value: string, |
| 37 | + options: Record<string, unknown>, |
| 38 | + ) { |
| 39 | + this.id = id; |
| 40 | + this.type = type; |
| 41 | + this.name = name; |
| 42 | + this.value = value; |
| 43 | + this.options = options; |
| 44 | + } |
| 45 | + |
| 46 | + getValue(context: ExecutionContext): number | null { |
| 47 | + const value = readStatePath(context, this.value); |
| 48 | + return typeof value === "number" ? value : null; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +class SetDecisionAction { |
| 53 | + static readonly TYPE = "set_decision"; |
| 54 | + |
| 55 | + readonly id: string; |
| 56 | + readonly type: string; |
| 57 | + private readonly params: ParameterInterface[]; |
| 58 | + readonly options: ActionOptions; |
| 59 | + private readonly neuron: Neuron; |
| 60 | + |
| 61 | + constructor( |
| 62 | + id: string, |
| 63 | + type: string, |
| 64 | + params: ParameterInterface[], |
| 65 | + options: ActionOptions, |
| 66 | + neuron: Neuron, |
| 67 | + ) { |
| 68 | + this.id = id; |
| 69 | + this.type = type; |
| 70 | + this.params = params; |
| 71 | + this.options = options; |
| 72 | + this.neuron = neuron; |
| 73 | + } |
| 74 | + |
| 75 | + execute(context: ExecutionContext): ExecutionResult<string | null> { |
| 76 | + const decisionParam = this.params.find((param) => param.name === "decision"); |
| 77 | + if (!decisionParam) { |
| 78 | + return new ExecutionResult(false, context, null, ["Missing decision parameter"]); |
| 79 | + } |
| 80 | + |
| 81 | + const ParamCtor = this.neuron.getParameter(decisionParam.type); |
| 82 | + const decision = ParamCtor |
| 83 | + ? new ParamCtor(decisionParam.id, decisionParam.type, decisionParam.name, decisionParam.value, decisionParam.options, decisionParam.defaultValue).getValue(context) |
| 84 | + : null; |
| 85 | + |
| 86 | + if (typeof decision !== "string") { |
| 87 | + return new ExecutionResult(false, context, null, ["Invalid decision value"]); |
| 88 | + } |
| 89 | + |
| 90 | + const nextContext: ExecutionContext = { |
| 91 | + ...context, |
| 92 | + messages: [ |
| 93 | + ...context.messages, |
| 94 | + { type: MessageType.INFO, text: `Eligibility decision: ${decision}` }, |
| 95 | + ], |
| 96 | + state: { |
| 97 | + ...context.state, |
| 98 | + eligibility: { eligible: decision === "approved", decision }, |
| 99 | + }, |
| 100 | + }; |
| 101 | + |
| 102 | + return new ExecutionResult(true, nextContext, decision); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +const neuron = new Neuron(); |
| 107 | +neuron.registerParameter(StateNumberParameter.TYPE, StateNumberParameter); |
| 108 | +neuron.registerAction(SetDecisionAction.TYPE, SetDecisionAction); |
| 109 | + |
| 110 | +const result = new Synapse(neuron).execute(script, input as ExecutionContext); |
| 111 | +const eligibility = result.context.state.eligibility as |
| 112 | + | { eligible?: boolean; decision?: string } |
| 113 | + | undefined; |
| 114 | +const actual = { |
| 115 | + ok: result.isSuccessful(), |
| 116 | + rulesExecuted: result.value, |
| 117 | + eligible: eligibility?.eligible, |
| 118 | + decision: eligibility?.decision, |
| 119 | + messages: result.context.messages.map((message) => message.text), |
| 120 | +}; |
| 121 | + |
| 122 | +if (JSON.stringify(actual) !== JSON.stringify(expectedOutput)) { |
| 123 | + console.error(JSON.stringify({ expected: expectedOutput, actual }, null, 2)); |
| 124 | + process.exit(1); |
| 125 | +} |
| 126 | + |
| 127 | +console.log(JSON.stringify(actual, null, 2)); |
| 128 | + |
0 commit comments