-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSplitsCacheInLocal.spec.ts
More file actions
233 lines (166 loc) · 9.29 KB
/
Copy pathSplitsCacheInLocal.spec.ts
File metadata and controls
233 lines (166 loc) · 9.29 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
import { SplitsCacheInLocal } from '../SplitsCacheInLocal';
import { KeyBuilderCS } from '../../KeyBuilderCS';
import { splitWithUserTT, splitWithAccountTT, splitWithAccountTTAndUsesSegments, something, somethingElse, featureFlagOne, featureFlagTwo, featureFlagThree, featureFlagWithEmptyFS, featureFlagWithoutFS } from '../../__tests__/testUtils';
import { ISplit } from '../../../dtos/types';
import { fullSettings } from '../../../utils/settingsValidation/__tests__/settings.mocks';
import { storages, PREFIX } from './wrapper.mock';
describe.each(storages)('SPLITS CACHE', (storage) => {
test('LocalStorage', () => {
const cache = new SplitsCacheInLocal(fullSettings, new KeyBuilderCS(PREFIX, 'user'), storage);
cache.clear();
cache.update([something, somethingElse], [], -1);
let values = cache.getAll();
expect(values).toEqual([something, somethingElse]);
cache.removeSplit(something.name);
const splits = cache.getSplits([something.name, somethingElse.name]);
expect(splits[something.name]).toEqual(null);
expect(splits[somethingElse.name]).toEqual(somethingElse);
values = cache.getAll();
expect(values).toEqual([somethingElse]);
expect(cache.getSplit(something.name)).toEqual(null);
expect(cache.getSplit(somethingElse.name)).toEqual(somethingElse);
expect(cache.getChangeNumber()).toBe(-1);
cache.setChangeNumber(123);
expect(cache.getChangeNumber()).toBe(123);
});
test('LocalStorage / Get Keys', () => {
const cache = new SplitsCacheInLocal(fullSettings, new KeyBuilderCS(PREFIX, 'user'), storage);
cache.update([something, somethingElse], [], 1);
const keys = cache.getSplitNames();
expect(keys.indexOf(something.name) !== -1).toBe(true);
expect(keys.indexOf(somethingElse.name) !== -1).toBe(true);
});
test('LocalStorage / Update Splits', () => {
const cache = new SplitsCacheInLocal(fullSettings, new KeyBuilderCS(PREFIX, 'user'), storage);
cache.update([something, somethingElse], [], 1);
cache.update([], [something, somethingElse], 1);
expect(cache.getSplit(something.name)).toBe(null);
expect(cache.getSplit(somethingElse.name)).toBe(null);
});
test('LocalStorage / trafficTypeExists and ttcache tests', () => {
const cache = new SplitsCacheInLocal(fullSettings, new KeyBuilderCS(PREFIX, 'user'), storage);
cache.update([
{ ...splitWithUserTT, name: 'split1' },
{ ...splitWithAccountTT, name: 'split2' },
{ ...splitWithUserTT, name: 'split3' },
], [], 1);
cache.addSplit({ ...splitWithUserTT, name: 'split4' });
expect(cache.trafficTypeExists('user_tt')).toBe(true);
expect(cache.trafficTypeExists('account_tt')).toBe(true);
expect(cache.trafficTypeExists('not_existent_tt')).toBe(false);
cache.removeSplit('split4');
expect(cache.trafficTypeExists('user_tt')).toBe(true);
expect(cache.trafficTypeExists('account_tt')).toBe(true);
cache.removeSplit('split3');
cache.removeSplit('split2');
expect(cache.trafficTypeExists('user_tt')).toBe(true);
expect(cache.trafficTypeExists('account_tt')).toBe(false);
cache.removeSplit('split1');
expect(cache.trafficTypeExists('user_tt')).toBe(false);
expect(cache.trafficTypeExists('account_tt')).toBe(false);
cache.addSplit({ ...splitWithUserTT, name: 'split1' });
expect(cache.trafficTypeExists('user_tt')).toBe(true);
cache.addSplit({ ...splitWithAccountTT, name: 'split1' });
expect(cache.trafficTypeExists('account_tt')).toBe(true);
expect(cache.trafficTypeExists('user_tt')).toBe(false);
});
test('LocalStorage / killLocally', () => {
const cache = new SplitsCacheInLocal(fullSettings, new KeyBuilderCS(PREFIX, 'user'), storage);
cache.addSplit(something);
cache.addSplit(somethingElse);
const initialChangeNumber = cache.getChangeNumber();
// kill an non-existent split
let updated = cache.killLocally('nonexistent_split', 'other_treatment', 101);
const nonexistentSplit = cache.getSplit('nonexistent_split');
expect(updated).toBe(false); // killLocally resolves without update if split doesn't exist
expect(nonexistentSplit).toBe(null); // non-existent split keeps being non-existent
// kill an existent split
updated = cache.killLocally(something.name, 'some_treatment', 100);
let lol1Split = cache.getSplit(something.name) as ISplit;
expect(updated).toBe(true); // killLocally resolves with update if split is changed
expect(lol1Split.killed).toBe(true); // existing split must be killed
expect(lol1Split.defaultTreatment).toBe('some_treatment'); // existing split must have new default treatment
expect(lol1Split.changeNumber).toBe(100); // existing split must have the given change number
expect(cache.getChangeNumber()).toBe(initialChangeNumber); // cache changeNumber is not changed
// not update if changeNumber is old
updated = cache.killLocally(something.name, 'some_treatment_2', 90);
lol1Split = cache.getSplit(something.name) as ISplit;
expect(updated).toBe(false); // killLocally resolves without update if changeNumber is old
expect(lol1Split.defaultTreatment).not.toBe('some_treatment_2'); // existing split is not updated if given changeNumber is older
});
test('LocalStorage / usesSegments', () => {
const cache = new SplitsCacheInLocal(fullSettings, new KeyBuilderCS(PREFIX, 'user'), storage);
expect(cache.usesSegments()).toBe(true); // true initially, until data is synchronized
cache.setChangeNumber(1); // to indicate that data has been synced.
cache.update([splitWithUserTT, splitWithAccountTT], [], 1);
expect(cache.usesSegments()).toBe(false); // 0 splits using segments
cache.addSplit({ ...splitWithAccountTTAndUsesSegments, name: 'split3' });
expect(cache.usesSegments()).toBe(true); // 1 split using segments
cache.addSplit({ ...splitWithAccountTTAndUsesSegments, name: 'split4' });
expect(cache.usesSegments()).toBe(true); // 2 splits using segments
cache.removeSplit('split3');
expect(cache.usesSegments()).toBe(true); // 1 split using segments
cache.removeSplit('split4');
expect(cache.usesSegments()).toBe(false); // 0 splits using segments
});
test('LocalStorage / flag set cache tests', () => {
// @ts-ignore
const cache = new SplitsCacheInLocal({
...fullSettings,
sync: { // @ts-expect-error
__splitFiltersValidation: {
groupedFilters: { bySet: ['o', 'n', 'e', 'x'], byName: [], byPrefix: [] },
queryString: '&sets=e,n,o,x',
}
}
}, new KeyBuilderCS(PREFIX, 'user'), storage);
const emptySet = new Set([]);
cache.update([
featureFlagOne,
featureFlagTwo,
featureFlagThree,
], [], -1);
cache.addSplit(featureFlagWithEmptyFS);
// Adding an existing FF should not affect the cache
cache.update([featureFlagTwo], [], -1);
expect(cache.getNamesByFlagSets(['o'])).toEqual([new Set(['ff_one', 'ff_two'])]);
expect(cache.getNamesByFlagSets(['n'])).toEqual([new Set(['ff_one'])]);
expect(cache.getNamesByFlagSets(['e'])).toEqual([new Set(['ff_one', 'ff_three'])]);
expect(cache.getNamesByFlagSets(['t'])).toEqual([emptySet]); // 't' not in filter
expect(cache.getNamesByFlagSets(['o', 'n', 'e'])).toEqual([new Set(['ff_one', 'ff_two']), new Set(['ff_one']), new Set(['ff_one', 'ff_three'])]);
cache.addSplit({ ...featureFlagOne, sets: ['1'] });
expect(cache.getNamesByFlagSets(['1'])).toEqual([emptySet]); // '1' not in filter
expect(cache.getNamesByFlagSets(['o'])).toEqual([new Set(['ff_two'])]);
expect(cache.getNamesByFlagSets(['n'])).toEqual([emptySet]);
cache.addSplit({ ...featureFlagOne, sets: ['x'] });
expect(cache.getNamesByFlagSets(['x'])).toEqual([new Set(['ff_one'])]);
expect(cache.getNamesByFlagSets(['o', 'e', 'x'])).toEqual([new Set(['ff_two']), new Set(['ff_three']), new Set(['ff_one'])]);
cache.removeSplit(featureFlagOne.name);
expect(cache.getNamesByFlagSets(['x'])).toEqual([emptySet]);
cache.removeSplit(featureFlagOne.name);
expect(cache.getNamesByFlagSets(['y'])).toEqual([emptySet]); // 'y' not in filter
expect(cache.getNamesByFlagSets([])).toEqual([]);
cache.addSplit(featureFlagWithoutFS);
expect(cache.getNamesByFlagSets([])).toEqual([]);
});
// if FlagSets are not defined, it should store all FlagSets in memory.
test('LocalStorage / flag set cache tests without filters', () => {
const cache = new SplitsCacheInLocal(fullSettings, new KeyBuilderCS(PREFIX, 'user'), storage);
const emptySet = new Set([]);
cache.update([
featureFlagOne,
featureFlagTwo,
featureFlagThree,
], [], -1);
cache.addSplit(featureFlagWithEmptyFS);
expect(cache.getNamesByFlagSets(['o'])).toEqual([new Set(['ff_one', 'ff_two'])]);
expect(cache.getNamesByFlagSets(['n'])).toEqual([new Set(['ff_one'])]);
expect(cache.getNamesByFlagSets(['e'])).toEqual([new Set(['ff_one', 'ff_three'])]);
expect(cache.getNamesByFlagSets(['t'])).toEqual([new Set(['ff_two', 'ff_three'])]);
expect(cache.getNamesByFlagSets(['y'])).toEqual([emptySet]);
expect(cache.getNamesByFlagSets(['o', 'n', 'e'])).toEqual([new Set(['ff_one', 'ff_two']), new Set(['ff_one']), new Set(['ff_one', 'ff_three'])]);
// Validate that the feature flag cache is cleared when calling `clear` method
cache.clear();
expect(storage.length).toBe(0);
});
});