Skip to content

Commit b0b614f

Browse files
committed
vuidman test
1 parent 653b2f0 commit b0b614f

File tree

3 files changed

+142
-79
lines changed

3 files changed

+142
-79
lines changed

lib/vuid/vuid_manager.spec.ts

Lines changed: 124 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,71 +16,72 @@
1616

1717
import { describe, it, expect, vi } from 'vitest';
1818

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';
2122
import { isVuid } from './vuid';
2223
import { resolvablePromise } from '../utils/promise/resolvablePromise';
2324
import { exhaustMicrotasks } from '../tests/testUtils';
2425

2526
const vuidCacheKey = 'optimizely-vuid';
2627

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');
4332

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+
});
5138

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>();
5541

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+
});
5848

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);
6458
});
6559

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>();
7062

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+
});
7670

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');
8182
});
8283

83-
it('should sequence configure calls', async() => {
84+
it('should sequence remove and load calls', async() => {
8485
const cache = getMockAsyncCache<string>();
8586
const removeSpy = vi.spyOn(cache, 'remove');
8687
const getSpy = vi.spyOn(cache, 'get');
@@ -95,16 +96,16 @@ describe('DefaultVuidManager', () => {;
9596
const setPromise = resolvablePromise();
9697
setSpy.mockReturnValueOnce(setPromise.promise);
9798

98-
const manager = new DefaultVuidManager(cache);
99+
const manager = new VuidCacheManager(cache);
99100

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();
102103

103104
// this should try to get the vuid from store
104-
const configure2 = manager.configure({ enableVuid: true });
105+
const call2 = manager.load();
105106

106107
// this should again try to remove vuid
107-
const configure3 = manager.configure({ enableVuid: false });
108+
const call3 = manager.remove();
108109

109110
await exhaustMicrotasks();
110111

@@ -114,7 +115,7 @@ describe('DefaultVuidManager', () => {;
114115
// this will resolve the first configure call
115116
removePromise.resolve(true);
116117
await exhaustMicrotasks();
117-
await expect(configure1).resolves.not.toThrow();
118+
await expect(call1).resolves.not.toThrow();
118119

119120
// this get call is from the second configure call
120121
expect(getSpy).toHaveBeenCalledTimes(1);
@@ -123,11 +124,77 @@ describe('DefaultVuidManager', () => {;
123124
// as the get call is pending, remove call from the third configure call should not yet happen
124125
expect(removeSpy).toHaveBeenCalledTimes(1);
125126

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
127128
getPromise.reject(new Error('get failed'));
128129
await exhaustMicrotasks();
129-
await expect(configure2).rejects.toThrow();
130+
await expect(call2).rejects.toThrow();
130131

131132
expect(removeSpy).toHaveBeenCalledTimes(2);
132133
});
133134
});
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+
});

lib/vuid/vuid_manager.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { LogHandler } from '../modules/logging';
16+
import { LoggerFacade } from '../modules/logging';
1717
import { Cache } from '../utils/cache/cache';
1818
import { AsyncProducer, Maybe } from '../utils/type';
1919
import { isVuid, makeVuid } from './vuid';
@@ -25,25 +25,26 @@ export interface VuidManager {
2525
}
2626

2727
export class VuidCacheManager {
28-
private logger?: LogHandler;
28+
private logger?: LoggerFacade;
2929
private vuidCacheKey = 'optimizely-vuid';
3030
private cache: Cache<string>;
31-
// if this value is not undefined, this means the same value is in the cache
31+
// if this value is not undefined, this means the same value is in the cache.
3232
// if this is undefined, it could either mean that there is no value in the cache
33-
// or that there is a value in the cache but it has not been loaded yet
33+
// or that there is a value in the cache but it has not been loaded yet or failed
34+
// to load.
3435
private vuid?: string;
3536
private waitPromise: Promise<unknown> = Promise.resolve();
3637

37-
constructor(cache: Cache<string>, logger?: LogHandler) {
38+
constructor(cache: Cache<string>, logger?: LoggerFacade) {
3839
this.cache = cache;
3940
this.logger = logger;
4041
}
4142

42-
setLogger(logger: LogHandler): void {
43+
setLogger(logger: LoggerFacade): void {
4344
this.logger = logger;
4445
}
4546

46-
async serialize<T>(fn: AsyncProducer<T>): Promise<T> {
47+
private async serialize<T>(fn: AsyncProducer<T>): Promise<T> {
4748
const resultPromise = this.waitPromise.then(fn, fn);
4849
this.waitPromise = resultPromise.catch(() => {});
4950
return resultPromise;
@@ -63,19 +64,18 @@ export class VuidCacheManager {
6364
return this.vuid;
6465
}
6566

66-
const cachedValue = await this.cache.get(this.vuidCacheKey);
67-
if (cachedValue && isVuid(cachedValue)) {
68-
this.vuid = cachedValue;
69-
return this.vuid;
70-
}
71-
72-
const saveFn = async () => {
67+
const loadFn = async () => {
68+
const cachedValue = await this.cache.get(this.vuidCacheKey);
69+
if (cachedValue && isVuid(cachedValue)) {
70+
this.vuid = cachedValue;
71+
return this.vuid;
72+
}
7373
const newVuid = makeVuid();
7474
await this.cache.set(this.vuidCacheKey, newVuid);
7575
this.vuid = newVuid;
7676
return newVuid;
7777
}
78-
return this.serialize(saveFn);
78+
return this.serialize(loadFn);
7979
}
8080
}
8181

@@ -84,12 +84,10 @@ export type VuidManagerConfig = {
8484
vuidCacheManager: VuidCacheManager;
8585
}
8686

87-
export class DefaultVuidManger implements VuidManager {
87+
export class DefaultVuidManager implements VuidManager {
8888
private vuidCacheManager: VuidCacheManager;
89-
private logger?: LogHandler;
9089
private vuid?: string;
9190
private vuidEnabled = false;
92-
private initialized = false;
9391

9492
constructor(config: VuidManagerConfig) {
9593
this.vuidCacheManager = config.vuidCacheManager;
@@ -111,11 +109,9 @@ export class DefaultVuidManger implements VuidManager {
111109
async initialize(): Promise<void> {
112110
if (!this.vuidEnabled) {
113111
await this.vuidCacheManager.remove();
114-
this.initialized = true;
115112
return;
116113
}
117114

118115
this.vuid = await this.vuidCacheManager.load();
119-
this.initialized = true;
120116
}
121117
}

lib/vuid/vuid_manager_factory.browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { DefaultVuidManger, VuidCacheManager, VuidManager } from './vuid_manager';
16+
import { DefaultVuidMaanger, VuidCacheManager, VuidManager } from './vuid_manager';
1717
import { LocalStorageCache } from '../utils/cache/local_storage_cache.browser';
1818
import { VuidManagerOptions } from './vuid_manager_factory';
1919

2020
export const vuidCacheManager = new VuidCacheManager(new LocalStorageCache<string>());
2121

2222
export const createVuidManager = (options: VuidManagerOptions): VuidManager => {
23-
return new DefaultVuidManger({
23+
return new DefaultVuidMaanger({
2424
vuidCacheManager,
2525
enableVuid: options.enableVuid
2626
});

0 commit comments

Comments
 (0)