diff --git a/src/condition.mjs b/src/condition.mjs index 4f0ca65..42d742f 100644 --- a/src/condition.mjs +++ b/src/condition.mjs @@ -70,6 +70,9 @@ export default class Condition { if (this.factResult !== undefined) { props.factResult = this.factResult; } + if (this.valueResult !== undefined) { + props.valueResult = this.valueResult + } if (this.result !== undefined) { props.result = this.result; } diff --git a/src/rule.mjs b/src/rule.mjs index 922b570..38dd530 100644 --- a/src/rule.mjs +++ b/src/rule.mjs @@ -229,6 +229,7 @@ class Rule extends EventEmitter { .then((evaluationResult) => { const passes = evaluationResult.result; condition.factResult = evaluationResult.leftHandSideValue; + condition.valueResult = evaluationResult.rightHandSideValue; condition.result = passes; return passes; }); diff --git a/test/acceptance/acceptance.test.mjs b/test/acceptance/acceptance.test.mjs index db70389..68d75fd 100644 --- a/test/acceptance/acceptance.test.mjs +++ b/test/acceptance/acceptance.test.mjs @@ -26,6 +26,7 @@ describe("Acceptance", () => { path: "$.values", value: 2, factResult: [2], + valueResult: 2, result: true, }, { @@ -33,6 +34,7 @@ describe("Acceptance", () => { operator: "in", value: [2], factResult: 2, + valueResult: [2], result: true, }, ], @@ -172,6 +174,7 @@ describe("Acceptance", () => { path: "$.values", result: true, value: 2, + valueResult: 2 }, { fact: "low-priority", @@ -179,6 +182,9 @@ describe("Acceptance", () => { operator: "in", result: true, value: [2], + valueResult: [ + 2 + ] }, ], operator: "all", @@ -200,6 +206,7 @@ describe("Acceptance", () => { { fact: "high-priority", factResult: [2], + valueResult: 2, operator: "containsDivisibleValuesOf", params: { factParam: 1, diff --git a/test/engine-event.test.mjs b/test/engine-event.test.mjs index 7cdabbc..ec9f634 100644 --- a/test/engine-event.test.mjs +++ b/test/engine-event.test.mjs @@ -687,7 +687,7 @@ describe("Engine: event", () => { await engine.run(); const ruleResult = successSpy.mock.calls[0][2]; const expected = - '{"conditions":{"priority":1,"any":[{"name":"over 21","operator":"greaterThanInclusive","value":21,"fact":"age","factResult":21,"result":true},{"operator":"equal","value":true,"fact":"qualified","factResult":false,"result":false}]},"event":{"type":"setDrinkingFlag","params":{"canOrderDrinks":true}},"priority":100,"result":true}'; + '{"conditions":{"priority":1,"any":[{"name":"over 21","operator":"greaterThanInclusive","value":21,"fact":"age","factResult":21,"valueResult":21,"result":true},{"operator":"equal","value":true,"fact":"qualified","factResult":false,"valueResult":true,"result":false}]},"event":{"type":"setDrinkingFlag","params":{"canOrderDrinks":true}},"priority":100,"result":true}'; expect(JSON.stringify(ruleResult)).toBe(expected); }); }); @@ -696,7 +696,7 @@ describe("Engine: event", () => { beforeEach(() => setupWithConditionReference()) it('serializes properties', async () => { const { results: [ruleResult] } = await engine.run() - const expected = '{"conditions":{"priority":1,"any":[{"priority":1,"all":[{"name":"over 21","operator":"greaterThanInclusive","value":21,"fact":"age","factResult":21,"result":true}]}]},"event":{"type":"awesome"},"priority":100,"result":true}' + const expected = '{"conditions":{"priority":1,"any":[{"priority":1,"all":[{"name":"over 21","operator":"greaterThanInclusive","value":21,"fact":"age","factResult":21,"valueResult":21,"result":true}]}]},"event":{"type":"awesome"},"priority":100,"result":true}' expect(JSON.stringify(ruleResult)).toEqual(expected) }) }) @@ -707,7 +707,7 @@ describe("Engine: event", () => { const { results: [ruleResult] } = await engine.run() const { conditions: { any: [conditionReference] } } = ruleResult expect(conditionReference.result).toEqual(false) - const expected = '{"conditions":{"priority":1,"any":[{"name":"nameOfTheUndefinedConditionReference","condition":"conditionThatIsNotDefined"},{"name":"over 21","operator":"greaterThanInclusive","value":21,"fact":"age","factResult":21,"result":true}]},"event":{"type":"awesome"},"priority":100,"result":true}' + const expected = '{"conditions":{"priority":1,"any":[{"name":"nameOfTheUndefinedConditionReference","condition":"conditionThatIsNotDefined"},{"name":"over 21","operator":"greaterThanInclusive","value":21,"fact":"age","factResult":21,"valueResult":21,"result":true}]},"event":{"type":"awesome"},"priority":100,"result":true}' expect(JSON.stringify(ruleResult)).toEqual(expected) }) }) diff --git a/test/engine-fact-comparison.test.mjs b/test/engine-fact-comparison.test.mjs index 585f33c..63e7ea6 100644 --- a/test/engine-fact-comparison.test.mjs +++ b/test/engine-fact-comparison.test.mjs @@ -118,4 +118,23 @@ describe("Engine: fact to fact comparison", () => { expect(eventSpy).not.toHaveBeenCalled(); }); }); + + describe('constant facts: checking valueResult and factResult', () => { + const constantCondition = { + all: [{ + fact: 'height', + operator: 'lessThanInclusive', + value: { + fact: 'width' + } + }] + } + it('result has the correct valueResult and factResult properties', async () => { + setup(constantCondition) + const result = await engine.run({ height: 1, width: 2 }) + + expect(result.results[0].conditions.all[0].factResult).toEqual(1) + expect(result.results[0].conditions.all[0].valueResult).toEqual(2) + }) + }) }); diff --git a/types/index.d.ts b/types/index.d.ts index 596cedc..db6b873 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -209,6 +209,7 @@ interface BooleanConditionResultProperties { interface ConditionResultProperties extends BooleanConditionResultProperties { factResult?: unknown + valueResult?: unknown } interface ConditionProperties {