-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmappers.test.ts
More file actions
443 lines (379 loc) · 17.7 KB
/
Copy pathmappers.test.ts
File metadata and controls
443 lines (379 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import { test, expect, describe } from 'vitest';
import { getEvaluationContext } from '../../../flagsmith-engine/evaluation/evaluationContext/mappers.js';
import { buildEnvironmentModel } from '../../../flagsmith-engine/environments/util.js';
import { EnvironmentModel } from '../../../flagsmith-engine/environments/models.js';
import { IdentityModel } from '../../../flagsmith-engine/identities/models.js';
import { TraitModel } from '../../../flagsmith-engine/identities/traits/models.js';
import { FeatureModel, FeatureStateModel } from '../../../flagsmith-engine/features/models.js';
import {
MultivariateFeatureOptionModel,
MultivariateFeatureStateValueModel
} from '../../../flagsmith-engine/features/models.js';
import { CONSTANTS } from '../../../flagsmith-engine/features/constants.js';
import { readFileSync } from 'fs';
import { IDENTITY_OVERRIDE_SEGMENT_NAME } from '../../../flagsmith-engine/segments/constants.js';
import { SegmentSource } from '../../../flagsmith-engine/evaluation/models.js';
const DATA_DIR = __dirname + '/../../sdk/data/';
describe('getEvaluationContext', () => {
const environmentJSON = JSON.parse(readFileSync(DATA_DIR + 'environment.json', 'utf-8'));
const testEnvironment = buildEnvironmentModel(environmentJSON);
test('produces evaluation context from environment document', () => {
// When
const context = getEvaluationContext(testEnvironment);
// Then - verify environment
expect(context).toBeDefined();
expect(context.environment?.key).toBe('B62qaMZNwfiqT76p38ggrQ');
expect(context.environment?.name).toBe('Test environment');
expect(context.identity).toBeUndefined();
// Verify segments
expect(context.segments).toBeDefined();
expect(context.segments).toHaveProperty('1');
const segment = context.segments!['1'];
expect(segment.key).toBe('1');
expect(segment.name).toBe('regular_segment');
expect(segment.rules.length).toBe(1);
expect(segment.overrides).toBeDefined();
expect(Array.isArray(segment.overrides)).toBe(true);
expect(segment.metadata?.source).toBe(SegmentSource.API);
expect(segment.metadata?.id).toBe(1);
// Verify segment rules
expect(segment.rules[0].type).toBe('ALL');
expect(segment.rules[0].conditions).toEqual([]);
expect(segment.rules[0].rules?.length).toBe(1);
const nestedRule = segment.rules[0].rules?.[0]!;
expect(nestedRule.type).toBe('ANY');
expect(nestedRule.conditions?.length).toBe(1);
expect(nestedRule.rules?.length).toEqual(0);
const condition = nestedRule.conditions?.[0]!;
expect(condition.property).toBe('age');
expect(condition.operator).toBe('LESS_THAN');
expect(condition.value).toBe('40');
// Verify identity override segment
const identityOverrideSegment = Object.values(context.segments!).find(
s => s.name === IDENTITY_OVERRIDE_SEGMENT_NAME
);
expect(identityOverrideSegment).toBeDefined();
expect(identityOverrideSegment!.name).toBe(IDENTITY_OVERRIDE_SEGMENT_NAME);
expect(identityOverrideSegment!.rules.length).toBe(1);
expect(identityOverrideSegment!.overrides?.length).toBe(1);
const overrideRule = identityOverrideSegment!.rules?.[0]!;
expect(overrideRule.type).toBe('ALL');
expect(overrideRule.conditions?.length).toBe(1);
const overrideCondition = overrideRule.conditions?.[0]!;
expect(overrideCondition.property).toBe('$.identity.identifier');
expect(overrideCondition.operator).toBe('IN');
expect(overrideCondition.value).toContain('overridden-id');
const override = identityOverrideSegment!.overrides?.[0]!;
expect(override.name).toBe('some_feature');
expect(override.enabled).toBe(false);
expect(override.value).toBe('some-overridden-value');
expect(override.priority).toBe(-Infinity);
expect(override.metadata?.id).toBe(1);
// Verify features
expect(context.features).toBeDefined();
expect(context.features).toHaveProperty('some_feature');
const someFeature = context.features!['some_feature'];
expect(someFeature.name).toBe('some_feature');
expect(someFeature.enabled).toBe(true);
expect(someFeature.value).toBe('some-value');
expect(someFeature.priority).toBeUndefined();
expect(someFeature.metadata?.id).toBe(1);
// Verify multivariate feature
expect(context.features).toHaveProperty('mv_feature');
const mvFeature = context.features!['mv_feature'];
expect(mvFeature.name).toBe('mv_feature');
expect(mvFeature.enabled).toBe(false);
expect(mvFeature.value).toBe('foo');
expect(mvFeature.priority).toBeUndefined();
expect(mvFeature.variants?.length).toBe(1);
const variant = mvFeature.variants![0];
expect(variant.value).toBe('bar');
expect(variant.weight).toBe(100);
expect(variant.priority).toBe(1);
});
test('maps multivariate features with multiple variants correctly', () => {
// Given
const mvOption1 = new MultivariateFeatureOptionModel('variant_a', 100);
const mvOption2 = new MultivariateFeatureOptionModel('variant_b', 200);
const mvOption3 = new MultivariateFeatureOptionModel('variant_c', 150);
const mvValue1 = new MultivariateFeatureStateValueModel(
mvOption1,
30,
100,
'00000000-0000-0000-0000-000000000001'
);
const mvValue2 = new MultivariateFeatureStateValueModel(
mvOption2,
50,
200,
'00000000-0000-0000-0000-000000000002'
);
const mvValue3 = new MultivariateFeatureStateValueModel(
mvOption3,
20,
150,
'00000000-0000-0000-0000-000000000003'
);
const feature = new FeatureModel(999, 'multi_variant_feature', CONSTANTS.MULTIVARIATE);
const featureState = new FeatureStateModel(feature, true, 999);
featureState.setValue('control');
featureState.multivariateFeatureStateValues = [mvValue1, mvValue2, mvValue3];
const envWithMv = new EnvironmentModel(1, 'test_key', testEnvironment.project, 'Test Env');
envWithMv.featureStates = [featureState];
// When
const context = getEvaluationContext(envWithMv);
// Then
const featureContext = context.features!['multi_variant_feature'];
expect(featureContext.variants?.length).toBe(3);
expect(featureContext.variants![0].value).toBe('variant_a');
expect(featureContext.variants![0].weight).toBe(30);
expect(featureContext.variants![0].priority).toBe(100);
expect(featureContext.variants![1].value).toBe('variant_b');
expect(featureContext.variants![1].weight).toBe(50);
expect(featureContext.variants![1].priority).toBe(200);
expect(featureContext.variants![2].value).toBe('variant_c');
expect(featureContext.variants![2].weight).toBe(20);
expect(featureContext.variants![2].priority).toBe(150);
});
test('handles multivariate features without IDs using UUID', () => {
// Given
const mvOption1 = new MultivariateFeatureOptionModel('option_x', undefined);
const mvOption2 = new MultivariateFeatureOptionModel('option_y', undefined);
const mvValue1 = new MultivariateFeatureStateValueModel(
mvOption1,
60,
undefined as any,
'aaaaaaaa-bbbb-cccc-dddd-000000000001'
);
const mvValue2 = new MultivariateFeatureStateValueModel(
mvOption2,
40,
undefined as any,
'aaaaaaaa-bbbb-cccc-dddd-000000000002'
);
const feature = new FeatureModel(888, 'uuid_variant_feature', CONSTANTS.MULTIVARIATE);
const featureState = new FeatureStateModel(feature, true, 888);
featureState.setValue('default');
featureState.multivariateFeatureStateValues = [mvValue1, mvValue2];
const envWithUuid = new EnvironmentModel(
1,
'test_key',
testEnvironment.project,
'Test Env'
);
envWithUuid.featureStates = [featureState];
// When
const context = getEvaluationContext(envWithUuid);
// Then
const featureContext = context.features!['uuid_variant_feature'];
expect(featureContext.variants?.length).toBe(2);
// When using UUID-based priorities, they become bigints
expect(
typeof featureContext.variants![0].priority === 'number' ||
typeof featureContext.variants![0].priority === 'bigint'
).toBe(true);
expect(
typeof featureContext.variants![1].priority === 'number' ||
typeof featureContext.variants![1].priority === 'bigint'
).toBe(true);
expect(featureContext.variants![0].priority).not.toBe(featureContext.variants![1].priority);
});
test('handles environment with no features', () => {
// Given - create a copy with no features
const emptyEnvJSON = { ...environmentJSON, feature_states: [] };
const emptyEnv = buildEnvironmentModel(emptyEnvJSON);
// When
const context = getEvaluationContext(emptyEnv);
// Then
expect(context.features).toEqual({});
expect(context.environment?.key).toBe('B62qaMZNwfiqT76p38ggrQ');
expect(context.environment?.name).toBe('Test environment');
});
test('produces evaluation context with identity', () => {
// Given
const identity = new IdentityModel(
'2024-01-01T00:00:00Z',
[new TraitModel('email', 'test@example.com'), new TraitModel('age', 25)],
[],
'B62qaMZNwfiqT76p38ggrQ',
'test_user'
);
// When
const context = getEvaluationContext(testEnvironment, identity);
// Then
expect(context.identity).toBeDefined();
expect(context.identity?.identifier).toBe('test_user');
expect(context.identity?.traits).toEqual({
email: 'test@example.com',
age: 25
});
});
test('produces evaluation context with override traits', () => {
// Given
const identity = new IdentityModel(
'2024-01-01T00:00:00Z',
[new TraitModel('email', 'original@example.com')],
[],
'B62qaMZNwfiqT76p38ggrQ',
'test_user',
undefined,
456
);
const overrideTraits = [
new TraitModel('email', 'override@example.com'),
new TraitModel('premium', true)
];
// When
const context = getEvaluationContext(testEnvironment, identity, overrideTraits);
// Then
expect(context.identity?.traits).toEqual({
email: 'override@example.com',
premium: true
});
});
test('produces evaluation context without identity when isEnvironmentEvaluation is true', () => {
// Given
const identity = new IdentityModel(
'2024-01-01T00:00:00Z',
[new TraitModel('test', 'value')],
[],
'B62qaMZNwfiqT76p38ggrQ',
'test_user',
undefined,
789
);
// When
const context = getEvaluationContext(testEnvironment, identity, undefined, true);
// Then
expect(context.identity).toBeUndefined();
expect(context.environment).toBeDefined();
expect(context.features).toBeDefined();
expect(context.segments).toBeDefined();
});
test('handles identity without django_id', () => {
// Given
const identity = new IdentityModel(
'2024-01-01T00:00:00Z',
[new TraitModel('name', 'John')],
[],
'B62qaMZNwfiqT76p38ggrQ',
'john_doe',
undefined,
undefined
);
// When
const context = getEvaluationContext(testEnvironment, identity);
// Then
expect(context.identity?.identifier).toBe('john_doe');
expect(context.identity?.key).toBeUndefined();
expect(context.identity?.traits).toEqual({ name: 'John' });
});
test('maps segment override priorities correctly', () => {
// When - using fixture which has segment with priority
const context = getEvaluationContext(testEnvironment);
// Then - verify regular_segment has a feature override
const segment = context.segments!['1'];
expect(segment.overrides?.length).toBeGreaterThan(0);
// The segment override from the fixture has no explicit priority, should be undefined
const segmentOverride = segment.overrides?.[0]!;
expect(segmentOverride.name).toBe('some_feature');
expect(segmentOverride.priority).toBeUndefined();
});
test('handles multiple identity overrides with same features', () => {
// Given - the fixture already has identity override with 'overridden-id'
// Verify it's mapped correctly
const context = getEvaluationContext(testEnvironment);
// Then
const overrideSegments = Object.values(context.segments!).filter(
s => s.name === IDENTITY_OVERRIDE_SEGMENT_NAME
);
// The fixture has one identity override
expect(overrideSegments.length).toBe(1);
expect(overrideSegments[0].rules?.[0].conditions?.[0].value).toContain('overridden-id');
expect(overrideSegments[0].overrides?.length).toBe(1);
});
// Regression: identity override on a multivariate flag whose mv values have no
// django `id` made mapIdentityOverridesToSegments JSON.stringify a BigInt priority.
test('handles identity override on a multivariate feature without django ids', () => {
const envWithMvOverride = JSON.parse(JSON.stringify(environmentJSON));
envWithMvOverride.identity_overrides = [
{
identifier: 'overridden-mv-identifier',
identity_uuid: '11111111-1111-1111-1111-111111111111',
environment_api_key: 'B62qaMZNwfiqT76p38ggrQ',
identity_features: [
{
feature: { id: 2, name: 'mv_feature', type: 'MULTIVARIATE' },
featurestate_uuid: '22222222-2222-2222-2222-222222222222',
feature_state_value: 'control',
enabled: true,
feature_segment: null,
multivariate_feature_state_values: [
{
percentage_allocation: 33,
multivariate_feature_option: { value: 'variant_1' },
mv_fs_value_uuid: 'aaaaaaaa-0000-0000-0000-000000000001'
},
{
percentage_allocation: 33,
multivariate_feature_option: { value: 'variant_2' },
mv_fs_value_uuid: 'aaaaaaaa-0000-0000-0000-000000000002'
},
{
percentage_allocation: 34,
multivariate_feature_option: { value: 'variant_3' },
mv_fs_value_uuid: 'aaaaaaaa-0000-0000-0000-000000000003'
}
]
}
]
}
];
const env = buildEnvironmentModel(envWithMvOverride);
expect(() => getEvaluationContext(env)).not.toThrow();
const context = getEvaluationContext(env);
const overrideSegment = Object.values(context.segments!).find(
s =>
s.name === IDENTITY_OVERRIDE_SEGMENT_NAME &&
s.rules?.[0]?.conditions?.[0]?.value === 'overridden-mv-identifier'
);
expect(overrideSegment).toBeDefined();
const mvOverride = overrideSegment!.overrides?.find(o => o.name === 'mv_feature');
expect(mvOverride).toBeDefined();
expect(mvOverride!.variants?.length).toBe(3);
expect(mvOverride!.variants?.map(v => v.value)).toEqual([
'variant_1',
'variant_2',
'variant_3'
]);
expect(mvOverride!.key).toBe('22222222-2222-2222-2222-222222222222');
});
test('still dedupes identical plain identity overrides into one segment', () => {
const envWithDupes = JSON.parse(JSON.stringify(environmentJSON));
const makeOverride = (identifier: string, fsUuid: string) => ({
identifier,
identity_uuid: identifier,
environment_api_key: 'B62qaMZNwfiqT76p38ggrQ',
identity_features: [
{
feature: { id: 1, name: 'some_feature', type: 'STANDARD' },
featurestate_uuid: fsUuid,
feature_state_value: 'shared-override-value',
enabled: false,
feature_segment: null
}
]
});
envWithDupes.identity_overrides = [
makeOverride('user-a', '33333333-3333-3333-3333-333333333333'),
makeOverride('user-b', '44444444-4444-4444-4444-444444444444')
];
const env = buildEnvironmentModel(envWithDupes);
const context = getEvaluationContext(env);
const overrideSegments = Object.values(context.segments!).filter(
s => s.name === IDENTITY_OVERRIDE_SEGMENT_NAME
);
expect(overrideSegments.length).toBe(1);
expect(overrideSegments[0].rules?.[0].conditions?.[0].value).toBe('user-a,user-b');
});
});