16
16
17
17
import { describe , it , expect , vi } from 'vitest' ;
18
18
19
- import { DefaultVuidManager } from './vuid_manager'
20
- import { getMockAsyncCache , getMockSyncCache } from '../tests/mock/mock_cache' ;
19
+ import { DefaultVuidManager , VuidCacheManager } from './vuid_manager' ;
20
+
21
+ import { getMockAsyncCache } from '../tests/mock/mock_cache' ;
21
22
import { isVuid } from './vuid' ;
22
23
import { resolvablePromise } from '../utils/promise/resolvablePromise' ;
23
24
import { exhaustMicrotasks } from '../tests/testUtils' ;
24
25
25
26
const vuidCacheKey = 'optimizely-vuid' ;
26
27
27
- describe ( 'DefaultVuidManager' , ( ) => { ;
28
- describe ( 'when configured with enableVuid = true' , ( ) => {
29
- it ( 'should create and save a new vuid if there is no vuid in cache' , async ( ) => {
30
- const cache = getMockSyncCache < string > ( ) ;
31
- const manager = new DefaultVuidManager ( cache ) ;
32
-
33
- await manager . configure ( { enableVuid : true } ) ;
34
-
35
- const savedVuid = cache . get ( vuidCacheKey ) ;
36
- expect ( isVuid ( manager . getVuid ( ) ! ) ) . toBe ( true ) ;
37
- expect ( savedVuid ) . toBe ( manager . getVuid ( ) ) ;
38
- } ) ;
39
-
40
- it ( 'should create and save a new vuid if old VUID from cache is not valid' , async ( ) => {
41
- const cache = getMockSyncCache < string > ( ) ;
42
- cache . set ( vuidCacheKey , 'invalid-vuid' ) ;
28
+ describe ( 'VuidCacheManager' , ( ) => {
29
+ it ( 'should remove vuid from cache' , async ( ) => {
30
+ const cache = getMockAsyncCache < string > ( ) ;
31
+ await cache . set ( vuidCacheKey , 'vuid_valid' ) ;
43
32
44
- const manager = new DefaultVuidManager ( cache ) ;
45
- await manager . configure ( { enableVuid : true } ) ;
46
-
47
- const savedVuid = cache . get ( vuidCacheKey ) ;
48
- expect ( isVuid ( manager . getVuid ( ) ! ) ) . toBe ( true ) ;
49
- expect ( savedVuid ) . toBe ( manager . getVuid ( ) ) ;
50
- } ) ;
33
+ const manager = new VuidCacheManager ( cache ) ;
34
+ await manager . remove ( ) ;
35
+ const vuidInCache = await cache . get ( vuidCacheKey ) ;
36
+ expect ( vuidInCache ) . toBeUndefined ( ) ;
37
+ } ) ;
51
38
52
- it ( 'should use the vuid in cache if available' , async ( ) => {
53
- const cache = getMockSyncCache < string > ( ) ;
54
- cache . set ( vuidCacheKey , 'vuid_valid' ) ;
39
+ it ( 'should create and save a new vuid if there is no vuid in cache' , async ( ) => {
40
+ const cache = getMockAsyncCache < string > ( ) ;
55
41
56
- const manager = new DefaultVuidManager ( cache ) ;
57
- await manager . configure ( { enableVuid : true } ) ;
42
+ const manager = new VuidCacheManager ( cache ) ;
43
+ const vuid = await manager . load ( ) ;
44
+ const vuidInCache = await cache . get ( vuidCacheKey ) ;
45
+ expect ( vuidInCache ) . toBe ( vuid ) ;
46
+ expect ( isVuid ( vuid ) ) . toBe ( true ) ;
47
+ } ) ;
58
48
59
- const savedVuid = cache . get ( vuidCacheKey ) ;
60
- expect ( isVuid ( manager . getVuid ( ) ! ) ) . toBe ( true ) ;
61
- expect ( savedVuid ) . toBe ( manager . getVuid ( ) ) ;
62
- expect ( savedVuid ) . toBe ( 'vuid_valid' ) ;
63
- } ) ;
49
+ it ( 'should create and save a new vuid if old VUID from cache is not valid' , async ( ) => {
50
+ const cache = getMockAsyncCache < string > ( ) ;
51
+ await cache . set ( vuidCacheKey , 'invalid-vuid' ) ;
52
+
53
+ const manager = new VuidCacheManager ( cache ) ;
54
+ const vuid = await manager . load ( ) ;
55
+ const vuidInCache = await cache . get ( vuidCacheKey ) ;
56
+ expect ( vuidInCache ) . toBe ( vuid ) ;
57
+ expect ( isVuid ( vuid ) ) . toBe ( true ) ;
64
58
} ) ;
65
59
66
- describe ( 'when configured with enableVuid = false' , ( ) => {
67
- it ( 'should remove existing vuid form memory and cache' , async ( ) => {
68
- const cache = getMockSyncCache < string > ( ) ;
69
- const manager = new DefaultVuidManager ( cache ) ;
60
+ it ( 'should return the same vuid without modifying the cache after creating a new vuid' , async ( ) => {
61
+ const cache = getMockAsyncCache < string > ( ) ;
70
62
71
- await manager . configure ( { enableVuid : true } ) ;
72
-
73
- const savedVuid = cache . get ( vuidCacheKey ) ;
74
- expect ( isVuid ( manager . getVuid ( ) ! ) ) . toBe ( true ) ;
75
- expect ( savedVuid ) . toBe ( manager . getVuid ( ) ) ;
63
+ const manager = new VuidCacheManager ( cache ) ;
64
+ const vuid1 = await manager . load ( ) ;
65
+ const vuid2 = await manager . load ( ) ;
66
+ expect ( vuid1 ) . toBe ( vuid2 ) ;
67
+ const vuidInCache = await cache . get ( vuidCacheKey ) ;
68
+ expect ( vuidInCache ) . toBe ( vuid1 ) ;
69
+ } ) ;
76
70
77
- await manager . configure ( { enableVuid : false } ) ;
78
- expect ( manager . getVuid ( ) ) . toBeUndefined ( ) ;
79
- expect ( cache . get ( vuidCacheKey ) ) . toBeUndefined ( ) ;
80
- } ) ;
71
+ it ( 'should use the vuid in cache if available' , async ( ) => {
72
+ const cache = getMockAsyncCache < string > ( ) ;
73
+ cache . set ( vuidCacheKey , 'vuid_valid' ) ;
74
+
75
+ const manager = new VuidCacheManager ( cache ) ;
76
+ const vuid1 = await manager . load ( ) ;
77
+ const vuid2 = await manager . load ( ) ;
78
+ expect ( vuid1 ) . toBe ( 'vuid_valid' ) ;
79
+ expect ( vuid1 ) . toBe ( 'vuid_valid' ) ;
80
+ const vuidInCache = await cache . get ( vuidCacheKey ) ;
81
+ expect ( vuidInCache ) . toBe ( 'vuid_valid' ) ;
81
82
} ) ;
82
83
83
- it ( 'should sequence configure calls' , async ( ) => {
84
+ it ( 'should sequence remove and load calls' , async ( ) => {
84
85
const cache = getMockAsyncCache < string > ( ) ;
85
86
const removeSpy = vi . spyOn ( cache , 'remove' ) ;
86
87
const getSpy = vi . spyOn ( cache , 'get' ) ;
@@ -95,16 +96,16 @@ describe('DefaultVuidManager', () => {;
95
96
const setPromise = resolvablePromise ( ) ;
96
97
setSpy . mockReturnValueOnce ( setPromise . promise ) ;
97
98
98
- const manager = new DefaultVuidManager ( cache ) ;
99
+ const manager = new VuidCacheManager ( cache ) ;
99
100
100
- // this should try to remove vuid , which should stay pending
101
- const configure1 = manager . configure ( { enableVuid : false } ) ;
101
+ // this should try to remove from cached , which should stay pending
102
+ const call1 = manager . remove ( ) ;
102
103
103
104
// this should try to get the vuid from store
104
- const configure2 = manager . configure ( { enableVuid : true } ) ;
105
+ const call2 = manager . load ( ) ;
105
106
106
107
// this should again try to remove vuid
107
- const configure3 = manager . configure ( { enableVuid : false } ) ;
108
+ const call3 = manager . remove ( ) ;
108
109
109
110
await exhaustMicrotasks ( ) ;
110
111
@@ -114,7 +115,7 @@ describe('DefaultVuidManager', () => {;
114
115
// this will resolve the first configure call
115
116
removePromise . resolve ( true ) ;
116
117
await exhaustMicrotasks ( ) ;
117
- await expect ( configure1 ) . resolves . not . toThrow ( ) ;
118
+ await expect ( call1 ) . resolves . not . toThrow ( ) ;
118
119
119
120
// this get call is from the second configure call
120
121
expect ( getSpy ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -123,11 +124,77 @@ describe('DefaultVuidManager', () => {;
123
124
// as the get call is pending, remove call from the third configure call should not yet happen
124
125
expect ( removeSpy ) . toHaveBeenCalledTimes ( 1 ) ;
125
126
126
- // this should fail the second configure call, allowing the third configure call to proceed
127
+ // this should fail the load call, allowing the second remnove call to proceed
127
128
getPromise . reject ( new Error ( 'get failed' ) ) ;
128
129
await exhaustMicrotasks ( ) ;
129
- await expect ( configure2 ) . rejects . toThrow ( ) ;
130
+ await expect ( call2 ) . rejects . toThrow ( ) ;
130
131
131
132
expect ( removeSpy ) . toHaveBeenCalledTimes ( 2 ) ;
132
133
} ) ;
133
134
} ) ;
135
+
136
+ describe ( 'DefaultVuidManager' , ( ) => {
137
+ it ( 'should return undefined for getVuid() before initialization' , async ( ) => {
138
+ const vuidCacheManager = {
139
+ remove : vi . fn ( ) ,
140
+ load : vi . fn ( ) ,
141
+ } as unknown as VuidCacheManager ;
142
+
143
+ const manager = new DefaultVuidManager ( {
144
+ vuidCacheManager,
145
+ enableVuid : true
146
+ } ) ;
147
+
148
+ expect ( manager . getVuid ( ) ) . toBeUndefined ( ) ;
149
+ } ) ;
150
+
151
+ it ( 'should call remove on VuidCacheManager if enableVuid is false' , async ( ) => {
152
+ const vuidCacheManager = {
153
+ remove : vi . fn ( ) ,
154
+ load : vi . fn ( ) ,
155
+ } as unknown as VuidCacheManager ;
156
+
157
+ const manager = new DefaultVuidManager ( {
158
+ vuidCacheManager,
159
+ enableVuid : false
160
+ } ) ;
161
+
162
+ await manager . initialize ( ) ;
163
+ expect ( vuidCacheManager . remove ) . toHaveBeenCalled ( ) ;
164
+ } ) ;
165
+
166
+ it ( 'should return undefined for getVuid() after initialization if enableVuid is false' , async ( ) => {
167
+ const vuidCacheManager = {
168
+ remove : vi . fn ( ) ,
169
+ load : vi . fn ( ) ,
170
+ } as unknown as VuidCacheManager ;
171
+
172
+ const manager = new DefaultVuidManager ( {
173
+ vuidCacheManager,
174
+ enableVuid : false
175
+ } ) ;
176
+
177
+ await manager . initialize ( ) ;
178
+ expect ( manager . getVuid ( ) ) . toBeUndefined ( ) ;
179
+ } ) ;
180
+
181
+ it ( 'should load vuid using VuidCacheManger if enableVuid=true' , async ( ) => {
182
+ const load = vi . fn ( ) ;
183
+
184
+ const vuidCacheManager = {
185
+ remove : vi . fn ( ) ,
186
+ load,
187
+ } as unknown as VuidCacheManager ;
188
+
189
+ load . mockResolvedValue ( 'vuid_valid' ) ;
190
+
191
+ const manager = new DefaultVuidManager ( {
192
+ vuidCacheManager,
193
+ enableVuid : true
194
+ } ) ;
195
+
196
+ await manager . initialize ( ) ;
197
+ expect ( vuidCacheManager . load ) . toHaveBeenCalled ( ) ;
198
+ expect ( manager . getVuid ( ) ) . toBe ( 'vuid_valid' ) ;
199
+ } ) ;
200
+ } ) ;
0 commit comments