Skip to content

Commit

Permalink
fix: reset actions emitted when no rulesets remains activated
Browse files Browse the repository at this point in the history
  • Loading branch information
mrednic-1A committed Dec 18, 2024
1 parent a582fa5 commit 3c2bac0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BehaviorSubject, of } from 'rxjs';
import { BehaviorSubject, firstValueFrom, of } from 'rxjs';
import { Operator } from '../operator/operator.interface';
import { operatorList } from '../operator/operators/index';
import { RulesetExecutor } from '../ruleset-executor';
import { ActionBlock, Ruleset } from '../structure';
import { Ruleset } from '../structure';
import { filterRulesetsEventStream } from './filter-ruleset-event.operator';

describe('Filter rulesets event operator', () => {
Expand Down Expand Up @@ -134,68 +134,45 @@ describe('Filter rulesets event operator', () => {
}, {});

const firstValue = rulesets.reduce<Record<string, RulesetExecutor>>((accRuleset, ruleset) => {
accRuleset[ruleset.id] = new RulesetExecutor(ruleset, {retrieveOrCreateFactStream: () => of(undefined), operators} as any);
accRuleset[ruleset.id] = new RulesetExecutor(ruleset, { retrieveOrCreateFactStream: () => of(undefined), operators } as any);
return accRuleset;
}, {});

const rulesetsMapSubject$ = new BehaviorSubject<Record<string, RulesetExecutor>>(firstValue);

test('should consider only first ruleset', (done) => {
test('should consider only first ruleset', async () => {

rulesetsMapSubject$.pipe(
filterRulesetsEventStream(['ruleset1'])
).subscribe(data => {
expect(data.length).toBe(2);
done();
});
const data = await firstValueFrom(rulesetsMapSubject$.pipe(filterRulesetsEventStream(['ruleset1'])));
expect(data.length).toBe(2);

});

test('should consider only second ruleset', (done) => {
test('should consider only second ruleset', async () => {

rulesetsMapSubject$.pipe(
filterRulesetsEventStream(['ruleset2'])
).subscribe(data => {
expect(data.length).toBe(1);
done();
});
const data = await firstValueFrom(rulesetsMapSubject$.pipe(filterRulesetsEventStream(['ruleset2'])));
expect(data.length).toBe(1);

});

test('should consider all rulesets by not passing any filter', (done) => {
test('should consider all rulesets by not passing any filter', async () => {

rulesetsMapSubject$.pipe(
filterRulesetsEventStream()
).subscribe(data => {
expect(data.length).toBe(3);
done();
});
const data = await firstValueFrom(rulesetsMapSubject$.pipe(filterRulesetsEventStream()));
expect(data.length).toBe(3);

});

test('should consider all rulesets ids passed', (done) => {
test('should consider all rulesets ids passed', async () => {

rulesetsMapSubject$.pipe(
filterRulesetsEventStream(['ruleset1', 'ruleset2'])
).subscribe(data => {
expect(data.length).toBe(3);
done();
});
const data = await firstValueFrom(rulesetsMapSubject$.pipe(filterRulesetsEventStream(['ruleset1', 'ruleset2'])));
expect(data.length).toBe(3);

});

test('should not emit if ruleset id does not match any registered ruleset', async () => {

let emittedActions: ActionBlock[] | undefined;
const data = await firstValueFrom(rulesetsMapSubject$.pipe(filterRulesetsEventStream(['ruleset3'])));
expect(data.length).toBe(0);

rulesetsMapSubject$.pipe(
filterRulesetsEventStream(['ruleset3'])
).subscribe(data => {
emittedActions = data;
});

await jest.advanceTimersByTimeAsync(500);
expect(emittedActions).toBe(undefined);
});

});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { combineLatest, Observable } from 'rxjs';
import { combineLatest, Observable, of } from 'rxjs';
import { map, shareReplay, switchMap } from 'rxjs/operators';
import { RulesetExecutor } from '../ruleset-executor';

Expand All @@ -14,11 +14,13 @@ export function filterRulesetsEventStream(restrictiveRuleSets?: string[]) {
Object.values(rulesets).filter((ruleSet) => restrictiveRuleSets.indexOf(ruleSet.id) > -1) :
Object.values(rulesets);

return combineLatest(activeRulesets.map((ruleset) => ruleset.rulesResultsSubject$)).pipe(
map((item) => item.reduce((acc, currentValue) => {
acc.push(...currentValue);
return acc;
}, [])));
return activeRulesets?.length > 0
? combineLatest(activeRulesets.map((ruleset) => ruleset.rulesResultsSubject$)).pipe(
map((item) => item.reduce((acc, currentValue) => {
acc.push(...currentValue);
return acc;
}, [])))
: of([]);
}),
shareReplay(1)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ describe('Rules engine service', () => {
next: value => nextFn(value)
});
// should output no actions as all rulesets are on demand
expect(nextFn).not.toHaveBeenCalled();
expect(nextFn).toHaveBeenCalledWith([]);
sub.unsubscribe();

// activate ruleset 1 via his own linked component
Expand Down

0 comments on commit 3c2bac0

Please sign in to comment.