@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,118 +1,118 @@
1
- import { BuilderCondition, type BuilderConditionDto } from "./Builder-condition";
2
- import { BuilderObject } from "../../BuilderObject";
3
- import type { BuilderVariable } from "../RuleVariable";
4
- import { Condition } from "@media-quest/engine";
5
-
6
- const ConditionGroupType = {
7
- all: true,
8
- any: true,
9
- count: true,
10
- };
11
-
12
- export type ConditionGroupType = keyof typeof ConditionGroupType;
13
- export interface BuilderConditionGroupDto {
14
- readonly kind: "condition-group";
15
- readonly name: string;
16
- readonly count?: number;
17
- readonly type: ConditionGroupType;
18
- readonly conditions: ReadonlyArray<BuilderConditionDto>;
19
- }
20
-
21
- export class BuilderConditionGroup extends BuilderObject<"builder-condition-group", BuilderConditionGroupDto> {
22
- static readonly isConditionGroupType = (value: string | symbol): value is ConditionGroupType => {
23
- if (typeof value !== "string") {
24
- return false;
25
- }
26
- const validValues = Object.keys(ConditionGroupType);
27
- return validValues.includes(value);
28
- };
29
-
30
- readonly objectType: "builder-condition-group" = "builder-condition-group";
31
- private _type: ConditionGroupType;
32
- name = "";
33
- private readonly _conditions: Array<BuilderCondition>;
34
- private readonly _variableList: ReadonlyArray<BuilderVariable>;
35
-
36
- public static readonly fromDto = (dto: BuilderConditionGroupDto, variableList: ReadonlyArray<BuilderVariable>) => {
37
- return new BuilderConditionGroup(dto, variableList);
38
- };
39
- protected constructor(dto: BuilderConditionGroupDto, variableList: ReadonlyArray<BuilderVariable>) {
40
- super(dto);
41
- this.name = dto.name;
42
- this._type = dto.type;
43
- const conditionList = Array.isArray(dto.conditions) ? dto.conditions : [];
44
- this._conditions = conditionList.map((dto) => BuilderCondition.fromDto(dto, variableList));
45
- this._variableList = variableList;
46
- }
47
- get conditions(): ReadonlyArray<BuilderCondition> {
48
- return this._conditions;
49
- }
50
-
51
- get conditionCount() {
52
- return this._conditions.length;
53
- }
54
-
55
- addCondition(): BuilderCondition {
56
- const newConditions = BuilderCondition.create(this._variableList);
57
- this._conditions.push(newConditions);
58
- return newConditions;
59
- }
60
-
61
- removeCondition(condition: BuilderCondition): boolean {
62
- // this._conditions.
63
- const index = this._conditions.indexOf(condition);
64
- if (index < 0) {
65
- return false;
66
- }
67
- this._conditions.splice(index, 1);
68
- return true;
69
- }
70
-
71
- clone(): BuilderConditionGroupDto {
72
- return this.toJson();
73
- }
74
-
75
- toJson(): BuilderConditionGroupDto {
76
- const conditions: ReadonlyArray<BuilderConditionDto> = [...this._conditions.map((c) => c.toJson())];
77
- return {
78
- name: this.name,
79
- conditions,
80
- type: this._type,
81
- kind: "condition-group",
82
- };
83
- }
84
-
85
- toEngineConditionComplex(): Condition.Complex | false {
86
- const comp: Condition.Complex = {
87
- kind: "complex-condition",
88
- all: [],
89
- some: [],
90
- name: "",
91
- };
92
- const conditions: Condition.Simple[] = [];
93
- this.conditions.forEach((c) => {
94
- const maybeSimple = c.toEngineCondition();
95
- if (maybeSimple) {
96
- conditions.push(maybeSimple);
97
- }
98
- });
99
- if (this._type === "all") {
100
- return { ...comp, all: conditions };
101
- }
102
- if (this._type === "any") {
103
- return { ...comp, some: conditions };
104
- }
105
-
106
- console.log("INVALID COMPLEX CONDITION. TODO IMPLEMENT range, and count.");
107
-
108
- return false;
109
- }
110
- get type(): ConditionGroupType {
111
- return this._type;
112
- }
113
- set type(conditionGroupType: ConditionGroupType) {
114
- if (BuilderConditionGroup.isConditionGroupType(conditionGroupType)) {
115
- this._type = conditionGroupType;
116
- }
117
- }
118
- }
1
+ import { BuilderCondition, type BuilderConditionDto } from "./Builder-condition";
2
+ import { BuilderObject } from "../../BuilderObject";
3
+ import type { BuilderVariable } from "../RuleVariable";
4
+ import { Condition } from "@media-quest/engine";
5
+
6
+ const ConditionGroupType = {
7
+ all: true,
8
+ any: true,
9
+ count: true,
10
+ };
11
+
12
+ export type ConditionGroupType = keyof typeof ConditionGroupType;
13
+ export interface BuilderConditionGroupDto {
14
+ readonly kind: "condition-group";
15
+ readonly name: string;
16
+ readonly count?: number;
17
+ readonly type: ConditionGroupType;
18
+ readonly conditions: ReadonlyArray<BuilderConditionDto>;
19
+ }
20
+
21
+ export class BuilderConditionGroup extends BuilderObject<"builder-condition-group", BuilderConditionGroupDto> {
22
+ static readonly isConditionGroupType = (value: string | symbol): value is ConditionGroupType => {
23
+ if (typeof value !== "string") {
24
+ return false;
25
+ }
26
+ const validValues = Object.keys(ConditionGroupType);
27
+ return validValues.includes(value);
28
+ };
29
+
30
+ readonly objectType: "builder-condition-group" = "builder-condition-group";
31
+ private _type: ConditionGroupType;
32
+ name = "";
33
+ private readonly _conditions: Array<BuilderCondition>;
34
+ private readonly _variableList: ReadonlyArray<BuilderVariable>;
35
+
36
+ public static readonly fromDto = (dto: BuilderConditionGroupDto, variableList: ReadonlyArray<BuilderVariable>) => {
37
+ return new BuilderConditionGroup(dto, variableList);
38
+ };
39
+ protected constructor(dto: BuilderConditionGroupDto, variableList: ReadonlyArray<BuilderVariable>) {
40
+ super(dto);
41
+ this.name = dto.name;
42
+ this._type = dto.type;
43
+ const conditionList = Array.isArray(dto.conditions) ? dto.conditions : [];
44
+ this._conditions = conditionList.map((dto) => BuilderCondition.fromDto(dto, variableList));
45
+ this._variableList = variableList;
46
+ }
47
+ get conditions(): ReadonlyArray<BuilderCondition> {
48
+ return this._conditions;
49
+ }
50
+
51
+ get conditionCount() {
52
+ return this._conditions.length;
53
+ }
54
+
55
+ addCondition(): BuilderCondition {
56
+ const newConditions = BuilderCondition.create(this._variableList);
57
+ this._conditions.push(newConditions);
58
+ return newConditions;
59
+ }
60
+
61
+ removeCondition(condition: BuilderCondition): boolean {
62
+ // this._conditions.
63
+ const index = this._conditions.indexOf(condition);
64
+ if (index < 0) {
65
+ return false;
66
+ }
67
+ this._conditions.splice(index, 1);
68
+ return true;
69
+ }
70
+
71
+ clone(): BuilderConditionGroupDto {
72
+ return this.toJson();
73
+ }
74
+
75
+ toJson(): BuilderConditionGroupDto {
76
+ const conditions: ReadonlyArray<BuilderConditionDto> = [...this._conditions.map((c) => c.toJson())];
77
+ return {
78
+ name: this.name,
79
+ conditions,
80
+ type: this._type,
81
+ kind: "condition-group",
82
+ };
83
+ }
84
+
85
+ toEngineConditionComplex(): Condition.Complex | false {
86
+ const comp: Condition.Complex = {
87
+ kind: "complex-condition",
88
+ all: [],
89
+ some: [],
90
+ name: "",
91
+ };
92
+ const conditions: Condition.Simple[] = [];
93
+ this.conditions.forEach((c) => {
94
+ const maybeSimple = c.toEngineCondition();
95
+ if (maybeSimple) {
96
+ conditions.push(maybeSimple);
97
+ }
98
+ });
99
+ if (this._type === "all") {
100
+ return { ...comp, all: conditions };
101
+ }
102
+ if (this._type === "any") {
103
+ return { ...comp, some: conditions };
104
+ }
105
+
106
+ console.log("INVALID COMPLEX CONDITION. TODO IMPLEMENT range, and count.");
107
+
108
+ return false;
109
+ }
110
+ get type(): ConditionGroupType {
111
+ return this._type;
112
+ }
113
+ set type(conditionGroupType: ConditionGroupType) {
114
+ if (BuilderConditionGroup.isConditionGroupType(conditionGroupType)) {
115
+ this._type = conditionGroupType;
116
+ }
117
+ }
118
+ }
@@ -1,195 +1,195 @@
1
- import { BuilderCondition, type BuilderConditionDto } from "./Builder-condition";
2
- import { RuleBuilderTestUtils } from "../RuleBuilder-test-utils";
3
- import type { BuilderVariable, BuilderVariableOption } from "../RuleVariable";
4
- import { QuestionVariable } from "../RuleVariable";
5
-
6
- import { SchemaPrefix } from "../../primitives/schema-prefix";
7
-
8
- let condition = BuilderCondition.create([]);
9
-
10
- beforeEach(() => {
11
- condition = BuilderCondition.create([]);
12
- });
13
-
14
- const as = SchemaPrefix.fromValueOrThrow("as");
15
-
16
- describe("Builder Operator", () => {
17
- test("Can create", () => {
18
- expect(condition).toBeInstanceOf(BuilderCondition);
19
- });
20
- test("Can resolve dto from Variables in universe.", () => {
21
- const vs = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
22
- const a = vs.items.a;
23
- const dto: BuilderConditionDto = {
24
- kind: "condition",
25
- name: "a",
26
- variableId: a.varId,
27
- operator: "equal",
28
- value: 1,
29
- };
30
- const c = BuilderCondition.fromDto(dto, vs.list);
31
- // c.setVariableList(vs);
32
- expect(c.operatorsSelectItems.length).toBe(2);
33
- expect(c.operator === "equal").toBe(true);
34
- expect(c.value).toBeTruthy();
35
- });
36
- test("Can not resolve value if invalid value", () => {
37
- const vs = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
38
- const a = vs.items.a;
39
- const dto: BuilderConditionDto = {
40
- kind: "condition",
41
- name: "a",
42
- variableId: a.varId,
43
- operator: "equal",
44
- value: 8,
45
- };
46
- const c = BuilderCondition.fromDto(dto, vs.list);
47
- expect(c.operatorsSelectItems.length).toBe(2);
48
- expect(c.value).toBe(false);
49
- // expect(match).toBe(false);
50
- });
51
- test("Will nullify dto if not matchedFrom is called", () => {
52
- const vs = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
53
- const a = vs.items.a;
54
- const dto: BuilderConditionDto = {
55
- kind: "condition",
56
- name: "a",
57
- variableId: a.varId,
58
- operator: "equal",
59
- value: 0,
60
- };
61
- const c = BuilderCondition.fromDto(dto, []);
62
- expect(c.operatorsSelectItems.length).toBe(0);
63
- expect(c.toJson().name).toBe(dto.name);
64
- expect(c.toJson().value).toBe("");
65
- expect(c.toJson().operator).toBe("");
66
- expect(c.toJson().variableId).toBe("");
67
- // expect(match).toBe(false);
68
- });
69
- test("Will not nullify if created with valid universe", () => {
70
- const vs = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
71
- const a = vs.items.a;
72
- const dto: BuilderConditionDto = {
73
- kind: "condition",
74
- name: "a",
75
- variableId: a.varId,
76
- operator: "equal",
77
- value: 0,
78
- };
79
- const c = BuilderCondition.fromDto(dto, vs.list);
80
- expect(c.variable).toBeInstanceOf(QuestionVariable);
81
- expect(c.operator === "equal").toBe(true);
82
- const value = c.value as BuilderVariableOption;
83
- expect(value.value).toBe(0);
84
- expect(value.label).toBe("Nei");
85
- expect(c.variable).toBe(a);
86
- });
87
- test("Condition is valid, when in sync with universe.", () => {
88
- const vs = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
89
- const a = vs.items.a;
90
- const dto: BuilderConditionDto = {
91
- kind: "condition",
92
-
93
- name: "name of a",
94
- variableId: a.varId,
95
- operator: "equal",
96
- value: 0,
97
- };
98
- const c = BuilderCondition.fromDto(dto, vs.list);
99
- expect(c.variable).toBeInstanceOf(QuestionVariable);
100
- expect(c.operator === "equal").toBe(true);
101
- const value = c.value as BuilderVariableOption;
102
- expect(value.value).toBe(0);
103
- expect(value.label).toBe("Nei");
104
- expect(c.validate().isValid).toBe(true);
105
- });
106
- test("Condition is invalid, when not matched against universe", () => {
107
- const dto: BuilderConditionDto = {
108
- kind: "condition",
109
- name: "name of a",
110
- variableId: "a",
111
- operator: "equal",
112
- value: 0,
113
- };
114
- const c = BuilderCondition.fromDto(dto, []);
115
- expect(c.validate().isValid).toBe(false);
116
- });
117
- test("Condition is invalid, when variable dont exist in universe.", () => {
118
- const variables = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
119
- const dto: BuilderConditionDto = {
120
- kind: "condition",
121
- name: "invalid variable name in dto",
122
- variableId: "kkk",
123
- operator: "equal",
124
- value: 9,
125
- };
126
- const c = BuilderCondition.fromDto(dto, variables.list);
127
- expect(c.variable).toBe(false);
128
- expect(c.validate().isValid).toBe(false);
129
- });
130
- test("Condition is invalid if not all set, when variable dont exist in variables.", () => {
131
- const variables = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
132
- const dto: BuilderConditionDto = {
133
- kind: "condition",
134
- name: "invalid variable name in dto",
135
- variableId: "kkk",
136
- operator: "equal",
137
- value: 9,
138
- };
139
- const c = BuilderCondition.fromDto(dto, variables.list);
140
- expect(c.variable).toBe(false);
141
- expect(c.validate().isValid).toBe(false);
142
- });
143
-
144
- test("Condition is invalid if operator is not set", () => {
145
- const variables = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
146
- const a = variables.items.a;
147
- const dto: BuilderConditionDto = {
148
- kind: "condition",
149
- name: "invalid variable name in dto",
150
- variableId: a.varId,
151
- operator: "",
152
- value: 1,
153
- };
154
- expect(a.varId).toBe("as_a");
155
- const c = BuilderCondition.fromDto(dto, variables.list);
156
- expect(c.variable).toBeInstanceOf(QuestionVariable);
157
- expect(c.validate().isValid).toBe(false);
158
- expect(c.value).toBe(false);
159
- });
160
- test("Condition is invalid if value (from dto) is not found in variable", () => {
161
- const { list, items } = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
162
- expect(items.a.varId).toBe("as_a");
163
- const dto: BuilderConditionDto = {
164
- kind: "condition",
165
- name: "invalid variable name in dto",
166
- variableId: items.a.varId,
167
- operator: "equal",
168
- value: 42,
169
- };
170
- const c = BuilderCondition.fromDto(dto, list);
171
- expect(c).toBeInstanceOf(BuilderCondition);
172
- expect(c.variable).toBeInstanceOf(QuestionVariable);
173
- expect(c.operator).toBe("equal");
174
- expect(c.operatorsSelectItems.length).toBe(BuilderCondition.NUMBER_OPERATORS.length);
175
- expect(c.value).toBe(false);
176
- expect(c.validate().isValid).toBe(false);
177
- });
178
- test("toEngineConditionWorks", () => {
179
- const variables = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
180
-
181
- const dto: BuilderConditionDto = {
182
- kind: "condition",
183
- name: "invalid variable name in dto",
184
- variableId: variables.items.a.varId,
185
- operator: "equal",
186
- value: 7,
187
- };
188
- const c = BuilderCondition.fromDto(dto, variables.list);
189
- expect(c.variable).toBeInstanceOf(QuestionVariable);
190
- expect(c.operator).toBe("equal");
191
- expect(c.operatorsSelectItems.length).toBe(BuilderCondition.NUMBER_OPERATORS.length);
192
- expect(c.value).toBe(false);
193
- expect(c.validate().isValid).toBe(false);
194
- });
195
- });
1
+ import { BuilderCondition, type BuilderConditionDto } from "./Builder-condition";
2
+ import { RuleBuilderTestUtils } from "../RuleBuilder-test-utils";
3
+ import type { BuilderVariable, BuilderVariableOption } from "../RuleVariable";
4
+ import { QuestionVariable } from "../RuleVariable";
5
+
6
+ import { SchemaPrefix } from "../../primitives/schema-prefix";
7
+
8
+ let condition = BuilderCondition.create([]);
9
+
10
+ beforeEach(() => {
11
+ condition = BuilderCondition.create([]);
12
+ });
13
+
14
+ const as = SchemaPrefix.fromValueOrThrow("as");
15
+
16
+ describe("Builder Operator", () => {
17
+ test("Can create", () => {
18
+ expect(condition).toBeInstanceOf(BuilderCondition);
19
+ });
20
+ test("Can resolve dto from Variables in universe.", () => {
21
+ const vs = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
22
+ const a = vs.items.a;
23
+ const dto: BuilderConditionDto = {
24
+ kind: "condition",
25
+ name: "a",
26
+ variableId: a.varId,
27
+ operator: "equal",
28
+ value: 1,
29
+ };
30
+ const c = BuilderCondition.fromDto(dto, vs.list);
31
+ // c.setVariableList(vs);
32
+ expect(c.operatorsSelectItems.length).toBe(2);
33
+ expect(c.operator === "equal").toBe(true);
34
+ expect(c.value).toBeTruthy();
35
+ });
36
+ test("Can not resolve value if invalid value", () => {
37
+ const vs = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
38
+ const a = vs.items.a;
39
+ const dto: BuilderConditionDto = {
40
+ kind: "condition",
41
+ name: "a",
42
+ variableId: a.varId,
43
+ operator: "equal",
44
+ value: 8,
45
+ };
46
+ const c = BuilderCondition.fromDto(dto, vs.list);
47
+ expect(c.operatorsSelectItems.length).toBe(2);
48
+ expect(c.value).toBe(false);
49
+ // expect(match).toBe(false);
50
+ });
51
+ test("Will nullify dto if not matchedFrom is called", () => {
52
+ const vs = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
53
+ const a = vs.items.a;
54
+ const dto: BuilderConditionDto = {
55
+ kind: "condition",
56
+ name: "a",
57
+ variableId: a.varId,
58
+ operator: "equal",
59
+ value: 0,
60
+ };
61
+ const c = BuilderCondition.fromDto(dto, []);
62
+ expect(c.operatorsSelectItems.length).toBe(0);
63
+ expect(c.toJson().name).toBe(dto.name);
64
+ expect(c.toJson().value).toBe("");
65
+ expect(c.toJson().operator).toBe("");
66
+ expect(c.toJson().variableId).toBe("");
67
+ // expect(match).toBe(false);
68
+ });
69
+ test("Will not nullify if created with valid universe", () => {
70
+ const vs = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
71
+ const a = vs.items.a;
72
+ const dto: BuilderConditionDto = {
73
+ kind: "condition",
74
+ name: "a",
75
+ variableId: a.varId,
76
+ operator: "equal",
77
+ value: 0,
78
+ };
79
+ const c = BuilderCondition.fromDto(dto, vs.list);
80
+ expect(c.variable).toBeInstanceOf(QuestionVariable);
81
+ expect(c.operator === "equal").toBe(true);
82
+ const value = c.value as BuilderVariableOption;
83
+ expect(value.value).toBe(0);
84
+ expect(value.label).toBe("Nei");
85
+ expect(c.variable).toBe(a);
86
+ });
87
+ test("Condition is valid, when in sync with universe.", () => {
88
+ const vs = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
89
+ const a = vs.items.a;
90
+ const dto: BuilderConditionDto = {
91
+ kind: "condition",
92
+
93
+ name: "name of a",
94
+ variableId: a.varId,
95
+ operator: "equal",
96
+ value: 0,
97
+ };
98
+ const c = BuilderCondition.fromDto(dto, vs.list);
99
+ expect(c.variable).toBeInstanceOf(QuestionVariable);
100
+ expect(c.operator === "equal").toBe(true);
101
+ const value = c.value as BuilderVariableOption;
102
+ expect(value.value).toBe(0);
103
+ expect(value.label).toBe("Nei");
104
+ expect(c.validate().isValid).toBe(true);
105
+ });
106
+ test("Condition is invalid, when not matched against universe", () => {
107
+ const dto: BuilderConditionDto = {
108
+ kind: "condition",
109
+ name: "name of a",
110
+ variableId: "a",
111
+ operator: "equal",
112
+ value: 0,
113
+ };
114
+ const c = BuilderCondition.fromDto(dto, []);
115
+ expect(c.validate().isValid).toBe(false);
116
+ });
117
+ test("Condition is invalid, when variable dont exist in universe.", () => {
118
+ const variables = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
119
+ const dto: BuilderConditionDto = {
120
+ kind: "condition",
121
+ name: "invalid variable name in dto",
122
+ variableId: "kkk",
123
+ operator: "equal",
124
+ value: 9,
125
+ };
126
+ const c = BuilderCondition.fromDto(dto, variables.list);
127
+ expect(c.variable).toBe(false);
128
+ expect(c.validate().isValid).toBe(false);
129
+ });
130
+ test("Condition is invalid if not all set, when variable dont exist in variables.", () => {
131
+ const variables = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
132
+ const dto: BuilderConditionDto = {
133
+ kind: "condition",
134
+ name: "invalid variable name in dto",
135
+ variableId: "kkk",
136
+ operator: "equal",
137
+ value: 9,
138
+ };
139
+ const c = BuilderCondition.fromDto(dto, variables.list);
140
+ expect(c.variable).toBe(false);
141
+ expect(c.validate().isValid).toBe(false);
142
+ });
143
+
144
+ test("Condition is invalid if operator is not set", () => {
145
+ const variables = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
146
+ const a = variables.items.a;
147
+ const dto: BuilderConditionDto = {
148
+ kind: "condition",
149
+ name: "invalid variable name in dto",
150
+ variableId: a.varId,
151
+ operator: "",
152
+ value: 1,
153
+ };
154
+ expect(a.varId).toBe("as_a");
155
+ const c = BuilderCondition.fromDto(dto, variables.list);
156
+ expect(c.variable).toBeInstanceOf(QuestionVariable);
157
+ expect(c.validate().isValid).toBe(false);
158
+ expect(c.value).toBe(false);
159
+ });
160
+ test("Condition is invalid if value (from dto) is not found in variable", () => {
161
+ const { list, items } = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
162
+ expect(items.a.varId).toBe("as_a");
163
+ const dto: BuilderConditionDto = {
164
+ kind: "condition",
165
+ name: "invalid variable name in dto",
166
+ variableId: items.a.varId,
167
+ operator: "equal",
168
+ value: 42,
169
+ };
170
+ const c = BuilderCondition.fromDto(dto, list);
171
+ expect(c).toBeInstanceOf(BuilderCondition);
172
+ expect(c.variable).toBeInstanceOf(QuestionVariable);
173
+ expect(c.operator).toBe("equal");
174
+ expect(c.operatorsSelectItems.length).toBe(BuilderCondition.NUMBER_OPERATORS.length);
175
+ expect(c.value).toBe(false);
176
+ expect(c.validate().isValid).toBe(false);
177
+ });
178
+ test("toEngineConditionWorks", () => {
179
+ const variables = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
180
+
181
+ const dto: BuilderConditionDto = {
182
+ kind: "condition",
183
+ name: "invalid variable name in dto",
184
+ variableId: variables.items.a.varId,
185
+ operator: "equal",
186
+ value: 7,
187
+ };
188
+ const c = BuilderCondition.fromDto(dto, variables.list);
189
+ expect(c.variable).toBeInstanceOf(QuestionVariable);
190
+ expect(c.operator).toBe("equal");
191
+ expect(c.operatorsSelectItems.length).toBe(BuilderCondition.NUMBER_OPERATORS.length);
192
+ expect(c.value).toBe(false);
193
+ expect(c.validate().isValid).toBe(false);
194
+ });
195
+ });