-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathutils.test.ts
More file actions
1667 lines (1377 loc) · 56.4 KB
/
Copy pathutils.test.ts
File metadata and controls
1667 lines (1377 loc) · 56.4 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable class-methods-use-this */
// eslint-disable-next-line max-classes-per-file
import type { Destination } from '@rudderstack/analytics-js-common/types/Destination';
import type {
SourceConfigurationOverride,
SourceConfigurationOverrideDestination,
} from '@rudderstack/analytics-js-common/types/LoadOptions';
import { defaultErrorHandler } from '@rudderstack/analytics-js-common/__mocks__/ErrorHandler';
import {
wait,
isDestinationReady,
createDestinationInstance,
isDestinationSDKMounted,
applySourceConfigurationOverrides,
applyOverrideToDestination,
filterDisabledDestination,
getCumulativeIntegrationsConfig,
initializeDestination,
} from '../../src/deviceModeDestinations/utils';
import type { DeviceModeDestinationsAnalyticsInstance } from '../../src/deviceModeDestinations/types';
import type { LogLevel } from '../../src/types/plugins';
import { resetState, state } from '../../__mocks__/state';
import { defaultLogger } from '@rudderstack/analytics-js-common/__mocks__/Logger';
describe('deviceModeDestinations utils', () => {
describe('wait', () => {
beforeEach(() => {
jest.useFakeTimers();
jest.setSystemTime(0);
});
afterEach(() => {
jest.useRealTimers();
});
it('should return a promise that resolves after the specified time', async () => {
const time = 1000;
const startTime = Date.now();
const waitPromise = wait(time);
// Advance the timers by the specified time
jest.runAllTimers();
await waitPromise;
const endTime = Date.now();
expect(endTime - startTime).toBeGreaterThanOrEqual(time);
});
it('should return a promise that resolves immediately even if the time is 0', async () => {
const time = 0;
const startTime = Date.now();
const waitPromise = wait(time);
// Advance the timers by the specified time
jest.runAllTimers();
await waitPromise;
const endTime = Date.now();
expect(endTime - startTime).toBeGreaterThanOrEqual(time);
});
it('should return a promise that resolves immediately even if the time is negative', async () => {
const time = -1000;
const startTime = Date.now();
const waitPromise = wait(time);
// Advance the timers by the next tick
jest.runAllTimers();
await waitPromise;
const endTime = Date.now();
expect(endTime - startTime).toBeGreaterThanOrEqual(0);
});
it('should return a promise that resolves immediately even if the time is not a number', async () => {
const time = '2 seconds';
const startTime = Date.now();
// @ts-expect-error intentionally passing a string
const waitPromise = wait(time);
// Advance the timers by the next tick
jest.runAllTimers();
await waitPromise;
const endTime = Date.now();
expect(endTime - startTime).toBeGreaterThanOrEqual(0);
});
});
describe('isDestinationReady', () => {
let isLoadedResponse = false;
const destination = {
instance: {
isLoaded: () => isLoadedResponse,
},
userFriendlyId: 'GA4___1234567890',
};
beforeEach(() => {
jest.useFakeTimers();
jest.setSystemTime(0);
});
afterEach(() => {
jest.useRealTimers();
isLoadedResponse = false;
});
it('should return a promise that gets resolved when the destination is ready immediately', async () => {
isLoadedResponse = true;
const isReadyPromise = isDestinationReady(destination as Destination);
// Fast-forward the timers
jest.runAllTimers();
await expect(isReadyPromise).resolves.toEqual(true);
});
it('should return a promise that gets resolved when the destination is ready after some time', async () => {
setTimeout(() => {
isLoadedResponse = true;
}, 1000);
const isReadyPromise = isDestinationReady(destination as Destination);
// Fast-forward the timers
jest.advanceTimersByTime(1000);
await expect(isReadyPromise).resolves.toEqual(true);
});
it('should return a promise that gets rejected when the destination is not ready after the timeout', async () => {
const isReadyPromise = isDestinationReady(destination as Destination);
// Fast-forward the timers to cause a timeout
jest.advanceTimersByTime(11000);
await expect(isReadyPromise).rejects.toThrow(new Error(`A timeout of 11000 ms occurred`));
});
});
describe('createDestinationInstance', () => {
class MockAnalytics implements DeviceModeDestinationsAnalyticsInstance {
page = () => {};
track = () => {};
identify = () => {};
group = () => {};
alias = () => {};
getAnonymousId = () => 'anonymousId';
getUserId = () => 'userId';
getUserTraits = () => ({ trait1: 'value1' });
getGroupId = () => 'groupId';
getGroupTraits = () => ({ trait2: 'value2' });
getSessionId = () => 123;
loadIntegration = true;
logLevel = 'DEBUG' as LogLevel;
loadOnlyIntegrations = { All: true };
}
// create two mock instances to later choose based on the write key
const mockAnalyticsInstanceWriteKey1 = new MockAnalytics();
const mockAnalyticsInstanceWriteKey2 = new MockAnalytics();
class MockRudderAnalytics {
getAnalyticsInstance = (writeKey: string) => {
const instancesMap: Record<string, MockAnalytics> = {
'1234567890': mockAnalyticsInstanceWriteKey1,
'12345678910': mockAnalyticsInstanceWriteKey2,
};
return instancesMap[writeKey];
};
}
const mockRudderAnalyticsInstance = new MockRudderAnalytics();
// put destination SDK code on the window object
const destSDKIdentifier = 'GA4_RS';
const sdkTypeName = 'GA4';
beforeAll(() => {
(window as any).rudderanalytics = mockRudderAnalyticsInstance;
(window as any)[destSDKIdentifier] = {
[sdkTypeName]: class {
config: any;
analytics: any;
constructor(config: any, analytics: any) {
this.config = config;
this.analytics = analytics;
}
},
};
});
afterAll(() => {
delete (window as any).rudderanalytics;
delete (window as any).GA4_RS;
});
it('should return an instance of the destination', () => {
state.lifecycle.writeKey.value = '12345678910'; // write key 2
const destination = {
config: {
apiKey: '1234',
},
areTransformationsConnected: false,
id: 'GA4___5678',
} as unknown as Destination;
const destinationInstance = createDestinationInstance(
destSDKIdentifier,
sdkTypeName,
destination,
state,
);
expect(destinationInstance).toBeInstanceOf((window as any)[destSDKIdentifier][sdkTypeName]);
expect(destinationInstance.config).toEqual(destination.config);
expect(destinationInstance.analytics).toEqual({
loadIntegration: true,
loadOnlyIntegrations: {},
logLevel: 'ERROR',
alias: expect.any(Function),
group: expect.any(Function),
identify: expect.any(Function),
page: expect.any(Function),
track: expect.any(Function),
getAnonymousId: expect.any(Function),
getGroupId: expect.any(Function),
getUserId: expect.any(Function),
getUserTraits: expect.any(Function),
getGroupTraits: expect.any(Function),
getSessionId: expect.any(Function),
});
expect(destinationInstance.analytics.getAnonymousId()).toEqual('anonymousId');
expect(destinationInstance.analytics.getUserId()).toEqual('userId');
expect(destinationInstance.analytics.getUserTraits()).toEqual({ trait1: 'value1' });
expect(destinationInstance.analytics.getGroupId()).toEqual('groupId');
expect(destinationInstance.analytics.getGroupTraits()).toEqual({ trait2: 'value2' });
expect(destinationInstance.analytics.getSessionId()).toEqual(123);
// Making sure that the call gets forwarded to the correct instance
const pageCallSpy = jest.spyOn(mockAnalyticsInstanceWriteKey2, 'page');
destinationInstance.analytics.page();
expect(mockAnalyticsInstanceWriteKey2.page).toHaveBeenCalled();
pageCallSpy.mockRestore();
resetState();
});
it('should handle when rudderanalytics global is missing', () => {
const originalRudderAnalytics = (globalThis as any).rudderanalytics;
delete (globalThis as any).rudderanalytics;
const destination = {
config: {
apiKey: '1234',
},
areTransformationsConnected: false,
id: 'GA4___5678',
} as unknown as Destination;
expect(() => {
createDestinationInstance(destSDKIdentifier, sdkTypeName, destination, state);
}).toThrow();
(globalThis as any).rudderanalytics = originalRudderAnalytics;
});
});
describe('isDestinationSDKMounted', () => {
const destSDKIdentifier = 'GA4_RS';
const sdkTypeName = 'GA4';
beforeEach(() => {});
afterEach(() => {
delete (window as any)[destSDKIdentifier];
});
it('should return false if the destination SDK is not evaluated', () => {
expect(isDestinationSDKMounted(destSDKIdentifier, sdkTypeName)).toEqual(false);
});
it('should return false if the destination SDK is mounted but it is not a constructable type', () => {
(window as any)[destSDKIdentifier] = {
[sdkTypeName]: 'not a constructable type',
};
expect(isDestinationSDKMounted(destSDKIdentifier, sdkTypeName)).toEqual(false);
});
it('should return true if the destination SDK is a constructable type', () => {
(window as any)[destSDKIdentifier] = {
[sdkTypeName]: class {
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
constructor() {}
},
};
expect(isDestinationSDKMounted(destSDKIdentifier, sdkTypeName)).toEqual(true);
});
it('should return false when SDK identifier exists but no SDK type', () => {
(window as any)[destSDKIdentifier] = {};
const result = isDestinationSDKMounted(destSDKIdentifier, sdkTypeName);
expect(result).toBe(false);
});
it('should return false when SDK type exists but no prototype', () => {
(window as any)[destSDKIdentifier] = {
[sdkTypeName]: {},
};
const result = isDestinationSDKMounted(destSDKIdentifier, sdkTypeName);
expect(result).toBe(false);
});
it('should work with logger parameter', () => {
const result = isDestinationSDKMounted(destSDKIdentifier, sdkTypeName, defaultLogger);
expect(result).toBe(false);
});
});
describe('applySourceConfigurationOverrides', () => {
const mockDestinations: Destination[] = [
{
id: 'dest1',
displayName: 'Destination 1',
userFriendlyId: 'dest1_friendly',
enabled: true,
shouldApplyDeviceModeTransformation: true,
propagateEventsUntransformedOnError: false,
config: {
apiKey: 'key1',
blacklistedEvents: [],
whitelistedEvents: [],
eventFilteringOption: 'disable' as const,
},
},
{
id: 'dest2',
displayName: 'Destination 2',
userFriendlyId: 'dest2_friendly',
enabled: false,
shouldApplyDeviceModeTransformation: false,
propagateEventsUntransformedOnError: true,
config: {
apiKey: 'key2',
blacklistedEvents: [],
whitelistedEvents: [],
eventFilteringOption: 'disable' as const,
},
},
{
id: 'dest3',
displayName: 'Destination 3',
userFriendlyId: 'dest3_friendly',
enabled: true,
shouldApplyDeviceModeTransformation: true,
propagateEventsUntransformedOnError: false,
config: {
apiKey: 'key3',
blacklistedEvents: [],
whitelistedEvents: [],
eventFilteringOption: 'disable' as const,
},
},
];
beforeEach(() => {
jest.clearAllMocks();
});
it('should return original enabled destinations when no override is provided', () => {
const result = applySourceConfigurationOverrides(mockDestinations, { destinations: [] });
expect(result).toEqual([mockDestinations[0], mockDestinations[2]]);
});
it('should return original enabled destinations when override is undefined', () => {
const result = applySourceConfigurationOverrides(mockDestinations, undefined as any);
expect(result).toEqual([mockDestinations[0], mockDestinations[2]]);
});
it('should return original enabled destinations when override destinations is undefined', () => {
const result = applySourceConfigurationOverrides(mockDestinations, {} as any);
expect(result).toEqual([mockDestinations[0], mockDestinations[2]]);
});
it('should apply enabled status override correctly', () => {
const override = {
destinations: [
{ id: 'dest1', enabled: false },
{ id: 'dest2', enabled: true },
],
};
const result = applySourceConfigurationOverrides(mockDestinations, override);
expect(result).toHaveLength(2);
expect(result[0]?.enabled).toBe(true);
expect(result[0]?.overridden).toBe(true);
expect(result[1]?.enabled).toBe(true);
expect(result[1]?.overridden).toBeUndefined();
});
it('should not override when enabled status matches existing value', () => {
const override = {
destinations: [
{ id: 'dest1', enabled: true }, // Same as existing
{ id: 'dest2', enabled: false }, // Same as existing
],
};
const result = applySourceConfigurationOverrides(mockDestinations, override);
expect(result).toHaveLength(2);
expect(result[0]).toBe(mockDestinations[0]); // Same reference
expect(result[0]?.enabled).toBe(true);
expect(result[0]?.overridden).toBeUndefined();
expect(result[1]).toBe(mockDestinations[2]); // Same reference
});
it('should log warning for unmatched destination IDs', () => {
const override = {
destinations: [
{ id: 'dest1', enabled: false },
{ id: 'nonexistent1', enabled: true },
{ id: 'nonexistent2', enabled: false },
],
};
applySourceConfigurationOverrides(mockDestinations, override, defaultLogger);
expect(defaultLogger.warn).toHaveBeenCalledWith(
'DeviceModeDestinationsPlugin:: Source configuration override - Unable to identify the destinations with the following IDs: "nonexistent1, nonexistent2"',
);
});
it('should not mutate original destinations', () => {
const originalDest1 = { ...mockDestinations[0]! };
const override = {
destinations: [{ id: 'dest1', enabled: false }],
};
applySourceConfigurationOverrides(mockDestinations, override);
expect(mockDestinations[0]!.enabled).toBe(originalDest1.enabled);
expect(mockDestinations[0]!.overridden).toBeUndefined();
});
it('should handle undefined enabled property in override', () => {
const override = {
destinations: [{ id: 'dest1', config: { newProperty: 'value' } }],
};
const result = applySourceConfigurationOverrides(mockDestinations, override);
expect(result[0]).not.toBe(mockDestinations[0]); // Different reference due to config override
expect(result[0]?.enabled).toBe(true); // Original value preserved
expect(result[0]?.overridden).toBe(true); // Marked as overridden due to config changes
expect((result[0]?.config as any).newProperty).toBe('value'); // Config override applied
});
it('should apply config override when destination is enabled via override', () => {
const override = {
destinations: [{ id: 'dest2', enabled: true, config: { newProperty: 'value' } }], // dest2 enabled via override
};
const result = applySourceConfigurationOverrides(mockDestinations, override);
expect(result[1]).not.toBe(mockDestinations[1]); // Different reference due to override
expect(result[1]?.enabled).toBe(true); // Enabled via override
expect(result[1]?.overridden).toBe(true); // Marked as overridden
expect((result[1]?.config as any).newProperty).toBe('value'); // Config override applied
});
it('should handle non-boolean enabled values in override', () => {
const override = {
destinations: [
{ id: 'dest1', enabled: 'true' as any }, // Invalid type
{ id: 'dest2', enabled: 1 as any }, // Invalid type
],
};
const result = applySourceConfigurationOverrides(mockDestinations, override);
expect(result[0]).toBe(mockDestinations[0]); // Same reference since no valid changes
expect(result[0]?.enabled).toBe(true); // Original value preserved
expect(result[0]?.overridden).toBeUndefined();
expect(result[1]).toBe(mockDestinations[2]); // Same reference since no valid changes
expect(result[1]?.enabled).toBe(true); // Original value preserved
expect(result[1]?.overridden).toBeUndefined();
});
it('should handle empty destinations array in override', () => {
const override = {
destinations: [],
};
const result = applySourceConfigurationOverrides(mockDestinations, override);
expect(result).toEqual([mockDestinations[0], mockDestinations[2]]);
});
// --- Cloning scenario ---
it('should clone destination when multiple overrides exist for the same destination id', () => {
const override = {
destinations: [
{ id: 'dest1', enabled: true, config: { apiKey: 'clone1' } },
{ id: 'dest1', enabled: true, config: { apiKey: 'clone2' } },
],
};
const result = applySourceConfigurationOverrides(mockDestinations, override);
// Should have 3 destinations: 2 clones for dest1, dest3
expect(result).toHaveLength(3);
// Both clones should be marked as cloned and 2nd clones config should be updated
const dest1Clones = result.filter(d => d.id.startsWith('dest1'));
expect(dest1Clones).toHaveLength(2);
expect(dest1Clones[0]?.cloned).toBe(true);
expect(dest1Clones[1]?.cloned).toBe(true);
expect(dest1Clones[0]?.config.apiKey).toBe('clone1');
expect(dest1Clones[1]?.config.apiKey).toBe('clone2');
// Enabled status should match override
expect(dest1Clones[0]?.enabled).toBe(true);
expect(dest1Clones[1]?.enabled).toBe(true);
// cloned destination should have originalId and originalId should be the original destination id
expect(dest1Clones[0]?.originalId).toBe('dest1');
expect(dest1Clones[1]?.originalId).toBe('dest1');
// dest2 and dest3 should be unchanged
expect(result.find(d => d.id === 'dest3')).toBe(mockDestinations[2]);
});
it('should clone destination and assign unique ids/userFriendlyIds for each clone', () => {
const override = {
destinations: [
{ id: 'dest2', enabled: true, config: { apiKey: 'cloneA' } },
{ id: 'dest2', enabled: false, config: { apiKey: 'cloneB' } },
],
};
const result = applySourceConfigurationOverrides(mockDestinations, override);
// Should have 3 destinations: 1 clones for dest2, dest1, dest3
expect(result).toHaveLength(3);
const dest2Clones = result.filter(d => d.id.startsWith('dest2'));
expect(dest2Clones).toHaveLength(1);
// Each clone should have a unique id and userFriendlyId
expect(dest2Clones[0]).toBeDefined();
expect(dest2Clones[0]?.id).toBe('dest2_1');
expect(dest2Clones[0]?.userFriendlyId).toBe('dest2_friendly_1');
expect(dest2Clones[0]?.originalId).toBe('dest2');
// Config and enabled status should match each override
expect(dest2Clones[0]?.config.apiKey).toBe('cloneA');
expect([true, false]).toContain(dest2Clones[0]?.enabled);
// dest1 and dest3 should be unchanged
expect(result.find(d => d.id === 'dest1')).toBe(mockDestinations[0]);
expect(result.find(d => d.id === 'dest3')).toBe(mockDestinations[2]);
});
it('should clone destination for each override and preserve other properties', () => {
const override = {
destinations: [
{ id: 'dest3', enabled: true, config: { apiKey: 'A' } },
{ id: 'dest3', enabled: true, config: { apiKey: 'B', extra: 123 } },
],
};
const result = applySourceConfigurationOverrides(mockDestinations, override);
// Should have 3 destinations: 2 clones for dest3, dest1
expect(result).toHaveLength(3);
const dest3Clones = result.filter(d => d.id.startsWith('dest3'));
expect(dest3Clones).toHaveLength(2);
// Properties from original should be preserved
dest3Clones.forEach(clone => {
expect(clone.displayName).toBe('Destination 3');
expect(clone.shouldApplyDeviceModeTransformation).toBe(true);
expect(clone.propagateEventsUntransformedOnError).toBe(false);
expect(clone.cloned).toBe(true);
});
// Config and enabled status should match each override
expect(dest3Clones[0]?.config?.apiKey).toBe('A');
expect(dest3Clones[1]?.config?.apiKey).toBe('B');
expect(dest3Clones[1]?.config?.extra).toBe(123);
expect(dest3Clones[0]?.enabled).toBe(true);
expect(dest3Clones[1]?.enabled).toBe(true);
// cloned destination should have originalId and originalId should be the original destination id
expect(dest3Clones[0]?.originalId).toBe('dest3');
expect(dest3Clones[1]?.originalId).toBe('dest3');
// inherit other config properties
expect(dest3Clones[0]?.config?.eventFilteringOption).toBe('disable');
expect(dest3Clones[1]?.config?.eventFilteringOption).toBe('disable');
expect(dest3Clones[0]?.config?.blacklistedEvents).toEqual([]);
expect(dest3Clones[1]?.config?.blacklistedEvents).toEqual([]);
expect(dest3Clones[0]?.config?.whitelistedEvents).toEqual([]);
expect(dest3Clones[1]?.config?.whitelistedEvents).toEqual([]);
// dest1 should be unchanged
expect(result.find(d => d.id === 'dest1')).toBe(mockDestinations[0]);
});
// ---- Filter destination ----
it('should filter out destinations that are not enabled', () => {
const override = {
destinations: [
{ id: 'dest1', enabled: true },
{ id: 'dest2', enabled: false }, // This should be filtered out
{ id: 'dest3', enabled: true },
],
};
const result = applySourceConfigurationOverrides(mockDestinations, override);
expect(result).toHaveLength(2);
expect(result[0]?.id).toBe('dest1');
expect(result[1]?.id).toBe('dest3');
});
it('should filter out destinations that are not enabled even if they have config overrides', () => {
const override = {
destinations: [
{ id: 'dest2', enabled: true },
{ id: 'dest1', enabled: false, config: { newProperty: 'value' } }, // This should be filtered out
{ id: 'dest3', enabled: true },
],
};
const result = applySourceConfigurationOverrides(mockDestinations, override);
expect(result).toHaveLength(2);
expect(result[0]?.id).toBe('dest2');
expect(result[1]?.id).toBe('dest3');
});
it('should not filter out destinations that are enabled', () => {
const override = {
destinations: [
{ id: 'dest1', enabled: true },
{ id: 'dest2', enabled: true }, // This should not be filtered out
{ id: 'dest3', enabled: true },
],
};
const result = applySourceConfigurationOverrides(mockDestinations, override);
expect(result).toHaveLength(3);
expect(result[0]?.id).toBe('dest1');
expect(result[1]?.id).toBe('dest2');
expect(result[2]?.id).toBe('dest3');
});
it('should filter out original destinations that are not enabled when override destination is provided', () => {
const override = {
destinations: [
{ id: 'dest1', enabled: true, config: { newProperty: 'value' } }, // This should be included
],
};
const result = applySourceConfigurationOverrides(mockDestinations, override);
expect(result).toHaveLength(2);
expect(result[0]?.id).toBe('dest1');
expect(result[1]?.id).toBe('dest3');
});
it('should filter out original destinations that are not enabled when override destination is empty', () => {
const override = {
destinations: [],
};
const result = applySourceConfigurationOverrides(mockDestinations, override);
expect(result).toHaveLength(2);
expect(result[0]?.id).toBe('dest1');
expect(result[1]?.id).toBe('dest3');
});
it('should apply multiple overrides to different destinations', () => {
const override: SourceConfigurationOverride = {
destinations: [
{ id: 'dest1', enabled: false },
{ id: 'dest2', enabled: true },
],
};
const result = applySourceConfigurationOverrides(mockDestinations, override);
expect(result).toHaveLength(2);
expect(result[0]!.enabled).toBe(true);
expect(result[0]!.overridden).toBe(true);
expect(result[1]!.enabled).toBe(true);
expect(result[1]!.overridden).toBeUndefined();
});
it('should not log warning when all destination IDs match', () => {
const mockWarn = jest.fn();
const mockLogger = { warn: mockWarn } as any;
const override: SourceConfigurationOverride = {
destinations: [
{ id: 'dest1', enabled: false },
{ id: 'dest2', enabled: true },
],
};
applySourceConfigurationOverrides(mockDestinations, override, mockLogger);
expect(mockWarn).not.toHaveBeenCalled();
});
it('should work without logger parameter', () => {
const override: SourceConfigurationOverride = {
destinations: [{ id: 'nonexistent', enabled: true }],
};
expect(() => {
applySourceConfigurationOverrides(mockDestinations, override);
}).not.toThrow();
});
it('should handle empty destinations array', () => {
const override: SourceConfigurationOverride = {
destinations: [{ id: 'dest1', enabled: false }],
};
const result = applySourceConfigurationOverrides([], override);
expect(result).toEqual([]);
});
it('should handle complex override scenarios', () => {
const override: SourceConfigurationOverride = {
destinations: [
{ id: 'dest1', enabled: false },
{ id: 'nonexistent', enabled: true },
],
};
const mockLogger = { warn: jest.fn() } as any;
const result = applySourceConfigurationOverrides(mockDestinations, override, mockLogger);
expect(result).toHaveLength(1);
expect(result[0]).toEqual(mockDestinations[2]); // unchanged
expect(mockLogger.warn).toHaveBeenCalledWith(expect.stringContaining('nonexistent'));
});
});
describe('applyOverrideToDestination', () => {
const mockDestination: Destination = {
id: 'dest1',
displayName: 'Destination 1',
userFriendlyId: 'dest1_friendly',
enabled: true,
shouldApplyDeviceModeTransformation: true,
propagateEventsUntransformedOnError: false,
config: {
apiKey: 'key1',
blacklistedEvents: [],
whitelistedEvents: [],
eventFilteringOption: 'disable' as const,
},
};
it('should return original destination when enabled status matches and no config override', () => {
const override = { id: 'dest1', enabled: true };
const result = applyOverrideToDestination(mockDestination, override);
expect(result).toBe(mockDestination); // Same reference
expect(result.overridden).toBeUndefined();
});
it('should clone and override enabled status when different', () => {
const override = { id: 'dest1', enabled: false };
const result = applyOverrideToDestination(mockDestination, override);
expect(result).not.toBe(mockDestination); // Different reference
expect(result.enabled).toBe(false);
expect(result.overridden).toBe(true);
expect(mockDestination.enabled).toBe(true); // Original unchanged
});
it('should clone when config override is provided even if enabled status matches', () => {
const override = {
id: 'dest1',
enabled: true,
config: { newProperty: 'value' },
};
const result = applyOverrideToDestination(mockDestination, override);
expect(result).not.toBe(mockDestination); // Different reference due to config override
expect(result.enabled).toBe(true);
expect(result.overridden).toBe(true); // Marked as overridden due to config changes
expect((result.config as any).newProperty).toBe('value'); // Config override applied
});
it('should return original destination when no changes needed', () => {
const override = { id: 'dest1' }; // No enabled or config specified
const result = applyOverrideToDestination(mockDestination, override);
expect(result).toBe(mockDestination); // Same reference since no changes
expect(result.overridden).toBeUndefined();
});
it('should return original destination when empty config override provided', () => {
const override = { id: 'dest1', config: {} }; // Empty config object
const result = applyOverrideToDestination(mockDestination, override);
expect(result).toBe(mockDestination); // Same reference since no actual changes
expect(result.overridden).toBeUndefined();
});
it('should handle non-boolean enabled values', () => {
const override = { id: 'dest1', enabled: 'false' as any }; // Invalid type
const result = applyOverrideToDestination(mockDestination, override);
expect(result).toBe(mockDestination); // Same reference since invalid enabled value
expect(result.enabled).toBe(true); // Original value preserved
expect(result.overridden).toBeUndefined();
});
it('should handle undefined enabled value', () => {
const override = { id: 'dest1', enabled: undefined };
const result = applyOverrideToDestination(mockDestination, override);
expect(result).toBe(mockDestination); // Same reference since no valid changes
expect(result.enabled).toBe(true); // Original value preserved
expect(result.overridden).toBeUndefined();
});
it('should handle null enabled value', () => {
const override = { id: 'dest1', enabled: null as any };
const result = applyOverrideToDestination(mockDestination, override);
expect(result).toBe(mockDestination); // Same reference since invalid enabled value
expect(result.enabled).toBe(true); // Original value preserved
expect(result.overridden).toBeUndefined();
});
it('should clone destination when cloneId is provided even with no other changes', () => {
const override = { id: 'dest1', enabled: true }; // Same as existing
const result = applyOverrideToDestination(mockDestination, override, 'clone1');
expect(result).not.toBe(mockDestination); // Different reference due to cloneId
expect(result.id).toBe('dest1_clone1');
expect(result.userFriendlyId).toBe('dest1_friendly_clone1');
expect(result.enabled).toBe(true);
expect(result.overridden).toBeUndefined(); // No override applied, just cloned
});
it('should clone and override when both cloneId and enabled change are provided', () => {
const override = { id: 'dest1', enabled: false };
const result = applyOverrideToDestination(mockDestination, override, 'clone1');
expect(result).not.toBe(mockDestination); // Different reference
expect(result.id).toBe('dest1_clone1');
expect(result.userFriendlyId).toBe('dest1_friendly_clone1');
expect(result.enabled).toBe(false);
expect(result.overridden).toBe(true);
});
it('should preserve all other destination properties when cloning', () => {
const override = { id: 'dest1', enabled: false };
const result = applyOverrideToDestination(mockDestination, override);
expect(result.displayName).toBe(mockDestination.displayName);
expect(result.shouldApplyDeviceModeTransformation).toBe(
mockDestination.shouldApplyDeviceModeTransformation,
);
expect(result.propagateEventsUntransformedOnError).toBe(
mockDestination.propagateEventsUntransformedOnError,
);
expect(result.config).toEqual(mockDestination.config);
expect(result.config).not.toBe(mockDestination.config); // Deep cloned
});
// Config override specific tests
it('should apply config overrides correctly', () => {
const override = {
id: 'dest1',
config: {
newProperty: 'newValue',
apiKey: 'overriddenKey', // Override existing property
},
};
const result = applyOverrideToDestination(mockDestination, override);
expect(result).not.toBe(mockDestination);
expect(result.overridden).toBe(true);
expect((result.config as any).newProperty).toBe('newValue');
expect(result.config.apiKey).toBe('overriddenKey');
expect(result.config.eventFilteringOption).toBe('disable'); // Inherited property
});
it('should remove properties when config value is null', () => {
const override = {
id: 'dest1',
config: {
apiKey: null, // Remove this property
newProperty: 'value',
},
};
const result = applyOverrideToDestination(mockDestination, override);
expect(result).not.toBe(mockDestination);
expect(result.overridden).toBe(true);
expect(result.config.apiKey).toBeUndefined(); // Property removed
expect((result.config as any).newProperty).toBe('value');
expect(result.config.eventFilteringOption).toBe('disable'); // Inherited property
});
it('should remove properties when config value is undefined', () => {
const override = {
id: 'dest1',
config: {
apiKey: undefined, // Remove this property
newProperty: 'value',
},
};
const result = applyOverrideToDestination(mockDestination, override);
expect(result).not.toBe(mockDestination);
expect(result.overridden).toBe(true);
expect(result.config.apiKey).toBeUndefined(); // Property removed
expect((result.config as any).newProperty).toBe('value');
expect(result.config.eventFilteringOption).toBe('disable'); // Inherited property
});
it('should handle data type changes in config override', () => {
const override = {
id: 'dest1',
config: {
apiKey: { nested: 'object' }, // String to object
blacklistedEvents: 'stringValue', // Array to string
},
};
const result = applyOverrideToDestination(mockDestination, override);
expect(result).not.toBe(mockDestination);
expect(result.overridden).toBe(true);
expect(result.config.apiKey).toEqual({ nested: 'object' });
expect(result.config.blacklistedEvents).toBe('stringValue');
});
it('should combine enabled and config overrides when destination remains enabled', () => {
const override = {
id: 'dest1',
enabled: true,
config: {
newProperty: 'value',
},
};
const result = applyOverrideToDestination(mockDestination, override);
expect(result).not.toBe(mockDestination);
expect(result.enabled).toBe(true);
expect(result.overridden).toBe(true);
expect((result.config as any).newProperty).toBe('value');
});
it('should not apply config override when destination is disabled via enabled override', () => {
const override = {
id: 'dest1',
enabled: false,
config: {
newProperty: 'value',
},
};
const result = applyOverrideToDestination(mockDestination, override);
expect(result).not.toBe(mockDestination);
expect(result.enabled).toBe(false);
expect(result.overridden).toBe(true); // Marked as overridden due to enabled change