@datadog/mobile-react-native-openfeature 3.5.3 → 3.6.0

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 (90) hide show
  1. package/README.md +77 -0
  2. package/lib/commonjs/coreProvider.js +79 -0
  3. package/lib/commonjs/coreProvider.js.map +1 -0
  4. package/lib/commonjs/index.js +14 -0
  5. package/lib/commonjs/index.js.map +1 -1
  6. package/lib/commonjs/mappers.js +50 -0
  7. package/lib/commonjs/mappers.js.map +1 -0
  8. package/lib/commonjs/offlineProvider.js +149 -0
  9. package/lib/commonjs/offlineProvider.js.map +1 -0
  10. package/lib/commonjs/provider.js +9 -73
  11. package/lib/commonjs/provider.js.map +1 -1
  12. package/lib/module/coreProvider.js +72 -0
  13. package/lib/module/coreProvider.js.map +1 -0
  14. package/lib/module/index.js +3 -1
  15. package/lib/module/index.js.map +1 -1
  16. package/lib/module/mappers.js +42 -0
  17. package/lib/module/mappers.js.map +1 -0
  18. package/lib/module/offlineProvider.js +143 -0
  19. package/lib/module/offlineProvider.js.map +1 -0
  20. package/lib/module/provider.js +7 -71
  21. package/lib/module/provider.js.map +1 -1
  22. package/lib/typescript/core/src/DdSdkReactNative.d.ts.map +1 -1
  23. package/lib/typescript/core/src/config/FileBasedConfiguration.d.ts.map +1 -1
  24. package/lib/typescript/core/src/config/FileBasedConfiguration.type.d.ts +1 -0
  25. package/lib/typescript/core/src/config/FileBasedConfiguration.type.d.ts.map +1 -1
  26. package/lib/typescript/core/src/config/async/AutoInstrumentationConfiguration.d.ts +2 -0
  27. package/lib/typescript/core/src/config/async/AutoInstrumentationConfiguration.d.ts.map +1 -1
  28. package/lib/typescript/core/src/config/features/RumConfiguration.d.ts +2 -0
  29. package/lib/typescript/core/src/config/features/RumConfiguration.d.ts.map +1 -1
  30. package/lib/typescript/core/src/config/features/RumConfiguration.type.d.ts +7 -0
  31. package/lib/typescript/core/src/config/features/RumConfiguration.type.d.ts.map +1 -1
  32. package/lib/typescript/core/src/flags/FlagsClient.d.ts +96 -0
  33. package/lib/typescript/core/src/flags/FlagsClient.d.ts.map +1 -1
  34. package/lib/typescript/core/src/flags/configuration/context.d.ts +20 -0
  35. package/lib/typescript/core/src/flags/configuration/context.d.ts.map +1 -0
  36. package/lib/typescript/core/src/flags/configuration/index.d.ts +5 -0
  37. package/lib/typescript/core/src/flags/configuration/index.d.ts.map +1 -0
  38. package/lib/typescript/core/src/flags/configuration/precomputed.d.ts +25 -0
  39. package/lib/typescript/core/src/flags/configuration/precomputed.d.ts.map +1 -0
  40. package/lib/typescript/core/src/flags/configuration/types.d.ts +24 -0
  41. package/lib/typescript/core/src/flags/configuration/types.d.ts.map +1 -0
  42. package/lib/typescript/core/src/flags/configuration/wire.d.ts +2 -0
  43. package/lib/typescript/core/src/flags/configuration/wire.d.ts.map +1 -0
  44. package/lib/typescript/core/src/flags/internal.d.ts.map +1 -1
  45. package/lib/typescript/core/src/flags/types.d.ts +1 -1
  46. package/lib/typescript/core/src/flags/types.d.ts.map +1 -1
  47. package/lib/typescript/core/src/index.d.ts +4 -2
  48. package/lib/typescript/core/src/index.d.ts.map +1 -1
  49. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/DdRumResourceTracking.d.ts +3 -2
  50. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/DdRumResourceTracking.d.ts.map +1 -1
  51. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/FetchProxy/FetchProxy.d.ts +29 -0
  52. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/FetchProxy/FetchProxy.d.ts.map +1 -0
  53. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.d.ts +1 -1
  54. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.d.ts.map +1 -1
  55. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/common/FetchProxyState.d.ts +8 -0
  56. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/common/FetchProxyState.d.ts.map +1 -0
  57. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/common/RequestContext.d.ts +20 -0
  58. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/common/RequestContext.d.ts.map +1 -0
  59. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/{XHRProxy/DatadogRumResource → common}/ResourceReporter.d.ts +2 -2
  60. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/common/ResourceReporter.d.ts.map +1 -0
  61. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/common/internalDevResourceBlocklist.d.ts +3 -0
  62. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/common/internalDevResourceBlocklist.d.ts.map +1 -0
  63. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/common/requestHeaders.d.ts +18 -0
  64. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/common/requestHeaders.d.ts.map +1 -0
  65. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/{XHRProxy/DatadogRumResource → common}/resourceTiming.d.ts +0 -6
  66. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/common/resourceTiming.d.ts.map +1 -0
  67. package/lib/typescript/core/src/version.d.ts +1 -1
  68. package/lib/typescript/react-native-openfeature/src/coreProvider.d.ts +34 -0
  69. package/lib/typescript/react-native-openfeature/src/coreProvider.d.ts.map +1 -0
  70. package/lib/typescript/react-native-openfeature/src/index.d.ts +3 -1
  71. package/lib/typescript/react-native-openfeature/src/index.d.ts.map +1 -1
  72. package/lib/typescript/react-native-openfeature/src/mappers.d.ts +20 -0
  73. package/lib/typescript/react-native-openfeature/src/mappers.d.ts.map +1 -0
  74. package/lib/typescript/react-native-openfeature/src/offlineProvider.d.ts +59 -0
  75. package/lib/typescript/react-native-openfeature/src/offlineProvider.d.ts.map +1 -0
  76. package/lib/typescript/react-native-openfeature/src/provider.d.ts +8 -21
  77. package/lib/typescript/react-native-openfeature/src/provider.d.ts.map +1 -1
  78. package/package.json +5 -5
  79. package/src/__tests__/offlineProvider.integration.test.ts +271 -0
  80. package/src/__tests__/offlineProvider.test.ts +252 -0
  81. package/src/__tests__/provider.test.ts +74 -0
  82. package/src/coreProvider.ts +142 -0
  83. package/src/index.ts +8 -1
  84. package/src/mappers.ts +47 -0
  85. package/src/offlineProvider.ts +185 -0
  86. package/src/provider.ts +10 -139
  87. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.d.ts.map +0 -1
  88. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/internalDevResourceBlocklist.d.ts +0 -7
  89. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/internalDevResourceBlocklist.d.ts.map +0 -1
  90. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/resourceTiming.d.ts.map +0 -1
@@ -0,0 +1,271 @@
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
+ // Integration test: exercises the offline provider against the REAL core FlagsClient (no mock of
8
+ // @datadog/mobile-react-native) AND through the REAL OpenFeature Web SDK lifecycle, so the actual
9
+ // configuration parse, context reconcile, provider-status transitions, and evaluation-detail
10
+ // mapping run. Assertions read the SDK's provider status (the status-overwrite bug is only visible
11
+ // end to end), the evaluation error code, and served values. Only the native TurboModule is stubbed
12
+ // (it is unavailable under jest) so ready-state evaluation can track exposures without throwing.
13
+
14
+ import { configurationFromString } from '@datadog/mobile-react-native';
15
+ import { ErrorCode, OpenFeature, ProviderStatus } from '@openfeature/web-sdk';
16
+
17
+ import { DatadogOfflineOpenFeatureProvider } from '../offlineProvider';
18
+
19
+ // Stub the native flags TurboModule (TurboModuleRegistry.get returns null under jest), so
20
+ // ready-state evaluation can call trackEvaluation without throwing.
21
+ jest.mock('../../../core/src/specs/NativeDdFlags', () => ({
22
+ __esModule: true,
23
+ default: {
24
+ enable: jest.fn(() => Promise.resolve()),
25
+ setEvaluationContext: jest.fn(() => Promise.resolve({})),
26
+ trackEvaluation: jest.fn(() => Promise.resolve())
27
+ }
28
+ }));
29
+
30
+ const wireFor = (targetingKey: string): string =>
31
+ JSON.stringify({
32
+ version: 1,
33
+ precomputed: {
34
+ response: JSON.stringify({
35
+ data: {
36
+ attributes: {
37
+ obfuscated: false,
38
+ flags: {
39
+ 'new-feature': {
40
+ variationType: 'boolean',
41
+ variationValue: true,
42
+ variationKey: 'true',
43
+ allocationKey: 'alloc-1',
44
+ reason: 'STATIC',
45
+ doLog: false,
46
+ extraLogging: {}
47
+ }
48
+ }
49
+ }
50
+ }
51
+ }),
52
+ context: { targetingKey }
53
+ }
54
+ });
55
+
56
+ // A unique OpenFeature domain + Datadog clientName per test keeps providers isolated (separate
57
+ // domains otherwise share the same underlying FlagsClient).
58
+ let seq = 0;
59
+ const freshNames = () => {
60
+ seq += 1;
61
+ return { domain: `offline-int-${seq}`, clientName: `offline-int-${seq}` };
62
+ };
63
+
64
+ describe('DatadogOfflineOpenFeatureProvider (integration, real FlagsClient + OpenFeature)', () => {
65
+ afterEach(async () => {
66
+ await OpenFeature.clearProviders();
67
+ // Reset the global context so a context set by one test does not leak into the next.
68
+ await OpenFeature.clearContext();
69
+ });
70
+
71
+ it('is READY when a matching configuration is loaded before registration', async () => {
72
+ const { domain, clientName } = freshNames();
73
+ const provider = new DatadogOfflineOpenFeatureProvider({ clientName });
74
+ provider.setConfiguration(configurationFromString(wireFor('user-123')));
75
+
76
+ await OpenFeature.setProviderAndWait(domain, provider);
77
+
78
+ const client = OpenFeature.getClient(domain);
79
+ expect(client.providerStatus).toBe(ProviderStatus.READY);
80
+ expect(client.getBooleanValue('new-feature', false)).toBe(true);
81
+ });
82
+
83
+ it('enters ERROR and serves defaults on a mismatching setContext, then recovers on a matching one', async () => {
84
+ const { domain, clientName } = freshNames();
85
+ const provider = new DatadogOfflineOpenFeatureProvider({ clientName });
86
+ provider.setConfiguration(configurationFromString(wireFor('user-123')));
87
+ await OpenFeature.setProviderAndWait(domain, provider);
88
+
89
+ // A runtime context that does not match the snapshot cannot be served.
90
+ await OpenFeature.setContext(domain, { targetingKey: 'someone-else' });
91
+
92
+ const client = OpenFeature.getClient(domain);
93
+ expect(client.providerStatus).toBe(ProviderStatus.ERROR);
94
+ // Serving the coded default, with the precise error code.
95
+ const details = client.getBooleanDetails('new-feature', false);
96
+ expect(details.value).toBe(false);
97
+ expect(details.errorCode).toBe(ErrorCode.INVALID_CONTEXT);
98
+
99
+ // Setting the matching context again recovers automatically and serves the retained value.
100
+ await OpenFeature.setContext(domain, { targetingKey: 'user-123' });
101
+ expect(client.providerStatus).toBe(ProviderStatus.READY);
102
+ expect(client.getBooleanValue('new-feature', false)).toBe(true);
103
+ });
104
+
105
+ it('errors on an explicit empty-string targeting key (a real anonymous subject, not "cleared")', async () => {
106
+ const { domain, clientName } = freshNames();
107
+ const provider = new DatadogOfflineOpenFeatureProvider({ clientName });
108
+ provider.setConfiguration(configurationFromString(wireFor('user-123')));
109
+ await OpenFeature.setProviderAndWait(domain, provider);
110
+
111
+ // `{ targetingKey: '' }` is an anonymous subject, distinct from the user-123 snapshot — not
112
+ // the same as clearing context. It must error rather than silently serve user-123's flags.
113
+ await OpenFeature.setContext(domain, { targetingKey: '' });
114
+
115
+ const client = OpenFeature.getClient(domain);
116
+ expect(client.providerStatus).toBe(ProviderStatus.ERROR);
117
+ expect(client.getBooleanDetails('new-feature', false).errorCode).toBe(
118
+ ErrorCode.INVALID_CONTEXT
119
+ );
120
+ });
121
+
122
+ it('stays READY when the context is cleared (empty = re-adopt embedded)', async () => {
123
+ const { domain, clientName } = freshNames();
124
+ const provider = new DatadogOfflineOpenFeatureProvider({ clientName });
125
+ provider.setConfiguration(configurationFromString(wireFor('user-123')));
126
+ await OpenFeature.setProviderAndWait(domain, provider);
127
+
128
+ await OpenFeature.setContext(domain, { targetingKey: 'user-123' });
129
+ expect(OpenFeature.getClient(domain).providerStatus).toBe(
130
+ ProviderStatus.READY
131
+ );
132
+
133
+ // clearContext(domain) falls back to the empty global context; empty means "no override", so
134
+ // the embedded context is re-adopted and the provider stays READY.
135
+ await OpenFeature.clearContext(domain);
136
+ expect(OpenFeature.getClient(domain).providerStatus).toBe(
137
+ ProviderStatus.READY
138
+ );
139
+ });
140
+
141
+ it('starts in ERROR when registered before any configuration, then recovers via setConfiguration', async () => {
142
+ const { domain, clientName } = freshNames();
143
+ const provider = new DatadogOfflineOpenFeatureProvider({ clientName });
144
+
145
+ // Provider-first: initialize rejects (no usable configuration), so registration surfaces
146
+ // an error and the provider is ERROR rather than a misleading READY.
147
+ await expect(
148
+ OpenFeature.setProviderAndWait(domain, provider)
149
+ ).rejects.toThrow();
150
+ const client = OpenFeature.getClient(domain);
151
+ expect(client.providerStatus).toBe(ProviderStatus.ERROR);
152
+
153
+ // Loading a valid configuration recovers via the emitted PROVIDER_READY.
154
+ provider.setConfiguration(configurationFromString(wireFor('user-123')));
155
+ expect(client.providerStatus).toBe(ProviderStatus.READY);
156
+ expect(client.getBooleanValue('new-feature', false)).toBe(true);
157
+ });
158
+
159
+ it('recovers via setConfiguration when a config matching the current context is loaded', async () => {
160
+ const { domain, clientName } = freshNames();
161
+ const provider = new DatadogOfflineOpenFeatureProvider({ clientName });
162
+ provider.setConfiguration(configurationFromString(wireFor('user-123')));
163
+ await OpenFeature.setProviderAndWait(domain, provider);
164
+
165
+ await OpenFeature.setContext(domain, { targetingKey: 'someone-else' });
166
+ const client = OpenFeature.getClient(domain);
167
+ expect(client.providerStatus).toBe(ProviderStatus.ERROR);
168
+
169
+ // Load a configuration computed for the now-current context: it reconciles to ready and
170
+ // setConfiguration emits PROVIDER_READY.
171
+ provider.setConfiguration(
172
+ configurationFromString(wireFor('someone-else'))
173
+ );
174
+ expect(client.providerStatus).toBe(ProviderStatus.READY);
175
+ });
176
+
177
+ describe('domain / global-context isolation', () => {
178
+ it('stays READY with the documented setup: global mismatch set, explicit empty domain context, then register', async () => {
179
+ const { domain, clientName } = freshNames();
180
+ const provider = new DatadogOfflineOpenFeatureProvider({
181
+ clientName
182
+ });
183
+ provider.setConfiguration(
184
+ configurationFromString(wireFor('user-123'))
185
+ );
186
+
187
+ // The documented order: a mismatching GLOBAL context is already in place, the dedicated
188
+ // domain is given an explicit empty context (isolating it), and only then is the provider
189
+ // registered. It initializes against the empty domain context → embedded → READY.
190
+ await OpenFeature.setContext({ targetingKey: 'global-user' });
191
+ await OpenFeature.setContext(domain, {});
192
+ await OpenFeature.setProviderAndWait(domain, provider);
193
+
194
+ expect(OpenFeature.getClient(domain).providerStatus).toBe(
195
+ ProviderStatus.READY
196
+ );
197
+ });
198
+
199
+ it('inherits a mismatching global context when the domain has none, entering ERROR', async () => {
200
+ const { domain, clientName } = freshNames();
201
+ const provider = new DatadogOfflineOpenFeatureProvider({
202
+ clientName
203
+ });
204
+ provider.setConfiguration(
205
+ configurationFromString(wireFor('user-123'))
206
+ );
207
+
208
+ // A global (non-domain) context is set; the domain has no context of its own, so it
209
+ // inherits the global one at registration → mismatch → ERROR.
210
+ await OpenFeature.setContext({ targetingKey: 'global-user' });
211
+ await expect(
212
+ OpenFeature.setProviderAndWait(domain, provider)
213
+ ).rejects.toThrow();
214
+
215
+ expect(OpenFeature.getClient(domain).providerStatus).toBe(
216
+ ProviderStatus.ERROR
217
+ );
218
+ });
219
+
220
+ it('ignores later global context changes once the domain has an explicit empty context', async () => {
221
+ const { domain, clientName } = freshNames();
222
+ const provider = new DatadogOfflineOpenFeatureProvider({
223
+ clientName
224
+ });
225
+ provider.setConfiguration(
226
+ configurationFromString(wireFor('user-123'))
227
+ );
228
+ await OpenFeature.setProviderAndWait(domain, provider);
229
+
230
+ // Give the domain its own (empty) context: this provider reads it as "no override".
231
+ await OpenFeature.setContext(domain, {});
232
+ expect(OpenFeature.getClient(domain).providerStatus).toBe(
233
+ ProviderStatus.READY
234
+ );
235
+
236
+ // A later mismatching GLOBAL context does not reach a domain that has its own context.
237
+ await OpenFeature.setContext({ targetingKey: 'global-mismatch' });
238
+ expect(OpenFeature.getClient(domain).providerStatus).toBe(
239
+ ProviderStatus.READY
240
+ );
241
+ });
242
+
243
+ it('falls back to a mismatching global context when the domain context is cleared', async () => {
244
+ const { domain, clientName } = freshNames();
245
+ const provider = new DatadogOfflineOpenFeatureProvider({
246
+ clientName
247
+ });
248
+ provider.setConfiguration(
249
+ configurationFromString(wireFor('user-123'))
250
+ );
251
+ await OpenFeature.setProviderAndWait(domain, provider);
252
+
253
+ await OpenFeature.setContext(domain, { targetingKey: 'user-123' });
254
+ expect(OpenFeature.getClient(domain).providerStatus).toBe(
255
+ ProviderStatus.READY
256
+ );
257
+
258
+ // A mismatching global context does not reach the domain while it has its own context.
259
+ await OpenFeature.setContext({ targetingKey: 'global-mismatch' });
260
+ expect(OpenFeature.getClient(domain).providerStatus).toBe(
261
+ ProviderStatus.READY
262
+ );
263
+
264
+ // Clearing the domain context falls back to the (mismatching) global context → ERROR.
265
+ await OpenFeature.clearContext(domain);
266
+ expect(OpenFeature.getClient(domain).providerStatus).toBe(
267
+ ProviderStatus.ERROR
268
+ );
269
+ });
270
+ });
271
+ });
@@ -0,0 +1,252 @@
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 {
8
+ ErrorCode,
9
+ GeneralError,
10
+ InvalidContextError,
11
+ ProviderEvents,
12
+ ProviderNotReadyError
13
+ } from '@openfeature/web-sdk';
14
+
15
+ import { DatadogOfflineOpenFeatureProvider } from '../offlineProvider';
16
+
17
+ const READY = { status: 'ready' as const };
18
+ const mismatch = { status: 'error' as const, errorCode: 'INVALID_CONTEXT' };
19
+ const notReady = { status: 'error' as const, errorCode: 'PROVIDER_NOT_READY' };
20
+ const generalError = { status: 'error' as const, errorCode: 'GENERAL' };
21
+
22
+ const mockFlagsClient = {
23
+ setConfiguration: jest.fn(() => READY),
24
+ setEvaluationContextWithoutFetching: jest.fn(() => READY),
25
+ resetEvaluationContextWithoutFetching: jest.fn(() => READY),
26
+ setEvaluationContext: jest.fn(() => Promise.resolve()),
27
+ getBooleanDetails: jest.fn(() => ({
28
+ key: 'flag',
29
+ value: true,
30
+ reason: 'STATIC',
31
+ variant: 'true'
32
+ }))
33
+ };
34
+
35
+ jest.mock('@datadog/mobile-react-native', () => {
36
+ return {
37
+ DdFlags: { getClient: jest.fn(() => mockFlagsClient) },
38
+ configurationFromString: jest.fn()
39
+ };
40
+ });
41
+
42
+ describe('DatadogOfflineOpenFeatureProvider', () => {
43
+ beforeEach(() => {
44
+ jest.clearAllMocks();
45
+ mockFlagsClient.setConfiguration.mockReturnValue(READY);
46
+ mockFlagsClient.setEvaluationContextWithoutFetching.mockReturnValue(
47
+ READY
48
+ );
49
+ mockFlagsClient.resetEvaluationContextWithoutFetching.mockReturnValue(
50
+ READY
51
+ );
52
+ });
53
+
54
+ it('advertises the offline provider name', () => {
55
+ expect(new DatadogOfflineOpenFeatureProvider().metadata.name).toBe(
56
+ 'datadog-react-native-offline'
57
+ );
58
+ });
59
+
60
+ it('re-adopts the embedded context on an empty initialize context', async () => {
61
+ const provider = new DatadogOfflineOpenFeatureProvider();
62
+
63
+ await provider.initialize({});
64
+
65
+ // An empty context means "no external override": reset to the embedded context rather
66
+ // than setting an (empty) override.
67
+ expect(
68
+ mockFlagsClient.resetEvaluationContextWithoutFetching
69
+ ).toHaveBeenCalled();
70
+ expect(
71
+ mockFlagsClient.setEvaluationContextWithoutFetching
72
+ ).not.toHaveBeenCalled();
73
+ expect(mockFlagsClient.setEvaluationContext).not.toHaveBeenCalled();
74
+ });
75
+
76
+ it('records a non-empty context without fetching', async () => {
77
+ const provider = new DatadogOfflineOpenFeatureProvider();
78
+
79
+ await provider.initialize({ targetingKey: 'user-1' });
80
+
81
+ expect(
82
+ mockFlagsClient.setEvaluationContextWithoutFetching
83
+ ).toHaveBeenCalledWith(
84
+ expect.objectContaining({ targetingKey: 'user-1' })
85
+ );
86
+ expect(mockFlagsClient.setEvaluationContext).not.toHaveBeenCalled();
87
+ });
88
+
89
+ it('rejects initialize when the initial context does not match', async () => {
90
+ const provider = new DatadogOfflineOpenFeatureProvider();
91
+ mockFlagsClient.setEvaluationContextWithoutFetching.mockReturnValueOnce(
92
+ mismatch
93
+ );
94
+
95
+ await expect(
96
+ provider.initialize({ targetingKey: 'user-2' })
97
+ ).rejects.toThrow(InvalidContextError);
98
+ });
99
+
100
+ it('rejects initialize when no configuration is loaded (provider-first)', async () => {
101
+ const provider = new DatadogOfflineOpenFeatureProvider();
102
+ mockFlagsClient.resetEvaluationContextWithoutFetching.mockReturnValueOnce(
103
+ notReady
104
+ );
105
+
106
+ await expect(provider.initialize({})).rejects.toThrow(
107
+ ProviderNotReadyError
108
+ );
109
+ });
110
+
111
+ it('reconciles a matching context change without fetching or signalling a change', () => {
112
+ const provider = new DatadogOfflineOpenFeatureProvider();
113
+ const emitSpy = jest.spyOn(provider.events, 'emit');
114
+
115
+ provider.onContextChange({}, { targetingKey: 'user-2' });
116
+
117
+ expect(
118
+ mockFlagsClient.setEvaluationContextWithoutFetching
119
+ ).toHaveBeenCalledWith(
120
+ expect.objectContaining({ targetingKey: 'user-2' })
121
+ );
122
+ expect(mockFlagsClient.setEvaluationContext).not.toHaveBeenCalled();
123
+ // The Web SDK owns the READY/CONTEXT_CHANGED transition on a resolving context change;
124
+ // the provider itself emits nothing.
125
+ expect(emitSpy).not.toHaveBeenCalledWith(
126
+ ProviderEvents.ConfigurationChanged
127
+ );
128
+ expect(emitSpy).not.toHaveBeenCalledWith(ProviderEvents.Ready);
129
+ });
130
+
131
+ it('throws synchronously from onContextChange on a mismatching context', () => {
132
+ const provider = new DatadogOfflineOpenFeatureProvider();
133
+ mockFlagsClient.setEvaluationContextWithoutFetching.mockReturnValueOnce(
134
+ mismatch
135
+ );
136
+
137
+ // Synchronous throw so the Web SDK transitions straight to ERROR (no async race).
138
+ expect(() =>
139
+ provider.onContextChange({}, { targetingKey: 'user-2' })
140
+ ).toThrow(InvalidContextError);
141
+ });
142
+
143
+ it('re-adopts the embedded context on clearContext / empty context change', () => {
144
+ const provider = new DatadogOfflineOpenFeatureProvider();
145
+
146
+ provider.onContextChange({ targetingKey: 'user-1' }, {});
147
+
148
+ // Clearing context is not a mismatch: it re-adopts the embedded context and does not throw.
149
+ expect(
150
+ mockFlagsClient.resetEvaluationContextWithoutFetching
151
+ ).toHaveBeenCalled();
152
+ expect(
153
+ mockFlagsClient.setEvaluationContextWithoutFetching
154
+ ).not.toHaveBeenCalled();
155
+ });
156
+
157
+ it('treats a context with only an undefined targetingKey as empty', () => {
158
+ const provider = new DatadogOfflineOpenFeatureProvider();
159
+
160
+ provider.onContextChange({}, { targetingKey: undefined });
161
+
162
+ // `{ targetingKey: undefined }` carries no information: reset to the embedded context.
163
+ expect(
164
+ mockFlagsClient.resetEvaluationContextWithoutFetching
165
+ ).toHaveBeenCalled();
166
+ expect(
167
+ mockFlagsClient.setEvaluationContextWithoutFetching
168
+ ).not.toHaveBeenCalled();
169
+ });
170
+
171
+ it('delegates setConfiguration to the client and emits CONFIGURATION_CHANGED', () => {
172
+ const provider = new DatadogOfflineOpenFeatureProvider();
173
+ const emitSpy = jest.spyOn(provider.events, 'emit');
174
+
175
+ provider.setConfiguration({} as never);
176
+
177
+ expect(mockFlagsClient.setConfiguration).toHaveBeenCalled();
178
+ // A healthy (re)loaded config is a configuration change, not a status transition.
179
+ expect(emitSpy).toHaveBeenCalledWith(
180
+ ProviderEvents.ConfigurationChanged
181
+ );
182
+ expect(emitSpy).not.toHaveBeenCalledWith(ProviderEvents.Ready);
183
+ });
184
+
185
+ it('emits PROVIDER_ERROR with a top-level errorCode on an invalid configuration', () => {
186
+ const provider = new DatadogOfflineOpenFeatureProvider();
187
+ const emitSpy = jest.spyOn(provider.events, 'emit');
188
+
189
+ mockFlagsClient.setConfiguration.mockReturnValueOnce(generalError);
190
+ provider.setConfiguration({} as never);
191
+
192
+ expect(emitSpy).toHaveBeenCalledWith(
193
+ ProviderEvents.Error,
194
+ expect.objectContaining({
195
+ message: expect.any(String),
196
+ errorCode: ErrorCode.GENERAL
197
+ })
198
+ );
199
+ });
200
+
201
+ it('recovers on a later valid configuration, emitting READY then CONFIGURATION_CHANGED', () => {
202
+ const provider = new DatadogOfflineOpenFeatureProvider();
203
+ const emitSpy = jest.spyOn(provider.events, 'emit');
204
+
205
+ mockFlagsClient.setConfiguration.mockReturnValueOnce(mismatch);
206
+ provider.setConfiguration({} as never);
207
+ expect(emitSpy).toHaveBeenCalledWith(
208
+ ProviderEvents.Error,
209
+ expect.objectContaining({ errorCode: ErrorCode.INVALID_CONTEXT })
210
+ );
211
+
212
+ // A subsequent valid config recovers: READY clears the ERROR status, CONFIGURATION_CHANGED
213
+ // signals the new flags.
214
+ provider.setConfiguration({} as never);
215
+ expect(emitSpy).toHaveBeenCalledWith(ProviderEvents.Ready);
216
+ expect(emitSpy).toHaveBeenCalledWith(
217
+ ProviderEvents.ConfigurationChanged
218
+ );
219
+ });
220
+
221
+ it('rejects initialize when a config was loaded (pre-registration) and is invalid', async () => {
222
+ const provider = new DatadogOfflineOpenFeatureProvider();
223
+ mockFlagsClient.setConfiguration.mockReturnValueOnce(generalError);
224
+ provider.setConfiguration({} as never);
225
+
226
+ // A pre-registration setConfiguration error had no listeners, and the empty initialize
227
+ // context reconciles to the same error, so initialize rejects -> the Web SDK starts the
228
+ // provider in ERROR rather than a misleading READY.
229
+ mockFlagsClient.resetEvaluationContextWithoutFetching.mockReturnValueOnce(
230
+ generalError
231
+ );
232
+ await expect(provider.initialize({})).rejects.toThrow(GeneralError);
233
+ });
234
+
235
+ it('resolves boolean evaluation through the client', () => {
236
+ const provider = new DatadogOfflineOpenFeatureProvider();
237
+
238
+ const result = provider.resolveBooleanEvaluation(
239
+ 'flag',
240
+ false,
241
+ {},
242
+ // eslint-disable-next-line no-console
243
+ console as never
244
+ );
245
+
246
+ expect(result.value).toBe(true);
247
+ expect(mockFlagsClient.getBooleanDetails).toHaveBeenCalledWith(
248
+ 'flag',
249
+ false
250
+ );
251
+ });
252
+ });
@@ -0,0 +1,74 @@
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 { DatadogOpenFeatureProvider } from '../provider';
8
+
9
+ const mockFlagsClient = {
10
+ setEvaluationContext: jest.fn(() => Promise.resolve()),
11
+ setEvaluationContextWithoutFetching: jest.fn(() => 'ready'),
12
+ getBooleanDetails: jest.fn(() => ({
13
+ key: 'flag',
14
+ value: true,
15
+ reason: 'STATIC',
16
+ variant: 'true'
17
+ }))
18
+ };
19
+
20
+ jest.mock('@datadog/mobile-react-native', () => {
21
+ return {
22
+ DdFlags: { getClient: jest.fn(() => mockFlagsClient) },
23
+ configurationFromString: jest.fn()
24
+ };
25
+ });
26
+
27
+ describe('DatadogOpenFeatureProvider', () => {
28
+ beforeEach(() => {
29
+ jest.clearAllMocks();
30
+ });
31
+
32
+ it('advertises the online provider name', () => {
33
+ expect(new DatadogOpenFeatureProvider().metadata.name).toBe(
34
+ 'datadog-react-native'
35
+ );
36
+ });
37
+
38
+ it('fetches on initialize (uses the fetching setEvaluationContext)', async () => {
39
+ const provider = new DatadogOpenFeatureProvider();
40
+
41
+ await provider.initialize({ targetingKey: 'user-1' });
42
+
43
+ expect(mockFlagsClient.setEvaluationContext).toHaveBeenCalledWith(
44
+ expect.objectContaining({ targetingKey: 'user-1' })
45
+ );
46
+ expect(
47
+ mockFlagsClient.setEvaluationContextWithoutFetching
48
+ ).not.toHaveBeenCalled();
49
+ });
50
+
51
+ it('fetches on a context change', async () => {
52
+ const provider = new DatadogOpenFeatureProvider();
53
+
54
+ await provider.onContextChange({}, { targetingKey: 'user-2' });
55
+
56
+ expect(mockFlagsClient.setEvaluationContext).toHaveBeenCalledWith(
57
+ expect.objectContaining({ targetingKey: 'user-2' })
58
+ );
59
+ });
60
+
61
+ it('resolves boolean evaluation through the client', () => {
62
+ const provider = new DatadogOpenFeatureProvider();
63
+
64
+ const result = provider.resolveBooleanEvaluation(
65
+ 'flag',
66
+ false,
67
+ {},
68
+ // eslint-disable-next-line no-console
69
+ console as never
70
+ );
71
+
72
+ expect(result.value).toBe(true);
73
+ });
74
+ });