Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re-apply changes from #396 onto v8 #402

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/condition.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/rule.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
7 changes: 7 additions & 0 deletions test/acceptance/acceptance.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ describe("Acceptance", () => {
path: "$.values",
value: 2,
factResult: [2],
valueResult: 2,
result: true,
},
{
fact: "low-priority",
operator: "in",
value: [2],
factResult: 2,
valueResult: [2],
result: true,
},
],
Expand Down Expand Up @@ -172,13 +174,17 @@ describe("Acceptance", () => {
path: "$.values",
result: true,
value: 2,
valueResult: 2
},
{
fact: "low-priority",
factResult: 2,
operator: "in",
result: true,
value: [2],
valueResult: [
2
]
},
],
operator: "all",
Expand All @@ -200,6 +206,7 @@ describe("Acceptance", () => {
{
fact: "high-priority",
factResult: [2],
valueResult: 2,
operator: "containsDivisibleValuesOf",
params: {
factParam: 1,
Expand Down
6 changes: 3 additions & 3 deletions test/engine-event.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand All @@ -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)
})
})
Expand All @@ -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)
})
})
Expand Down
19 changes: 19 additions & 0 deletions test/engine-fact-comparison.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
});
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ interface BooleanConditionResultProperties {

interface ConditionResultProperties extends BooleanConditionResultProperties {
factResult?: unknown
valueResult?: unknown
}

interface ConditionProperties {
Expand Down
Loading