From 865489f93a03233d810dae2449664c4d75b29ed1 Mon Sep 17 00:00:00 2001 From: junners Date: Sat, 16 Nov 2024 00:59:41 -0800 Subject: [PATCH] feat(RuleEngine): make rule imports for non default rule store rules --- src/main/libs/DynamicRule.ts | 10 +++- tests/SameRecordFieldUpdates.test.ts | 39 +++++++++++++- .../Same_Record_Field_Updates.flow-meta.xml | 52 +++++++++++++++++++ 3 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 tests/xmlfiles/Same_Record_Field_Updates.flow-meta.xml diff --git a/src/main/libs/DynamicRule.ts b/src/main/libs/DynamicRule.ts index 008a574..1c92e11 100644 --- a/src/main/libs/DynamicRule.ts +++ b/src/main/libs/DynamicRule.ts @@ -1,8 +1,16 @@ import { DefaultRuleStore } from "../store/DefaultRuleStore"; - +import { RuleLoader } from "./RuleLoader"; +import * as p from "path-browserify"; export class DynamicRule { constructor(className: string) { if (DefaultRuleStore[className] === undefined || DefaultRuleStore[className] === null) { + const customRule = RuleLoader.loadCustomRule( + className, + p.join(__dirname, `../rules/${className}`) + ); + if (customRule) { + return customRule; + } throw new Error(`Rule \'${className}\' does not exist in the store.`); } return new DefaultRuleStore[className](); diff --git a/tests/SameRecordFieldUpdates.test.ts b/tests/SameRecordFieldUpdates.test.ts index a12e2a9..98ec37e 100644 --- a/tests/SameRecordFieldUpdates.test.ts +++ b/tests/SameRecordFieldUpdates.test.ts @@ -2,7 +2,8 @@ import "mocha"; import { ParsedFlow } from "../src/main/models/ParsedFlow"; import { SameRecordFieldUpdates } from "../src/main/rules/SameRecordFieldUpdates"; -import { RuleResult, Flow } from "../src"; +import { RuleResult, Flow, parse, scan, ScanResult } from "../src"; +import * as path from "path-browserify"; describe("SameRecordFieldUpdates", () => { let expect; @@ -133,4 +134,40 @@ describe("SameRecordFieldUpdates", () => { expect(ruleResult.occurs).to.be.false; }); + + it("should not trigger from default configuration on store", async () => { + let example_uri1 = path.join(__dirname, "./xmlfiles/Same_Record_Field_Updates.flow-meta.xml"); + let flows = await parse([example_uri1]); + const ruleConfig = { + rules: {}, + exceptions: {}, + }; + const results: ScanResult[] = scan(flows, ruleConfig); + const scanResults = results.pop(); + + scanResults?.ruleResults.forEach((rule) => { + expect(rule.occurs).to.be.false; + }); + }); + + it("should trigger when opt-in configuration", async () => { + let example_uri1 = path.join(__dirname, "./xmlfiles/Same_Record_Field_Updates.flow-meta.xml"); + let flows = await parse([example_uri1]); + const ruleConfig = { + rules: { + SameRecordFieldUpdates: { + severity: "error", + }, + }, + exceptions: {}, + }; + const results: ScanResult[] = scan(flows, ruleConfig); + const scanResults = results.pop(); + + const expectedRule = scanResults?.ruleResults.find( + (rule) => rule.ruleName === "SameRecordFieldUpdates" + ); + expect(expectedRule).to.be.ok; + expect(expectedRule?.occurs).to.be.true; + }); }); diff --git a/tests/xmlfiles/Same_Record_Field_Updates.flow-meta.xml b/tests/xmlfiles/Same_Record_Field_Updates.flow-meta.xml new file mode 100644 index 0000000..3e50389 --- /dev/null +++ b/tests/xmlfiles/Same_Record_Field_Updates.flow-meta.xml @@ -0,0 +1,52 @@ + + + 62.0 + test + Default + Test Flow {!$Flow.CurrentDateTime} + + + BuilderType + + LightningFlowBuilder + + + + CanvasMode + + AUTO_LAYOUT_CANVAS + + + + OriginBuilderType + + LightningFlowBuilder + + + AutoLaunchedFlow + + test + Update_triggering_records + + 176 + 287 + + Active__c + + Yes + + + $Record + + + 50 + 0 + + Update_triggering_records + + Account + Create + RecordBeforeSave + + Active + \ No newline at end of file