@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,1593 +0,0 @@
1
- import type { DatafileContent } from "@featurevisor/types";
2
-
3
- import {
4
- applyContextToSegments,
5
- removeRedundantGroupSegments,
6
- applyContextToGroupSegments,
7
- } from "./applyContextToSegments";
8
- import { createFeaturevisor } from "@featurevisor/sdk";
9
-
10
- describe("core: applyContextToSegments", function () {
11
- describe("applyContextToSegments", function () {
12
- const datafileWithSegments: DatafileContent = {
13
- schemaVersion: "2",
14
- revision: "unknown",
15
- segments: {
16
- web: {
17
- conditions: [{ attribute: "platform", operator: "equals", value: "web" }],
18
- },
19
- mobile: {
20
- conditions: [{ attribute: "platform", operator: "equals", value: "mobile" }],
21
- },
22
- chrome: {
23
- conditions: [{ attribute: "browser", operator: "equals", value: "chrome" }],
24
- },
25
- safari: {
26
- conditions: [{ attribute: "browser", operator: "equals", value: "safari" }],
27
- },
28
- premium: {
29
- conditions: [
30
- {
31
- and: [
32
- { attribute: "tier", operator: "equals", value: "premium" },
33
- { attribute: "status", operator: "equals", value: "active" },
34
- ],
35
- },
36
- ],
37
- },
38
- us: {
39
- conditions: [{ attribute: "country", operator: "equals", value: "us" }],
40
- },
41
- eu: {
42
- conditions: [{ attribute: "country", operator: "equals", value: "eu" }],
43
- },
44
- },
45
- features: {},
46
- };
47
-
48
- const featurevisorWithSegments = createFeaturevisor({
49
- datafile: datafileWithSegments,
50
- logLevel: "fatal",
51
- });
52
-
53
- test("applyContextToSegments is a function", function () {
54
- expect(applyContextToSegments).toBeInstanceOf(Function);
55
- });
56
-
57
- test("simple cases", function () {
58
- // "*" remains "*"
59
- expect(applyContextToSegments(featurevisorWithSegments, "*", {})).toEqual("*");
60
-
61
- // Plain segment that matches
62
- expect(
63
- applyContextToSegments(featurevisorWithSegments, "web", {
64
- platform: "web",
65
- }),
66
- ).toEqual("*");
67
-
68
- // Plain segment that doesn't match
69
- expect(
70
- applyContextToSegments(featurevisorWithSegments, "web", {
71
- platform: "mobile",
72
- }),
73
- ).toEqual("web");
74
-
75
- // Array of segments - partial match (redundant "*" removed)
76
- expect(
77
- applyContextToSegments(featurevisorWithSegments, ["web", "chrome"], {
78
- platform: "web",
79
- }),
80
- ).toEqual(["chrome"]);
81
-
82
- // Array of segments - full match (all "*" becomes "*")
83
- expect(
84
- applyContextToSegments(featurevisorWithSegments, ["web", "chrome"], {
85
- platform: "web",
86
- browser: "chrome",
87
- }),
88
- ).toEqual("*");
89
-
90
- // Array of segments - no match
91
- expect(
92
- applyContextToSegments(featurevisorWithSegments, ["web", "chrome"], {
93
- platform: "mobile",
94
- browser: "safari",
95
- }),
96
- ).toEqual(["web", "chrome"]);
97
- });
98
-
99
- test("AND group segments", function () {
100
- // AND with all matching segments (all "*" becomes "*")
101
- expect(
102
- applyContextToSegments(
103
- featurevisorWithSegments,
104
- {
105
- and: ["web", "chrome"],
106
- },
107
- {
108
- platform: "web",
109
- browser: "chrome",
110
- },
111
- ),
112
- ).toEqual("*");
113
-
114
- // AND with partial match (redundant "*" removed)
115
- expect(
116
- applyContextToSegments(
117
- featurevisorWithSegments,
118
- {
119
- and: ["web", "chrome"],
120
- },
121
- {
122
- platform: "web",
123
- },
124
- ),
125
- ).toEqual({
126
- and: ["chrome"],
127
- });
128
-
129
- // AND with no matches
130
- expect(
131
- applyContextToSegments(
132
- featurevisorWithSegments,
133
- {
134
- and: ["web", "chrome"],
135
- },
136
- {
137
- platform: "mobile",
138
- browser: "safari",
139
- },
140
- ),
141
- ).toEqual({
142
- and: ["web", "chrome"],
143
- });
144
-
145
- // AND with "*" in it (all "*" becomes "*")
146
- expect(
147
- applyContextToSegments(
148
- featurevisorWithSegments,
149
- {
150
- and: ["*", "chrome"],
151
- },
152
- {
153
- browser: "chrome",
154
- },
155
- ),
156
- ).toEqual("*");
157
- });
158
-
159
- test("OR group segments", function () {
160
- // OR with all matching segments (all "*" becomes "*")
161
- expect(
162
- applyContextToSegments(
163
- featurevisorWithSegments,
164
- {
165
- or: ["web", "chrome"],
166
- },
167
- {
168
- platform: "web",
169
- browser: "chrome",
170
- },
171
- ),
172
- ).toEqual("*");
173
-
174
- // OR with partial match (redundant "*" removed, but OR structure remains)
175
- expect(
176
- applyContextToSegments(
177
- featurevisorWithSegments,
178
- {
179
- or: ["web", "chrome"],
180
- },
181
- {
182
- platform: "web",
183
- },
184
- ),
185
- ).toEqual({
186
- or: ["chrome"],
187
- });
188
-
189
- // OR with no matches
190
- expect(
191
- applyContextToSegments(
192
- featurevisorWithSegments,
193
- {
194
- or: ["web", "mobile"],
195
- },
196
- {
197
- platform: "desktop",
198
- },
199
- ),
200
- ).toEqual({
201
- or: ["web", "mobile"],
202
- });
203
- });
204
-
205
- test("NOT group segments", function () {
206
- // NOT with matching segment remains an always-false expression
207
- expect(
208
- applyContextToSegments(
209
- featurevisorWithSegments,
210
- {
211
- not: ["web"],
212
- },
213
- {
214
- platform: "web",
215
- },
216
- ),
217
- ).toEqual({
218
- not: ["*"],
219
- });
220
-
221
- // NOT with non-matching segment
222
- expect(
223
- applyContextToSegments(
224
- featurevisorWithSegments,
225
- {
226
- not: ["web"],
227
- },
228
- {
229
- platform: "mobile",
230
- },
231
- ),
232
- ).toEqual({
233
- not: ["web"],
234
- });
235
-
236
- // NOT with multiple matching segments remains an always-false expression
237
- expect(
238
- applyContextToSegments(
239
- featurevisorWithSegments,
240
- {
241
- not: ["web", "chrome"],
242
- },
243
- {
244
- platform: "web",
245
- browser: "chrome",
246
- },
247
- ),
248
- ).toEqual({
249
- not: ["*"],
250
- });
251
- });
252
-
253
- test("nested AND group segments", function () {
254
- // Nested AND with all matching (all "*" becomes "*")
255
- expect(
256
- applyContextToSegments(
257
- featurevisorWithSegments,
258
- {
259
- and: [
260
- "web",
261
- {
262
- and: ["chrome", "us"],
263
- },
264
- ],
265
- },
266
- {
267
- platform: "web",
268
- browser: "chrome",
269
- country: "us",
270
- },
271
- ),
272
- ).toEqual("*");
273
-
274
- // Nested AND with partial match (redundant "*" removed)
275
- expect(
276
- applyContextToSegments(
277
- featurevisorWithSegments,
278
- {
279
- and: [
280
- "web",
281
- {
282
- and: ["chrome", "us"],
283
- },
284
- ],
285
- },
286
- {
287
- platform: "web",
288
- browser: "chrome",
289
- },
290
- ),
291
- ).toEqual({
292
- and: [
293
- {
294
- and: ["us"],
295
- },
296
- ],
297
- });
298
- });
299
-
300
- test("nested OR group segments", function () {
301
- // Nested OR with outer match (redundant "*" removed, but inner OR remains)
302
- expect(
303
- applyContextToSegments(
304
- featurevisorWithSegments,
305
- {
306
- or: [
307
- "web",
308
- {
309
- or: ["mobile", "chrome"],
310
- },
311
- ],
312
- },
313
- {
314
- platform: "web",
315
- },
316
- ),
317
- ).toEqual({
318
- or: [
319
- {
320
- or: ["mobile", "chrome"],
321
- },
322
- ],
323
- });
324
-
325
- // Nested OR with inner match (redundant "*" removed, but outer OR remains)
326
- expect(
327
- applyContextToSegments(
328
- featurevisorWithSegments,
329
- {
330
- or: [
331
- "web",
332
- {
333
- or: ["chrome", "safari"],
334
- },
335
- ],
336
- },
337
- {
338
- browser: "chrome",
339
- },
340
- ),
341
- ).toEqual({
342
- or: [
343
- "web",
344
- {
345
- or: ["safari"],
346
- },
347
- ],
348
- });
349
- });
350
-
351
- test("nested NOT group segments", function () {
352
- // Nested NOT preserves non-broadening false expressions
353
- expect(
354
- applyContextToSegments(
355
- featurevisorWithSegments,
356
- {
357
- not: [
358
- "web",
359
- {
360
- not: ["chrome"],
361
- },
362
- ],
363
- },
364
- {
365
- platform: "web",
366
- browser: "chrome",
367
- },
368
- ),
369
- ).toEqual({
370
- not: [
371
- {
372
- not: ["*"],
373
- },
374
- ],
375
- });
376
- });
377
-
378
- test("mixed nested group segments", function () {
379
- // AND with nested OR (redundant "*" removed, but OR structure remains if not all match)
380
- expect(
381
- applyContextToSegments(
382
- featurevisorWithSegments,
383
- {
384
- and: [
385
- "web",
386
- {
387
- or: ["chrome", "safari"],
388
- },
389
- ],
390
- },
391
- {
392
- platform: "web",
393
- browser: "chrome",
394
- },
395
- ),
396
- ).toEqual({
397
- and: [
398
- {
399
- or: ["safari"],
400
- },
401
- ],
402
- });
403
-
404
- // OR with nested AND (redundant "*" removed, but OR structure remains if not all match)
405
- expect(
406
- applyContextToSegments(
407
- featurevisorWithSegments,
408
- {
409
- or: [
410
- "web",
411
- {
412
- and: ["mobile", "chrome"],
413
- },
414
- ],
415
- },
416
- {
417
- platform: "mobile",
418
- browser: "chrome",
419
- },
420
- ),
421
- ).toEqual({
422
- or: ["web"],
423
- });
424
-
425
- // AND with nested NOT (redundant "*" removed)
426
- expect(
427
- applyContextToSegments(
428
- featurevisorWithSegments,
429
- {
430
- and: [
431
- "web",
432
- {
433
- not: ["mobile"],
434
- },
435
- ],
436
- },
437
- {
438
- platform: "web",
439
- },
440
- ),
441
- ).toEqual({
442
- and: [
443
- {
444
- not: ["mobile"],
445
- },
446
- ],
447
- });
448
-
449
- // Complex nested structure (redundant "*" removed)
450
- expect(
451
- applyContextToSegments(
452
- featurevisorWithSegments,
453
- {
454
- and: [
455
- "web",
456
- {
457
- or: [
458
- {
459
- and: ["chrome", "us"],
460
- },
461
- "safari",
462
- ],
463
- },
464
- ],
465
- },
466
- {
467
- platform: "web",
468
- browser: "chrome",
469
- country: "us",
470
- },
471
- ),
472
- ).toEqual({
473
- and: [
474
- {
475
- or: ["safari"],
476
- },
477
- ],
478
- });
479
- });
480
-
481
- test("arrays with nested group segments", function () {
482
- // Array with AND group segment (redundant "*" removed)
483
- expect(
484
- applyContextToSegments(
485
- featurevisorWithSegments,
486
- [
487
- "web",
488
- {
489
- and: ["chrome", "us"],
490
- },
491
- ],
492
- {
493
- platform: "web",
494
- browser: "chrome",
495
- country: "us",
496
- },
497
- ),
498
- ).toEqual("*");
499
-
500
- // Array with OR group segment (redundant "*" removed, but OR structure remains if not all match)
501
- expect(
502
- applyContextToSegments(
503
- featurevisorWithSegments,
504
- [
505
- "web",
506
- {
507
- or: ["chrome", "safari"],
508
- },
509
- ],
510
- {
511
- platform: "web",
512
- browser: "chrome",
513
- },
514
- ),
515
- ).toEqual([
516
- {
517
- or: ["safari"],
518
- },
519
- ]);
520
- });
521
-
522
- test("segments with complex conditions", function () {
523
- // Segment with AND conditions in its definition
524
- expect(
525
- applyContextToSegments(featurevisorWithSegments, "premium", {
526
- tier: "premium",
527
- status: "active",
528
- }),
529
- ).toEqual("*");
530
-
531
- // Segment with AND conditions that doesn't match
532
- expect(
533
- applyContextToSegments(featurevisorWithSegments, "premium", {
534
- tier: "premium",
535
- status: "inactive",
536
- }),
537
- ).toEqual("premium");
538
- });
539
-
540
- test("edge cases", function () {
541
- // Empty context
542
- expect(applyContextToSegments(featurevisorWithSegments, "web", {})).toEqual("web");
543
-
544
- // Empty array (all "*" becomes "*")
545
- expect(applyContextToSegments(featurevisorWithSegments, [], {})).toEqual("*");
546
-
547
- // "*" in array with matching segment (all "*" becomes "*")
548
- expect(
549
- applyContextToSegments(featurevisorWithSegments, ["*", "web"], {
550
- platform: "web",
551
- }),
552
- ).toEqual("*");
553
-
554
- // AND with empty array (all "*" becomes "*")
555
- expect(
556
- applyContextToSegments(
557
- featurevisorWithSegments,
558
- {
559
- and: [],
560
- },
561
- {},
562
- ),
563
- ).toEqual("*");
564
-
565
- // OR with empty array (all "*" becomes "*")
566
- expect(
567
- applyContextToSegments(
568
- featurevisorWithSegments,
569
- {
570
- or: [],
571
- },
572
- {},
573
- ),
574
- ).toEqual("*");
575
- });
576
- });
577
-
578
- describe("removeRedundantGroupSegments", function () {
579
- test("removeRedundantGroupSegments is a function", function () {
580
- expect(removeRedundantGroupSegments).toBeInstanceOf(Function);
581
- });
582
-
583
- test("simple cases", function () {
584
- // * is *
585
- expect(removeRedundantGroupSegments("*")).toEqual("*");
586
-
587
- // [*, *, *] is "*"
588
- expect(removeRedundantGroupSegments(["*", "*", "*"])).toEqual("*");
589
-
590
- // Array with mixed group segments
591
- expect(removeRedundantGroupSegments(["*", "web", "*", "chrome"])).toEqual(["web", "chrome"]);
592
- });
593
-
594
- test("AND group segments", function () {
595
- // All "*" in AND
596
- expect(removeRedundantGroupSegments({ and: ["*", "*", "*"] })).toEqual("*");
597
-
598
- // Mixed with "*" in AND
599
- expect(
600
- removeRedundantGroupSegments({
601
- and: ["*", "web", "*"],
602
- }),
603
- ).toEqual({
604
- and: ["web"],
605
- });
606
-
607
- // Multiple non-* group segments in AND
608
- expect(
609
- removeRedundantGroupSegments({
610
- and: ["*", "web", "*", "chrome", "*"],
611
- }),
612
- ).toEqual({
613
- and: ["web", "chrome"],
614
- });
615
- });
616
-
617
- test("OR group segments", function () {
618
- // All "*" in OR
619
- expect(removeRedundantGroupSegments({ or: ["*", "*", "*"] })).toEqual("*");
620
-
621
- // Mixed with "*" in OR
622
- expect(
623
- removeRedundantGroupSegments({
624
- or: ["*", "web", "*"],
625
- }),
626
- ).toEqual({
627
- or: ["web"],
628
- });
629
-
630
- // Multiple non-* group segments in OR
631
- expect(
632
- removeRedundantGroupSegments({
633
- or: ["*", "web", "*", "mobile", "*"],
634
- }),
635
- ).toEqual({
636
- or: ["web", "mobile"],
637
- });
638
- });
639
-
640
- test("NOT group segments", function () {
641
- // All "*" in NOT
642
- expect(removeRedundantGroupSegments({ not: ["*", "*", "*"] })).toEqual({
643
- not: ["*"],
644
- });
645
-
646
- // Mixed with "*" in NOT
647
- expect(
648
- removeRedundantGroupSegments({
649
- not: ["*", "web", "*"],
650
- }),
651
- ).toEqual({
652
- not: ["web"],
653
- });
654
- });
655
-
656
- test("nested AND group segments", function () {
657
- // Nested AND with all "*"
658
- expect(
659
- removeRedundantGroupSegments({
660
- and: ["*", { and: ["*", "*", "*"] }, "*"],
661
- }),
662
- ).toEqual("*");
663
-
664
- // Nested AND with mixed group segments
665
- expect(
666
- removeRedundantGroupSegments({
667
- and: [
668
- "*",
669
- "web",
670
- {
671
- and: ["*", "chrome", "*"],
672
- },
673
- "*",
674
- ],
675
- }),
676
- ).toEqual({
677
- and: [
678
- "web",
679
- {
680
- and: ["chrome"],
681
- },
682
- ],
683
- });
684
-
685
- // Deeply nested AND
686
- expect(
687
- removeRedundantGroupSegments({
688
- and: [
689
- "*",
690
- {
691
- and: [
692
- "*",
693
- {
694
- and: ["*", "*", "*"],
695
- },
696
- "*",
697
- ],
698
- },
699
- "*",
700
- ],
701
- }),
702
- ).toEqual("*");
703
- });
704
-
705
- test("nested OR group segments", function () {
706
- // Nested OR with all "*"
707
- expect(
708
- removeRedundantGroupSegments({
709
- or: ["*", { or: ["*", "*", "*"] }, "*"],
710
- }),
711
- ).toEqual("*");
712
-
713
- // Nested OR with mixed group segments
714
- expect(
715
- removeRedundantGroupSegments({
716
- or: [
717
- "*",
718
- "web",
719
- {
720
- or: ["*", "mobile", "*"],
721
- },
722
- "*",
723
- ],
724
- }),
725
- ).toEqual({
726
- or: [
727
- "web",
728
- {
729
- or: ["mobile"],
730
- },
731
- ],
732
- });
733
- });
734
-
735
- test("nested NOT group segments", function () {
736
- // Nested NOT with all "*"
737
- expect(
738
- removeRedundantGroupSegments({
739
- not: ["*", { not: ["*", "*", "*"] }, "*"],
740
- }),
741
- ).toEqual({
742
- not: [
743
- {
744
- not: ["*"],
745
- },
746
- ],
747
- });
748
-
749
- // Nested NOT with mixed group segments
750
- expect(
751
- removeRedundantGroupSegments({
752
- not: [
753
- "*",
754
- "web",
755
- {
756
- not: ["*", "chrome", "*"],
757
- },
758
- "*",
759
- ],
760
- }),
761
- ).toEqual({
762
- not: [
763
- "web",
764
- {
765
- not: ["chrome"],
766
- },
767
- ],
768
- });
769
- });
770
-
771
- test("mixed nested group segments", function () {
772
- // AND with nested OR
773
- expect(
774
- removeRedundantGroupSegments({
775
- and: [
776
- "*",
777
- "web",
778
- {
779
- or: ["*", "chrome", "*"],
780
- },
781
- "*",
782
- ],
783
- }),
784
- ).toEqual({
785
- and: [
786
- "web",
787
- {
788
- or: ["chrome"],
789
- },
790
- ],
791
- });
792
-
793
- // OR with nested AND
794
- expect(
795
- removeRedundantGroupSegments({
796
- or: [
797
- "*",
798
- "web",
799
- {
800
- and: ["*", "chrome", "*"],
801
- },
802
- "*",
803
- ],
804
- }),
805
- ).toEqual({
806
- or: [
807
- "web",
808
- {
809
- and: ["chrome"],
810
- },
811
- ],
812
- });
813
-
814
- // AND with nested NOT
815
- expect(
816
- removeRedundantGroupSegments({
817
- and: [
818
- "*",
819
- "web",
820
- {
821
- not: ["*", "mobile", "*"],
822
- },
823
- "*",
824
- ],
825
- }),
826
- ).toEqual({
827
- and: [
828
- "web",
829
- {
830
- not: ["mobile"],
831
- },
832
- ],
833
- });
834
-
835
- // Complex nested structure
836
- expect(
837
- removeRedundantGroupSegments({
838
- and: [
839
- "*",
840
- "web",
841
- {
842
- or: [
843
- "*",
844
- {
845
- and: ["*", "chrome", "*"],
846
- },
847
- "*",
848
- ],
849
- },
850
- "*",
851
- ],
852
- }),
853
- ).toEqual({
854
- and: [
855
- "web",
856
- {
857
- or: [
858
- {
859
- and: ["chrome"],
860
- },
861
- ],
862
- },
863
- ],
864
- });
865
- });
866
-
867
- test("arrays with nested group segments", function () {
868
- // Array with nested AND
869
- expect(
870
- removeRedundantGroupSegments([
871
- "*",
872
- {
873
- and: ["*", "web", "*"],
874
- },
875
- "*",
876
- ]),
877
- ).toEqual([
878
- {
879
- and: ["web"],
880
- },
881
- ]);
882
-
883
- // Array with multiple nested group segments
884
- expect(
885
- removeRedundantGroupSegments([
886
- "*",
887
- "web",
888
- {
889
- or: ["*", "chrome", "*"],
890
- },
891
- "*",
892
- ]),
893
- ).toEqual([
894
- "web",
895
- {
896
- or: ["chrome"],
897
- },
898
- ]);
899
- });
900
-
901
- test("edge cases", function () {
902
- // Single group segment after filtering
903
- expect(
904
- removeRedundantGroupSegments({
905
- and: ["*", "web", "*"],
906
- }),
907
- ).toEqual({
908
- and: ["web"],
909
- });
910
-
911
- // Plain string group segment (no change)
912
- expect(removeRedundantGroupSegments("web")).toEqual("web");
913
-
914
- // Array with single non-* group segment
915
- expect(removeRedundantGroupSegments(["*", "web", "*"])).toEqual(["web"]);
916
- });
917
- });
918
-
919
- describe("applyContextToGroupSegments", function () {
920
- const datafileWithSegments: DatafileContent = {
921
- schemaVersion: "2",
922
- revision: "unknown",
923
- segments: {
924
- web: {
925
- conditions: [{ attribute: "platform", operator: "equals", value: "web" }],
926
- },
927
- mobile: {
928
- conditions: [{ attribute: "platform", operator: "equals", value: "mobile" }],
929
- },
930
- chrome: {
931
- conditions: [{ attribute: "browser", operator: "equals", value: "chrome" }],
932
- },
933
- safari: {
934
- conditions: [{ attribute: "browser", operator: "equals", value: "safari" }],
935
- },
936
- premium: {
937
- conditions: [
938
- {
939
- and: [
940
- { attribute: "tier", operator: "equals", value: "premium" },
941
- { attribute: "status", operator: "equals", value: "active" },
942
- ],
943
- },
944
- ],
945
- },
946
- us: {
947
- conditions: [{ attribute: "country", operator: "equals", value: "us" }],
948
- },
949
- eu: {
950
- conditions: [{ attribute: "country", operator: "equals", value: "eu" }],
951
- },
952
- },
953
- features: {},
954
- };
955
-
956
- const featurevisorWithSegments = createFeaturevisor({
957
- datafile: datafileWithSegments,
958
- logLevel: "fatal",
959
- });
960
-
961
- test("applyContextToGroupSegments is a function", function () {
962
- expect(applyContextToGroupSegments).toBeInstanceOf(Function);
963
- });
964
-
965
- test("simple cases", function () {
966
- // "*" remains "*"
967
- expect(applyContextToGroupSegments(featurevisorWithSegments, "*", {})).toEqual("*");
968
-
969
- // Plain segment that matches
970
- expect(
971
- applyContextToGroupSegments(featurevisorWithSegments, "web", {
972
- platform: "web",
973
- }),
974
- ).toEqual("*");
975
-
976
- // Plain segment that doesn't match
977
- expect(
978
- applyContextToGroupSegments(featurevisorWithSegments, "web", {
979
- platform: "mobile",
980
- }),
981
- ).toEqual("web");
982
-
983
- // Array of segments - partial match
984
- expect(
985
- applyContextToGroupSegments(featurevisorWithSegments, ["web", "chrome"], {
986
- platform: "web",
987
- }),
988
- ).toEqual(["*", "chrome"]);
989
-
990
- // Array of segments - full match (all "*" - will be cleaned by removeRedundantGroupSegments later)
991
- expect(
992
- applyContextToGroupSegments(featurevisorWithSegments, ["web", "chrome"], {
993
- platform: "web",
994
- browser: "chrome",
995
- }),
996
- ).toEqual(["*", "*"]);
997
-
998
- // Array of segments - no match
999
- expect(
1000
- applyContextToGroupSegments(featurevisorWithSegments, ["web", "chrome"], {
1001
- platform: "mobile",
1002
- browser: "safari",
1003
- }),
1004
- ).toEqual(["web", "chrome"]);
1005
- });
1006
-
1007
- test("AND group segments", function () {
1008
- // AND with all matching segments (all "*" becomes "*")
1009
- expect(
1010
- applyContextToGroupSegments(
1011
- featurevisorWithSegments,
1012
- {
1013
- and: ["web", "chrome"],
1014
- },
1015
- {
1016
- platform: "web",
1017
- browser: "chrome",
1018
- },
1019
- ),
1020
- ).toEqual({
1021
- and: ["*", "*"],
1022
- });
1023
-
1024
- // AND with partial match
1025
- expect(
1026
- applyContextToGroupSegments(
1027
- featurevisorWithSegments,
1028
- {
1029
- and: ["web", "chrome"],
1030
- },
1031
- {
1032
- platform: "web",
1033
- },
1034
- ),
1035
- ).toEqual({
1036
- and: ["*", "chrome"],
1037
- });
1038
-
1039
- // AND with no matches
1040
- expect(
1041
- applyContextToGroupSegments(
1042
- featurevisorWithSegments,
1043
- {
1044
- and: ["web", "chrome"],
1045
- },
1046
- {
1047
- platform: "mobile",
1048
- browser: "safari",
1049
- },
1050
- ),
1051
- ).toEqual({
1052
- and: ["web", "chrome"],
1053
- });
1054
-
1055
- // AND with "*" in it
1056
- expect(
1057
- applyContextToGroupSegments(
1058
- featurevisorWithSegments,
1059
- {
1060
- and: ["*", "chrome"],
1061
- },
1062
- {
1063
- browser: "chrome",
1064
- },
1065
- ),
1066
- ).toEqual({
1067
- and: ["*", "*"],
1068
- });
1069
- });
1070
-
1071
- test("OR group segments", function () {
1072
- // OR with all matching segments (all "*" becomes "*")
1073
- expect(
1074
- applyContextToGroupSegments(
1075
- featurevisorWithSegments,
1076
- {
1077
- or: ["web", "chrome"],
1078
- },
1079
- {
1080
- platform: "web",
1081
- browser: "chrome",
1082
- },
1083
- ),
1084
- ).toEqual({
1085
- or: ["*", "*"],
1086
- });
1087
-
1088
- // OR with partial match
1089
- expect(
1090
- applyContextToGroupSegments(
1091
- featurevisorWithSegments,
1092
- {
1093
- or: ["web", "chrome"],
1094
- },
1095
- {
1096
- platform: "web",
1097
- },
1098
- ),
1099
- ).toEqual({
1100
- or: ["*", "chrome"],
1101
- });
1102
-
1103
- // OR with no matches
1104
- expect(
1105
- applyContextToGroupSegments(
1106
- featurevisorWithSegments,
1107
- {
1108
- or: ["web", "mobile"],
1109
- },
1110
- {
1111
- platform: "desktop",
1112
- },
1113
- ),
1114
- ).toEqual({
1115
- or: ["web", "mobile"],
1116
- });
1117
- });
1118
-
1119
- test("NOT group segments", function () {
1120
- // NOT with matching segment
1121
- expect(
1122
- applyContextToGroupSegments(
1123
- featurevisorWithSegments,
1124
- {
1125
- not: ["web"],
1126
- },
1127
- {
1128
- platform: "web",
1129
- },
1130
- ),
1131
- ).toEqual({
1132
- not: ["*"],
1133
- });
1134
-
1135
- // NOT with non-matching segment
1136
- expect(
1137
- applyContextToGroupSegments(
1138
- featurevisorWithSegments,
1139
- {
1140
- not: ["web"],
1141
- },
1142
- {
1143
- platform: "mobile",
1144
- },
1145
- ),
1146
- ).toEqual({
1147
- not: ["web"],
1148
- });
1149
-
1150
- // NOT with multiple segments (all "*" becomes "*")
1151
- expect(
1152
- applyContextToGroupSegments(
1153
- featurevisorWithSegments,
1154
- {
1155
- not: ["web", "chrome"],
1156
- },
1157
- {
1158
- platform: "web",
1159
- browser: "chrome",
1160
- },
1161
- ),
1162
- ).toEqual({
1163
- not: ["*", "*"],
1164
- });
1165
- });
1166
-
1167
- test("nested AND group segments", function () {
1168
- // Nested AND with all matching (all "*" becomes "*")
1169
- expect(
1170
- applyContextToGroupSegments(
1171
- featurevisorWithSegments,
1172
- {
1173
- and: [
1174
- "web",
1175
- {
1176
- and: ["chrome", "us"],
1177
- },
1178
- ],
1179
- },
1180
- {
1181
- platform: "web",
1182
- browser: "chrome",
1183
- country: "us",
1184
- },
1185
- ),
1186
- ).toEqual({
1187
- and: [
1188
- "*",
1189
- {
1190
- and: ["*", "*"],
1191
- },
1192
- ],
1193
- });
1194
-
1195
- // Nested AND with partial match
1196
- expect(
1197
- applyContextToGroupSegments(
1198
- featurevisorWithSegments,
1199
- {
1200
- and: [
1201
- "web",
1202
- {
1203
- and: ["chrome", "us"],
1204
- },
1205
- ],
1206
- },
1207
- {
1208
- platform: "web",
1209
- browser: "chrome",
1210
- },
1211
- ),
1212
- ).toEqual({
1213
- and: [
1214
- "*",
1215
- {
1216
- and: ["*", "us"],
1217
- },
1218
- ],
1219
- });
1220
- });
1221
-
1222
- test("nested OR group segments", function () {
1223
- // Nested OR with all matching
1224
- expect(
1225
- applyContextToGroupSegments(
1226
- featurevisorWithSegments,
1227
- {
1228
- or: [
1229
- "web",
1230
- {
1231
- or: ["chrome", "safari"],
1232
- },
1233
- ],
1234
- },
1235
- {
1236
- platform: "web",
1237
- browser: "chrome",
1238
- },
1239
- ),
1240
- ).toEqual({
1241
- or: [
1242
- "*",
1243
- {
1244
- or: ["*", "safari"],
1245
- },
1246
- ],
1247
- });
1248
-
1249
- // Nested OR with partial match
1250
- expect(
1251
- applyContextToGroupSegments(
1252
- featurevisorWithSegments,
1253
- {
1254
- or: [
1255
- "web",
1256
- {
1257
- or: ["chrome", "safari"],
1258
- },
1259
- ],
1260
- },
1261
- {
1262
- platform: "web",
1263
- },
1264
- ),
1265
- ).toEqual({
1266
- or: [
1267
- "*",
1268
- {
1269
- or: ["chrome", "safari"],
1270
- },
1271
- ],
1272
- });
1273
- });
1274
-
1275
- test("complex nested structures", function () {
1276
- // Complex nested with AND, OR, NOT
1277
- expect(
1278
- applyContextToGroupSegments(
1279
- featurevisorWithSegments,
1280
- {
1281
- and: [
1282
- "web",
1283
- {
1284
- or: ["chrome", "safari"],
1285
- },
1286
- {
1287
- not: ["mobile"],
1288
- },
1289
- ],
1290
- },
1291
- {
1292
- platform: "web",
1293
- browser: "chrome",
1294
- },
1295
- ),
1296
- ).toEqual({
1297
- and: [
1298
- "*",
1299
- {
1300
- or: ["*", "safari"],
1301
- },
1302
- {
1303
- not: ["mobile"],
1304
- },
1305
- ],
1306
- });
1307
- });
1308
-
1309
- test("segments with complex conditions", function () {
1310
- // Segment with AND conditions in its definition
1311
- expect(
1312
- applyContextToGroupSegments(featurevisorWithSegments, "premium", {
1313
- tier: "premium",
1314
- status: "active",
1315
- }),
1316
- ).toEqual("*");
1317
-
1318
- // Segment with AND conditions that doesn't match
1319
- expect(
1320
- applyContextToGroupSegments(featurevisorWithSegments, "premium", {
1321
- tier: "premium",
1322
- status: "inactive",
1323
- }),
1324
- ).toEqual("premium");
1325
- });
1326
-
1327
- test("removeSegments parameter", function () {
1328
- // Simple segment in removeSegments
1329
- expect(applyContextToGroupSegments(featurevisorWithSegments, "web", {}, ["web"])).toEqual(
1330
- "*",
1331
- );
1332
-
1333
- // Segment not in removeSegments
1334
- expect(applyContextToGroupSegments(featurevisorWithSegments, "web", {}, ["mobile"])).toEqual(
1335
- "web",
1336
- );
1337
-
1338
- // Array with one segment in removeSegments
1339
- expect(
1340
- applyContextToGroupSegments(featurevisorWithSegments, ["web", "chrome"], {}, ["web"]),
1341
- ).toEqual(["*", "chrome"]);
1342
-
1343
- // Array with all segments in removeSegments
1344
- expect(
1345
- applyContextToGroupSegments(featurevisorWithSegments, ["web", "chrome"], {}, [
1346
- "web",
1347
- "chrome",
1348
- ]),
1349
- ).toEqual(["*", "*"]);
1350
-
1351
- // Array with multiple segments, some in removeSegments
1352
- expect(
1353
- applyContextToGroupSegments(
1354
- featurevisorWithSegments,
1355
- ["web", "chrome", "mobile", "safari"],
1356
- {},
1357
- ["web", "mobile"],
1358
- ),
1359
- ).toEqual(["*", "chrome", "*", "safari"]);
1360
-
1361
- // AND group segment with removeSegments
1362
- expect(
1363
- applyContextToGroupSegments(
1364
- featurevisorWithSegments,
1365
- {
1366
- and: ["web", "chrome"],
1367
- },
1368
- {},
1369
- ["web"],
1370
- ),
1371
- ).toEqual({
1372
- and: ["*", "chrome"],
1373
- });
1374
-
1375
- // AND group segment with all segments in removeSegments
1376
- expect(
1377
- applyContextToGroupSegments(
1378
- featurevisorWithSegments,
1379
- {
1380
- and: ["web", "chrome"],
1381
- },
1382
- {},
1383
- ["web", "chrome"],
1384
- ),
1385
- ).toEqual({
1386
- and: ["*", "*"],
1387
- });
1388
-
1389
- // OR group segment with removeSegments
1390
- expect(
1391
- applyContextToGroupSegments(
1392
- featurevisorWithSegments,
1393
- {
1394
- or: ["web", "chrome"],
1395
- },
1396
- {},
1397
- ["web"],
1398
- ),
1399
- ).toEqual({
1400
- or: ["*", "chrome"],
1401
- });
1402
-
1403
- // OR group segment with all segments in removeSegments
1404
- expect(
1405
- applyContextToGroupSegments(
1406
- featurevisorWithSegments,
1407
- {
1408
- or: ["web", "chrome"],
1409
- },
1410
- {},
1411
- ["web", "chrome"],
1412
- ),
1413
- ).toEqual({
1414
- or: ["*", "*"],
1415
- });
1416
-
1417
- // NOT group segment with removeSegments
1418
- expect(
1419
- applyContextToGroupSegments(
1420
- featurevisorWithSegments,
1421
- {
1422
- not: ["web", "chrome"],
1423
- },
1424
- {},
1425
- ["web"],
1426
- ),
1427
- ).toEqual({
1428
- not: ["*", "chrome"],
1429
- });
1430
-
1431
- // NOT group segment with all segments in removeSegments
1432
- expect(
1433
- applyContextToGroupSegments(
1434
- featurevisorWithSegments,
1435
- {
1436
- not: ["web", "chrome"],
1437
- },
1438
- {},
1439
- ["web", "chrome"],
1440
- ),
1441
- ).toEqual({
1442
- not: ["*", "*"],
1443
- });
1444
-
1445
- // Nested AND with removeSegments
1446
- expect(
1447
- applyContextToGroupSegments(
1448
- featurevisorWithSegments,
1449
- {
1450
- and: [
1451
- "web",
1452
- {
1453
- and: ["chrome", "us"],
1454
- },
1455
- ],
1456
- },
1457
- {},
1458
- ["web"],
1459
- ),
1460
- ).toEqual({
1461
- and: [
1462
- "*",
1463
- {
1464
- and: ["chrome", "us"],
1465
- },
1466
- ],
1467
- });
1468
-
1469
- // Nested OR with removeSegments
1470
- expect(
1471
- applyContextToGroupSegments(
1472
- featurevisorWithSegments,
1473
- {
1474
- or: [
1475
- "web",
1476
- {
1477
- or: ["chrome", "safari"],
1478
- },
1479
- ],
1480
- },
1481
- {},
1482
- ["chrome"],
1483
- ),
1484
- ).toEqual({
1485
- or: [
1486
- "web",
1487
- {
1488
- or: ["*", "safari"],
1489
- },
1490
- ],
1491
- });
1492
-
1493
- // Mixed nested structure with removeSegments
1494
- expect(
1495
- applyContextToGroupSegments(
1496
- featurevisorWithSegments,
1497
- {
1498
- and: [
1499
- "web",
1500
- {
1501
- or: ["chrome", "safari"],
1502
- },
1503
- {
1504
- not: ["mobile"],
1505
- },
1506
- ],
1507
- },
1508
- {},
1509
- ["web", "safari"],
1510
- ),
1511
- ).toEqual({
1512
- and: [
1513
- "*",
1514
- {
1515
- or: ["chrome", "*"],
1516
- },
1517
- {
1518
- not: ["mobile"],
1519
- },
1520
- ],
1521
- });
1522
-
1523
- // removeSegments combined with matching context
1524
- expect(
1525
- applyContextToGroupSegments(
1526
- featurevisorWithSegments,
1527
- ["web", "chrome"],
1528
- {
1529
- platform: "web",
1530
- },
1531
- ["chrome"],
1532
- ),
1533
- ).toEqual(["*", "*"]);
1534
-
1535
- // removeSegments with segment that would match context
1536
- expect(
1537
- applyContextToGroupSegments(
1538
- featurevisorWithSegments,
1539
- ["web", "chrome"],
1540
- {
1541
- platform: "web",
1542
- browser: "chrome",
1543
- },
1544
- ["web"],
1545
- ),
1546
- ).toEqual(["*", "*"]);
1547
-
1548
- // Complex: removeSegments, context matching, and nested structures
1549
- expect(
1550
- applyContextToGroupSegments(
1551
- featurevisorWithSegments,
1552
- {
1553
- and: [
1554
- "web",
1555
- {
1556
- or: ["chrome", "safari", "mobile"],
1557
- },
1558
- "us",
1559
- ],
1560
- },
1561
- {
1562
- platform: "web",
1563
- browser: "chrome",
1564
- },
1565
- ["us"],
1566
- ),
1567
- ).toEqual({
1568
- and: [
1569
- "*",
1570
- {
1571
- or: ["*", "safari", "mobile"],
1572
- },
1573
- "*",
1574
- ],
1575
- });
1576
-
1577
- // Empty removeSegments array (should behave like no removeSegments)
1578
- expect(applyContextToGroupSegments(featurevisorWithSegments, "web", {}, [])).toEqual("web");
1579
-
1580
- // removeSegments with segment that doesn't exist in datafile
1581
- expect(
1582
- applyContextToGroupSegments(featurevisorWithSegments, ["web", "nonexistent"], {}, [
1583
- "nonexistent",
1584
- ]),
1585
- ).toEqual(["web", "*"]);
1586
-
1587
- // removeSegments with "*" in array (should still work)
1588
- expect(
1589
- applyContextToGroupSegments(featurevisorWithSegments, ["*", "web", "chrome"], {}, ["web"]),
1590
- ).toEqual(["*", "*", "chrome"]);
1591
- });
1592
- });
1593
- });