@featurevisor/core 3.1.0 → 3.3.0

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 (213) hide show
  1. package/lib/datasource/datasource.d.ts +1 -1
  2. package/lib/datasource/datasource.js +4 -14
  3. package/lib/datasource/datasource.js.map +1 -1
  4. package/lib/datasource/requiredFeatures.d.ts +7 -0
  5. package/lib/datasource/requiredFeatures.js +38 -0
  6. package/lib/datasource/requiredFeatures.js.map +1 -0
  7. package/lib/linter/checkCircularDependency.d.ts +1 -1
  8. package/lib/linter/checkCircularDependency.js +3 -22
  9. package/lib/linter/checkCircularDependency.js.map +1 -1
  10. package/lib/linter/conditionSchema.js +27 -3
  11. package/lib/linter/conditionSchema.js.map +1 -1
  12. package/lib/linter/testSchema.d.ts +4 -0
  13. package/lib/linter/testSchema.js +46 -4
  14. package/lib/linter/testSchema.js.map +1 -1
  15. package/lib/promoter/index.js +62 -1
  16. package/lib/promoter/index.js.map +1 -1
  17. package/lib/tester/testFeature.js +17 -16
  18. package/lib/tester/testFeature.js.map +1 -1
  19. package/package.json +10 -7
  20. package/CHANGELOG.md +0 -2246
  21. package/coverage/clover.xml +0 -5456
  22. package/coverage/coverage-final.json +0 -64
  23. package/coverage/lcov-report/base.css +0 -224
  24. package/coverage/lcov-report/block-navigation.js +0 -87
  25. package/coverage/lcov-report/favicon.png +0 -0
  26. package/coverage/lcov-report/index.html +0 -371
  27. package/coverage/lcov-report/prettify.css +0 -1
  28. package/coverage/lcov-report/prettify.js +0 -2
  29. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  30. package/coverage/lcov-report/sorter.js +0 -210
  31. package/coverage/lcov-report/src/assess-distribution/index.html +0 -116
  32. package/coverage/lcov-report/src/assess-distribution/index.ts.html +0 -754
  33. package/coverage/lcov-report/src/benchmark/index.html +0 -116
  34. package/coverage/lcov-report/src/benchmark/index.ts.html +0 -793
  35. package/coverage/lcov-report/src/builder/allocator.ts.html +0 -220
  36. package/coverage/lcov-report/src/builder/applyContextToConditions.ts.html +0 -502
  37. package/coverage/lcov-report/src/builder/applyContextToDatafile.ts.html +0 -772
  38. package/coverage/lcov-report/src/builder/applyContextToSegments.ts.html +0 -580
  39. package/coverage/lcov-report/src/builder/buildDatafile.ts.html +0 -2053
  40. package/coverage/lcov-report/src/builder/buildProject.ts.html +0 -1117
  41. package/coverage/lcov-report/src/builder/buildRuntimeDatafiles.ts.html +0 -256
  42. package/coverage/lcov-report/src/builder/getFeatureRanges.ts.html +0 -268
  43. package/coverage/lcov-report/src/builder/hashes.ts.html +0 -412
  44. package/coverage/lcov-report/src/builder/index.html +0 -311
  45. package/coverage/lcov-report/src/builder/index.ts.html +0 -91
  46. package/coverage/lcov-report/src/builder/mutateVariables.ts.html +0 -421
  47. package/coverage/lcov-report/src/builder/mutator.ts.html +0 -796
  48. package/coverage/lcov-report/src/builder/revision.ts.html +0 -151
  49. package/coverage/lcov-report/src/builder/traffic.ts.html +0 -661
  50. package/coverage/lcov-report/src/cli/cli.ts.html +0 -400
  51. package/coverage/lcov-report/src/cli/index.html +0 -131
  52. package/coverage/lcov-report/src/cli/plugins.ts.html +0 -214
  53. package/coverage/lcov-report/src/config/index.html +0 -131
  54. package/coverage/lcov-report/src/config/index.ts.html +0 -88
  55. package/coverage/lcov-report/src/config/projectConfig.ts.html +0 -925
  56. package/coverage/lcov-report/src/datasource/adapter.ts.html +0 -253
  57. package/coverage/lcov-report/src/datasource/datasource.ts.html +0 -1000
  58. package/coverage/lcov-report/src/datasource/filesystemAdapter.ts.html +0 -1576
  59. package/coverage/lcov-report/src/datasource/index.html +0 -161
  60. package/coverage/lcov-report/src/datasource/index.ts.html +0 -94
  61. package/coverage/lcov-report/src/evaluate/index.html +0 -116
  62. package/coverage/lcov-report/src/evaluate/index.ts.html +0 -814
  63. package/coverage/lcov-report/src/find-duplicate-segments/findDuplicateSegments.ts.html +0 -313
  64. package/coverage/lcov-report/src/find-duplicate-segments/index.html +0 -131
  65. package/coverage/lcov-report/src/find-duplicate-segments/index.ts.html +0 -307
  66. package/coverage/lcov-report/src/find-usage/index.html +0 -116
  67. package/coverage/lcov-report/src/find-usage/index.ts.html +0 -1780
  68. package/coverage/lcov-report/src/generate-code/index.html +0 -131
  69. package/coverage/lcov-report/src/generate-code/index.ts.html +0 -403
  70. package/coverage/lcov-report/src/generate-code/typescript.ts.html +0 -1909
  71. package/coverage/lcov-report/src/index.html +0 -131
  72. package/coverage/lcov-report/src/info/index.html +0 -116
  73. package/coverage/lcov-report/src/info/index.ts.html +0 -472
  74. package/coverage/lcov-report/src/init/index.html +0 -116
  75. package/coverage/lcov-report/src/init/index.ts.html +0 -280
  76. package/coverage/lcov-report/src/linter/attributeSchema.ts.html +0 -1963
  77. package/coverage/lcov-report/src/linter/checkCircularDependency.ts.html +0 -220
  78. package/coverage/lcov-report/src/linter/checkPercentageExceedingSlot.ts.html +0 -268
  79. package/coverage/lcov-report/src/linter/conditionSchema.ts.html +0 -2224
  80. package/coverage/lcov-report/src/linter/featureSchema.ts.html +0 -5851
  81. package/coverage/lcov-report/src/linter/groupSchema.ts.html +0 -226
  82. package/coverage/lcov-report/src/linter/index.html +0 -326
  83. package/coverage/lcov-report/src/linter/index.ts.html +0 -88
  84. package/coverage/lcov-report/src/linter/lintProject.ts.html +0 -2704
  85. package/coverage/lcov-report/src/linter/mutationNotation.ts.html +0 -1411
  86. package/coverage/lcov-report/src/linter/printError.ts.html +0 -289
  87. package/coverage/lcov-report/src/linter/schema.ts.html +0 -1558
  88. package/coverage/lcov-report/src/linter/segmentSchema.ts.html +0 -139
  89. package/coverage/lcov-report/src/linter/targetSchema.ts.html +0 -223
  90. package/coverage/lcov-report/src/linter/testSchema.ts.html +0 -529
  91. package/coverage/lcov-report/src/linter/zodHelpers.ts.html +0 -133
  92. package/coverage/lcov-report/src/list/index.html +0 -131
  93. package/coverage/lcov-report/src/list/index.ts.html +0 -2065
  94. package/coverage/lcov-report/src/list/matrix.ts.html +0 -721
  95. package/coverage/lcov-report/src/promoter/index.html +0 -116
  96. package/coverage/lcov-report/src/promoter/index.ts.html +0 -3883
  97. package/coverage/lcov-report/src/sets.ts.html +0 -313
  98. package/coverage/lcov-report/src/targeting.ts.html +0 -403
  99. package/coverage/lcov-report/src/tester/cliFormat.ts.html +0 -133
  100. package/coverage/lcov-report/src/tester/helpers.ts.html +0 -313
  101. package/coverage/lcov-report/src/tester/index.html +0 -206
  102. package/coverage/lcov-report/src/tester/prettyDuration.ts.html +0 -187
  103. package/coverage/lcov-report/src/tester/printTestResult.ts.html +0 -337
  104. package/coverage/lcov-report/src/tester/testFeature.ts.html +0 -1186
  105. package/coverage/lcov-report/src/tester/testProject.ts.html +0 -1018
  106. package/coverage/lcov-report/src/tester/testSegment.ts.html +0 -316
  107. package/coverage/lcov-report/src/utils/extractKeys.ts.html +0 -493
  108. package/coverage/lcov-report/src/utils/git.ts.html +0 -559
  109. package/coverage/lcov-report/src/utils/index.html +0 -161
  110. package/coverage/lcov-report/src/utils/index.ts.html +0 -94
  111. package/coverage/lcov-report/src/utils/pretty.ts.html +0 -121
  112. package/coverage/lcov.info +0 -11615
  113. package/jest.config.js +0 -22
  114. package/json-schema/README.md +0 -5
  115. package/json-schema/attribute.json +0 -40
  116. package/json-schema/feature.json +0 -394
  117. package/json-schema/segment.json +0 -137
  118. package/src/assess-distribution/index.ts +0 -223
  119. package/src/benchmark/index.ts +0 -236
  120. package/src/builder/allocator.spec.ts +0 -105
  121. package/src/builder/allocator.ts +0 -45
  122. package/src/builder/applyContextToConditions.spec.ts +0 -2677
  123. package/src/builder/applyContextToConditions.ts +0 -139
  124. package/src/builder/applyContextToDatafile.spec.ts +0 -2626
  125. package/src/builder/applyContextToDatafile.ts +0 -229
  126. package/src/builder/applyContextToSegments.spec.ts +0 -1593
  127. package/src/builder/applyContextToSegments.ts +0 -165
  128. package/src/builder/buildDatafile.spec.ts +0 -445
  129. package/src/builder/buildDatafile.ts +0 -656
  130. package/src/builder/buildProject.ts +0 -344
  131. package/src/builder/buildRuntimeDatafiles.ts +0 -57
  132. package/src/builder/getFeatureRanges.ts +0 -61
  133. package/src/builder/hashes.ts +0 -109
  134. package/src/builder/index.ts +0 -2
  135. package/src/builder/mutateVariables.spec.ts +0 -1179
  136. package/src/builder/mutateVariables.ts +0 -112
  137. package/src/builder/mutator.spec.ts +0 -413
  138. package/src/builder/mutator.ts +0 -237
  139. package/src/builder/revision.spec.ts +0 -22
  140. package/src/builder/revision.ts +0 -22
  141. package/src/builder/traffic.spec.ts +0 -946
  142. package/src/builder/traffic.ts +0 -192
  143. package/src/cli/cli.ts +0 -105
  144. package/src/cli/index.ts +0 -2
  145. package/src/cli/plugins.ts +0 -43
  146. package/src/config/index.ts +0 -1
  147. package/src/config/projectConfig.spec.ts +0 -194
  148. package/src/config/projectConfig.ts +0 -280
  149. package/src/datasource/adapter.ts +0 -56
  150. package/src/datasource/datasource.ts +0 -305
  151. package/src/datasource/filesystemAdapter.spec.ts +0 -212
  152. package/src/datasource/filesystemAdapter.ts +0 -497
  153. package/src/datasource/index.ts +0 -3
  154. package/src/dependencies.ts +0 -13
  155. package/src/evaluate/index.ts +0 -243
  156. package/src/find-duplicate-segments/findDuplicateSegments.ts +0 -76
  157. package/src/find-duplicate-segments/index.ts +0 -74
  158. package/src/find-usage/index.ts +0 -565
  159. package/src/generate-code/index.ts +0 -106
  160. package/src/generate-code/typescript.spec.ts +0 -214
  161. package/src/generate-code/typescript.ts +0 -608
  162. package/src/index.ts +0 -20
  163. package/src/info/index.ts +0 -129
  164. package/src/init/index.ts +0 -65
  165. package/src/linter/attributeSchema.spec.ts +0 -342
  166. package/src/linter/attributeSchema.ts +0 -626
  167. package/src/linter/checkCircularDependency.ts +0 -45
  168. package/src/linter/checkPercentageExceedingSlot.ts +0 -61
  169. package/src/linter/conditionSchema.spec.ts +0 -510
  170. package/src/linter/conditionSchema.ts +0 -713
  171. package/src/linter/featureSchema.spec.ts +0 -1985
  172. package/src/linter/featureSchema.ts +0 -1922
  173. package/src/linter/groupSchema.ts +0 -47
  174. package/src/linter/index.ts +0 -1
  175. package/src/linter/lintProject.spec.ts +0 -336
  176. package/src/linter/lintProject.ts +0 -873
  177. package/src/linter/mutationNotation.spec.ts +0 -704
  178. package/src/linter/mutationNotation.ts +0 -442
  179. package/src/linter/printError.ts +0 -68
  180. package/src/linter/schema.spec.ts +0 -690
  181. package/src/linter/schema.ts +0 -491
  182. package/src/linter/segmentSchema.spec.ts +0 -358
  183. package/src/linter/segmentSchema.ts +0 -18
  184. package/src/linter/targetSchema.spec.ts +0 -159
  185. package/src/linter/targetSchema.ts +0 -46
  186. package/src/linter/testSchema.spec.ts +0 -258
  187. package/src/linter/testSchema.ts +0 -148
  188. package/src/linter/zodHelpers.ts +0 -16
  189. package/src/list/index.spec.ts +0 -135
  190. package/src/list/index.ts +0 -660
  191. package/src/list/matrix.spec.ts +0 -49
  192. package/src/list/matrix.ts +0 -212
  193. package/src/promoter/index.spec.ts +0 -617
  194. package/src/promoter/index.ts +0 -1266
  195. package/src/sets.ts +0 -76
  196. package/src/targeting.spec.ts +0 -74
  197. package/src/targeting.ts +0 -106
  198. package/src/tester/cliFormat.ts +0 -16
  199. package/src/tester/helpers.spec.ts +0 -149
  200. package/src/tester/helpers.ts +0 -76
  201. package/src/tester/index.ts +0 -2
  202. package/src/tester/prettyDuration.ts +0 -34
  203. package/src/tester/printTestResult.ts +0 -84
  204. package/src/tester/testFeature.ts +0 -367
  205. package/src/tester/testProject.spec.ts +0 -50
  206. package/src/tester/testProject.ts +0 -311
  207. package/src/tester/testSegment.ts +0 -77
  208. package/src/utils/extractKeys.ts +0 -136
  209. package/src/utils/git.ts +0 -158
  210. package/src/utils/index.ts +0 -3
  211. package/src/utils/pretty.ts +0 -12
  212. package/tsconfig.cjs.json +0 -13
  213. package/tsconfig.typecheck.json +0 -9
@@ -1,2677 +0,0 @@
1
- import type { Condition, DatafileContent } from "@featurevisor/types";
2
-
3
- import {
4
- applyContextToCondition,
5
- removeRedundantConditions,
6
- applyContextToConditions,
7
- } from "./applyContextToConditions";
8
- import { createFeaturevisor } from "@featurevisor/sdk";
9
-
10
- describe("core: applyContextToConditions", function () {
11
- const emptyDatafile: DatafileContent = {
12
- schemaVersion: "2",
13
- revision: "unknown",
14
- segments: {},
15
- features: {},
16
- };
17
-
18
- const featurevisor = createFeaturevisor({
19
- datafile: emptyDatafile,
20
- logLevel: "fatal",
21
- });
22
-
23
- describe("applyContextToConditions (plural)", function () {
24
- test("applyContextToConditions is a function", function () {
25
- expect(applyContextToConditions).toBeInstanceOf(Function);
26
- });
27
-
28
- test("simple cases", function () {
29
- // "*" remains "*"
30
- expect(applyContextToConditions(featurevisor, "*", {})).toEqual("*");
31
-
32
- // Plain condition that matches
33
- expect(
34
- applyContextToConditions(
35
- featurevisor,
36
- {
37
- attribute: "platform",
38
- operator: "equals",
39
- value: "web",
40
- },
41
- {
42
- platform: "web",
43
- },
44
- ),
45
- ).toEqual("*");
46
-
47
- // Plain condition that doesn't match
48
- expect(
49
- applyContextToConditions(
50
- featurevisor,
51
- {
52
- attribute: "platform",
53
- operator: "equals",
54
- value: "web",
55
- },
56
- {
57
- platform: "mobile",
58
- },
59
- ),
60
- ).toEqual({
61
- attribute: "platform",
62
- operator: "equals",
63
- value: "web",
64
- });
65
-
66
- // Array of conditions - partial match (redundant "*" removed)
67
- expect(
68
- applyContextToConditions(
69
- featurevisor,
70
- [
71
- {
72
- attribute: "platform",
73
- operator: "equals",
74
- value: "web",
75
- },
76
- {
77
- attribute: "browser",
78
- operator: "equals",
79
- value: "chrome",
80
- },
81
- ],
82
- {
83
- platform: "web",
84
- },
85
- ),
86
- ).toEqual([
87
- {
88
- attribute: "browser",
89
- operator: "equals",
90
- value: "chrome",
91
- },
92
- ]);
93
-
94
- // Array of conditions - full match (all "*" becomes "*")
95
- expect(
96
- applyContextToConditions(
97
- featurevisor,
98
- [
99
- {
100
- attribute: "platform",
101
- operator: "equals",
102
- value: "web",
103
- },
104
- {
105
- attribute: "browser",
106
- operator: "equals",
107
- value: "chrome",
108
- },
109
- ],
110
- {
111
- platform: "web",
112
- browser: "chrome",
113
- },
114
- ),
115
- ).toEqual("*");
116
-
117
- // Array of conditions - no match
118
- expect(
119
- applyContextToConditions(
120
- featurevisor,
121
- [
122
- {
123
- attribute: "platform",
124
- operator: "equals",
125
- value: "web",
126
- },
127
- {
128
- attribute: "browser",
129
- operator: "equals",
130
- value: "chrome",
131
- },
132
- ],
133
- {
134
- platform: "mobile",
135
- browser: "safari",
136
- },
137
- ),
138
- ).toEqual([
139
- {
140
- attribute: "platform",
141
- operator: "equals",
142
- value: "web",
143
- },
144
- {
145
- attribute: "browser",
146
- operator: "equals",
147
- value: "chrome",
148
- },
149
- ]);
150
- });
151
-
152
- test("AND conditions", function () {
153
- // AND with all matching conditions (all "*" becomes "*")
154
- expect(
155
- applyContextToConditions(
156
- featurevisor,
157
- {
158
- and: [
159
- {
160
- attribute: "platform",
161
- operator: "equals",
162
- value: "web",
163
- },
164
- {
165
- attribute: "browser",
166
- operator: "equals",
167
- value: "chrome",
168
- },
169
- ],
170
- },
171
- {
172
- platform: "web",
173
- browser: "chrome",
174
- },
175
- ),
176
- ).toEqual("*");
177
-
178
- // AND with partial match (redundant "*" removed)
179
- expect(
180
- applyContextToConditions(
181
- featurevisor,
182
- {
183
- and: [
184
- {
185
- attribute: "platform",
186
- operator: "equals",
187
- value: "web",
188
- },
189
- {
190
- attribute: "browser",
191
- operator: "equals",
192
- value: "chrome",
193
- },
194
- ],
195
- },
196
- {
197
- platform: "web",
198
- },
199
- ),
200
- ).toEqual({
201
- and: [
202
- {
203
- attribute: "browser",
204
- operator: "equals",
205
- value: "chrome",
206
- },
207
- ],
208
- });
209
-
210
- // AND with no matches
211
- expect(
212
- applyContextToConditions(
213
- featurevisor,
214
- {
215
- and: [
216
- {
217
- attribute: "platform",
218
- operator: "equals",
219
- value: "web",
220
- },
221
- {
222
- attribute: "browser",
223
- operator: "equals",
224
- value: "chrome",
225
- },
226
- ],
227
- },
228
- {
229
- platform: "mobile",
230
- browser: "safari",
231
- },
232
- ),
233
- ).toEqual({
234
- and: [
235
- {
236
- attribute: "platform",
237
- operator: "equals",
238
- value: "web",
239
- },
240
- {
241
- attribute: "browser",
242
- operator: "equals",
243
- value: "chrome",
244
- },
245
- ],
246
- });
247
-
248
- // AND with "*" in it (all "*" becomes "*")
249
- expect(
250
- applyContextToConditions(
251
- featurevisor,
252
- {
253
- and: [
254
- "*",
255
- {
256
- attribute: "platform",
257
- operator: "equals",
258
- value: "web",
259
- },
260
- ],
261
- },
262
- {
263
- platform: "web",
264
- },
265
- ),
266
- ).toEqual("*");
267
- });
268
-
269
- test("OR conditions", function () {
270
- // OR with all matching conditions (all "*" becomes "*")
271
- // Note: This would require both conditions to match, which isn't possible with same attribute
272
- // So testing with different attributes that both match
273
- expect(
274
- applyContextToConditions(
275
- featurevisor,
276
- {
277
- or: [
278
- {
279
- attribute: "platform",
280
- operator: "equals",
281
- value: "web",
282
- },
283
- {
284
- attribute: "browser",
285
- operator: "equals",
286
- value: "chrome",
287
- },
288
- ],
289
- },
290
- {
291
- platform: "web",
292
- browser: "chrome",
293
- },
294
- ),
295
- ).toEqual("*");
296
-
297
- // OR with partial match (redundant "*" removed, but OR structure remains)
298
- expect(
299
- applyContextToConditions(
300
- featurevisor,
301
- {
302
- or: [
303
- {
304
- attribute: "platform",
305
- operator: "equals",
306
- value: "web",
307
- },
308
- {
309
- attribute: "browser",
310
- operator: "equals",
311
- value: "chrome",
312
- },
313
- ],
314
- },
315
- {
316
- platform: "web",
317
- },
318
- ),
319
- ).toEqual({
320
- or: [
321
- {
322
- attribute: "browser",
323
- operator: "equals",
324
- value: "chrome",
325
- },
326
- ],
327
- });
328
-
329
- // OR with no matches
330
- expect(
331
- applyContextToConditions(
332
- featurevisor,
333
- {
334
- or: [
335
- {
336
- attribute: "platform",
337
- operator: "equals",
338
- value: "web",
339
- },
340
- {
341
- attribute: "platform",
342
- operator: "equals",
343
- value: "mobile",
344
- },
345
- ],
346
- },
347
- {
348
- platform: "desktop",
349
- },
350
- ),
351
- ).toEqual({
352
- or: [
353
- {
354
- attribute: "platform",
355
- operator: "equals",
356
- value: "web",
357
- },
358
- {
359
- attribute: "platform",
360
- operator: "equals",
361
- value: "mobile",
362
- },
363
- ],
364
- });
365
- });
366
-
367
- test("NOT conditions", function () {
368
- // NOT with matching condition remains an always-false expression
369
- expect(
370
- applyContextToConditions(
371
- featurevisor,
372
- {
373
- not: [
374
- {
375
- attribute: "platform",
376
- operator: "equals",
377
- value: "web",
378
- },
379
- ],
380
- },
381
- {
382
- platform: "web",
383
- },
384
- ),
385
- ).toEqual({
386
- not: ["*"],
387
- });
388
-
389
- // NOT with non-matching condition
390
- expect(
391
- applyContextToConditions(
392
- featurevisor,
393
- {
394
- not: [
395
- {
396
- attribute: "platform",
397
- operator: "equals",
398
- value: "web",
399
- },
400
- ],
401
- },
402
- {
403
- platform: "mobile",
404
- },
405
- ),
406
- ).toEqual({
407
- not: [
408
- {
409
- attribute: "platform",
410
- operator: "equals",
411
- value: "web",
412
- },
413
- ],
414
- });
415
-
416
- // NOT with multiple matching conditions remains an always-false expression
417
- expect(
418
- applyContextToConditions(
419
- featurevisor,
420
- {
421
- not: [
422
- {
423
- attribute: "platform",
424
- operator: "equals",
425
- value: "web",
426
- },
427
- {
428
- attribute: "browser",
429
- operator: "equals",
430
- value: "chrome",
431
- },
432
- ],
433
- },
434
- {
435
- platform: "web",
436
- browser: "chrome",
437
- },
438
- ),
439
- ).toEqual({
440
- not: ["*"],
441
- });
442
- });
443
-
444
- test("nested AND conditions", function () {
445
- // Nested AND with all matching (all "*" becomes "*")
446
- expect(
447
- applyContextToConditions(
448
- featurevisor,
449
- {
450
- and: [
451
- {
452
- attribute: "platform",
453
- operator: "equals",
454
- value: "web",
455
- },
456
- {
457
- and: [
458
- {
459
- attribute: "browser",
460
- operator: "equals",
461
- value: "chrome",
462
- },
463
- {
464
- attribute: "version",
465
- operator: "equals",
466
- value: "1.0",
467
- },
468
- ],
469
- },
470
- ],
471
- },
472
- {
473
- platform: "web",
474
- browser: "chrome",
475
- version: "1.0",
476
- },
477
- ),
478
- ).toEqual("*");
479
-
480
- // Nested AND with partial match (redundant "*" removed)
481
- expect(
482
- applyContextToConditions(
483
- featurevisor,
484
- {
485
- and: [
486
- {
487
- attribute: "platform",
488
- operator: "equals",
489
- value: "web",
490
- },
491
- {
492
- and: [
493
- {
494
- attribute: "browser",
495
- operator: "equals",
496
- value: "chrome",
497
- },
498
- {
499
- attribute: "version",
500
- operator: "equals",
501
- value: "1.0",
502
- },
503
- ],
504
- },
505
- ],
506
- },
507
- {
508
- platform: "web",
509
- browser: "chrome",
510
- },
511
- ),
512
- ).toEqual({
513
- and: [
514
- {
515
- and: [
516
- {
517
- attribute: "version",
518
- operator: "equals",
519
- value: "1.0",
520
- },
521
- ],
522
- },
523
- ],
524
- });
525
- });
526
-
527
- test("nested OR conditions", function () {
528
- // Nested OR with outer match (redundant "*" removed, but inner OR remains)
529
- expect(
530
- applyContextToConditions(
531
- featurevisor,
532
- {
533
- or: [
534
- {
535
- attribute: "platform",
536
- operator: "equals",
537
- value: "web",
538
- },
539
- {
540
- or: [
541
- {
542
- attribute: "platform",
543
- operator: "equals",
544
- value: "mobile",
545
- },
546
- {
547
- attribute: "platform",
548
- operator: "equals",
549
- value: "desktop",
550
- },
551
- ],
552
- },
553
- ],
554
- },
555
- {
556
- platform: "web",
557
- },
558
- ),
559
- ).toEqual({
560
- or: [
561
- {
562
- or: [
563
- {
564
- attribute: "platform",
565
- operator: "equals",
566
- value: "mobile",
567
- },
568
- {
569
- attribute: "platform",
570
- operator: "equals",
571
- value: "desktop",
572
- },
573
- ],
574
- },
575
- ],
576
- });
577
-
578
- // Nested OR with inner match (redundant "*" removed, but outer OR remains)
579
- expect(
580
- applyContextToConditions(
581
- featurevisor,
582
- {
583
- or: [
584
- {
585
- attribute: "platform",
586
- operator: "equals",
587
- value: "web",
588
- },
589
- {
590
- or: [
591
- {
592
- attribute: "platform",
593
- operator: "equals",
594
- value: "mobile",
595
- },
596
- {
597
- attribute: "platform",
598
- operator: "equals",
599
- value: "desktop",
600
- },
601
- ],
602
- },
603
- ],
604
- },
605
- {
606
- platform: "mobile",
607
- },
608
- ),
609
- ).toEqual({
610
- or: [
611
- {
612
- attribute: "platform",
613
- operator: "equals",
614
- value: "web",
615
- },
616
- {
617
- or: [
618
- {
619
- attribute: "platform",
620
- operator: "equals",
621
- value: "desktop",
622
- },
623
- ],
624
- },
625
- ],
626
- });
627
- });
628
-
629
- test("nested NOT conditions", function () {
630
- // Nested NOT preserves non-broadening false expressions
631
- expect(
632
- applyContextToConditions(
633
- featurevisor,
634
- {
635
- not: [
636
- {
637
- attribute: "platform",
638
- operator: "equals",
639
- value: "web",
640
- },
641
- {
642
- not: [
643
- {
644
- attribute: "browser",
645
- operator: "equals",
646
- value: "chrome",
647
- },
648
- ],
649
- },
650
- ],
651
- },
652
- {
653
- platform: "web",
654
- browser: "chrome",
655
- },
656
- ),
657
- ).toEqual({
658
- not: [
659
- {
660
- not: ["*"],
661
- },
662
- ],
663
- });
664
- });
665
-
666
- test("mixed nested conditions", function () {
667
- // AND with nested OR (redundant "*" removed, but OR structure remains if not all match)
668
- expect(
669
- applyContextToConditions(
670
- featurevisor,
671
- {
672
- and: [
673
- {
674
- attribute: "platform",
675
- operator: "equals",
676
- value: "web",
677
- },
678
- {
679
- or: [
680
- {
681
- attribute: "browser",
682
- operator: "equals",
683
- value: "chrome",
684
- },
685
- {
686
- attribute: "browser",
687
- operator: "equals",
688
- value: "firefox",
689
- },
690
- ],
691
- },
692
- ],
693
- },
694
- {
695
- platform: "web",
696
- browser: "chrome",
697
- },
698
- ),
699
- ).toEqual({
700
- and: [
701
- {
702
- or: [
703
- {
704
- attribute: "browser",
705
- operator: "equals",
706
- value: "firefox",
707
- },
708
- ],
709
- },
710
- ],
711
- });
712
-
713
- // OR with nested AND (redundant "*" removed, but OR structure remains if not all match)
714
- expect(
715
- applyContextToConditions(
716
- featurevisor,
717
- {
718
- or: [
719
- {
720
- attribute: "platform",
721
- operator: "equals",
722
- value: "web",
723
- },
724
- {
725
- and: [
726
- {
727
- attribute: "platform",
728
- operator: "equals",
729
- value: "mobile",
730
- },
731
- {
732
- attribute: "browser",
733
- operator: "equals",
734
- value: "chrome",
735
- },
736
- ],
737
- },
738
- ],
739
- },
740
- {
741
- platform: "mobile",
742
- browser: "chrome",
743
- },
744
- ),
745
- ).toEqual({
746
- or: [
747
- {
748
- attribute: "platform",
749
- operator: "equals",
750
- value: "web",
751
- },
752
- ],
753
- });
754
-
755
- // AND with nested NOT (redundant "*" removed)
756
- expect(
757
- applyContextToConditions(
758
- featurevisor,
759
- {
760
- and: [
761
- {
762
- attribute: "platform",
763
- operator: "equals",
764
- value: "web",
765
- },
766
- {
767
- not: [
768
- {
769
- attribute: "browser",
770
- operator: "equals",
771
- value: "safari",
772
- },
773
- ],
774
- },
775
- ],
776
- },
777
- {
778
- platform: "web",
779
- browser: "chrome",
780
- },
781
- ),
782
- ).toEqual({
783
- and: [
784
- {
785
- not: [
786
- {
787
- attribute: "browser",
788
- operator: "equals",
789
- value: "safari",
790
- },
791
- ],
792
- },
793
- ],
794
- });
795
-
796
- // Complex nested structure (redundant "*" removed)
797
- expect(
798
- applyContextToConditions(
799
- featurevisor,
800
- {
801
- and: [
802
- {
803
- attribute: "platform",
804
- operator: "equals",
805
- value: "web",
806
- },
807
- {
808
- or: [
809
- {
810
- and: [
811
- {
812
- attribute: "browser",
813
- operator: "equals",
814
- value: "chrome",
815
- },
816
- {
817
- attribute: "version",
818
- operator: "equals",
819
- value: "1.0",
820
- },
821
- ],
822
- },
823
- {
824
- attribute: "browser",
825
- operator: "equals",
826
- value: "firefox",
827
- },
828
- ],
829
- },
830
- ],
831
- },
832
- {
833
- platform: "web",
834
- browser: "chrome",
835
- version: "1.0",
836
- },
837
- ),
838
- ).toEqual({
839
- and: [
840
- {
841
- or: [
842
- {
843
- attribute: "browser",
844
- operator: "equals",
845
- value: "firefox",
846
- },
847
- ],
848
- },
849
- ],
850
- });
851
- });
852
-
853
- test("arrays with nested conditions", function () {
854
- // Array with AND condition (redundant "*" removed)
855
- expect(
856
- applyContextToConditions(
857
- featurevisor,
858
- [
859
- {
860
- attribute: "platform",
861
- operator: "equals",
862
- value: "web",
863
- },
864
- {
865
- and: [
866
- {
867
- attribute: "browser",
868
- operator: "equals",
869
- value: "chrome",
870
- },
871
- {
872
- attribute: "version",
873
- operator: "equals",
874
- value: "1.0",
875
- },
876
- ],
877
- },
878
- ],
879
- {
880
- platform: "web",
881
- browser: "chrome",
882
- version: "1.0",
883
- },
884
- ),
885
- ).toEqual("*");
886
-
887
- // Array with OR condition (redundant "*" removed, but OR structure remains if not all match)
888
- expect(
889
- applyContextToConditions(
890
- featurevisor,
891
- [
892
- {
893
- attribute: "platform",
894
- operator: "equals",
895
- value: "web",
896
- },
897
- {
898
- or: [
899
- {
900
- attribute: "browser",
901
- operator: "equals",
902
- value: "chrome",
903
- },
904
- {
905
- attribute: "browser",
906
- operator: "equals",
907
- value: "firefox",
908
- },
909
- ],
910
- },
911
- ],
912
- {
913
- platform: "web",
914
- browser: "chrome",
915
- },
916
- ),
917
- ).toEqual([
918
- {
919
- or: [
920
- {
921
- attribute: "browser",
922
- operator: "equals",
923
- value: "firefox",
924
- },
925
- ],
926
- },
927
- ]);
928
- });
929
-
930
- test("edge cases", function () {
931
- // Empty context
932
- expect(
933
- applyContextToConditions(
934
- featurevisor,
935
- {
936
- attribute: "platform",
937
- operator: "equals",
938
- value: "web",
939
- },
940
- {},
941
- ),
942
- ).toEqual({
943
- attribute: "platform",
944
- operator: "equals",
945
- value: "web",
946
- });
947
-
948
- // Empty array (all "*" becomes "*")
949
- expect(applyContextToConditions(featurevisor, [], {})).toEqual("*");
950
-
951
- // "*" in array with matching condition (all "*" becomes "*")
952
- expect(
953
- applyContextToConditions(
954
- featurevisor,
955
- [
956
- "*",
957
- {
958
- attribute: "platform",
959
- operator: "equals",
960
- value: "web",
961
- },
962
- ],
963
- {
964
- platform: "web",
965
- },
966
- ),
967
- ).toEqual("*");
968
-
969
- // AND with empty array (all "*" becomes "*")
970
- expect(
971
- applyContextToConditions(
972
- featurevisor,
973
- {
974
- and: [],
975
- },
976
- {},
977
- ),
978
- ).toEqual("*");
979
-
980
- // OR with empty array (all "*" becomes "*")
981
- expect(
982
- applyContextToConditions(
983
- featurevisor,
984
- {
985
- or: [],
986
- },
987
- {},
988
- ),
989
- ).toEqual("*");
990
- });
991
- });
992
-
993
- describe("applyContextToCondition (singular)", function () {
994
- test("applyContextToCondition is a function", function () {
995
- expect(applyContextToCondition).toBeInstanceOf(Function);
996
- });
997
-
998
- test("simple cases", function () {
999
- // "*" remains "*"
1000
- expect(applyContextToCondition(featurevisor, "*", {})).toEqual("*");
1001
-
1002
- // Plain condition that matches
1003
- expect(
1004
- applyContextToCondition(
1005
- featurevisor,
1006
- {
1007
- attribute: "platform",
1008
- operator: "equals",
1009
- value: "web",
1010
- },
1011
- {
1012
- platform: "web",
1013
- },
1014
- ),
1015
- ).toEqual("*");
1016
-
1017
- // Plain condition that doesn't match
1018
- expect(
1019
- applyContextToCondition(
1020
- featurevisor,
1021
- {
1022
- attribute: "platform",
1023
- operator: "equals",
1024
- value: "web",
1025
- },
1026
- {
1027
- platform: "mobile",
1028
- },
1029
- ),
1030
- ).toEqual({
1031
- attribute: "platform",
1032
- operator: "equals",
1033
- value: "web",
1034
- });
1035
-
1036
- // Array of conditions
1037
- const originalConditions: Condition[] = [
1038
- {
1039
- attribute: "platform",
1040
- operator: "equals",
1041
- value: "web",
1042
- },
1043
- {
1044
- attribute: "browser",
1045
- operator: "equals",
1046
- value: "chrome",
1047
- },
1048
- ];
1049
-
1050
- // Partial match
1051
- expect(
1052
- applyContextToCondition(featurevisor, originalConditions, {
1053
- platform: "web",
1054
- }),
1055
- ).toEqual([
1056
- "*",
1057
- {
1058
- attribute: "browser",
1059
- operator: "equals",
1060
- value: "chrome",
1061
- },
1062
- ]);
1063
-
1064
- // Full match
1065
- expect(
1066
- applyContextToCondition(featurevisor, originalConditions, {
1067
- platform: "web",
1068
- browser: "chrome",
1069
- }),
1070
- ).toEqual(["*", "*"]);
1071
-
1072
- // No match
1073
- expect(
1074
- applyContextToCondition(featurevisor, originalConditions, {
1075
- platform: "mobile",
1076
- browser: "safari",
1077
- }),
1078
- ).toEqual([
1079
- {
1080
- attribute: "platform",
1081
- operator: "equals",
1082
- value: "web",
1083
- },
1084
- {
1085
- attribute: "browser",
1086
- operator: "equals",
1087
- value: "chrome",
1088
- },
1089
- ]);
1090
- });
1091
-
1092
- test("AND conditions", function () {
1093
- // AND with all matching conditions
1094
- expect(
1095
- applyContextToCondition(
1096
- featurevisor,
1097
- {
1098
- and: [
1099
- {
1100
- attribute: "platform",
1101
- operator: "equals",
1102
- value: "web",
1103
- },
1104
- {
1105
- attribute: "browser",
1106
- operator: "equals",
1107
- value: "chrome",
1108
- },
1109
- ],
1110
- },
1111
- {
1112
- platform: "web",
1113
- browser: "chrome",
1114
- },
1115
- ),
1116
- ).toEqual({
1117
- and: ["*", "*"],
1118
- });
1119
-
1120
- // AND with partial match
1121
- expect(
1122
- applyContextToCondition(
1123
- featurevisor,
1124
- {
1125
- and: [
1126
- {
1127
- attribute: "platform",
1128
- operator: "equals",
1129
- value: "web",
1130
- },
1131
- {
1132
- attribute: "browser",
1133
- operator: "equals",
1134
- value: "chrome",
1135
- },
1136
- ],
1137
- },
1138
- {
1139
- platform: "web",
1140
- },
1141
- ),
1142
- ).toEqual({
1143
- and: [
1144
- "*",
1145
- {
1146
- attribute: "browser",
1147
- operator: "equals",
1148
- value: "chrome",
1149
- },
1150
- ],
1151
- });
1152
-
1153
- // AND with no matches
1154
- expect(
1155
- applyContextToCondition(
1156
- featurevisor,
1157
- {
1158
- and: [
1159
- {
1160
- attribute: "platform",
1161
- operator: "equals",
1162
- value: "web",
1163
- },
1164
- {
1165
- attribute: "browser",
1166
- operator: "equals",
1167
- value: "chrome",
1168
- },
1169
- ],
1170
- },
1171
- {
1172
- platform: "mobile",
1173
- browser: "safari",
1174
- },
1175
- ),
1176
- ).toEqual({
1177
- and: [
1178
- {
1179
- attribute: "platform",
1180
- operator: "equals",
1181
- value: "web",
1182
- },
1183
- {
1184
- attribute: "browser",
1185
- operator: "equals",
1186
- value: "chrome",
1187
- },
1188
- ],
1189
- });
1190
-
1191
- // AND with "*" in it
1192
- expect(
1193
- applyContextToCondition(
1194
- featurevisor,
1195
- {
1196
- and: [
1197
- "*",
1198
- {
1199
- attribute: "platform",
1200
- operator: "equals",
1201
- value: "web",
1202
- },
1203
- ],
1204
- },
1205
- {
1206
- platform: "web",
1207
- },
1208
- ),
1209
- ).toEqual({
1210
- and: ["*", "*"],
1211
- });
1212
- });
1213
-
1214
- test("OR conditions", function () {
1215
- // OR with all matching conditions
1216
- expect(
1217
- applyContextToCondition(
1218
- featurevisor,
1219
- {
1220
- or: [
1221
- {
1222
- attribute: "platform",
1223
- operator: "equals",
1224
- value: "web",
1225
- },
1226
- {
1227
- attribute: "platform",
1228
- operator: "equals",
1229
- value: "mobile",
1230
- },
1231
- ],
1232
- },
1233
- {
1234
- platform: "web",
1235
- },
1236
- ),
1237
- ).toEqual({
1238
- or: ["*", { attribute: "platform", operator: "equals", value: "mobile" }],
1239
- });
1240
-
1241
- // OR with partial match (first matches)
1242
- expect(
1243
- applyContextToCondition(
1244
- featurevisor,
1245
- {
1246
- or: [
1247
- {
1248
- attribute: "platform",
1249
- operator: "equals",
1250
- value: "web",
1251
- },
1252
- {
1253
- attribute: "browser",
1254
- operator: "equals",
1255
- value: "chrome",
1256
- },
1257
- ],
1258
- },
1259
- {
1260
- platform: "web",
1261
- },
1262
- ),
1263
- ).toEqual({
1264
- or: [
1265
- "*",
1266
- {
1267
- attribute: "browser",
1268
- operator: "equals",
1269
- value: "chrome",
1270
- },
1271
- ],
1272
- });
1273
-
1274
- // OR with no matches
1275
- expect(
1276
- applyContextToCondition(
1277
- featurevisor,
1278
- {
1279
- or: [
1280
- {
1281
- attribute: "platform",
1282
- operator: "equals",
1283
- value: "web",
1284
- },
1285
- {
1286
- attribute: "platform",
1287
- operator: "equals",
1288
- value: "mobile",
1289
- },
1290
- ],
1291
- },
1292
- {
1293
- platform: "desktop",
1294
- },
1295
- ),
1296
- ).toEqual({
1297
- or: [
1298
- {
1299
- attribute: "platform",
1300
- operator: "equals",
1301
- value: "web",
1302
- },
1303
- {
1304
- attribute: "platform",
1305
- operator: "equals",
1306
- value: "mobile",
1307
- },
1308
- ],
1309
- });
1310
- });
1311
-
1312
- test("NOT conditions", function () {
1313
- // NOT with matching condition (should not match)
1314
- expect(
1315
- applyContextToCondition(
1316
- featurevisor,
1317
- {
1318
- not: [
1319
- {
1320
- attribute: "platform",
1321
- operator: "equals",
1322
- value: "web",
1323
- },
1324
- ],
1325
- },
1326
- {
1327
- platform: "web",
1328
- },
1329
- ),
1330
- ).toEqual({
1331
- not: ["*"],
1332
- });
1333
-
1334
- // NOT with non-matching condition
1335
- expect(
1336
- applyContextToCondition(
1337
- featurevisor,
1338
- {
1339
- not: [
1340
- {
1341
- attribute: "platform",
1342
- operator: "equals",
1343
- value: "web",
1344
- },
1345
- ],
1346
- },
1347
- {
1348
- platform: "mobile",
1349
- },
1350
- ),
1351
- ).toEqual({
1352
- not: [
1353
- {
1354
- attribute: "platform",
1355
- operator: "equals",
1356
- value: "web",
1357
- },
1358
- ],
1359
- });
1360
-
1361
- // NOT with multiple conditions
1362
- expect(
1363
- applyContextToCondition(
1364
- featurevisor,
1365
- {
1366
- not: [
1367
- {
1368
- attribute: "platform",
1369
- operator: "equals",
1370
- value: "web",
1371
- },
1372
- {
1373
- attribute: "browser",
1374
- operator: "equals",
1375
- value: "chrome",
1376
- },
1377
- ],
1378
- },
1379
- {
1380
- platform: "web",
1381
- browser: "chrome",
1382
- },
1383
- ),
1384
- ).toEqual({
1385
- not: ["*", "*"],
1386
- });
1387
- });
1388
-
1389
- test("nested AND conditions", function () {
1390
- // Nested AND with all matching
1391
- expect(
1392
- applyContextToCondition(
1393
- featurevisor,
1394
- {
1395
- and: [
1396
- {
1397
- attribute: "platform",
1398
- operator: "equals",
1399
- value: "web",
1400
- },
1401
- {
1402
- and: [
1403
- {
1404
- attribute: "browser",
1405
- operator: "equals",
1406
- value: "chrome",
1407
- },
1408
- {
1409
- attribute: "version",
1410
- operator: "equals",
1411
- value: "1.0",
1412
- },
1413
- ],
1414
- },
1415
- ],
1416
- },
1417
- {
1418
- platform: "web",
1419
- browser: "chrome",
1420
- version: "1.0",
1421
- },
1422
- ),
1423
- ).toEqual({
1424
- and: [
1425
- "*",
1426
- {
1427
- and: ["*", "*"],
1428
- },
1429
- ],
1430
- });
1431
-
1432
- // Nested AND with partial match
1433
- expect(
1434
- applyContextToCondition(
1435
- featurevisor,
1436
- {
1437
- and: [
1438
- {
1439
- attribute: "platform",
1440
- operator: "equals",
1441
- value: "web",
1442
- },
1443
- {
1444
- and: [
1445
- {
1446
- attribute: "browser",
1447
- operator: "equals",
1448
- value: "chrome",
1449
- },
1450
- {
1451
- attribute: "version",
1452
- operator: "equals",
1453
- value: "1.0",
1454
- },
1455
- ],
1456
- },
1457
- ],
1458
- },
1459
- {
1460
- platform: "web",
1461
- browser: "chrome",
1462
- },
1463
- ),
1464
- ).toEqual({
1465
- and: [
1466
- "*",
1467
- {
1468
- and: [
1469
- "*",
1470
- {
1471
- attribute: "version",
1472
- operator: "equals",
1473
- value: "1.0",
1474
- },
1475
- ],
1476
- },
1477
- ],
1478
- });
1479
- });
1480
-
1481
- test("nested OR conditions", function () {
1482
- // Nested OR with matching
1483
- expect(
1484
- applyContextToCondition(
1485
- featurevisor,
1486
- {
1487
- or: [
1488
- {
1489
- attribute: "platform",
1490
- operator: "equals",
1491
- value: "web",
1492
- },
1493
- {
1494
- or: [
1495
- {
1496
- attribute: "platform",
1497
- operator: "equals",
1498
- value: "mobile",
1499
- },
1500
- {
1501
- attribute: "platform",
1502
- operator: "equals",
1503
- value: "desktop",
1504
- },
1505
- ],
1506
- },
1507
- ],
1508
- },
1509
- {
1510
- platform: "web",
1511
- },
1512
- ),
1513
- ).toEqual({
1514
- or: [
1515
- "*",
1516
- {
1517
- or: [
1518
- {
1519
- attribute: "platform",
1520
- operator: "equals",
1521
- value: "mobile",
1522
- },
1523
- {
1524
- attribute: "platform",
1525
- operator: "equals",
1526
- value: "desktop",
1527
- },
1528
- ],
1529
- },
1530
- ],
1531
- });
1532
-
1533
- // Nested OR with inner match
1534
- expect(
1535
- applyContextToCondition(
1536
- featurevisor,
1537
- {
1538
- or: [
1539
- {
1540
- attribute: "platform",
1541
- operator: "equals",
1542
- value: "web",
1543
- },
1544
- {
1545
- or: [
1546
- {
1547
- attribute: "platform",
1548
- operator: "equals",
1549
- value: "mobile",
1550
- },
1551
- {
1552
- attribute: "platform",
1553
- operator: "equals",
1554
- value: "desktop",
1555
- },
1556
- ],
1557
- },
1558
- ],
1559
- },
1560
- {
1561
- platform: "mobile",
1562
- },
1563
- ),
1564
- ).toEqual({
1565
- or: [
1566
- {
1567
- attribute: "platform",
1568
- operator: "equals",
1569
- value: "web",
1570
- },
1571
- {
1572
- or: [
1573
- "*",
1574
- {
1575
- attribute: "platform",
1576
- operator: "equals",
1577
- value: "desktop",
1578
- },
1579
- ],
1580
- },
1581
- ],
1582
- });
1583
- });
1584
-
1585
- test("nested NOT conditions", function () {
1586
- // Nested NOT
1587
- expect(
1588
- applyContextToCondition(
1589
- featurevisor,
1590
- {
1591
- not: [
1592
- {
1593
- attribute: "platform",
1594
- operator: "equals",
1595
- value: "web",
1596
- },
1597
- {
1598
- not: [
1599
- {
1600
- attribute: "browser",
1601
- operator: "equals",
1602
- value: "chrome",
1603
- },
1604
- ],
1605
- },
1606
- ],
1607
- },
1608
- {
1609
- platform: "web",
1610
- browser: "chrome",
1611
- },
1612
- ),
1613
- ).toEqual({
1614
- not: [
1615
- "*",
1616
- {
1617
- not: ["*"],
1618
- },
1619
- ],
1620
- });
1621
- });
1622
-
1623
- test("mixed nested conditions", function () {
1624
- // AND with nested OR
1625
- expect(
1626
- applyContextToCondition(
1627
- featurevisor,
1628
- {
1629
- and: [
1630
- {
1631
- attribute: "platform",
1632
- operator: "equals",
1633
- value: "web",
1634
- },
1635
- {
1636
- or: [
1637
- {
1638
- attribute: "browser",
1639
- operator: "equals",
1640
- value: "chrome",
1641
- },
1642
- {
1643
- attribute: "browser",
1644
- operator: "equals",
1645
- value: "firefox",
1646
- },
1647
- ],
1648
- },
1649
- ],
1650
- },
1651
- {
1652
- platform: "web",
1653
- browser: "chrome",
1654
- },
1655
- ),
1656
- ).toEqual({
1657
- and: [
1658
- "*",
1659
- {
1660
- or: [
1661
- "*",
1662
- {
1663
- attribute: "browser",
1664
- operator: "equals",
1665
- value: "firefox",
1666
- },
1667
- ],
1668
- },
1669
- ],
1670
- });
1671
-
1672
- // OR with nested AND
1673
- expect(
1674
- applyContextToCondition(
1675
- featurevisor,
1676
- {
1677
- or: [
1678
- {
1679
- attribute: "platform",
1680
- operator: "equals",
1681
- value: "web",
1682
- },
1683
- {
1684
- and: [
1685
- {
1686
- attribute: "platform",
1687
- operator: "equals",
1688
- value: "mobile",
1689
- },
1690
- {
1691
- attribute: "browser",
1692
- operator: "equals",
1693
- value: "chrome",
1694
- },
1695
- ],
1696
- },
1697
- ],
1698
- },
1699
- {
1700
- platform: "mobile",
1701
- browser: "chrome",
1702
- },
1703
- ),
1704
- ).toEqual({
1705
- or: [
1706
- {
1707
- attribute: "platform",
1708
- operator: "equals",
1709
- value: "web",
1710
- },
1711
- {
1712
- and: ["*", "*"],
1713
- },
1714
- ],
1715
- });
1716
-
1717
- // AND with nested NOT
1718
- expect(
1719
- applyContextToCondition(
1720
- featurevisor,
1721
- {
1722
- and: [
1723
- {
1724
- attribute: "platform",
1725
- operator: "equals",
1726
- value: "web",
1727
- },
1728
- {
1729
- not: [
1730
- {
1731
- attribute: "browser",
1732
- operator: "equals",
1733
- value: "safari",
1734
- },
1735
- ],
1736
- },
1737
- ],
1738
- },
1739
- {
1740
- platform: "web",
1741
- browser: "chrome",
1742
- },
1743
- ),
1744
- ).toEqual({
1745
- and: [
1746
- "*",
1747
- {
1748
- not: [
1749
- {
1750
- attribute: "browser",
1751
- operator: "equals",
1752
- value: "safari",
1753
- },
1754
- ],
1755
- },
1756
- ],
1757
- });
1758
-
1759
- // Complex nested structure
1760
- expect(
1761
- applyContextToCondition(
1762
- featurevisor,
1763
- {
1764
- and: [
1765
- {
1766
- attribute: "platform",
1767
- operator: "equals",
1768
- value: "web",
1769
- },
1770
- {
1771
- or: [
1772
- {
1773
- and: [
1774
- {
1775
- attribute: "browser",
1776
- operator: "equals",
1777
- value: "chrome",
1778
- },
1779
- {
1780
- attribute: "version",
1781
- operator: "equals",
1782
- value: "1.0",
1783
- },
1784
- ],
1785
- },
1786
- {
1787
- attribute: "browser",
1788
- operator: "equals",
1789
- value: "firefox",
1790
- },
1791
- ],
1792
- },
1793
- ],
1794
- },
1795
- {
1796
- platform: "web",
1797
- browser: "chrome",
1798
- version: "1.0",
1799
- },
1800
- ),
1801
- ).toEqual({
1802
- and: [
1803
- "*",
1804
- {
1805
- or: [
1806
- {
1807
- and: ["*", "*"],
1808
- },
1809
- {
1810
- attribute: "browser",
1811
- operator: "equals",
1812
- value: "firefox",
1813
- },
1814
- ],
1815
- },
1816
- ],
1817
- });
1818
- });
1819
-
1820
- test("arrays with nested conditions", function () {
1821
- // Array with AND condition
1822
- expect(
1823
- applyContextToCondition(
1824
- featurevisor,
1825
- [
1826
- {
1827
- attribute: "platform",
1828
- operator: "equals",
1829
- value: "web",
1830
- },
1831
- {
1832
- and: [
1833
- {
1834
- attribute: "browser",
1835
- operator: "equals",
1836
- value: "chrome",
1837
- },
1838
- {
1839
- attribute: "version",
1840
- operator: "equals",
1841
- value: "1.0",
1842
- },
1843
- ],
1844
- },
1845
- ],
1846
- {
1847
- platform: "web",
1848
- browser: "chrome",
1849
- version: "1.0",
1850
- },
1851
- ),
1852
- ).toEqual([
1853
- "*",
1854
- {
1855
- and: ["*", "*"],
1856
- },
1857
- ]);
1858
-
1859
- // Array with OR condition
1860
- expect(
1861
- applyContextToCondition(
1862
- featurevisor,
1863
- [
1864
- {
1865
- attribute: "platform",
1866
- operator: "equals",
1867
- value: "web",
1868
- },
1869
- {
1870
- or: [
1871
- {
1872
- attribute: "browser",
1873
- operator: "equals",
1874
- value: "chrome",
1875
- },
1876
- {
1877
- attribute: "browser",
1878
- operator: "equals",
1879
- value: "firefox",
1880
- },
1881
- ],
1882
- },
1883
- ],
1884
- {
1885
- platform: "web",
1886
- browser: "chrome",
1887
- },
1888
- ),
1889
- ).toEqual([
1890
- "*",
1891
- {
1892
- or: [
1893
- "*",
1894
- {
1895
- attribute: "browser",
1896
- operator: "equals",
1897
- value: "firefox",
1898
- },
1899
- ],
1900
- },
1901
- ]);
1902
- });
1903
-
1904
- test("edge cases", function () {
1905
- // Empty context
1906
- expect(
1907
- applyContextToCondition(
1908
- featurevisor,
1909
- {
1910
- attribute: "platform",
1911
- operator: "equals",
1912
- value: "web",
1913
- },
1914
- {},
1915
- ),
1916
- ).toEqual({
1917
- attribute: "platform",
1918
- operator: "equals",
1919
- value: "web",
1920
- });
1921
-
1922
- // Empty array
1923
- expect(applyContextToCondition(featurevisor, [], {})).toEqual([]);
1924
-
1925
- // "*" in array
1926
- expect(
1927
- applyContextToCondition(
1928
- featurevisor,
1929
- [
1930
- "*",
1931
- {
1932
- attribute: "platform",
1933
- operator: "equals",
1934
- value: "web",
1935
- },
1936
- ],
1937
- {
1938
- platform: "web",
1939
- },
1940
- ),
1941
- ).toEqual(["*", "*"]);
1942
-
1943
- // AND with empty array
1944
- expect(
1945
- applyContextToCondition(
1946
- featurevisor,
1947
- {
1948
- and: [],
1949
- },
1950
- {},
1951
- ),
1952
- ).toEqual({
1953
- and: [],
1954
- });
1955
-
1956
- // OR with empty array
1957
- expect(
1958
- applyContextToCondition(
1959
- featurevisor,
1960
- {
1961
- or: [],
1962
- },
1963
- {},
1964
- ),
1965
- ).toEqual({
1966
- or: [],
1967
- });
1968
- });
1969
- });
1970
-
1971
- describe("removeRedundantConditions", function () {
1972
- test("removeRedundantConditions is a function", function () {
1973
- expect(removeRedundantConditions).toBeInstanceOf(Function);
1974
- });
1975
-
1976
- test("simple cases", function () {
1977
- // * is *
1978
- expect(removeRedundantConditions("*")).toEqual("*");
1979
-
1980
- // [*, *, *] is "*"
1981
- expect(removeRedundantConditions(["*", "*", "*"])).toEqual("*");
1982
-
1983
- // Array with mixed conditions
1984
- expect(
1985
- removeRedundantConditions([
1986
- "*",
1987
- {
1988
- attribute: "platform",
1989
- operator: "equals",
1990
- value: "web",
1991
- },
1992
- "*",
1993
- {
1994
- attribute: "browser",
1995
- operator: "equals",
1996
- value: "chrome",
1997
- },
1998
- ]),
1999
- ).toEqual([
2000
- {
2001
- attribute: "platform",
2002
- operator: "equals",
2003
- value: "web",
2004
- },
2005
- {
2006
- attribute: "browser",
2007
- operator: "equals",
2008
- value: "chrome",
2009
- },
2010
- ]);
2011
- });
2012
-
2013
- test("AND conditions", function () {
2014
- // All "*" in AND
2015
- expect(removeRedundantConditions({ and: ["*", "*", "*"] })).toEqual("*");
2016
-
2017
- // Mixed with "*" in AND
2018
- expect(
2019
- removeRedundantConditions({
2020
- and: [
2021
- "*",
2022
- {
2023
- attribute: "platform",
2024
- operator: "equals",
2025
- value: "web",
2026
- },
2027
- "*",
2028
- ],
2029
- }),
2030
- ).toEqual({
2031
- and: [
2032
- {
2033
- attribute: "platform",
2034
- operator: "equals",
2035
- value: "web",
2036
- },
2037
- ],
2038
- });
2039
-
2040
- // Multiple non-* conditions in AND
2041
- expect(
2042
- removeRedundantConditions({
2043
- and: [
2044
- "*",
2045
- {
2046
- attribute: "platform",
2047
- operator: "equals",
2048
- value: "web",
2049
- },
2050
- "*",
2051
- {
2052
- attribute: "browser",
2053
- operator: "equals",
2054
- value: "chrome",
2055
- },
2056
- "*",
2057
- ],
2058
- }),
2059
- ).toEqual({
2060
- and: [
2061
- {
2062
- attribute: "platform",
2063
- operator: "equals",
2064
- value: "web",
2065
- },
2066
- {
2067
- attribute: "browser",
2068
- operator: "equals",
2069
- value: "chrome",
2070
- },
2071
- ],
2072
- });
2073
- });
2074
-
2075
- test("OR conditions", function () {
2076
- // All "*" in OR
2077
- expect(removeRedundantConditions({ or: ["*", "*", "*"] })).toEqual("*");
2078
-
2079
- // Mixed with "*" in OR
2080
- expect(
2081
- removeRedundantConditions({
2082
- or: [
2083
- "*",
2084
- {
2085
- attribute: "platform",
2086
- operator: "equals",
2087
- value: "web",
2088
- },
2089
- "*",
2090
- ],
2091
- }),
2092
- ).toEqual({
2093
- or: [
2094
- {
2095
- attribute: "platform",
2096
- operator: "equals",
2097
- value: "web",
2098
- },
2099
- ],
2100
- });
2101
-
2102
- // Multiple non-* conditions in OR
2103
- expect(
2104
- removeRedundantConditions({
2105
- or: [
2106
- "*",
2107
- {
2108
- attribute: "platform",
2109
- operator: "equals",
2110
- value: "web",
2111
- },
2112
- "*",
2113
- {
2114
- attribute: "platform",
2115
- operator: "equals",
2116
- value: "mobile",
2117
- },
2118
- "*",
2119
- ],
2120
- }),
2121
- ).toEqual({
2122
- or: [
2123
- {
2124
- attribute: "platform",
2125
- operator: "equals",
2126
- value: "web",
2127
- },
2128
- {
2129
- attribute: "platform",
2130
- operator: "equals",
2131
- value: "mobile",
2132
- },
2133
- ],
2134
- });
2135
- });
2136
-
2137
- test("NOT conditions", function () {
2138
- // All "*" in NOT
2139
- expect(removeRedundantConditions({ not: ["*", "*", "*"] })).toEqual({
2140
- not: ["*"],
2141
- });
2142
-
2143
- // Mixed with "*" in NOT
2144
- expect(
2145
- removeRedundantConditions({
2146
- not: [
2147
- "*",
2148
- {
2149
- attribute: "platform",
2150
- operator: "equals",
2151
- value: "web",
2152
- },
2153
- "*",
2154
- ],
2155
- }),
2156
- ).toEqual({
2157
- not: [
2158
- {
2159
- attribute: "platform",
2160
- operator: "equals",
2161
- value: "web",
2162
- },
2163
- ],
2164
- });
2165
- });
2166
-
2167
- test("nested AND conditions", function () {
2168
- // Nested AND with all "*"
2169
- expect(
2170
- removeRedundantConditions({
2171
- and: [
2172
- "*",
2173
- {
2174
- and: ["*", "*", "*"],
2175
- },
2176
- "*",
2177
- ],
2178
- }),
2179
- ).toEqual("*");
2180
-
2181
- // Nested AND with mixed conditions
2182
- expect(
2183
- removeRedundantConditions({
2184
- and: [
2185
- "*",
2186
- {
2187
- attribute: "platform",
2188
- operator: "equals",
2189
- value: "web",
2190
- },
2191
- {
2192
- and: [
2193
- "*",
2194
- {
2195
- attribute: "browser",
2196
- operator: "equals",
2197
- value: "chrome",
2198
- },
2199
- "*",
2200
- ],
2201
- },
2202
- "*",
2203
- ],
2204
- }),
2205
- ).toEqual({
2206
- and: [
2207
- {
2208
- attribute: "platform",
2209
- operator: "equals",
2210
- value: "web",
2211
- },
2212
- {
2213
- and: [
2214
- {
2215
- attribute: "browser",
2216
- operator: "equals",
2217
- value: "chrome",
2218
- },
2219
- ],
2220
- },
2221
- ],
2222
- });
2223
-
2224
- // Deeply nested AND
2225
- expect(
2226
- removeRedundantConditions({
2227
- and: [
2228
- "*",
2229
- {
2230
- and: [
2231
- "*",
2232
- {
2233
- and: ["*", "*", "*"],
2234
- },
2235
- "*",
2236
- ],
2237
- },
2238
- "*",
2239
- ],
2240
- }),
2241
- ).toEqual("*");
2242
- });
2243
-
2244
- test("nested OR conditions", function () {
2245
- // Nested OR with all "*"
2246
- expect(
2247
- removeRedundantConditions({
2248
- or: [
2249
- "*",
2250
- {
2251
- or: ["*", "*", "*"],
2252
- },
2253
- "*",
2254
- ],
2255
- }),
2256
- ).toEqual("*");
2257
-
2258
- // Nested OR with mixed conditions
2259
- expect(
2260
- removeRedundantConditions({
2261
- or: [
2262
- "*",
2263
- {
2264
- attribute: "platform",
2265
- operator: "equals",
2266
- value: "web",
2267
- },
2268
- {
2269
- or: [
2270
- "*",
2271
- {
2272
- attribute: "platform",
2273
- operator: "equals",
2274
- value: "mobile",
2275
- },
2276
- "*",
2277
- ],
2278
- },
2279
- "*",
2280
- ],
2281
- }),
2282
- ).toEqual({
2283
- or: [
2284
- {
2285
- attribute: "platform",
2286
- operator: "equals",
2287
- value: "web",
2288
- },
2289
- {
2290
- or: [
2291
- {
2292
- attribute: "platform",
2293
- operator: "equals",
2294
- value: "mobile",
2295
- },
2296
- ],
2297
- },
2298
- ],
2299
- });
2300
- });
2301
-
2302
- test("nested NOT conditions", function () {
2303
- // Nested NOT with all "*"
2304
- expect(
2305
- removeRedundantConditions({
2306
- not: [
2307
- "*",
2308
- {
2309
- not: ["*", "*", "*"],
2310
- },
2311
- "*",
2312
- ],
2313
- }),
2314
- ).toEqual({
2315
- not: [
2316
- {
2317
- not: ["*"],
2318
- },
2319
- ],
2320
- });
2321
-
2322
- // Nested NOT with mixed conditions
2323
- expect(
2324
- removeRedundantConditions({
2325
- not: [
2326
- "*",
2327
- {
2328
- attribute: "platform",
2329
- operator: "equals",
2330
- value: "web",
2331
- },
2332
- {
2333
- not: [
2334
- "*",
2335
- {
2336
- attribute: "browser",
2337
- operator: "equals",
2338
- value: "chrome",
2339
- },
2340
- "*",
2341
- ],
2342
- },
2343
- "*",
2344
- ],
2345
- }),
2346
- ).toEqual({
2347
- not: [
2348
- {
2349
- attribute: "platform",
2350
- operator: "equals",
2351
- value: "web",
2352
- },
2353
- {
2354
- not: [
2355
- {
2356
- attribute: "browser",
2357
- operator: "equals",
2358
- value: "chrome",
2359
- },
2360
- ],
2361
- },
2362
- ],
2363
- });
2364
- });
2365
-
2366
- test("mixed nested conditions", function () {
2367
- // AND with nested OR
2368
- expect(
2369
- removeRedundantConditions({
2370
- and: [
2371
- "*",
2372
- {
2373
- attribute: "platform",
2374
- operator: "equals",
2375
- value: "web",
2376
- },
2377
- {
2378
- or: [
2379
- "*",
2380
- {
2381
- attribute: "browser",
2382
- operator: "equals",
2383
- value: "chrome",
2384
- },
2385
- "*",
2386
- ],
2387
- },
2388
- "*",
2389
- ],
2390
- }),
2391
- ).toEqual({
2392
- and: [
2393
- {
2394
- attribute: "platform",
2395
- operator: "equals",
2396
- value: "web",
2397
- },
2398
- {
2399
- or: [
2400
- {
2401
- attribute: "browser",
2402
- operator: "equals",
2403
- value: "chrome",
2404
- },
2405
- ],
2406
- },
2407
- ],
2408
- });
2409
-
2410
- // OR with nested AND
2411
- expect(
2412
- removeRedundantConditions({
2413
- or: [
2414
- "*",
2415
- {
2416
- attribute: "platform",
2417
- operator: "equals",
2418
- value: "web",
2419
- },
2420
- {
2421
- and: [
2422
- "*",
2423
- {
2424
- attribute: "browser",
2425
- operator: "equals",
2426
- value: "chrome",
2427
- },
2428
- "*",
2429
- ],
2430
- },
2431
- "*",
2432
- ],
2433
- }),
2434
- ).toEqual({
2435
- or: [
2436
- {
2437
- attribute: "platform",
2438
- operator: "equals",
2439
- value: "web",
2440
- },
2441
- {
2442
- and: [
2443
- {
2444
- attribute: "browser",
2445
- operator: "equals",
2446
- value: "chrome",
2447
- },
2448
- ],
2449
- },
2450
- ],
2451
- });
2452
-
2453
- // AND with nested NOT
2454
- expect(
2455
- removeRedundantConditions({
2456
- and: [
2457
- "*",
2458
- {
2459
- attribute: "platform",
2460
- operator: "equals",
2461
- value: "web",
2462
- },
2463
- {
2464
- not: [
2465
- "*",
2466
- {
2467
- attribute: "browser",
2468
- operator: "equals",
2469
- value: "safari",
2470
- },
2471
- "*",
2472
- ],
2473
- },
2474
- "*",
2475
- ],
2476
- }),
2477
- ).toEqual({
2478
- and: [
2479
- {
2480
- attribute: "platform",
2481
- operator: "equals",
2482
- value: "web",
2483
- },
2484
- {
2485
- not: [
2486
- {
2487
- attribute: "browser",
2488
- operator: "equals",
2489
- value: "safari",
2490
- },
2491
- ],
2492
- },
2493
- ],
2494
- });
2495
-
2496
- // Complex nested structure
2497
- expect(
2498
- removeRedundantConditions({
2499
- and: [
2500
- "*",
2501
- {
2502
- attribute: "platform",
2503
- operator: "equals",
2504
- value: "web",
2505
- },
2506
- {
2507
- or: [
2508
- "*",
2509
- {
2510
- and: [
2511
- "*",
2512
- {
2513
- attribute: "browser",
2514
- operator: "equals",
2515
- value: "chrome",
2516
- },
2517
- "*",
2518
- ],
2519
- },
2520
- "*",
2521
- ],
2522
- },
2523
- "*",
2524
- ],
2525
- }),
2526
- ).toEqual({
2527
- and: [
2528
- {
2529
- attribute: "platform",
2530
- operator: "equals",
2531
- value: "web",
2532
- },
2533
- {
2534
- or: [
2535
- {
2536
- and: [
2537
- {
2538
- attribute: "browser",
2539
- operator: "equals",
2540
- value: "chrome",
2541
- },
2542
- ],
2543
- },
2544
- ],
2545
- },
2546
- ],
2547
- });
2548
- });
2549
-
2550
- test("arrays with nested conditions", function () {
2551
- // Array with nested AND
2552
- expect(
2553
- removeRedundantConditions([
2554
- "*",
2555
- {
2556
- and: [
2557
- "*",
2558
- {
2559
- attribute: "platform",
2560
- operator: "equals",
2561
- value: "web",
2562
- },
2563
- "*",
2564
- ],
2565
- },
2566
- "*",
2567
- ]),
2568
- ).toEqual([
2569
- {
2570
- and: [
2571
- {
2572
- attribute: "platform",
2573
- operator: "equals",
2574
- value: "web",
2575
- },
2576
- ],
2577
- },
2578
- ]);
2579
-
2580
- // Array with multiple nested conditions
2581
- expect(
2582
- removeRedundantConditions([
2583
- "*",
2584
- {
2585
- attribute: "platform",
2586
- operator: "equals",
2587
- value: "web",
2588
- },
2589
- {
2590
- or: [
2591
- "*",
2592
- {
2593
- attribute: "browser",
2594
- operator: "equals",
2595
- value: "chrome",
2596
- },
2597
- "*",
2598
- ],
2599
- },
2600
- "*",
2601
- ]),
2602
- ).toEqual([
2603
- {
2604
- attribute: "platform",
2605
- operator: "equals",
2606
- value: "web",
2607
- },
2608
- {
2609
- or: [
2610
- {
2611
- attribute: "browser",
2612
- operator: "equals",
2613
- value: "chrome",
2614
- },
2615
- ],
2616
- },
2617
- ]);
2618
- });
2619
-
2620
- test("edge cases", function () {
2621
- // Single condition after filtering
2622
- expect(
2623
- removeRedundantConditions({
2624
- and: [
2625
- "*",
2626
- {
2627
- attribute: "platform",
2628
- operator: "equals",
2629
- value: "web",
2630
- },
2631
- "*",
2632
- ],
2633
- }),
2634
- ).toEqual({
2635
- and: [
2636
- {
2637
- attribute: "platform",
2638
- operator: "equals",
2639
- value: "web",
2640
- },
2641
- ],
2642
- });
2643
-
2644
- // Plain condition object (no change)
2645
- expect(
2646
- removeRedundantConditions({
2647
- attribute: "platform",
2648
- operator: "equals",
2649
- value: "web",
2650
- }),
2651
- ).toEqual({
2652
- attribute: "platform",
2653
- operator: "equals",
2654
- value: "web",
2655
- });
2656
-
2657
- // Array with single non-* condition
2658
- expect(
2659
- removeRedundantConditions([
2660
- "*",
2661
- {
2662
- attribute: "platform",
2663
- operator: "equals",
2664
- value: "web",
2665
- },
2666
- "*",
2667
- ]),
2668
- ).toEqual([
2669
- {
2670
- attribute: "platform",
2671
- operator: "equals",
2672
- value: "web",
2673
- },
2674
- ]);
2675
- });
2676
- });
2677
- });