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