@datadog/mobile-react-native 3.5.3 → 3.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/android/build.gradle +8 -7
  2. package/android/src/main/kotlin/com/datadog/reactnative/SdkVersion.kt +1 -1
  3. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt +94 -49
  4. package/ios/Sources/SdkVersion.swift +1 -1
  5. package/lib/commonjs/flags/FlagsClient.js +268 -3
  6. package/lib/commonjs/flags/FlagsClient.js.map +1 -1
  7. package/lib/commonjs/flags/configuration/context.js +70 -0
  8. package/lib/commonjs/flags/configuration/context.js.map +1 -0
  9. package/lib/commonjs/flags/configuration/index.js +39 -0
  10. package/lib/commonjs/flags/configuration/index.js.map +1 -0
  11. package/lib/commonjs/flags/configuration/precomputed.js +172 -0
  12. package/lib/commonjs/flags/configuration/precomputed.js.map +1 -0
  13. package/lib/commonjs/flags/configuration/types.js +33 -0
  14. package/lib/commonjs/flags/configuration/types.js.map +1 -0
  15. package/lib/commonjs/flags/configuration/wire.js +19 -0
  16. package/lib/commonjs/flags/configuration/wire.js.map +1 -0
  17. package/lib/commonjs/flags/internal.js +7 -3
  18. package/lib/commonjs/flags/internal.js.map +1 -1
  19. package/lib/commonjs/index.js +13 -0
  20. package/lib/commonjs/index.js.map +1 -1
  21. package/lib/commonjs/version.js +1 -1
  22. package/lib/module/flags/FlagsClient.js +268 -3
  23. package/lib/module/flags/FlagsClient.js.map +1 -1
  24. package/lib/module/flags/configuration/context.js +62 -0
  25. package/lib/module/flags/configuration/context.js.map +1 -0
  26. package/lib/module/flags/configuration/index.js +18 -0
  27. package/lib/module/flags/configuration/index.js.map +1 -0
  28. package/lib/module/flags/configuration/precomputed.js +165 -0
  29. package/lib/module/flags/configuration/precomputed.js.map +1 -0
  30. package/lib/module/flags/configuration/types.js +27 -0
  31. package/lib/module/flags/configuration/types.js.map +1 -0
  32. package/lib/module/flags/configuration/wire.js +13 -0
  33. package/lib/module/flags/configuration/wire.js.map +1 -0
  34. package/lib/module/flags/internal.js +7 -3
  35. package/lib/module/flags/internal.js.map +1 -1
  36. package/lib/module/index.js +2 -1
  37. package/lib/module/index.js.map +1 -1
  38. package/lib/module/version.js +1 -1
  39. package/lib/typescript/flags/FlagsClient.d.ts +96 -0
  40. package/lib/typescript/flags/FlagsClient.d.ts.map +1 -1
  41. package/lib/typescript/flags/configuration/context.d.ts +20 -0
  42. package/lib/typescript/flags/configuration/context.d.ts.map +1 -0
  43. package/lib/typescript/flags/configuration/index.d.ts +5 -0
  44. package/lib/typescript/flags/configuration/index.d.ts.map +1 -0
  45. package/lib/typescript/flags/configuration/precomputed.d.ts +25 -0
  46. package/lib/typescript/flags/configuration/precomputed.d.ts.map +1 -0
  47. package/lib/typescript/flags/configuration/types.d.ts +24 -0
  48. package/lib/typescript/flags/configuration/types.d.ts.map +1 -0
  49. package/lib/typescript/flags/configuration/wire.d.ts +2 -0
  50. package/lib/typescript/flags/configuration/wire.d.ts.map +1 -0
  51. package/lib/typescript/flags/internal.d.ts.map +1 -1
  52. package/lib/typescript/flags/types.d.ts +1 -1
  53. package/lib/typescript/flags/types.d.ts.map +1 -1
  54. package/lib/typescript/index.d.ts +4 -2
  55. package/lib/typescript/index.d.ts.map +1 -1
  56. package/lib/typescript/version.d.ts +1 -1
  57. package/package.json +3 -2
  58. package/src/flags/FlagsClient.ts +305 -3
  59. package/src/flags/__tests__/FlagsClient.test.ts +596 -0
  60. package/src/flags/__tests__/internal.test.ts +45 -0
  61. package/src/flags/configuration/__tests__/context.test.ts +120 -0
  62. package/src/flags/configuration/__tests__/precomputed.test.ts +301 -0
  63. package/src/flags/configuration/__tests__/wire.test.ts +127 -0
  64. package/src/flags/configuration/context.ts +76 -0
  65. package/src/flags/configuration/index.ts +27 -0
  66. package/src/flags/configuration/precomputed.ts +215 -0
  67. package/src/flags/configuration/types.ts +47 -0
  68. package/src/flags/configuration/wire.ts +15 -0
  69. package/src/flags/internal.ts +6 -3
  70. package/src/flags/types.ts +3 -1
  71. package/src/index.tsx +8 -0
  72. package/src/version.ts +1 -1
@@ -9,6 +9,7 @@ import { NativeModules } from 'react-native';
9
9
  import { InternalLog } from '../../InternalLog';
10
10
  import { SdkVerbosity } from '../../config/types/SdkVerbosity';
11
11
  import { DdFlags } from '../DdFlags';
12
+ import { configurationFromString } from '../configuration';
12
13
 
13
14
  jest.spyOn(NativeModules.DdFlags, 'setEvaluationContext').mockResolvedValue({
14
15
  'test-boolean-flag': {
@@ -117,6 +118,29 @@ describe('FlagsClient', () => {
117
118
  SdkVerbosity.WARN
118
119
  );
119
120
  });
121
+
122
+ it('clears a prior offline error status on a successful fetch', async () => {
123
+ const flagsClient = DdFlags.getClient();
124
+
125
+ // An offline op with no configuration loaded leaves the client in an error status.
126
+ flagsClient.setEvaluationContextWithoutFetching({
127
+ targetingKey: 'test-user-1',
128
+ attributes: {}
129
+ });
130
+ expect(
131
+ flagsClient.getBooleanDetails('test-boolean-flag', false)
132
+ ).toMatchObject({ errorCode: 'PROVIDER_NOT_READY' });
133
+
134
+ // A successful online fetch is authoritative and must serve the fetched flags rather
135
+ // than keep returning coded defaults from the stale error status.
136
+ await flagsClient.setEvaluationContext({
137
+ targetingKey: 'test-user-1',
138
+ attributes: {}
139
+ });
140
+ expect(
141
+ flagsClient.getBooleanValue('test-boolean-flag', false)
142
+ ).toBe(true);
143
+ });
120
144
  });
121
145
 
122
146
  describe('getDetails', () => {
@@ -317,4 +341,576 @@ describe('FlagsClient', () => {
317
341
  expect(numberFlagAsString).toBe('default');
318
342
  });
319
343
  });
344
+
345
+ const offlineFlags = {
346
+ 'offline-bool': {
347
+ variationType: 'boolean',
348
+ variationValue: true,
349
+ variationKey: 'true',
350
+ allocationKey: 'alloc-1',
351
+ reason: 'STATIC',
352
+ doLog: false,
353
+ extraLogging: {}
354
+ }
355
+ };
356
+
357
+ const buildConfig = (
358
+ flags: Record<string, unknown>,
359
+ context?: Record<string, unknown>,
360
+ obfuscated = false
361
+ ) =>
362
+ configurationFromString(
363
+ JSON.stringify({
364
+ version: 1,
365
+ precomputed: {
366
+ response: JSON.stringify({
367
+ data: { attributes: { obfuscated, flags } }
368
+ }),
369
+ context
370
+ }
371
+ })
372
+ );
373
+
374
+ describe('setConfiguration', () => {
375
+ it('serves flags from the configuration without a native fetch', () => {
376
+ const flagsClient = DdFlags.getClient();
377
+
378
+ const result = flagsClient.setConfiguration(
379
+ buildConfig(offlineFlags, {
380
+ targetingKey: 'user-1',
381
+ country: 'US'
382
+ })
383
+ );
384
+
385
+ expect(result).toEqual({ status: 'ready' });
386
+ expect(flagsClient.getBooleanValue('offline-bool', false)).toBe(
387
+ true
388
+ );
389
+ expect(
390
+ NativeModules.DdFlags.setEvaluationContext
391
+ ).not.toHaveBeenCalled();
392
+ });
393
+
394
+ it('serves flags when an explicit matching context was set first', async () => {
395
+ const flagsClient = DdFlags.getClient();
396
+ await flagsClient.setEvaluationContext({
397
+ targetingKey: 'user-1',
398
+ attributes: { country: 'US' }
399
+ });
400
+
401
+ flagsClient.setConfiguration(
402
+ buildConfig(offlineFlags, {
403
+ targetingKey: 'user-1',
404
+ country: 'US'
405
+ })
406
+ );
407
+
408
+ expect(flagsClient.getBooleanValue('offline-bool', false)).toBe(
409
+ true
410
+ );
411
+ });
412
+
413
+ it('errors with INVALID_CONTEXT and serves defaults when an explicit context differs', async () => {
414
+ const flagsClient = DdFlags.getClient();
415
+ await flagsClient.setEvaluationContext({
416
+ targetingKey: 'user-1',
417
+ attributes: { country: 'US' }
418
+ });
419
+
420
+ const result = flagsClient.setConfiguration(
421
+ buildConfig(offlineFlags, {
422
+ targetingKey: 'user-2',
423
+ country: 'US'
424
+ })
425
+ );
426
+
427
+ // Offline precomputed is single-subject: a differing context cannot be served, so
428
+ // it is an error and evaluation returns the coded default with INVALID_CONTEXT.
429
+ expect(result).toEqual({
430
+ status: 'error',
431
+ errorCode: 'INVALID_CONTEXT'
432
+ });
433
+ expect(
434
+ flagsClient.getBooleanDetails('offline-bool', false)
435
+ ).toMatchObject({
436
+ value: false,
437
+ reason: 'ERROR',
438
+ errorCode: 'INVALID_CONTEXT'
439
+ });
440
+ // No exposure is tracked while serving defaults.
441
+ expect(
442
+ NativeModules.DdFlags.trackEvaluation
443
+ ).not.toHaveBeenCalled();
444
+ });
445
+
446
+ it('errors with GENERAL for an empty/unparseable configuration', () => {
447
+ const flagsClient = DdFlags.getClient();
448
+
449
+ const result = flagsClient.setConfiguration(
450
+ configurationFromString('garbage')
451
+ );
452
+
453
+ expect(result).toEqual({ status: 'error', errorCode: 'GENERAL' });
454
+ expect(
455
+ flagsClient.getBooleanDetails('offline-bool', false)
456
+ ).toMatchObject({
457
+ value: false,
458
+ reason: 'ERROR',
459
+ errorCode: 'GENERAL'
460
+ });
461
+ });
462
+
463
+ it('errors with GENERAL for an unsupported (obfuscated) configuration', () => {
464
+ const flagsClient = DdFlags.getClient();
465
+
466
+ const result = flagsClient.setConfiguration(
467
+ buildConfig(offlineFlags, { targetingKey: 'user-1' }, true)
468
+ );
469
+
470
+ expect(result).toEqual({ status: 'error', errorCode: 'GENERAL' });
471
+ expect(
472
+ flagsClient.getBooleanDetails('offline-bool', false)
473
+ ).toMatchObject({ errorCode: 'GENERAL' });
474
+ });
475
+
476
+ it('errors with GENERAL for a structurally malformed response envelope', () => {
477
+ const flagsClient = DdFlags.getClient();
478
+
479
+ // A wire whose precomputed response omits `data.attributes.flags` entirely.
480
+ const wire = JSON.stringify({
481
+ version: 1,
482
+ precomputed: {
483
+ response: JSON.stringify({ data: { attributes: {} } }),
484
+ context: { targetingKey: 'user-1' }
485
+ }
486
+ });
487
+
488
+ const result = flagsClient.setConfiguration(
489
+ configurationFromString(wire)
490
+ );
491
+
492
+ expect(result).toEqual({ status: 'error', errorCode: 'GENERAL' });
493
+ expect(
494
+ flagsClient.getBooleanDetails('offline-bool', false)
495
+ ).toMatchObject({ value: false, errorCode: 'GENERAL' });
496
+ });
497
+
498
+ it('serves a context-agnostic configuration (no embedded context)', () => {
499
+ const flagsClient = DdFlags.getClient();
500
+
501
+ flagsClient.setConfiguration(buildConfig(offlineFlags));
502
+
503
+ expect(flagsClient.getBooleanValue('offline-bool', false)).toBe(
504
+ true
505
+ );
506
+ expect(
507
+ NativeModules.DdFlags.setEvaluationContext
508
+ ).not.toHaveBeenCalled();
509
+ });
510
+
511
+ it('serves the coded default with TYPE_MISMATCH for a primitive-valued object flag', () => {
512
+ const flagsClient = DdFlags.getClient();
513
+
514
+ // The decoder accepts any JSON value for an `object` flag, but evaluation requires an
515
+ // object. A primitive value (only possible from a malformed/hand-crafted wire, never a
516
+ // real Datadog config) is therefore served the coded default with TYPE_MISMATCH.
517
+ flagsClient.setConfiguration(
518
+ buildConfig(
519
+ {
520
+ 'json-primitive': {
521
+ variationType: 'object',
522
+ variationValue: 'not-an-object',
523
+ variationKey: 'primitive',
524
+ allocationKey: 'alloc-1',
525
+ reason: 'STATIC',
526
+ doLog: false,
527
+ extraLogging: {}
528
+ }
529
+ },
530
+ { targetingKey: 'user-1' }
531
+ )
532
+ );
533
+
534
+ expect(
535
+ flagsClient.getObjectDetails('json-primitive', {
536
+ fallback: true
537
+ })
538
+ ).toMatchObject({
539
+ value: { fallback: true },
540
+ reason: 'ERROR',
541
+ errorCode: 'TYPE_MISMATCH'
542
+ });
543
+ });
544
+
545
+ it('replaces a previously loaded configuration', () => {
546
+ const flagsClient = DdFlags.getClient();
547
+
548
+ flagsClient.setConfiguration(
549
+ buildConfig(
550
+ { 'flag-a': offlineFlags['offline-bool'] },
551
+ { targetingKey: 'user-1' }
552
+ )
553
+ );
554
+ expect(flagsClient.getBooleanValue('flag-a', false)).toBe(true);
555
+
556
+ flagsClient.setConfiguration(
557
+ buildConfig(
558
+ { 'flag-b': offlineFlags['offline-bool'] },
559
+ { targetingKey: 'user-1' }
560
+ )
561
+ );
562
+
563
+ expect(
564
+ flagsClient.getBooleanDetails('flag-a', false)
565
+ ).toMatchObject({ errorCode: 'FLAG_NOT_FOUND' });
566
+ expect(flagsClient.getBooleanValue('flag-b', false)).toBe(true);
567
+ });
568
+
569
+ it('replaces a snapshot for one subject with a snapshot for another (no external context)', () => {
570
+ const flagsClient = DdFlags.getClient();
571
+
572
+ // Load A for user-1 — no external context is set, so A's embedded context is adopted.
573
+ expect(
574
+ flagsClient.setConfiguration(
575
+ buildConfig(
576
+ { 'flag-a': offlineFlags['offline-bool'] },
577
+ { targetingKey: 'user-1' }
578
+ )
579
+ )
580
+ ).toEqual({ status: 'ready' });
581
+
582
+ // Replace with B for a DIFFERENT subject. Because the app never set an external
583
+ // context, this must adopt B's embedded context and stay ready — not error as a
584
+ // mismatch against A's adopted context.
585
+ expect(
586
+ flagsClient.setConfiguration(
587
+ buildConfig(
588
+ { 'flag-b': offlineFlags['offline-bool'] },
589
+ { targetingKey: 'user-2' }
590
+ )
591
+ )
592
+ ).toEqual({ status: 'ready' });
593
+ expect(flagsClient.getBooleanValue('flag-b', false)).toBe(true);
594
+ expect(
595
+ flagsClient.getBooleanDetails('flag-a', false)
596
+ ).toMatchObject({ errorCode: 'FLAG_NOT_FOUND' });
597
+
598
+ // The exposure must be attributed to B's embedded context (user-2), not A's — a
599
+ // regression that updated the flags but kept A's context would misattribute here.
600
+ expect(NativeModules.DdFlags.trackEvaluation).toHaveBeenCalledWith(
601
+ 'default',
602
+ 'flag-b',
603
+ expect.any(Object),
604
+ 'user-2',
605
+ {}
606
+ );
607
+ });
608
+
609
+ it('adopts a subject-bound snapshot loaded after a context-agnostic one (no external context)', () => {
610
+ const flagsClient = DdFlags.getClient();
611
+
612
+ flagsClient.setConfiguration(buildConfig(offlineFlags));
613
+ expect(flagsClient.getBooleanValue('offline-bool', false)).toBe(
614
+ true
615
+ );
616
+
617
+ expect(
618
+ flagsClient.setConfiguration(
619
+ buildConfig(
620
+ { 'flag-b': offlineFlags['offline-bool'] },
621
+ { targetingKey: 'user-2' }
622
+ )
623
+ )
624
+ ).toEqual({ status: 'ready' });
625
+ expect(flagsClient.getBooleanValue('flag-b', false)).toBe(true);
626
+ });
627
+
628
+ it('does not resurrect a prior valid snapshot after an invalid replacement', () => {
629
+ const flagsClient = DdFlags.getClient();
630
+
631
+ // Valid A.
632
+ flagsClient.setConfiguration(
633
+ buildConfig(offlineFlags, { targetingKey: 'user-1' })
634
+ );
635
+ expect(flagsClient.getBooleanValue('offline-bool', false)).toBe(
636
+ true
637
+ );
638
+
639
+ // Invalid replacement.
640
+ expect(
641
+ flagsClient.setConfiguration(configurationFromString('garbage'))
642
+ ).toEqual({ status: 'error', errorCode: 'GENERAL' });
643
+
644
+ // A later context change must NOT promote the invalid load back to ready.
645
+ const afterContextChange = flagsClient.setEvaluationContextWithoutFetching(
646
+ { targetingKey: 'user-1', attributes: {} }
647
+ );
648
+ expect(afterContextChange).toEqual({
649
+ status: 'error',
650
+ errorCode: 'GENERAL'
651
+ });
652
+ expect(
653
+ flagsClient.getBooleanDetails('offline-bool', false)
654
+ ).toMatchObject({ value: false, errorCode: 'GENERAL' });
655
+
656
+ // A valid replacement recovers.
657
+ expect(
658
+ flagsClient.setConfiguration(
659
+ buildConfig(offlineFlags, { targetingKey: 'user-1' })
660
+ )
661
+ ).toEqual({ status: 'ready' });
662
+ expect(flagsClient.getBooleanValue('offline-bool', false)).toBe(
663
+ true
664
+ );
665
+ });
666
+
667
+ it('is superseded by a later native fetch', async () => {
668
+ const flagsClient = DdFlags.getClient();
669
+ flagsClient.setConfiguration(
670
+ buildConfig(offlineFlags, {
671
+ targetingKey: 'user-1',
672
+ country: 'US'
673
+ })
674
+ );
675
+
676
+ // A subsequent explicit context fetch replaces the offline configuration
677
+ // with the native snapshot (mocked in __mocks__/react-native.ts + above).
678
+ await flagsClient.setEvaluationContext({
679
+ targetingKey: 'user-1',
680
+ attributes: { country: 'US' }
681
+ });
682
+
683
+ expect(
684
+ flagsClient.getBooleanDetails('offline-bool', false)
685
+ ).toMatchObject({ errorCode: 'FLAG_NOT_FOUND' });
686
+ expect(
687
+ flagsClient.getBooleanValue('test-boolean-flag', false)
688
+ ).toBe(true);
689
+ });
690
+
691
+ it('drops the offline overlay when entering online mode, serving defaults if the fetch fails', async () => {
692
+ const flagsClient = DdFlags.getClient();
693
+ flagsClient.setConfiguration(
694
+ buildConfig(offlineFlags, { targetingKey: 'user-1' })
695
+ );
696
+ expect(flagsClient.getBooleanValue('offline-bool', false)).toBe(
697
+ true
698
+ );
699
+
700
+ NativeModules.DdFlags.setEvaluationContext.mockRejectedValueOnce(
701
+ new Error('network down')
702
+ );
703
+ await expect(
704
+ flagsClient.setEvaluationContext({
705
+ targetingKey: 'user-1',
706
+ attributes: {}
707
+ })
708
+ ).rejects.toThrow('network down');
709
+
710
+ // Using one client for both modes is unsupported: the online fetch discards the offline
711
+ // overlay up front and warns, so a failed fetch serves coded defaults rather than the
712
+ // stale offline snapshot.
713
+ expect(flagsClient.getBooleanValue('offline-bool', false)).toBe(
714
+ false
715
+ );
716
+ expect(InternalLog.log).toHaveBeenCalledWith(
717
+ expect.stringContaining('online fetch was requested'),
718
+ expect.anything()
719
+ );
720
+ });
721
+ });
722
+
723
+ describe('setEvaluationContextWithoutFetching', () => {
724
+ it('reconciles a loaded config against a matching context without fetching', () => {
725
+ const flagsClient = DdFlags.getClient();
726
+ flagsClient.setConfiguration(
727
+ buildConfig(offlineFlags, {
728
+ targetingKey: 'user-1',
729
+ country: 'US'
730
+ })
731
+ );
732
+
733
+ const result = flagsClient.setEvaluationContextWithoutFetching({
734
+ targetingKey: 'user-1',
735
+ attributes: { country: 'US' }
736
+ });
737
+
738
+ expect(result).toEqual({ status: 'ready' });
739
+ expect(flagsClient.getBooleanValue('offline-bool', false)).toBe(
740
+ true
741
+ );
742
+ expect(
743
+ NativeModules.DdFlags.setEvaluationContext
744
+ ).not.toHaveBeenCalled();
745
+ });
746
+
747
+ it('errors with INVALID_CONTEXT on a differing context change, serving defaults', () => {
748
+ const flagsClient = DdFlags.getClient();
749
+ flagsClient.setConfiguration(
750
+ buildConfig(offlineFlags, {
751
+ targetingKey: 'user-1',
752
+ country: 'US'
753
+ })
754
+ );
755
+
756
+ const result = flagsClient.setEvaluationContextWithoutFetching({
757
+ targetingKey: 'user-2',
758
+ attributes: { country: 'US' }
759
+ });
760
+
761
+ expect(result).toEqual({
762
+ status: 'error',
763
+ errorCode: 'INVALID_CONTEXT'
764
+ });
765
+ expect(
766
+ flagsClient.getBooleanDetails('offline-bool', false)
767
+ ).toMatchObject({ value: false, errorCode: 'INVALID_CONTEXT' });
768
+ expect(
769
+ NativeModules.DdFlags.setEvaluationContext
770
+ ).not.toHaveBeenCalled();
771
+ // Defaults are served, so no exposure is tracked.
772
+ expect(
773
+ NativeModules.DdFlags.trackEvaluation
774
+ ).not.toHaveBeenCalled();
775
+ });
776
+
777
+ it('recovers to ready when a matching context is set after a mismatch', () => {
778
+ const flagsClient = DdFlags.getClient();
779
+ flagsClient.setConfiguration(
780
+ buildConfig(offlineFlags, { targetingKey: 'user-1' })
781
+ );
782
+
783
+ flagsClient.setEvaluationContextWithoutFetching({
784
+ targetingKey: 'user-2',
785
+ attributes: {}
786
+ });
787
+ expect(
788
+ flagsClient.getBooleanDetails('offline-bool', false)
789
+ ).toMatchObject({ errorCode: 'INVALID_CONTEXT' });
790
+
791
+ const recovered = flagsClient.setEvaluationContextWithoutFetching({
792
+ targetingKey: 'user-1',
793
+ attributes: {}
794
+ });
795
+
796
+ expect(recovered).toEqual({ status: 'ready' });
797
+ expect(flagsClient.getBooleanValue('offline-bool', false)).toBe(
798
+ true
799
+ );
800
+ });
801
+
802
+ it('returns PROVIDER_NOT_READY (not FLAG_NOT_FOUND) with no configuration loaded', () => {
803
+ const flagsClient = DdFlags.getClient();
804
+
805
+ const result = flagsClient.setEvaluationContextWithoutFetching({
806
+ targetingKey: 'user-1',
807
+ attributes: {}
808
+ });
809
+
810
+ expect(result).toEqual({
811
+ status: 'error',
812
+ errorCode: 'PROVIDER_NOT_READY'
813
+ });
814
+ expect(
815
+ flagsClient.getBooleanDetails('offline-bool', false)
816
+ ).toMatchObject({
817
+ value: false,
818
+ reason: 'ERROR',
819
+ errorCode: 'PROVIDER_NOT_READY'
820
+ });
821
+ });
822
+
823
+ it('treats an empty-string targeting key as a real (anonymous) subject that must match', () => {
824
+ const flagsClient = DdFlags.getClient();
825
+ flagsClient.setConfiguration(
826
+ buildConfig(offlineFlags, { targetingKey: 'user-1' })
827
+ );
828
+
829
+ // An empty string is a distinct (anonymous) subject, not "no context": it must match the
830
+ // snapshot's subject. Against a user-1 snapshot it does not, so this errors rather than
831
+ // silently serving user-1's flags to an anonymous subject.
832
+ const result = flagsClient.setEvaluationContextWithoutFetching({
833
+ targetingKey: '',
834
+ attributes: {}
835
+ });
836
+
837
+ expect(result).toEqual({
838
+ status: 'error',
839
+ errorCode: 'INVALID_CONTEXT'
840
+ });
841
+ });
842
+ });
843
+
844
+ describe('resetEvaluationContextWithoutFetching', () => {
845
+ it('re-adopts the embedded context, recovering from a mismatch', () => {
846
+ const flagsClient = DdFlags.getClient();
847
+ flagsClient.setConfiguration(
848
+ buildConfig(offlineFlags, { targetingKey: 'user-1' })
849
+ );
850
+
851
+ flagsClient.setEvaluationContextWithoutFetching({
852
+ targetingKey: 'user-2',
853
+ attributes: {}
854
+ });
855
+ expect(
856
+ flagsClient.getBooleanDetails('offline-bool', false)
857
+ ).toMatchObject({ errorCode: 'INVALID_CONTEXT' });
858
+
859
+ const result = flagsClient.resetEvaluationContextWithoutFetching();
860
+
861
+ expect(result).toEqual({ status: 'ready' });
862
+ expect(flagsClient.getBooleanValue('offline-bool', false)).toBe(
863
+ true
864
+ );
865
+ });
866
+
867
+ it('clears the override so a config loaded after reset adopts its embedded context', () => {
868
+ const flagsClient = DdFlags.getClient();
869
+
870
+ // No config yet, an external context is set, then cleared.
871
+ flagsClient.setEvaluationContextWithoutFetching({
872
+ targetingKey: 'user-2',
873
+ attributes: {}
874
+ });
875
+ flagsClient.resetEvaluationContextWithoutFetching();
876
+
877
+ // Loading A now adopts A's embedded context (not the stale user-2 override).
878
+ const result = flagsClient.setConfiguration(
879
+ buildConfig(offlineFlags, { targetingKey: 'user-1' })
880
+ );
881
+
882
+ expect(result).toEqual({ status: 'ready' });
883
+ expect(flagsClient.getBooleanValue('offline-bool', false)).toBe(
884
+ true
885
+ );
886
+ });
887
+
888
+ it('clears the override after an invalid load so a later valid load is ready', () => {
889
+ const flagsClient = DdFlags.getClient();
890
+
891
+ flagsClient.setConfiguration(configurationFromString('garbage'));
892
+ flagsClient.setEvaluationContextWithoutFetching({
893
+ targetingKey: 'user-2',
894
+ attributes: {}
895
+ });
896
+ flagsClient.resetEvaluationContextWithoutFetching();
897
+
898
+ const result = flagsClient.setConfiguration(
899
+ buildConfig(offlineFlags, { targetingKey: 'user-1' })
900
+ );
901
+
902
+ expect(result).toEqual({ status: 'ready' });
903
+ expect(flagsClient.getBooleanValue('offline-bool', false)).toBe(
904
+ true
905
+ );
906
+ });
907
+
908
+ it('returns PROVIDER_NOT_READY with no configuration loaded', () => {
909
+ const flagsClient = DdFlags.getClient();
910
+
911
+ expect(
912
+ flagsClient.resetEvaluationContextWithoutFetching()
913
+ ).toEqual({ status: 'error', errorCode: 'PROVIDER_NOT_READY' });
914
+ });
915
+ });
320
916
  });
@@ -0,0 +1,45 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ import { processEvaluationContext } from '../internal';
8
+
9
+ jest.mock('../../InternalLog', () => {
10
+ return {
11
+ InternalLog: { log: jest.fn() },
12
+ DATADOG_MESSAGE_PREFIX: 'DATADOG:'
13
+ };
14
+ });
15
+
16
+ describe('processEvaluationContext', () => {
17
+ it('keeps primitive attributes and drops non-primitive ones', () => {
18
+ expect(
19
+ processEvaluationContext({
20
+ targetingKey: 'user-1',
21
+ attributes: {
22
+ country: 'US',
23
+ age: 25,
24
+ beta: true,
25
+ // Dropped: non-primitive.
26
+ nested: { a: 1 } as never
27
+ }
28
+ })
29
+ ).toEqual({
30
+ targetingKey: 'user-1',
31
+ attributes: { country: 'US', age: 25, beta: true }
32
+ });
33
+ });
34
+
35
+ it('does not null the prototype for a "__proto__": null attribute', () => {
36
+ const result = processEvaluationContext({
37
+ targetingKey: 'user-1',
38
+ attributes: { ['__proto__']: null }
39
+ });
40
+
41
+ // A plain `attributes[key] = value` would have set the object's prototype to
42
+ // null here; the Map + Object.fromEntries build keeps it a normal object.
43
+ expect(Object.getPrototypeOf(result.attributes)).toBe(Object.prototype);
44
+ });
45
+ });