Skip to content

Commit 9abba35

Browse files
committed
odpman test
1 parent 4a5f1ed commit 9abba35

File tree

2 files changed

+63
-268
lines changed

2 files changed

+63
-268
lines changed

lib/odp/odp_manager.spec.ts

Lines changed: 49 additions & 224 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ describe('DefaultOdpManager', () => {
289289

290290
it('fetches qualified segments correctly for both fs_user_id and vuid from segmentManager', async () => {
291291
const segmentManager = getMockOdpSegmentManager();
292-
segmentManager.fetchQualifiedSegments.mockImplementation((key: ODP_USER_KEY, ...arg) => {
292+
segmentManager.fetchQualifiedSegments.mockImplementation((key: ODP_USER_KEY) => {
293293
if (key === ODP_USER_KEY.FS_USER_ID) {
294294
return Promise.resolve(['fs1', 'fs2']);
295295
}
@@ -567,180 +567,70 @@ describe('DefaultOdpManager', () => {
567567
expect(data.get('device_type')).toEqual('phone');
568568
expect(data.get('model')).toEqual('model');
569569
});
570-
571-
// it('should call eventManager.identifyUser with correct parameters when identifyUser is called', async () => {
572-
// const odpIntegrationConfig: OdpIntegratedConfig = {
573-
// integrated: true,
574-
// odpConfig: new OdpConfig(keyA, hostA, pixelA, segmentsA)
575-
// };
576-
577-
// const odpManager = testOdpManager({
578-
// odpIntegrationConfig,
579-
// segmentManager,
580-
// eventManager,
581-
// logger,
582-
// vuidEnabled: true,
583-
// });
584-
585-
// await odpManager.onReady();
586-
587-
// const userId = 'user123';
588-
// const vuid = 'vuid_123';
589-
590-
// odpManager.identifyUser(userId, vuid);
591-
// const [userIdArg, vuidArg] = capture(mockEventManager.identifyUser).byCallIndex(0);
592-
// expect(userIdArg).toEqual(userId);
593-
// expect(vuidArg).toEqual(vuid);
594-
595-
// odpManager.identifyUser(userId);
596-
// const [userIdArg2, vuidArg2] = capture(mockEventManager.identifyUser).byCallIndex(1);
597-
// expect(userIdArg2).toEqual(userId);
598-
// expect(vuidArg2).toEqual(undefined);
599-
600-
// odpManager.identifyUser(vuid);
601-
// const [userIdArg3, vuidArg3] = capture(mockEventManager.identifyUser).byCallIndex(2);
602-
// expect(userIdArg3).toEqual(undefined);
603-
// expect(vuidArg3).toEqual(vuid);
604-
// });
605-
606-
// it('should send event with correct parameters', async () => {
607-
// const odpIntegrationConfig: OdpIntegratedConfig = {
608-
// integrated: true,
609-
// odpConfig: new OdpConfig(keyA, hostA, pixelA, segmentsA)
610-
// };
611-
612-
// const odpManager = testOdpManager({
613-
// odpIntegrationConfig,
614-
// segmentManager,
615-
// eventManager,
616-
// logger,
617-
// vuidEnabled: true,
618-
// });
619-
620-
// await odpManager.onReady();
621-
622-
// const identifiers = new Map([['email', '[email protected]']]);
623-
// const data = new Map([['key1', 'value1'], ['key2', 'value2']]);
624-
625-
// odpManager.sendEvent({
626-
// action: 'action',
627-
// type: 'type',
628-
// identifiers,
629-
// data,
630-
// });
631-
632-
// const [event] = capture(mockEventManager.sendEvent).byCallIndex(0);
633-
// expect(event.action).toEqual('action');
634-
// expect(event.type).toEqual('type');
635-
// expect(event.identifiers).toEqual(identifiers);
636-
// expect(event.data).toEqual(data);
637-
638-
// // should use `fullstack` as type if empty string is provided
639-
// odpManager.sendEvent({
640-
// type: '',
641-
// action: 'action',
642-
// identifiers,
643-
// data,
644-
// });
645570

646-
// const [event2] = capture(mockEventManager.sendEvent).byCallIndex(1);
647-
// expect(event2.action).toEqual('action');
648-
// expect(event2.type).toEqual('fullstack');
649-
// expect(event2.identifiers).toEqual(identifiers);
650-
// });
651-
652-
653-
// it('should throw an error if event action is empty string and not call eventManager', async () => {
654-
// const odpIntegrationConfig: OdpIntegratedConfig = {
655-
// integrated: true,
656-
// odpConfig: new OdpConfig(keyA, hostA, pixelA, segmentsA)
657-
// };
658-
659-
// const odpManager = testOdpManager({
660-
// odpIntegrationConfig,
661-
// segmentManager,
662-
// eventManager,
663-
// logger,
664-
// vuidEnabled: true,
665-
// });
666-
667-
// await odpManager.onReady();
668-
669-
// const identifiers = new Map([['email', '[email protected]']]);
670-
// const data = new Map([['key1', 'value1'], ['key2', 'value2']]);
671-
672-
// const sendEvent = () => odpManager.sendEvent({
673-
// action: '',
674-
// type: 'type',
675-
// identifiers,
676-
// data,
677-
// });
571+
it('sends identified event with both fs_user_id and vuid if both parameters are provided', async () => {
572+
const eventManager = getMockOdpEventManager();
573+
eventManager.onRunning.mockReturnValue(Promise.resolve());
678574

679-
// expect(sendEvent).toThrow('ODP action is not valid');
680-
// verify(mockEventManager.sendEvent(anything())).never();
681-
// });
575+
const mockSendEvents = vi.mocked(eventManager.sendEvent as OdpEventManager['sendEvent']);
682576

683-
// it('should throw an error if event data is invalid', async () => {
684-
// const odpIntegrationConfig: OdpIntegratedConfig = {
685-
// integrated: true,
686-
// odpConfig: new OdpConfig(keyA, hostA, pixelA, segmentsA)
687-
// };
577+
const odpManager = new DefaultOdpManager({
578+
segmentManager: getMockOdpSegmentManager(),
579+
eventManager,
580+
});
688581

689-
// const odpManager = testOdpManager({
690-
// odpIntegrationConfig,
691-
// segmentManager,
692-
// eventManager,
693-
// logger,
694-
// vuidEnabled: true,
695-
// });
582+
odpManager.start();
583+
odpManager.updateConfig({ integrated: true, odpConfig: config });
584+
await odpManager.onRunning();
696585

697-
// await odpManager.onReady();
586+
odpManager.identifyUser('user', 'vuid_a');
587+
expect(mockSendEvents).toHaveBeenCalledOnce();
588+
const { identifiers } = mockSendEvents.mock.calls[0][0];
589+
expect(identifiers).toEqual(new Map([['fs_user_id', 'user'], ['vuid', 'vuid_a']]));
590+
});
698591

699-
// const identifiers = new Map([['email', '[email protected]']]);
700-
// const data = new Map([['key1', {}]]);
592+
it('sends identified event when called with just fs_user_id in first parameter', async () => {
593+
const eventManager = getMockOdpEventManager();
594+
eventManager.onRunning.mockReturnValue(Promise.resolve());
701595

702-
// const sendEvent = () => odpManager.sendEvent({
703-
// action: 'action',
704-
// type: 'type',
705-
// identifiers,
706-
// data,
707-
// });
596+
const mockSendEvents = vi.mocked(eventManager.sendEvent as OdpEventManager['sendEvent']);
708597

709-
// expect(sendEvent).toThrow(ERROR_MESSAGES.ODP_INVALID_DATA);
710-
// verify(mockEventManager.sendEvent(anything())).never();
711-
// });
598+
const odpManager = new DefaultOdpManager({
599+
segmentManager: getMockOdpSegmentManager(),
600+
eventManager,
601+
});
712602

713-
// it('should fetch qualified segments correctly for both fs_user_id and vuid', async () => {
714-
// const userId = 'user123';
715-
// const vuid = 'vuid_123';
603+
odpManager.start();
604+
odpManager.updateConfig({ integrated: true, odpConfig: config });
605+
await odpManager.onRunning();
716606

717-
// when(mockSegmentManager.fetchQualifiedSegments(ODP_USER_KEY.FS_USER_ID, userId, anything()))
718-
// .thenResolve(['fs1', 'fs2']);
607+
odpManager.identifyUser('user');
608+
expect(mockSendEvents).toHaveBeenCalledOnce();
609+
const { identifiers } = mockSendEvents.mock.calls[0][0];
610+
expect(identifiers).toEqual(new Map([['fs_user_id', 'user']]));
611+
});
719612

720-
// when(mockSegmentManager.fetchQualifiedSegments(ODP_USER_KEY.VUID, vuid, anything()))
721-
// .thenResolve(['vuid1', 'vuid2']);
613+
it('sends identified event when called with just vuid in first parameter', async () => {
614+
const eventManager = getMockOdpEventManager();
615+
eventManager.onRunning.mockReturnValue(Promise.resolve());
722616

723-
// const odpIntegrationConfig: OdpIntegratedConfig = {
724-
// integrated: true,
725-
// odpConfig: new OdpConfig(keyA, hostA, pixelA, segmentsA)
726-
// };
617+
const mockSendEvents = vi.mocked(eventManager.sendEvent as OdpEventManager['sendEvent']);
727618

728-
// const odpManager = testOdpManager({
729-
// odpIntegrationConfig,
730-
// segmentManager: instance(mockSegmentManager),
731-
// eventManager,
732-
// logger,
733-
// vuidEnabled: true,
734-
// });
619+
const odpManager = new DefaultOdpManager({
620+
segmentManager: getMockOdpSegmentManager(),
621+
eventManager,
622+
});
735623

736-
// await odpManager.onReady();
624+
odpManager.start();
625+
odpManager.updateConfig({ integrated: true, odpConfig: config });
626+
await odpManager.onRunning();
737627

738-
// const fsSegments = await odpManager.fetchQualifiedSegments(userId);
739-
// expect(fsSegments).toEqual(['fs1', 'fs2']);
628+
odpManager.identifyUser('vuid_a');
629+
expect(mockSendEvents).toHaveBeenCalledOnce();
630+
const { identifiers } = mockSendEvents.mock.calls[0][0];
631+
expect(identifiers).toEqual(new Map([['vuid', 'vuid_a']]));
632+
});
740633

741-
// const vuidSegments = await odpManager.fetchQualifiedSegments(vuid);
742-
// expect(vuidSegments).toEqual(['vuid1', 'vuid2']);
743-
// });
744634

745635

746636
// it('should stop itself and eventManager if stop is called', async () => {
@@ -766,70 +656,5 @@ describe('DefaultOdpManager', () => {
766656
// });
767657

768658

769-
770-
// it('should drop relevant calls and log error when odpIntegrationConfig is not available', async () => {
771-
// const odpManager = testOdpManager({
772-
// odpIntegrationConfig: undefined,
773-
// segmentManager,
774-
// eventManager,
775-
// logger,
776-
// vuidEnabled: true,
777-
// });
778-
779-
// const segments = await odpManager.fetchQualifiedSegments('vuid_user1', []);
780-
// verify(mockLogger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_CONFIG_NOT_AVAILABLE)).once();
781-
// expect(segments).toBeNull();
782-
783-
// odpManager.identifyUser('vuid_user1');
784-
// verify(mockLogger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_CONFIG_NOT_AVAILABLE)).twice();
785-
// verify(mockEventManager.identifyUser(anything(), anything())).never();
786-
787-
// const identifiers = new Map([['email', '[email protected]']]);
788-
// const data = new Map([['key1', {}]]);
789-
790-
// odpManager.sendEvent({
791-
// action: 'action',
792-
// type: 'type',
793-
// identifiers,
794-
// data,
795-
// });
796-
797-
// verify(mockLogger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_CONFIG_NOT_AVAILABLE)).thrice();
798-
// verify(mockEventManager.sendEvent(anything())).never();
799-
800-
// });
801-
802-
// it('should drop relevant calls and log error when odp is not integrated', async () => {
803-
// const odpManager = testOdpManager({
804-
// odpIntegrationConfig: { integrated: false },
805-
// segmentManager,
806-
// eventManager,
807-
// logger,
808-
// vuidEnabled: true,
809-
// });
810-
811-
// await odpManager.onReady();
812-
813-
// const segments = await odpManager.fetchQualifiedSegments('vuid_user1', []);
814-
// verify(mockLogger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_NOT_INTEGRATED)).once();
815-
// expect(segments).toBeNull();
816-
817-
// odpManager.identifyUser('vuid_user1');
818-
// verify(mockLogger.log(LogLevel.INFO, ERROR_MESSAGES.ODP_NOT_INTEGRATED)).once();
819-
// verify(mockEventManager.identifyUser(anything(), anything())).never();
820-
821-
// const identifiers = new Map([['email', '[email protected]']]);
822-
// const data = new Map([['key1', {}]]);
823-
824-
// odpManager.sendEvent({
825-
// action: 'action',
826-
// type: 'type',
827-
// identifiers,
828-
// data,
829-
// });
830-
831-
// verify(mockLogger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_NOT_INTEGRATED)).twice();
832-
// verify(mockEventManager.sendEvent(anything())).never();
833-
// });
834659
});
835660

lib/odp/odp_manager.ts

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ import { UserAgentParser } from './ua_parser/user_agent_parser';
2828
import { CLIENT_VERSION, ERROR_MESSAGES, JAVASCRIPT_CLIENT_ENGINE } from '../utils/enums';
2929
import { ODP_DEFAULT_EVENT_TYPE, ODP_EVENT_ACTION, ODP_USER_KEY } from './constant';
3030
import { isVuid } from '../vuid/vuid';
31+
import { Maybe } from '../utils/type';
3132

3233
export interface OdpManager extends Service {
3334
updateConfig(odpIntegrationConfig: OdpIntegrationConfig): boolean;
3435
fetchQualifiedSegments(userId: string, options?: Array<OptimizelySegmentOption>): Promise<string[] | null>;
35-
identifyUser(userId?: string, vuid?: string): void;
36+
identifyUser(userId: string, vuid?: string): void;
3637
sendEvent(event: OdpEvent): void;
3738
setClientInfo(clientEngine: string, clientVersion: string): void;
3839
}
@@ -76,43 +77,8 @@ export class DefaultOdpManager extends BaseService implements OdpManager {
7677
Object.entries(userAgentInfo).filter(([_, value]) => value != null && value != undefined)
7778
);
7879
}
79-
80-
// const readinessDependencies: PromiseLike<unknown>[] = [this.configPromise, this.on];
81-
82-
// if (this.isVuidEnabled()) {
83-
// readinessDependencies.push(this.initializeVuid());
84-
// }
85-
// this.initPromise = Promise.all(readinessDependencies);
86-
87-
// this.onReady().then(() => {
88-
// this.ready = true;
89-
// if (this.isVuidEnabled() && this.status === Status.Running) {
90-
// this.registerVuid();
91-
// }
92-
// });
93-
94-
// if (odpIntegrationConfig) {
95-
// this.updateSettings(odpIntegrationConfig);
96-
// }
9780
}
9881

99-
// private async activate(): Promise<void> {
100-
// if (!this.odpIntegrationConfig) {
101-
// return;
102-
// }
103-
104-
// if (!this.odpIntegrationConfig.integrated) {
105-
// return;
106-
// }
107-
108-
// this.activityStatus = ActivityStatus.Activating;
109-
110-
// this.segmentManager.updateSettings(this.odpIntegrationConfig.odpConfig);
111-
// this.eventManager.updateSettings(this.odpIntegrationConfig.odpConfig);
112-
// this.eventManager.start();
113-
// return Promise.resolve();
114-
// }
115-
11682
setClientInfo(clientEngine: string, clientVersion: string): void {
11783
this.clientEngine = clientEngine;
11884
this.clientVersion = clientVersion;
@@ -203,19 +169,23 @@ export class DefaultOdpManager extends BaseService implements OdpManager {
203169
return this.segmentManager.fetchQualifiedSegments(ODP_USER_KEY.FS_USER_ID, userId, options);
204170
}
205171

206-
identifyUser(userId?: string, vuid?: string): void {
172+
identifyUser(userId: string, vuid?: string): void {
207173
const identifiers = new Map<string, string>();
208-
if (!userId && !vuid) {
209-
this.logger?.error(ERROR_MESSAGES.ODP_SEND_EVENT_FAILED_UID_MISSING);
210-
return;
174+
175+
let finalUserId: Maybe<string> = userId;
176+
let finalVuid: Maybe<string> = vuid;
177+
178+
if (!vuid && isVuid(userId)) {
179+
finalVuid = userId;
180+
finalUserId = undefined;
211181
}
212182

213-
if (vuid) {
214-
identifiers.set(ODP_USER_KEY.VUID, vuid);
183+
if (finalVuid) {
184+
identifiers.set(ODP_USER_KEY.VUID, finalVuid);
215185
}
216186

217-
if (userId) {
218-
identifiers.set(ODP_USER_KEY.FS_USER_ID, userId);
187+
if (finalUserId) {
188+
identifiers.set(ODP_USER_KEY.FS_USER_ID, finalUserId);
219189
}
220190

221191
const event = new OdpEvent(ODP_DEFAULT_EVENT_TYPE, ODP_EVENT_ACTION.IDENTIFIED, identifiers);

0 commit comments

Comments
 (0)