-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathRBSegmentsCacheSync.spec.ts
More file actions
74 lines (58 loc) · 3.18 KB
/
RBSegmentsCacheSync.spec.ts
File metadata and controls
74 lines (58 loc) · 3.18 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
import { RBSegmentsCacheInMemory } from '../inMemory/RBSegmentsCacheInMemory';
import { RBSegmentsCacheInLocal } from '../inLocalStorage/RBSegmentsCacheInLocal';
import { KeyBuilderCS } from '../KeyBuilderCS';
import { rbSegment, rbSegmentWithInSegmentMatcher } from '../__tests__/testUtils';
import { IRBSegmentsCacheSync } from '../types';
import { fullSettings } from '../../utils/settingsValidation/__tests__/settings.mocks';
const cacheInMemory = new RBSegmentsCacheInMemory();
const cacheInLocal = new RBSegmentsCacheInLocal(fullSettings, new KeyBuilderCS('SPLITIO', 'user'));
describe.each([cacheInMemory, cacheInLocal])('Rule-based segments cache sync (Memory & LocalStorage)', (cache: IRBSegmentsCacheSync) => {
beforeEach(() => {
cache.clear();
});
test('clear should reset the cache state', () => {
cache.update([rbSegment], [], 1);
expect(cache.getChangeNumber()).toBe(1);
expect(cache.get(rbSegment.name)).not.toBeNull();
cache.clear();
expect(cache.getChangeNumber()).toBe(-1);
expect(cache.get(rbSegment.name)).toBeNull();
});
test('update should add and remove segments correctly', () => {
// Add segments
expect(cache.update([rbSegment, rbSegmentWithInSegmentMatcher], [], 1)).toBe(true);
expect(cache.get(rbSegment.name)).toEqual(rbSegment);
expect(cache.get(rbSegmentWithInSegmentMatcher.name)).toEqual(rbSegmentWithInSegmentMatcher);
expect(cache.getChangeNumber()).toBe(1);
// Remove a segment
expect(cache.update([], [rbSegment], 2)).toBe(true);
expect(cache.get(rbSegment.name)).toBeNull();
expect(cache.get(rbSegmentWithInSegmentMatcher.name)).toEqual(rbSegmentWithInSegmentMatcher);
expect(cache.getChangeNumber()).toBe(2);
// Remove remaining segment
expect(cache.update([], [rbSegmentWithInSegmentMatcher], 3)).toBe(true);
expect(cache.get(rbSegment.name)).toBeNull();
expect(cache.get(rbSegmentWithInSegmentMatcher.name)).toBeNull();
expect(cache.getChangeNumber()).toBe(3);
// No changes
expect(cache.update([], [rbSegmentWithInSegmentMatcher], 4)).toBe(false);
expect(cache.getChangeNumber()).toBe(4);
});
test('contains should check for segment existence correctly', () => {
cache.update([rbSegment, rbSegmentWithInSegmentMatcher], [], 1);
expect(cache.contains(new Set([rbSegment.name]))).toBe(true);
expect(cache.contains(new Set([rbSegment.name, rbSegmentWithInSegmentMatcher.name]))).toBe(true);
expect(cache.contains(new Set(['nonexistent']))).toBe(false);
expect(cache.contains(new Set([rbSegment.name, 'nonexistent']))).toBe(false);
cache.update([], [rbSegment, rbSegmentWithInSegmentMatcher], 2);
});
test('usesSegments should track segments usage correctly', () => {
expect(cache.usesSegments()).toBe(true); // Initially true when changeNumber is -1
cache.update([rbSegment], [], 1); // rbSegment doesn't have IN_SEGMENT matcher
expect(cache.usesSegments()).toBe(false);
cache.update([rbSegmentWithInSegmentMatcher], [], 2); // rbSegmentWithInSegmentMatcher has IN_SEGMENT matcher
expect(cache.usesSegments()).toBe(true);
cache.clear();
expect(cache.usesSegments()).toBe(true); // True after clear since changeNumber is -1
});
});