@media-quest/builder 0.0.7 → 0.0.9

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.
@@ -6,7 +6,10 @@ import type { BuilderConditionDto } from "./condition/Builder-condition";
6
6
  import type { BuilderOperator } from "./condition/Builder-operator";
7
7
  import type { ExcludeByPageAction, ExcludeByTagAction, JumpToPageAction } from "./RuleAction";
8
8
  import { ExcludeByPageIdSelectItem, ExcludeByTagSelectItem } from "./multi-select-item";
9
+ import { PageID } from "@media-quest/engine";
10
+ import { PagePrefix, VarID, SchemaPrefix } from "../prefix";
9
11
 
12
+ const id = () => PageID.create();
10
13
  export namespace RuleBuilderTestUtils {
11
14
  export const createOptions = () => [
12
15
  BuilderOption.create(0, "Nei"),
@@ -25,7 +28,7 @@ export namespace RuleBuilderTestUtils {
25
28
  return action;
26
29
  };
27
30
 
28
- export const excludeByPageIdAction = (pageId: string, pageNumber: number) => {
31
+ export const excludeByPageIdAction = (pageId: PageID, pageNumber: number) => {
29
32
  const action: ExcludeByPageAction = {
30
33
  kind: "exclude-by-pageId",
31
34
  mainText: "",
@@ -34,7 +37,7 @@ export namespace RuleBuilderTestUtils {
34
37
  };
35
38
  return action;
36
39
  };
37
- export const jumpToPageAction = (pageId: string, pageNumber: number) => {
40
+ export const jumpToPageAction = (pageId: PageID, pageNumber: number) => {
38
41
  const action: JumpToPageAction = {
39
42
  kind: "jump-to-page",
40
43
  mainText: "TEXT: " + pageId,
@@ -59,22 +62,50 @@ export namespace RuleBuilderTestUtils {
59
62
  ] as const;
60
63
  return list;
61
64
  };
62
- export const createRuleVariable = (id: string, pageNumber: number): QuestionVariable =>
63
- new QuestionVariable(id, "Har du " + id + "?", createOptions(), pageNumber);
65
+ export const createRuleVariable = (varId: VarID, pageNumber: number): QuestionVariable =>
66
+ new QuestionVariable(varId, "Har du " + varId + "?", createOptions(), pageNumber);
64
67
 
65
68
  /**
66
69
  *
67
70
  */
68
- export const createBuilderVariables_A_H = (): ReadonlyArray<QuestionVariable> => [
69
- createRuleVariable("a", 3),
70
- createRuleVariable("b", 4),
71
- createRuleVariable("c", 5),
72
- createRuleVariable("d", 6),
73
- createRuleVariable("e", 7),
74
- createRuleVariable("f", 8),
75
- createRuleVariable("g", 9),
76
- createRuleVariable("h", 10),
77
- ];
71
+ export const createPagesAndVars_A_H = (schemaPrefix: SchemaPrefix) => {
72
+ const varId = (pagePrefix: string) => {
73
+ const pxx = PagePrefix.fromStringOrThrow(pagePrefix);
74
+ const qxx = VarID.create(schemaPrefix.value, pxx);
75
+ return qxx;
76
+ };
77
+ const pageAID = PageID.create();
78
+ const pageBID = PageID.create();
79
+ const pageCID = PageID.create();
80
+ const pageDID = PageID.create();
81
+ const pageEID = PageID.create();
82
+ const pageFID = PageID.create();
83
+ const pageGID = PageID.create();
84
+ const pageHID = PageID.create();
85
+ const a = createRuleVariable(varId("a"), 3);
86
+ const b = createRuleVariable(varId("b"), 4);
87
+ const c = createRuleVariable(varId("c"), 5);
88
+ const d = createRuleVariable(varId("d"), 6);
89
+ const e = createRuleVariable(varId("e"), 7);
90
+ const f = createRuleVariable(varId("f"), 8);
91
+ const g = createRuleVariable(varId("g"), 9);
92
+ const h = createRuleVariable(varId("h"), 10);
93
+ const list = [a, b, c, d, e, f, g, h];
94
+ const items = { a, b, c, d, e, f, g, h };
95
+ const pageIds = {
96
+ a: pageAID,
97
+ b: pageBID,
98
+ c: pageCID,
99
+ d: pageDID,
100
+ e: pageEID,
101
+ f: pageFID,
102
+ g: pageGID,
103
+
104
+ h: pageHID,
105
+ };
106
+ const pageIdList = [pageAID, pageBID, pageCID, pageDID, pageEID, pageFID, pageGID, pageHID];
107
+ return { list, items, pageIds, pageIdList };
108
+ };
78
109
 
79
110
  export const createConditionDto = (variable: BuilderVariable): BuilderConditionDto => {
80
111
  const operator: BuilderOperator = Math.random() > 0 ? "equal" : "notEqual";
@@ -97,12 +128,12 @@ export namespace RuleBuilderTestUtils {
97
128
  };
98
129
  };
99
130
 
100
- export const createBuilderRuleDto = (): ReadonlyArray<BuilderRuleDto> => {
101
- const variables = createBuilderVariables_A_H();
102
- const condition0 = createConditionDto(variables[0]);
103
- const condition1 = createConditionDto(variables[1]);
104
- const condition3 = createConditionDto(variables[3]);
105
- const condition5 = createConditionDto(variables[5]);
131
+ export const createBuilderRuleDto = (schemaPrefix: SchemaPrefix): ReadonlyArray<BuilderRuleDto> => {
132
+ const v = createPagesAndVars_A_H(schemaPrefix);
133
+ const condition0 = createConditionDto(v.items.a);
134
+ const condition1 = createConditionDto(v.items.b);
135
+ const condition3 = createConditionDto(v.items.c);
136
+ const condition5 = createConditionDto(v.items.e);
106
137
  const group = createConditionGroupDto([condition0, condition3]);
107
138
  // const action1: Exc
108
139
  const rule: BuilderRuleDto = {
@@ -119,7 +150,7 @@ export namespace RuleBuilderTestUtils {
119
150
  ExcludeByPageIdSelectItem.create(
120
151
  {
121
152
  kind: "exclude-by-pageId",
122
- pageId: "page_a",
153
+ pageId: id(),
123
154
  pageNumber: 5,
124
155
  mainText: "Har du noen gang vært deprimeri?? ",
125
156
  },
@@ -129,7 +160,7 @@ export namespace RuleBuilderTestUtils {
129
160
  ExcludeByPageIdSelectItem.create(
130
161
  {
131
162
  kind: "exclude-by-pageId",
132
- pageId: "page_b",
163
+ pageId: id(),
133
164
  pageNumber: 5,
134
165
  mainText: "Har du noen gang vært deprimeri?? ",
135
166
  },
@@ -139,7 +170,7 @@ export namespace RuleBuilderTestUtils {
139
170
  ExcludeByPageIdSelectItem.create(
140
171
  {
141
172
  kind: "exclude-by-pageId",
142
- pageId: "page_c",
173
+ pageId: id(),
143
174
  pageNumber: 5,
144
175
  mainText: "Har du noen gang vært deprimeri?? ",
145
176
  },
@@ -149,7 +180,7 @@ export namespace RuleBuilderTestUtils {
149
180
  ExcludeByPageIdSelectItem.create(
150
181
  {
151
182
  kind: "exclude-by-pageId",
152
- pageId: "page_d",
183
+ pageId: id(),
153
184
  pageNumber: 5,
154
185
  mainText: "Har du noen gang vært deprimeri?? ",
155
186
  },
@@ -159,7 +190,7 @@ export namespace RuleBuilderTestUtils {
159
190
  ExcludeByPageIdSelectItem.create(
160
191
  {
161
192
  kind: "exclude-by-pageId",
162
- pageId: "page_e",
193
+ pageId: id(),
163
194
  pageNumber: 5,
164
195
  mainText: "Har du noen gang vært deprimeri?? ",
165
196
  },
@@ -168,7 +199,7 @@ export namespace RuleBuilderTestUtils {
168
199
  ExcludeByPageIdSelectItem.create(
169
200
  {
170
201
  kind: "exclude-by-pageId",
171
- pageId: "page_f",
202
+ pageId: id(),
172
203
  pageNumber: 5,
173
204
  mainText: "Har du noen gang vært deprimeri?? ",
174
205
  },
@@ -178,7 +209,7 @@ export namespace RuleBuilderTestUtils {
178
209
  ExcludeByPageIdSelectItem.create(
179
210
  {
180
211
  kind: "exclude-by-pageId",
181
- pageId: "page_g",
212
+ pageId: id(),
182
213
  pageNumber: 5,
183
214
  mainText: "Har du noen gang vært deprimeri?? ",
184
215
  },
@@ -188,7 +219,7 @@ export namespace RuleBuilderTestUtils {
188
219
  ExcludeByPageIdSelectItem.create(
189
220
  {
190
221
  kind: "exclude-by-pageId",
191
- pageId: "page_h",
222
+ pageId: id(),
192
223
  pageNumber: 5,
193
224
  mainText: "Har du noen gang vært deprimeri?? ",
194
225
  },
@@ -198,7 +229,7 @@ export namespace RuleBuilderTestUtils {
198
229
  ExcludeByPageIdSelectItem.create(
199
230
  {
200
231
  kind: "exclude-by-pageId",
201
- pageId: "page_i",
232
+ pageId: id(),
202
233
  pageNumber: 5,
203
234
  mainText: "Har du noen gang vært deprimeri?? ",
204
235
  },
@@ -208,7 +239,7 @@ export namespace RuleBuilderTestUtils {
208
239
  ExcludeByPageIdSelectItem.create(
209
240
  {
210
241
  kind: "exclude-by-pageId",
211
- pageId: "page_j",
242
+ pageId: id(),
212
243
  pageNumber: 5,
213
244
  mainText: "Har du noen gang vært deprimeri?? ",
214
245
  },
@@ -1,3 +1,5 @@
1
+ import { PagePrefix, VarID, SchemaPrefix } from "../prefix";
2
+
1
3
  const BuilderVariableType = {
2
4
  numericWithOptions: true,
3
5
  numeric: true,
@@ -23,7 +25,9 @@ export class QuestionVariable {
23
25
  readonly kind: "question-variable" = "question-variable";
24
26
  readonly dataType: BuilderVariableType = "numericWithOptions";
25
27
  constructor(
26
- readonly varId: string,
28
+ // private schemaPrefix: SchemaPrefix,
29
+ // private pagePrefix: PagePrefix,
30
+ readonly varId: VarID,
27
31
  readonly label: string,
28
32
  readonly options: ReadonlyArray<BuilderVariableOption>,
29
33
  readonly pageNumber: number,
@@ -34,7 +38,7 @@ export class CustomVariable {
34
38
  readonly kind: "configuration-variable" = "configuration-variable";
35
39
  readonly dataType: BuilderVariableType = "numericWithOptions";
36
40
  constructor(
37
- readonly varId: string,
41
+ readonly varId: VarID,
38
42
  readonly label: string,
39
43
  readonly options: ReadonlyArray<BuilderVariableOption>,
40
44
  ) {}
@@ -82,7 +82,7 @@ export class BuilderConditionGroup extends BuilderObject<"builder-condition-grou
82
82
  };
83
83
  }
84
84
 
85
- toEngineConditionComplex(modulePrefix: string): Condition.Complex | false {
85
+ toEngineConditionComplex(): Condition.Complex | false {
86
86
  const comp: Condition.Complex = {
87
87
  kind: "complex-condition",
88
88
  all: [],
@@ -91,7 +91,7 @@ export class BuilderConditionGroup extends BuilderObject<"builder-condition-grou
91
91
  };
92
92
  const conditions: Condition.Simple[] = [];
93
93
  this.conditions.forEach((c) => {
94
- const maybeSimple = c.toEngineCondition(modulePrefix);
94
+ const maybeSimple = c.toEngineCondition();
95
95
  if (maybeSimple) {
96
96
  conditions.push(maybeSimple);
97
97
  }
@@ -2,6 +2,7 @@ import { BuilderCondition, type BuilderConditionDto } from "./Builder-condition"
2
2
  import { RuleBuilderTestUtils } from "../RuleBuilder-test-utils";
3
3
  import type { BuilderVariable, BuilderVariableOption } from "../RuleVariable";
4
4
  import { QuestionVariable } from "../RuleVariable";
5
+ import { SchemaPrefix } from "../../prefix";
5
6
 
6
7
  let condition = BuilderCondition.create([]);
7
8
 
@@ -9,13 +10,15 @@ beforeEach(() => {
9
10
  condition = BuilderCondition.create([]);
10
11
  });
11
12
 
13
+ const as = SchemaPrefix.fromValueOrThrow("as");
14
+
12
15
  describe("Builder Operator", () => {
13
16
  test("Can create", () => {
14
17
  expect(condition).toBeInstanceOf(BuilderCondition);
15
18
  });
16
19
  test("Can resolve dto from Variables in universe.", () => {
17
- const vs = RuleBuilderTestUtils.createBuilderVariables_A_H();
18
- const a = vs[0];
20
+ const vs = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
21
+ const a = vs.items.a;
19
22
  const dto: BuilderConditionDto = {
20
23
  kind: "condition",
21
24
  name: "a",
@@ -23,15 +26,15 @@ describe("Builder Operator", () => {
23
26
  operator: "equal",
24
27
  value: 1,
25
28
  };
26
- const c = BuilderCondition.fromDto(dto, vs);
29
+ const c = BuilderCondition.fromDto(dto, vs.list);
27
30
  // c.setVariableList(vs);
28
31
  expect(c.operatorsSelectItems.length).toBe(2);
29
32
  expect(c.operator === "equal").toBe(true);
30
33
  expect(c.value).toBeTruthy();
31
34
  });
32
35
  test("Can not resolve value if invalid value", () => {
33
- const vs = RuleBuilderTestUtils.createBuilderVariables_A_H();
34
- const a = vs[0];
36
+ const vs = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
37
+ const a = vs.items.a;
35
38
  const dto: BuilderConditionDto = {
36
39
  kind: "condition",
37
40
  name: "a",
@@ -39,14 +42,14 @@ describe("Builder Operator", () => {
39
42
  operator: "equal",
40
43
  value: 8,
41
44
  };
42
- const c = BuilderCondition.fromDto(dto, vs);
45
+ const c = BuilderCondition.fromDto(dto, vs.list);
43
46
  expect(c.operatorsSelectItems.length).toBe(2);
44
47
  expect(c.value).toBe(false);
45
48
  // expect(match).toBe(false);
46
49
  });
47
50
  test("Will nullify dto if not matchedFrom is called", () => {
48
- const vs = RuleBuilderTestUtils.createBuilderVariables_A_H();
49
- const a = vs[0];
51
+ const vs = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
52
+ const a = vs.items.a;
50
53
  const dto: BuilderConditionDto = {
51
54
  kind: "condition",
52
55
  name: "a",
@@ -63,8 +66,8 @@ describe("Builder Operator", () => {
63
66
  // expect(match).toBe(false);
64
67
  });
65
68
  test("Will not nullify if created with valid universe", () => {
66
- const vs = RuleBuilderTestUtils.createBuilderVariables_A_H();
67
- const a = vs[0];
69
+ const vs = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
70
+ const a = vs.items.a;
68
71
  const dto: BuilderConditionDto = {
69
72
  kind: "condition",
70
73
  name: "a",
@@ -72,7 +75,7 @@ describe("Builder Operator", () => {
72
75
  operator: "equal",
73
76
  value: 0,
74
77
  };
75
- const c = BuilderCondition.fromDto(dto, vs);
78
+ const c = BuilderCondition.fromDto(dto, vs.list);
76
79
  expect(c.variable).toBeInstanceOf(QuestionVariable);
77
80
  expect(c.operator === "equal").toBe(true);
78
81
  const value = c.value as BuilderVariableOption;
@@ -81,8 +84,8 @@ describe("Builder Operator", () => {
81
84
  expect(c.variable).toBe(a);
82
85
  });
83
86
  test("Condition is valid, when in sync with universe.", () => {
84
- const vs = RuleBuilderTestUtils.createBuilderVariables_A_H();
85
- const a = vs[0];
87
+ const vs = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
88
+ const a = vs.items.a;
86
89
  const dto: BuilderConditionDto = {
87
90
  kind: "condition",
88
91
 
@@ -91,7 +94,7 @@ describe("Builder Operator", () => {
91
94
  operator: "equal",
92
95
  value: 0,
93
96
  };
94
- const c = BuilderCondition.fromDto(dto, vs);
97
+ const c = BuilderCondition.fromDto(dto, vs.list);
95
98
  expect(c.variable).toBeInstanceOf(QuestionVariable);
96
99
  expect(c.operator === "equal").toBe(true);
97
100
  const value = c.value as BuilderVariableOption;
@@ -111,7 +114,7 @@ describe("Builder Operator", () => {
111
114
  expect(c.validate().isValid).toBe(false);
112
115
  });
113
116
  test("Condition is invalid, when variable dont exist in universe.", () => {
114
- const universe = RuleBuilderTestUtils.createBuilderVariables_A_H();
117
+ const variables = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
115
118
  const dto: BuilderConditionDto = {
116
119
  kind: "condition",
117
120
  name: "invalid variable name in dto",
@@ -119,12 +122,12 @@ describe("Builder Operator", () => {
119
122
  operator: "equal",
120
123
  value: 9,
121
124
  };
122
- const c = BuilderCondition.fromDto(dto, universe);
125
+ const c = BuilderCondition.fromDto(dto, variables.list);
123
126
  expect(c.variable).toBe(false);
124
127
  expect(c.validate().isValid).toBe(false);
125
128
  });
126
- test("Condition is invalid if not all set, when variable dont exist in universe.", () => {
127
- const universe = RuleBuilderTestUtils.createBuilderVariables_A_H();
129
+ test("Condition is invalid if not all set, when variable dont exist in variables.", () => {
130
+ const variables = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
128
131
  const dto: BuilderConditionDto = {
129
132
  kind: "condition",
130
133
  name: "invalid variable name in dto",
@@ -132,34 +135,39 @@ describe("Builder Operator", () => {
132
135
  operator: "equal",
133
136
  value: 9,
134
137
  };
135
- const c = BuilderCondition.fromDto(dto, universe);
138
+ const c = BuilderCondition.fromDto(dto, variables.list);
136
139
  expect(c.variable).toBe(false);
137
140
  expect(c.validate().isValid).toBe(false);
138
141
  });
142
+
139
143
  test("Condition is invalid if operator is not set", () => {
140
- const universe = RuleBuilderTestUtils.createBuilderVariables_A_H();
144
+ const variables = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
145
+ const a = variables.items.a;
141
146
  const dto: BuilderConditionDto = {
142
147
  kind: "condition",
143
148
  name: "invalid variable name in dto",
144
- variableId: "a",
149
+ variableId: a.varId,
145
150
  operator: "",
146
151
  value: 1,
147
152
  };
148
- const c = BuilderCondition.fromDto(dto, universe);
153
+ expect(a.varId).toBe("as_a");
154
+ const c = BuilderCondition.fromDto(dto, variables.list);
149
155
  expect(c.variable).toBeInstanceOf(QuestionVariable);
150
156
  expect(c.validate().isValid).toBe(false);
151
157
  expect(c.value).toBe(false);
152
158
  });
153
159
  test("Condition is invalid if value (from dto) is not found in variable", () => {
154
- const universe = RuleBuilderTestUtils.createBuilderVariables_A_H();
160
+ const { list, items } = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
161
+ expect(items.a.varId).toBe("as_a");
155
162
  const dto: BuilderConditionDto = {
156
163
  kind: "condition",
157
164
  name: "invalid variable name in dto",
158
- variableId: "a",
165
+ variableId: items.a.varId,
159
166
  operator: "equal",
160
- value: 7,
167
+ value: 42,
161
168
  };
162
- const c = BuilderCondition.fromDto(dto, universe);
169
+ const c = BuilderCondition.fromDto(dto, list);
170
+ expect(c).toBeInstanceOf(BuilderCondition);
163
171
  expect(c.variable).toBeInstanceOf(QuestionVariable);
164
172
  expect(c.operator).toBe("equal");
165
173
  expect(c.operatorsSelectItems.length).toBe(BuilderCondition.NUMBER_OPERATORS.length);
@@ -167,15 +175,16 @@ describe("Builder Operator", () => {
167
175
  expect(c.validate().isValid).toBe(false);
168
176
  });
169
177
  test("toEngineConditionWorks", () => {
170
- const universe = RuleBuilderTestUtils.createBuilderVariables_A_H();
178
+ const variables = RuleBuilderTestUtils.createPagesAndVars_A_H(as);
179
+
171
180
  const dto: BuilderConditionDto = {
172
181
  kind: "condition",
173
182
  name: "invalid variable name in dto",
174
- variableId: "a",
183
+ variableId: variables.items.a.varId,
175
184
  operator: "equal",
176
185
  value: 7,
177
186
  };
178
- const c = BuilderCondition.fromDto(dto, universe);
187
+ const c = BuilderCondition.fromDto(dto, variables.list);
179
188
  expect(c.variable).toBeInstanceOf(QuestionVariable);
180
189
  expect(c.operator).toBe("equal");
181
190
  expect(c.operatorsSelectItems.length).toBe(BuilderCondition.NUMBER_OPERATORS.length);
@@ -103,7 +103,7 @@ export class BuilderCondition extends BuilderObject<"builder-condition", Builder
103
103
  return { isValid: true };
104
104
  }
105
105
 
106
- toEngineCondition(modulePrefix: string): Condition.Simple | false {
106
+ toEngineCondition(): Condition.Simple | false {
107
107
  const val = this.value;
108
108
  const op = this._operator;
109
109
  const v = this._variable;
@@ -115,7 +115,7 @@ export class BuilderCondition extends BuilderObject<"builder-condition", Builder
115
115
  kind: "numeric-condition",
116
116
  value: val.value,
117
117
  valueLabel: val.label,
118
- referenceId: modulePrefix + "_" + v.varId,
118
+ referenceId: v.varId,
119
119
  referenceLabel: v.label,
120
120
  operator: "eq",
121
121
  };
@@ -40,7 +40,7 @@ export class DefaultThemeCompiler extends AbstractThemeCompiler<IDefaultTheme> {
40
40
  const ruleInput = builderSchema.getRuleInput();
41
41
  const pageQueRules: Rule<PageQueCommand, never>[] = [];
42
42
  source.rules.forEach((rule) => {
43
- const engineRule = BuilderRule.fromDto(rule, ruleInput).toEngineRule(source.prefix);
43
+ const engineRule = BuilderRule.fromDto(rule, ruleInput).toEngineRule();
44
44
  if (!Rule.isEmpty(engineRule)) {
45
45
  pageQueRules.push(engineRule);
46
46
  } else {
@@ -56,7 +56,7 @@ export class DefaultThemeCompiler extends AbstractThemeCompiler<IDefaultTheme> {
56
56
  compile(source: BuilderSchemaDto): SchemaDto {
57
57
  const pages = source.pages.map((p) => this.compilePage(p, source.prefix));
58
58
  const rules = this.compileRules(source);
59
- console.log(pages.map((p) => p.tags));
59
+ // console.log(pages.map((p) => p.tags));
60
60
 
61
61
  const dto: SchemaDto = {
62
62
  backgroundColor: source.backgroundColor,
@@ -66,7 +66,6 @@ export class DefaultThemeCompiler extends AbstractThemeCompiler<IDefaultTheme> {
66
66
  pageSequences: [],
67
67
  pages,
68
68
  predefinedFacts: [],
69
- prefix: source.prefix,
70
69
  rules,
71
70
  stateFromEvent: [
72
71
  {