@media-quest/builder 0.0.22 → 0.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/package.json +1 -1
  2. package/src/Builder-option.ts +66 -66
  3. package/src/Builder-page.spec.ts +320 -320
  4. package/src/Builder-page.ts +257 -257
  5. package/src/Builder-question.spec.ts +68 -68
  6. package/src/Builder-question.ts +101 -101
  7. package/src/Builder-schema.spec.ts +357 -306
  8. package/src/Builder-schema.ts +287 -254
  9. package/src/Builder-text.spec.ts +24 -24
  10. package/src/Builder-text.ts +57 -57
  11. package/src/BuilderMainImageDto.ts +7 -7
  12. package/src/BuilderMainText.ts +81 -81
  13. package/src/BuilderMainVideoDto.ts +10 -10
  14. package/src/BuilderObject.ts +61 -61
  15. package/src/BuilderTag.ts +97 -97
  16. package/src/builder-compiler.ts +14 -0
  17. package/src/codebook.ts +72 -72
  18. package/src/media-files.ts +28 -28
  19. package/src/primitives/page-prefix.ts +58 -58
  20. package/src/primitives/prefix.spec.ts +5 -5
  21. package/src/primitives/schema-prefix.ts +52 -52
  22. package/src/primitives/varID.ts +11 -11
  23. package/src/public-api.ts +3 -1
  24. package/src/rulebuilder/Builder-rule.spec.ts +322 -322
  25. package/src/rulebuilder/Builder-rule.ts +190 -190
  26. package/src/rulebuilder/RuleAction.ts +106 -106
  27. package/src/rulebuilder/RuleBuilder-test-utils.ts +316 -316
  28. package/src/rulebuilder/RuleInput.ts +44 -44
  29. package/src/rulebuilder/RuleVariable.ts +49 -49
  30. package/src/rulebuilder/SingleSelectItem.ts +135 -135
  31. package/src/rulebuilder/condition/Builder-condition-group.spec.ts +47 -47
  32. package/src/rulebuilder/condition/Builder-condition-group.ts +118 -118
  33. package/src/rulebuilder/condition/Builder-condition.spec.ts +195 -195
  34. package/src/rulebuilder/condition/Builder-condition.ts +208 -208
  35. package/src/rulebuilder/condition/Builder-operator.spec.ts +9 -9
  36. package/src/rulebuilder/condition/Builder-operator.ts +31 -31
  37. package/src/rulebuilder/index.ts +22 -22
  38. package/src/rulebuilder/jump-to-action-manager.ts +33 -33
  39. package/src/rulebuilder/multi-select-item.ts +73 -73
  40. package/src/rulebuilder/page-action-manager.ts +31 -31
  41. package/src/rulebuilder/rule2/Rule2.ts +211 -211
  42. package/src/rulebuilder/tag-action-manager.spec.ts +44 -44
  43. package/src/rulebuilder/tag-action-manager.ts +28 -28
  44. package/src/schema-config.ts +25 -25
  45. package/src/theme/AbstractThemeCompiler.ts +7 -7
  46. package/src/theme/IDefaultTheme.ts +226 -226
  47. package/src/theme/css-theme.ts +7 -7
  48. package/src/theme/default-theme-compiler.ts +358 -358
  49. package/src/theme/icon-urls.ts +29 -29
  50. package/src/theme/theme-utils.ts +57 -57
  51. package/src/theme/theme1.spec.ts +52 -52
  52. package/src/variable/mq-variable.spec.ts +91 -0
  53. package/src/{mq-variable.ts → variable/mq-variable.ts} +63 -61
  54. package/src/variable/sum-score-variable.ts +56 -0
  55. package/tsconfig.json +15 -15
  56. package/tsconfig.tsbuildinfo +1 -1
@@ -1,322 +1,322 @@
1
- import { BuilderRule, type BuilderRuleDto } from "./Builder-rule";
2
- import { RuleBuilderTestUtils as U } from "./RuleBuilder-test-utils";
3
- import { RuleInput } from "./RuleInput";
4
- import type { BuilderConditionGroupDto } from "./condition/Builder-condition-group";
5
- import { BuilderConditionDto } from "./condition/Builder-condition";
6
- import { Condition, PageID } from "@media-quest/engine";
7
- import { PagePrefix } from "../primitives/page-prefix";
8
- import { VarID } from "../primitives/varID";
9
- import { SchemaPrefix } from "../primitives/schema-prefix";
10
-
11
- const { createPagesAndVars_A_H } = U;
12
-
13
- const sxx = SchemaPrefix.fromValueOrThrow("sxx");
14
- let varsAndPages_A_H = createPagesAndVars_A_H(sxx);
15
-
16
- const varId = (sxx: SchemaPrefix, pxx: string): VarID => {
17
- const pagePrefix = PagePrefix.fromStringOrThrow(pxx);
18
- return VarID.create(sxx.value, pagePrefix);
19
- };
20
- const createDto = (
21
- modulePrefix: SchemaPrefix,
22
- ): {
23
- ruleInput: RuleInput;
24
- builderRuleDto: BuilderRuleDto;
25
- } => {
26
- const v1 = U.createRuleVariable(varId(modulePrefix, "v1"), 1);
27
- const v2 = U.createRuleVariable(varId(modulePrefix, "v2"), 2);
28
- const v3 = U.createRuleVariable(varId(modulePrefix, "v3"), 3);
29
- const v4 = U.createRuleVariable(varId(modulePrefix, "v4"), 4);
30
- const vg1 = U.createRuleVariable(varId(modulePrefix, "vg1"), 5);
31
- const vg2 = U.createRuleVariable(varId(modulePrefix, "vg2"), 6);
32
- const vg3 = U.createRuleVariable(varId(modulePrefix, "vg3"), 7);
33
- const vg4 = U.createRuleVariable(varId(modulePrefix, "vg4"), 8);
34
- const variableList = [v1, v2, v3, v4, vg1, vg2, vg3, vg4];
35
- const tagAction1 = U.excludeByTagAction("tag1");
36
- const tagAction2 = U.excludeByTagAction("tag2");
37
- const tagAction3 = U.excludeByTagAction("tag3");
38
- const tagAction4 = U.excludeByTagAction("tag4");
39
- const pageId1 = PageID.create();
40
- const pageId2 = PageID.create();
41
- const pageId3 = PageID.create();
42
- const pageId4 = PageID.create();
43
- const pageAction1 = U.excludeByPageIdAction(pageId1, v1.pageNumber);
44
- const pageAction2 = U.excludeByPageIdAction(pageId2, v2.pageNumber);
45
- const pageAction3 = U.excludeByPageIdAction(pageId3, v3.pageNumber);
46
- const pageAction4 = U.excludeByPageIdAction(pageId4, v4.pageNumber);
47
- const jumpToPageAction1 = U.jumpToPageAction(pageId1, v1.pageNumber);
48
- const jumpToPageAction2 = U.jumpToPageAction(pageId2, v2.pageNumber);
49
- const jumpToPageAction3 = U.jumpToPageAction(pageId3, v3.pageNumber);
50
- const jumpToPageAction4 = U.jumpToPageAction(pageId4, v1.pageNumber);
51
-
52
- const ruleInput = new RuleInput(
53
- variableList,
54
- [],
55
- [pageAction1, pageAction2, pageAction3, pageAction4],
56
- [tagAction1, tagAction2, tagAction3, tagAction4],
57
- [jumpToPageAction1, jumpToPageAction2, jumpToPageAction3, jumpToPageAction4],
58
- );
59
- const c1 = U.createConditionDto(v1);
60
- const c2 = U.createConditionDto(v2);
61
- const c3 = U.createConditionDto(v3);
62
- const c4 = U.createConditionDto(v4);
63
- const cg1 = U.createConditionDto(vg1);
64
- const cg2 = U.createConditionDto(vg2);
65
- const cg3 = U.createConditionDto(vg3);
66
- const cg4 = U.createConditionDto(vg4);
67
- const group3: BuilderConditionGroupDto = {
68
- kind: "condition-group",
69
- name: "group3",
70
- type: "all",
71
- conditions: [cg1, cg2, cg3, cg4],
72
- };
73
-
74
- const builderRuleDto1: BuilderRuleDto = {
75
- type: "any",
76
- jumpToPage: false,
77
- excludePages: [pageId1, pageId3],
78
- excludeTags: [tagAction1.tag, tagAction2.tag],
79
- name: "kitchen-sink",
80
- conditions: [c1, c2, group3, c3, c4],
81
- };
82
-
83
- return { ruleInput, builderRuleDto: builderRuleDto1 };
84
- };
85
- let dto: BuilderRuleDto = {
86
- name: "test-rule",
87
- conditions: [],
88
- type: "all",
89
- excludePages: [],
90
- excludeTags: [],
91
- jumpToPage: false,
92
- };
93
-
94
- const excludeByTagActionList = U.excludeByTagActionList();
95
- // const pageActions = questionVariables.list.map((q) => U.excludeByPageIdAction(q.pageId, q.pageNumber));
96
- // const pageActions = questionVariables.map((q) => U.excludeByPageIdAction(, q.pageNumber));
97
- // const pageActions = varsAndPages_A_H.pageIds((p) => U.excludeByPageIdAction(p.pageId, p.pageNumber));
98
- let ruleInput = new RuleInput(
99
- varsAndPages_A_H.list,
100
- [],
101
- [...varsAndPages_A_H.pageIdList.map((id, index) => U.excludeByPageIdAction(id, index))],
102
- excludeByTagActionList,
103
- [],
104
- );
105
- // let ruleInput = new RuleInput(questionVariables.list, [], [], excludeByTagActionList, []);
106
-
107
- let rule: BuilderRule = BuilderRule.fromDto(dto, ruleInput);
108
-
109
- beforeEach(() => {
110
- rule = BuilderRule.fromDto(dto, ruleInput);
111
- });
112
-
113
- describe("Builder Rule", () => {
114
- test("Can create rule", () => {
115
- expect(rule).toBeInstanceOf(BuilderRule);
116
- });
117
- test("Can add condition", () => {
118
- rule.addCondition();
119
- expect(rule.conditions.length).toBe(1);
120
- });
121
- test("Can delete condition", () => {
122
- const r1 = rule.addCondition();
123
- const r2 = rule.addCondition();
124
- const r3 = rule.addCondition();
125
- const r4 = rule.addCondition();
126
- const r5 = rule.addCondition();
127
- expect(rule.conditionCount).toBe(5);
128
- const result = rule.deleteCondition(r3);
129
- expect(result).toBeTruthy();
130
- expect(rule.conditionCount).toBe(4);
131
- expect(rule.conditions[0]).toBe(r1);
132
- expect(rule.conditions[1]).toBe(r2);
133
- expect(rule.conditions[2]).toBe(r4);
134
- expect(rule.conditions[3]).toBe(r5);
135
- expect(rule.deleteCondition(r3)).toBeFalsy();
136
- // expect(rule.conditions[0]).toBe(r1);
137
- });
138
- test("Can delete condition-group", () => {
139
- const r1 = rule.addCondition();
140
- const r2 = rule.addConditionGroup();
141
- const r3 = rule.addConditionGroup();
142
- const r4 = rule.addCondition();
143
- const r5 = rule.addConditionGroup();
144
- expect(rule.conditionCount).toBe(5);
145
- const result = rule.deleteCondition(r2);
146
- expect(result).toBeTruthy();
147
- expect(rule.conditionCount).toBe(4);
148
- expect(rule.conditions[0]).toBe(r1);
149
- expect(rule.conditions[1]).toBe(r3);
150
- expect(rule.conditions[2]).toBe(r4);
151
- expect(rule.conditions[3]).toBe(r5);
152
- expect(rule.deleteCondition(r2)).toBeFalsy();
153
- expect(rule.deleteCondition(r1)).toBeTruthy();
154
- expect(rule.deleteCondition(r5)).toBeTruthy();
155
- expect(rule.conditionCount).toBe(2);
156
- });
157
-
158
- test("Conditions have no operators available before variable is set.", () => {
159
- const c1 = rule.addCondition();
160
- expect(c1.operatorsSelectItems.length).toBe(0);
161
-
162
- const a = c1.variableSelectItemsInUniverse[0]?.data ?? false;
163
- c1.variable = a;
164
- expect(c1.operatorsSelectItems.length).toBe(2);
165
- });
166
-
167
- test("Conditions and actions in dto will exist: ", () => {
168
- const v1 = ruleInput.questionVars[0];
169
- const v2 = ruleInput.questionVars[1];
170
- const tag1 = ruleInput.excludeByTagActions[0];
171
- const pageAction1 = ruleInput.excludeByPageIdActions[0];
172
-
173
- const dtoWithOneCondition: BuilderRuleDto = {
174
- ...dto,
175
- excludePages: [pageAction1.pageId],
176
- excludeTags: [tag1.tag],
177
- jumpToPage: v1.varId,
178
- conditions: [
179
- {
180
- kind: "condition",
181
- name: "condition 1",
182
- variableId: v1.varId,
183
- operator: "equal",
184
- value: v1.options[0].value,
185
- },
186
- {
187
- kind: "condition-group",
188
- type: "all",
189
- name: "condtion-grup-from-testing.",
190
- conditions: [
191
- {
192
- name: "nested-condition",
193
- kind: "condition",
194
- value: v2.options[0].value,
195
- operator: "equal",
196
- variableId: v2.varId,
197
- },
198
- ],
199
- },
200
- ],
201
- };
202
-
203
- const rule = BuilderRule.fromDto(dtoWithOneCondition, ruleInput);
204
- // console.log(rule);
205
- expect(rule.conditions.length).toBe(2);
206
- const excludeTags = rule.toJson().excludeTags;
207
- expect(excludeTags.length).toBe(1);
208
- expect(rule.jumpToActionManager).toBeTruthy();
209
- });
210
-
211
- test("fromJSON -> toJSON -> are equal.", () => {
212
- const schemaPrefix = SchemaPrefix.fromValueOrThrow("ax");
213
- const data = createDto(schemaPrefix);
214
-
215
- const localRule = BuilderRule.fromDto(data.builderRuleDto, data.ruleInput);
216
-
217
- const json = localRule.toJson();
218
- expect(data.builderRuleDto).toStrictEqual(json);
219
- });
220
-
221
- test("invalid tags will be removed.", () => {
222
- const ruleInput = new RuleInput([], [], [], U.excludeByTagActionList(), []);
223
- const dto: BuilderRuleDto = {
224
- conditions: [],
225
- excludePages: [],
226
- excludeTags: ["tag3", "tag1"],
227
- jumpToPage: false,
228
- name: "Rule-name-in-test",
229
- type: "all",
230
- };
231
-
232
- const localRule = BuilderRule.fromDto(dto, ruleInput);
233
-
234
- expect(ruleInput.excludeByTagActions.length).toStrictEqual(10);
235
- expect(ruleInput.excludeByTagActions.length).toStrictEqual(10);
236
- });
237
-
238
- // TODO FIX THIS TEST.
239
- test("toEngineRuleWorks: ", () => {
240
- const schemaPrefix = SchemaPrefix.fromValueOrThrow("ax");
241
- const dto = createDto(schemaPrefix);
242
- const input = dto.ruleInput;
243
- const v1 = input.questionVars[0];
244
- const v2 = input.questionVars[1];
245
- const tag1 = input.excludeByTagActions[0];
246
- const pageAction1 = input.excludeByPageIdActions[0];
247
-
248
- const c1: BuilderConditionDto = {
249
- kind: "condition",
250
- name: "condition 1",
251
- variableId: v1.varId,
252
- operator: "equal",
253
- value: v1.options[0].value,
254
- };
255
- const c2: BuilderConditionDto = {
256
- name: "nested-condition",
257
- kind: "condition",
258
- value: v2.options[0].value,
259
- operator: "equal",
260
- variableId: v2.varId,
261
- };
262
- const conditionGroupANY: BuilderConditionGroupDto = {
263
- kind: "condition-group",
264
- conditions: [c1, c2],
265
- name: "or-condition",
266
- type: "any",
267
- };
268
-
269
- const conditionGroupALL: BuilderConditionGroupDto = {
270
- kind: "condition-group",
271
- conditions: [c2],
272
- name: "condition group ALL - in testing.",
273
- type: "all",
274
- };
275
-
276
- expect(c1.variableId).toBe(schemaPrefix.value + "_v1");
277
-
278
- const dtoWithThreeCondition: BuilderRuleDto = {
279
- type: "all",
280
- name: "dto-with-one-condition",
281
- excludePages: [pageAction1.pageId],
282
- excludeTags: [tag1.tag],
283
- jumpToPage: pageAction1.pageId,
284
- conditions: [c1, conditionGroupALL, conditionGroupANY],
285
- };
286
-
287
- const actionCount =
288
- dtoWithThreeCondition.excludeTags.length +
289
- dtoWithThreeCondition.excludePages.length +
290
- (dtoWithThreeCondition.jumpToPage ? 1 : 0);
291
-
292
- const rule = BuilderRule.fromDto(dtoWithThreeCondition, input);
293
-
294
- const engineRule = rule.toEngineRule();
295
-
296
- // console.log(rule);
297
- expect(engineRule.all.length).toBe(3);
298
- expect(engineRule.some.length).toBe(0);
299
- // expect(engineRule.all[0]).toBe(0);
300
-
301
- const simple1 = engineRule.all.find(
302
- (c) => c.kind === "numeric-condition" && c.referenceId === c1.variableId,
303
- ) as Condition.Numeric;
304
- const complex1 = engineRule.all.find(
305
- (c) => c.kind === "complex-condition" && c.all.length === 1,
306
- ) as Condition.Complex;
307
-
308
- const complexChild1 = complex1.all[0] as Condition.Numeric;
309
- expect(simple1).toBeTruthy();
310
- expect(simple1.operator === "eq").toBeTruthy();
311
- expect(simple1.value === c1.value).toBeTruthy();
312
- expect(simple1.referenceLabel).toBe(v1.label);
313
- expect(complex1).toBeTruthy();
314
- expect(complexChild1).toBeTruthy();
315
- expect(complexChild1.referenceId).toBe(c2.variableId);
316
- expect(complexChild1.value).toBe(c2.value);
317
- expect(complexChild1.referenceLabel).toBe(v2.label);
318
- expect(complexChild1.operator === "eq").toBeTruthy();
319
- // expect(engineRule.onSuccess).toBe(1);
320
- expect(engineRule.onSuccess.length).toBe(actionCount);
321
- });
322
- });
1
+ import { BuilderRule, type BuilderRuleDto } from "./Builder-rule";
2
+ import { RuleBuilderTestUtils as U } from "./RuleBuilder-test-utils";
3
+ import { RuleInput } from "./RuleInput";
4
+ import type { BuilderConditionGroupDto } from "./condition/Builder-condition-group";
5
+ import { BuilderConditionDto } from "./condition/Builder-condition";
6
+ import { Condition, PageID } from "@media-quest/engine";
7
+ import { PagePrefix } from "../primitives/page-prefix";
8
+ import { VarID } from "../primitives/varID";
9
+ import { SchemaPrefix } from "../primitives/schema-prefix";
10
+
11
+ const { createPagesAndVars_A_H } = U;
12
+
13
+ const sxx = SchemaPrefix.fromValueOrThrow("sxx");
14
+ let varsAndPages_A_H = createPagesAndVars_A_H(sxx);
15
+
16
+ const varId = (sxx: SchemaPrefix, pxx: string): VarID => {
17
+ const pagePrefix = PagePrefix.fromStringOrThrow(pxx);
18
+ return VarID.create(sxx.value, pagePrefix);
19
+ };
20
+ const createDto = (
21
+ modulePrefix: SchemaPrefix,
22
+ ): {
23
+ ruleInput: RuleInput;
24
+ builderRuleDto: BuilderRuleDto;
25
+ } => {
26
+ const v1 = U.createRuleVariable(varId(modulePrefix, "v1"), 1);
27
+ const v2 = U.createRuleVariable(varId(modulePrefix, "v2"), 2);
28
+ const v3 = U.createRuleVariable(varId(modulePrefix, "v3"), 3);
29
+ const v4 = U.createRuleVariable(varId(modulePrefix, "v4"), 4);
30
+ const vg1 = U.createRuleVariable(varId(modulePrefix, "vg1"), 5);
31
+ const vg2 = U.createRuleVariable(varId(modulePrefix, "vg2"), 6);
32
+ const vg3 = U.createRuleVariable(varId(modulePrefix, "vg3"), 7);
33
+ const vg4 = U.createRuleVariable(varId(modulePrefix, "vg4"), 8);
34
+ const variableList = [v1, v2, v3, v4, vg1, vg2, vg3, vg4];
35
+ const tagAction1 = U.excludeByTagAction("tag1");
36
+ const tagAction2 = U.excludeByTagAction("tag2");
37
+ const tagAction3 = U.excludeByTagAction("tag3");
38
+ const tagAction4 = U.excludeByTagAction("tag4");
39
+ const pageId1 = PageID.create();
40
+ const pageId2 = PageID.create();
41
+ const pageId3 = PageID.create();
42
+ const pageId4 = PageID.create();
43
+ const pageAction1 = U.excludeByPageIdAction(pageId1, v1.pageNumber);
44
+ const pageAction2 = U.excludeByPageIdAction(pageId2, v2.pageNumber);
45
+ const pageAction3 = U.excludeByPageIdAction(pageId3, v3.pageNumber);
46
+ const pageAction4 = U.excludeByPageIdAction(pageId4, v4.pageNumber);
47
+ const jumpToPageAction1 = U.jumpToPageAction(pageId1, v1.pageNumber);
48
+ const jumpToPageAction2 = U.jumpToPageAction(pageId2, v2.pageNumber);
49
+ const jumpToPageAction3 = U.jumpToPageAction(pageId3, v3.pageNumber);
50
+ const jumpToPageAction4 = U.jumpToPageAction(pageId4, v1.pageNumber);
51
+
52
+ const ruleInput = new RuleInput(
53
+ variableList,
54
+ [],
55
+ [pageAction1, pageAction2, pageAction3, pageAction4],
56
+ [tagAction1, tagAction2, tagAction3, tagAction4],
57
+ [jumpToPageAction1, jumpToPageAction2, jumpToPageAction3, jumpToPageAction4],
58
+ );
59
+ const c1 = U.createConditionDto(v1);
60
+ const c2 = U.createConditionDto(v2);
61
+ const c3 = U.createConditionDto(v3);
62
+ const c4 = U.createConditionDto(v4);
63
+ const cg1 = U.createConditionDto(vg1);
64
+ const cg2 = U.createConditionDto(vg2);
65
+ const cg3 = U.createConditionDto(vg3);
66
+ const cg4 = U.createConditionDto(vg4);
67
+ const group3: BuilderConditionGroupDto = {
68
+ kind: "condition-group",
69
+ name: "group3",
70
+ type: "all",
71
+ conditions: [cg1, cg2, cg3, cg4],
72
+ };
73
+
74
+ const builderRuleDto1: BuilderRuleDto = {
75
+ type: "any",
76
+ jumpToPage: false,
77
+ excludePages: [pageId1, pageId3],
78
+ excludeTags: [tagAction1.tag, tagAction2.tag],
79
+ name: "kitchen-sink",
80
+ conditions: [c1, c2, group3, c3, c4],
81
+ };
82
+
83
+ return { ruleInput, builderRuleDto: builderRuleDto1 };
84
+ };
85
+ let dto: BuilderRuleDto = {
86
+ name: "test-rule",
87
+ conditions: [],
88
+ type: "all",
89
+ excludePages: [],
90
+ excludeTags: [],
91
+ jumpToPage: false,
92
+ };
93
+
94
+ const excludeByTagActionList = U.excludeByTagActionList();
95
+ // const pageActions = questionVariables.list.map((q) => U.excludeByPageIdAction(q.pageId, q.pageNumber));
96
+ // const pageActions = questionVariables.map((q) => U.excludeByPageIdAction(, q.pageNumber));
97
+ // const pageActions = varsAndPages_A_H.pageIds((p) => U.excludeByPageIdAction(p.pageId, p.pageNumber));
98
+ let ruleInput = new RuleInput(
99
+ varsAndPages_A_H.list,
100
+ [],
101
+ [...varsAndPages_A_H.pageIdList.map((id, index) => U.excludeByPageIdAction(id, index))],
102
+ excludeByTagActionList,
103
+ [],
104
+ );
105
+ // let ruleInput = new RuleInput(questionVariables.list, [], [], excludeByTagActionList, []);
106
+
107
+ let rule: BuilderRule = BuilderRule.fromDto(dto, ruleInput);
108
+
109
+ beforeEach(() => {
110
+ rule = BuilderRule.fromDto(dto, ruleInput);
111
+ });
112
+
113
+ describe("Builder Rule", () => {
114
+ test("Can create rule", () => {
115
+ expect(rule).toBeInstanceOf(BuilderRule);
116
+ });
117
+ test("Can add condition", () => {
118
+ rule.addCondition();
119
+ expect(rule.conditions.length).toBe(1);
120
+ });
121
+ test("Can delete condition", () => {
122
+ const r1 = rule.addCondition();
123
+ const r2 = rule.addCondition();
124
+ const r3 = rule.addCondition();
125
+ const r4 = rule.addCondition();
126
+ const r5 = rule.addCondition();
127
+ expect(rule.conditionCount).toBe(5);
128
+ const result = rule.deleteCondition(r3);
129
+ expect(result).toBeTruthy();
130
+ expect(rule.conditionCount).toBe(4);
131
+ expect(rule.conditions[0]).toBe(r1);
132
+ expect(rule.conditions[1]).toBe(r2);
133
+ expect(rule.conditions[2]).toBe(r4);
134
+ expect(rule.conditions[3]).toBe(r5);
135
+ expect(rule.deleteCondition(r3)).toBeFalsy();
136
+ // expect(rule.conditions[0]).toBe(r1);
137
+ });
138
+ test("Can delete condition-group", () => {
139
+ const r1 = rule.addCondition();
140
+ const r2 = rule.addConditionGroup();
141
+ const r3 = rule.addConditionGroup();
142
+ const r4 = rule.addCondition();
143
+ const r5 = rule.addConditionGroup();
144
+ expect(rule.conditionCount).toBe(5);
145
+ const result = rule.deleteCondition(r2);
146
+ expect(result).toBeTruthy();
147
+ expect(rule.conditionCount).toBe(4);
148
+ expect(rule.conditions[0]).toBe(r1);
149
+ expect(rule.conditions[1]).toBe(r3);
150
+ expect(rule.conditions[2]).toBe(r4);
151
+ expect(rule.conditions[3]).toBe(r5);
152
+ expect(rule.deleteCondition(r2)).toBeFalsy();
153
+ expect(rule.deleteCondition(r1)).toBeTruthy();
154
+ expect(rule.deleteCondition(r5)).toBeTruthy();
155
+ expect(rule.conditionCount).toBe(2);
156
+ });
157
+
158
+ test("Conditions have no operators available before variable is set.", () => {
159
+ const c1 = rule.addCondition();
160
+ expect(c1.operatorsSelectItems.length).toBe(0);
161
+
162
+ const a = c1.variableSelectItemsInUniverse[0]?.data ?? false;
163
+ c1.variable = a;
164
+ expect(c1.operatorsSelectItems.length).toBe(2);
165
+ });
166
+
167
+ test("Conditions and actions in dto will exist: ", () => {
168
+ const v1 = ruleInput.questionVars[0];
169
+ const v2 = ruleInput.questionVars[1];
170
+ const tag1 = ruleInput.excludeByTagActions[0];
171
+ const pageAction1 = ruleInput.excludeByPageIdActions[0];
172
+
173
+ const dtoWithOneCondition: BuilderRuleDto = {
174
+ ...dto,
175
+ excludePages: [pageAction1.pageId],
176
+ excludeTags: [tag1.tag],
177
+ jumpToPage: v1.varId,
178
+ conditions: [
179
+ {
180
+ kind: "condition",
181
+ name: "condition 1",
182
+ variableId: v1.varId,
183
+ operator: "equal",
184
+ value: v1.options[0].value,
185
+ },
186
+ {
187
+ kind: "condition-group",
188
+ type: "all",
189
+ name: "condtion-grup-from-testing.",
190
+ conditions: [
191
+ {
192
+ name: "nested-condition",
193
+ kind: "condition",
194
+ value: v2.options[0].value,
195
+ operator: "equal",
196
+ variableId: v2.varId,
197
+ },
198
+ ],
199
+ },
200
+ ],
201
+ };
202
+
203
+ const rule = BuilderRule.fromDto(dtoWithOneCondition, ruleInput);
204
+ // console.log(rule);
205
+ expect(rule.conditions.length).toBe(2);
206
+ const excludeTags = rule.toJson().excludeTags;
207
+ expect(excludeTags.length).toBe(1);
208
+ expect(rule.jumpToActionManager).toBeTruthy();
209
+ });
210
+
211
+ test("fromJSON -> toJSON -> are equal.", () => {
212
+ const schemaPrefix = SchemaPrefix.fromValueOrThrow("ax");
213
+ const data = createDto(schemaPrefix);
214
+
215
+ const localRule = BuilderRule.fromDto(data.builderRuleDto, data.ruleInput);
216
+
217
+ const json = localRule.toJson();
218
+ expect(data.builderRuleDto).toStrictEqual(json);
219
+ });
220
+
221
+ test("invalid tags will be removed.", () => {
222
+ const ruleInput = new RuleInput([], [], [], U.excludeByTagActionList(), []);
223
+ const dto: BuilderRuleDto = {
224
+ conditions: [],
225
+ excludePages: [],
226
+ excludeTags: ["tag3", "tag1"],
227
+ jumpToPage: false,
228
+ name: "Rule-name-in-test",
229
+ type: "all",
230
+ };
231
+
232
+ const localRule = BuilderRule.fromDto(dto, ruleInput);
233
+
234
+ expect(ruleInput.excludeByTagActions.length).toStrictEqual(10);
235
+ expect(ruleInput.excludeByTagActions.length).toStrictEqual(10);
236
+ });
237
+
238
+ // TODO FIX THIS TEST.
239
+ test("toEngineRuleWorks: ", () => {
240
+ const schemaPrefix = SchemaPrefix.fromValueOrThrow("ax");
241
+ const dto = createDto(schemaPrefix);
242
+ const input = dto.ruleInput;
243
+ const v1 = input.questionVars[0];
244
+ const v2 = input.questionVars[1];
245
+ const tag1 = input.excludeByTagActions[0];
246
+ const pageAction1 = input.excludeByPageIdActions[0];
247
+
248
+ const c1: BuilderConditionDto = {
249
+ kind: "condition",
250
+ name: "condition 1",
251
+ variableId: v1.varId,
252
+ operator: "equal",
253
+ value: v1.options[0].value,
254
+ };
255
+ const c2: BuilderConditionDto = {
256
+ name: "nested-condition",
257
+ kind: "condition",
258
+ value: v2.options[0].value,
259
+ operator: "equal",
260
+ variableId: v2.varId,
261
+ };
262
+ const conditionGroupANY: BuilderConditionGroupDto = {
263
+ kind: "condition-group",
264
+ conditions: [c1, c2],
265
+ name: "or-condition",
266
+ type: "any",
267
+ };
268
+
269
+ const conditionGroupALL: BuilderConditionGroupDto = {
270
+ kind: "condition-group",
271
+ conditions: [c2],
272
+ name: "condition group ALL - in testing.",
273
+ type: "all",
274
+ };
275
+
276
+ expect(c1.variableId).toBe(schemaPrefix.value + "_v1");
277
+
278
+ const dtoWithThreeCondition: BuilderRuleDto = {
279
+ type: "all",
280
+ name: "dto-with-one-condition",
281
+ excludePages: [pageAction1.pageId],
282
+ excludeTags: [tag1.tag],
283
+ jumpToPage: pageAction1.pageId,
284
+ conditions: [c1, conditionGroupALL, conditionGroupANY],
285
+ };
286
+
287
+ const actionCount =
288
+ dtoWithThreeCondition.excludeTags.length +
289
+ dtoWithThreeCondition.excludePages.length +
290
+ (dtoWithThreeCondition.jumpToPage ? 1 : 0);
291
+
292
+ const rule = BuilderRule.fromDto(dtoWithThreeCondition, input);
293
+
294
+ const engineRule = rule.toEngineRule();
295
+
296
+ // console.log(rule);
297
+ expect(engineRule.all.length).toBe(3);
298
+ expect(engineRule.some.length).toBe(0);
299
+ // expect(engineRule.all[0]).toBe(0);
300
+
301
+ const simple1 = engineRule.all.find(
302
+ (c) => c.kind === "numeric-condition" && c.referenceId === c1.variableId,
303
+ ) as Condition.Numeric;
304
+ const complex1 = engineRule.all.find(
305
+ (c) => c.kind === "complex-condition" && c.all.length === 1,
306
+ ) as Condition.Complex;
307
+
308
+ const complexChild1 = complex1.all[0] as Condition.Numeric;
309
+ expect(simple1).toBeTruthy();
310
+ expect(simple1.operator === "eq").toBeTruthy();
311
+ expect(simple1.value === c1.value).toBeTruthy();
312
+ expect(simple1.referenceLabel).toBe(v1.label);
313
+ expect(complex1).toBeTruthy();
314
+ expect(complexChild1).toBeTruthy();
315
+ expect(complexChild1.referenceId).toBe(c2.variableId);
316
+ expect(complexChild1.value).toBe(c2.value);
317
+ expect(complexChild1.referenceLabel).toBe(v2.label);
318
+ expect(complexChild1.operator === "eq").toBeTruthy();
319
+ // expect(engineRule.onSuccess).toBe(1);
320
+ expect(engineRule.onSuccess.length).toBe(actionCount);
321
+ });
322
+ });