Skip to content

Commit

Permalink
changes from CacheControl#396
Browse files Browse the repository at this point in the history
  • Loading branch information
emilefokkemanavara committed Nov 18, 2024
1 parent 995e282 commit 8f9eba2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3 deletions.
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

0 comments on commit 8f9eba2

Please sign in to comment.