@datadog/mobile-react-native-openfeature 3.6.0 → 3.8.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 (35) hide show
  1. package/README.md +187 -15
  2. package/lib/commonjs/index.js +7 -0
  3. package/lib/commonjs/index.js.map +1 -1
  4. package/lib/commonjs/provider.js +1 -1
  5. package/lib/commonjs/provider.js.map +1 -1
  6. package/lib/commonjs/rumContext.js +44 -0
  7. package/lib/commonjs/rumContext.js.map +1 -0
  8. package/lib/module/index.js +2 -1
  9. package/lib/module/index.js.map +1 -1
  10. package/lib/module/provider.js +1 -1
  11. package/lib/module/provider.js.map +1 -1
  12. package/lib/module/rumContext.js +37 -0
  13. package/lib/module/rumContext.js.map +1 -0
  14. package/lib/typescript/core/src/flags/internal.d.ts.map +1 -1
  15. package/lib/typescript/core/src/flags/rumIntegration.d.ts +14 -0
  16. package/lib/typescript/core/src/flags/rumIntegration.d.ts.map +1 -0
  17. package/lib/typescript/core/src/index.d.ts +2 -1
  18. package/lib/typescript/core/src/index.d.ts.map +1 -1
  19. package/lib/typescript/core/src/version.d.ts +1 -1
  20. package/lib/typescript/react-native-openfeature/src/index.d.ts +4 -2
  21. package/lib/typescript/react-native-openfeature/src/index.d.ts.map +1 -1
  22. package/lib/typescript/react-native-openfeature/src/provider.d.ts.map +1 -1
  23. package/lib/typescript/react-native-openfeature/src/rumContext.d.ts +23 -0
  24. package/lib/typescript/react-native-openfeature/src/rumContext.d.ts.map +1 -0
  25. package/package.json +3 -3
  26. package/src/__tests__/__utils__/rumContext.types.ts +55 -0
  27. package/src/__tests__/__utils__/tsconfig.json +8 -0
  28. package/src/__tests__/provider.test.ts +30 -0
  29. package/src/__tests__/rumContext.compatibility.test.ts +122 -0
  30. package/src/__tests__/rumContext.integration.test.ts +414 -0
  31. package/src/__tests__/rumContext.test.ts +100 -0
  32. package/src/__tests__/rumContext.types.test.ts +27 -0
  33. package/src/index.ts +4 -1
  34. package/src/provider.ts +5 -3
  35. package/src/rumContext.ts +54 -0
@@ -58,6 +58,36 @@ describe('DatadogOpenFeatureProvider', () => {
58
58
  );
59
59
  });
60
60
 
61
+ it('recovers after a context update fails', async () => {
62
+ const provider = new DatadogOpenFeatureProvider();
63
+ mockFlagsClient.setEvaluationContext
64
+ .mockResolvedValueOnce(undefined)
65
+ .mockRejectedValueOnce(new Error('temporarily offline'))
66
+ .mockResolvedValueOnce(undefined);
67
+
68
+ await provider.initialize({ targetingKey: 'user-1' });
69
+
70
+ await expect(
71
+ provider.onContextChange(
72
+ { targetingKey: 'user-1' },
73
+ { targetingKey: 'user-2' }
74
+ )
75
+ ).rejects.toThrow('temporarily offline');
76
+
77
+ await expect(
78
+ provider.onContextChange(
79
+ { targetingKey: 'user-2' },
80
+ { targetingKey: 'user-3' }
81
+ )
82
+ ).resolves.toBeUndefined();
83
+
84
+ expect(mockFlagsClient.setEvaluationContext).toHaveBeenCalledTimes(3);
85
+ expect(mockFlagsClient.setEvaluationContext).toHaveBeenNthCalledWith(
86
+ 3,
87
+ expect.objectContaining({ targetingKey: 'user-3' })
88
+ );
89
+ });
90
+
61
91
  it('resolves boolean evaluation through the client', () => {
62
92
  const provider = new DatadogOpenFeatureProvider();
63
93
 
@@ -0,0 +1,122 @@
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 * as DatadogSdk from '@datadog/mobile-react-native';
8
+ import { OpenFeature } from '@openfeature/web-sdk';
9
+
10
+ import { DatadogOpenFeatureProvider, enrichWithRumUser } from '../index';
11
+
12
+ const mockFlagsClient = {
13
+ setEvaluationContext: jest.fn(() => Promise.resolve()),
14
+ getBooleanDetails: jest.fn(() => ({
15
+ value: true,
16
+ reason: 'TARGETING_MATCH',
17
+ variant: 'enabled'
18
+ }))
19
+ };
20
+
21
+ jest.mock('@datadog/mobile-react-native', () => ({
22
+ __esModule: true,
23
+ DdFlags: { getClient: jest.fn(() => mockFlagsClient) },
24
+ configurationFromString: jest.fn()
25
+ }));
26
+
27
+ describe('RUM context core compatibility', () => {
28
+ beforeEach(() => {
29
+ jest.clearAllMocks();
30
+ Reflect.deleteProperty(
31
+ DatadogSdk,
32
+ '__ddEnrichEvaluationContextWithRumUser'
33
+ );
34
+ jest.spyOn(console, 'warn').mockImplementation(() => {});
35
+ });
36
+
37
+ afterEach(async () => {
38
+ await OpenFeature.clearProviders();
39
+ await OpenFeature.clearContext();
40
+ jest.restoreAllMocks();
41
+ });
42
+ it('keeps the provider usable with a core version that predates enrichment', async () => {
43
+ const provider = new DatadogOpenFeatureProvider();
44
+
45
+ await provider.initialize({
46
+ targetingKey: 'explicit-user',
47
+ plan: 'pro'
48
+ });
49
+
50
+ expect(mockFlagsClient.setEvaluationContext).toHaveBeenCalledWith({
51
+ targetingKey: 'explicit-user',
52
+ attributes: { plan: 'pro' }
53
+ });
54
+ });
55
+
56
+ it('returns the context unchanged and warns when the helper and InternalLog are missing', () => {
57
+ const context = Object.freeze({
58
+ targetingKey: 'application-user',
59
+ email: undefined,
60
+ profile: { plan: 'pro' }
61
+ });
62
+
63
+ expect(DatadogSdk.InternalLog).toBeUndefined();
64
+ expect(enrichWithRumUser(context)).toBe(context);
65
+ expect(console.warn).toHaveBeenCalledTimes(1);
66
+ expect(console.warn).toHaveBeenCalledWith(
67
+ 'DATADOG: `enrichWithRumUser` could not access the core RUM enrichment helper. Returning the application context unchanged. Check SDK compatibility.'
68
+ );
69
+ });
70
+
71
+ it.each([undefined, null, false, 42, 'not a function', {}])(
72
+ 'returns the context unchanged when the helper is not callable (%p)',
73
+ enricher => {
74
+ Object.assign(DatadogSdk, {
75
+ __ddEnrichEvaluationContextWithRumUser: enricher
76
+ });
77
+ const context = { targetingKey: 'application-user', plan: 'pro' };
78
+
79
+ expect(enrichWithRumUser(context)).toBe(context);
80
+ expect(console.warn).toHaveBeenCalledTimes(1);
81
+ }
82
+ );
83
+
84
+ it('allows OpenFeature startup and flag evaluation with the application context', async () => {
85
+ const context = {
86
+ targetingKey: 'application-user',
87
+ plan: 'pro',
88
+ email: undefined
89
+ };
90
+
91
+ await OpenFeature.setContext(enrichWithRumUser(context));
92
+ await OpenFeature.setProviderAndWait(new DatadogOpenFeatureProvider());
93
+
94
+ expect(OpenFeature.getContext()).toStrictEqual(context);
95
+ expect(mockFlagsClient.setEvaluationContext).toHaveBeenCalledWith({
96
+ targetingKey: 'application-user',
97
+ attributes: { plan: 'pro', email: undefined }
98
+ });
99
+ expect(
100
+ OpenFeature.getClient().getBooleanValue('test-flag', false)
101
+ ).toBe(true);
102
+ expect(mockFlagsClient.getBooleanDetails).toHaveBeenCalledWith(
103
+ 'test-flag',
104
+ false
105
+ );
106
+ expect(console.warn).toHaveBeenCalledTimes(1);
107
+ });
108
+
109
+ it('delegates to an available helper without warning', () => {
110
+ const context = { region: 'us' };
111
+ const enriched = { targetingKey: 'rum-user', region: 'us' };
112
+ const enricher = jest.fn(() => enriched);
113
+ Object.assign(DatadogSdk, {
114
+ __ddEnrichEvaluationContextWithRumUser: enricher
115
+ });
116
+
117
+ expect(enrichWithRumUser(context)).toBe(enriched);
118
+ expect(enricher).toHaveBeenCalledWith(context);
119
+ expect(enricher).toHaveBeenCalledTimes(1);
120
+ expect(console.warn).not.toHaveBeenCalled();
121
+ });
122
+ });
@@ -0,0 +1,414 @@
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 { DdFlags, DdSdkReactNative } from '@datadog/mobile-react-native';
8
+ import { InMemoryProvider, OpenFeature } from '@openfeature/web-sdk';
9
+
10
+ import { InternalLog } from '../../../core/src/InternalLog';
11
+ import { SdkVerbosity } from '../../../core/src/config/types/SdkVerbosity';
12
+ import { UserInfoSingleton } from '../../../core/src/sdk/UserInfoSingleton/UserInfoSingleton';
13
+ import NativeDdFlags from '../../../core/src/specs/NativeDdFlags';
14
+ import { DatadogOpenFeatureProvider } from '../provider';
15
+ import { enrichWithRumUser } from '../rumContext';
16
+
17
+ jest.mock('../../../core/src/specs/NativeDdFlags', () => ({
18
+ __esModule: true,
19
+ default: {
20
+ enable: jest.fn(() => Promise.resolve()),
21
+ setEvaluationContext: jest.fn(() =>
22
+ Promise.resolve({
23
+ 'test-flag': {
24
+ key: 'test-flag',
25
+ value: true,
26
+ allocationKey: 'allocation',
27
+ variationKey: 'enabled',
28
+ reason: 'TARGETING_MATCH',
29
+ doLog: true,
30
+ variationType: 'boolean',
31
+ variationValue: 'true',
32
+ extraLogging: {}
33
+ }
34
+ })
35
+ ),
36
+ trackEvaluation: jest.fn(() => Promise.resolve())
37
+ }
38
+ }));
39
+
40
+ jest.mock('../../../core/src/specs/NativeDdSdk', () => ({
41
+ __esModule: true,
42
+ default: {
43
+ setUserInfo: jest.fn(() => Promise.resolve()),
44
+ clearUserInfo: jest.fn(() => Promise.resolve())
45
+ }
46
+ }));
47
+
48
+ let testSequence = 0;
49
+
50
+ const setupProvider = async (context: Record<string, unknown>) => {
51
+ testSequence += 1;
52
+ const domain = `rum-context-domain-${testSequence}`;
53
+ const clientName = `rum-context-client-${testSequence}`;
54
+
55
+ await OpenFeature.setContext(domain, context);
56
+ await OpenFeature.setProviderAndWait(
57
+ domain,
58
+ new DatadogOpenFeatureProvider({ clientName })
59
+ );
60
+
61
+ return { clientName, domain };
62
+ };
63
+
64
+ describe('explicit RUM context enrichment', () => {
65
+ beforeEach(async () => {
66
+ jest.clearAllMocks();
67
+ UserInfoSingleton.reset();
68
+ Object.assign(DdFlags, {
69
+ isFeatureEnabled: false,
70
+ clients: {}
71
+ });
72
+ await DdFlags.enable();
73
+ jest.spyOn(InternalLog, 'log').mockImplementation(() => {});
74
+ });
75
+
76
+ afterEach(async () => {
77
+ await OpenFeature.clearProviders();
78
+ await OpenFeature.clearContext();
79
+ jest.restoreAllMocks();
80
+ });
81
+
82
+ it('does not implicitly add the RUM user to provider context', async () => {
83
+ UserInfoSingleton.getInstance().setUserInfo({
84
+ id: 'rum-user',
85
+ email: 'rum@example.com'
86
+ });
87
+
88
+ const { clientName } = await setupProvider({});
89
+
90
+ expect(NativeDdFlags.setEvaluationContext).toHaveBeenCalledWith(
91
+ clientName,
92
+ '',
93
+ {}
94
+ );
95
+ });
96
+
97
+ it('makes explicitly enriched context visible to OpenFeature and evaluation tracking', async () => {
98
+ UserInfoSingleton.getInstance().setUserInfo({
99
+ id: 'rum-user',
100
+ email: 'rum@example.com',
101
+ extraInfo: {
102
+ targetingKey: 'custom-user',
103
+ name: 'custom-name',
104
+ email: 'custom@example.com',
105
+ company_name: 'Example, Inc.',
106
+ nullable: null
107
+ }
108
+ });
109
+ const enrichedContext = enrichWithRumUser({
110
+ email: 'explicit@example.com'
111
+ });
112
+
113
+ const { clientName, domain } = await setupProvider(enrichedContext);
114
+ await OpenFeature.getClient(domain).getBooleanValue('test-flag', false);
115
+
116
+ const expectedAttributes = {
117
+ name: 'custom-name',
118
+ email: 'explicit@example.com',
119
+ company_name: 'Example, Inc.'
120
+ };
121
+ expect(OpenFeature.getContext(domain)).toStrictEqual(enrichedContext);
122
+ expect(NativeDdFlags.setEvaluationContext).toHaveBeenCalledWith(
123
+ clientName,
124
+ 'rum-user',
125
+ expectedAttributes
126
+ );
127
+ expect(NativeDdFlags.trackEvaluation).toHaveBeenCalledWith(
128
+ clientName,
129
+ 'test-flag',
130
+ expect.any(Object),
131
+ 'rum-user',
132
+ expectedAttributes
133
+ );
134
+ });
135
+
136
+ it.each([
137
+ ['extraInfo', 42],
138
+ ['extraInfo', true],
139
+ ['extraInfo', false],
140
+ ['application', 42],
141
+ ['application', true],
142
+ ['application', false],
143
+ ['unenriched application', 42],
144
+ ['unenriched application', true],
145
+ ['unenriched application', false]
146
+ ])(
147
+ 'validates a final targeting key from %s with value %p only after merging',
148
+ async (source, targetingKey) => {
149
+ const applicationContext = { region: 'us' };
150
+ if (source === 'extraInfo') {
151
+ UserInfoSingleton.getInstance().addUserExtraInfo({
152
+ targetingKey,
153
+ plan: 'pro'
154
+ });
155
+ } else {
156
+ UserInfoSingleton.getInstance().setUserInfo({
157
+ id: 'rum-user',
158
+ extraInfo: { targetingKey: 'custom-user', plan: 'pro' }
159
+ });
160
+ }
161
+ const context =
162
+ source === 'unenriched application'
163
+ ? { targetingKey, plan: 'pro', ...applicationContext }
164
+ : enrichWithRumUser({
165
+ ...applicationContext,
166
+ ...(source === 'application'
167
+ ? { targetingKey: targetingKey as never }
168
+ : {})
169
+ });
170
+ const expectedAttributes = { plan: 'pro', region: 'us' };
171
+ expect(context).toStrictEqual({
172
+ targetingKey,
173
+ ...expectedAttributes
174
+ });
175
+ expect(InternalLog.log).not.toHaveBeenCalled();
176
+
177
+ const { clientName, domain } = await setupProvider(context);
178
+ await OpenFeature.getClient(domain).getBooleanValue(
179
+ 'test-flag',
180
+ false
181
+ );
182
+
183
+ expect(OpenFeature.getContext(domain)).toStrictEqual(context);
184
+ expect(InternalLog.log).toHaveBeenCalledWith(
185
+ "The evaluation context targetingKey is not a string. Using the anonymous subject ('') instead.",
186
+ SdkVerbosity.WARN
187
+ );
188
+ expect(NativeDdFlags.setEvaluationContext).toHaveBeenCalledWith(
189
+ clientName,
190
+ '',
191
+ expectedAttributes
192
+ );
193
+ expect(NativeDdFlags.trackEvaluation).toHaveBeenCalledWith(
194
+ clientName,
195
+ 'test-flag',
196
+ expect.any(Object),
197
+ '',
198
+ expectedAttributes
199
+ );
200
+ }
201
+ );
202
+
203
+ it('preserves the RUM targeting key through evaluation when a custom property throws', async () => {
204
+ UserInfoSingleton.getInstance().setUserInfo({
205
+ id: 'rum-user',
206
+ email: 'rum@example.com',
207
+ extraInfo: {
208
+ get broken() {
209
+ throw new Error('cannot read custom property');
210
+ }
211
+ }
212
+ });
213
+ const { clientName, domain } = await setupProvider(
214
+ enrichWithRumUser({ region: 'us' })
215
+ );
216
+ await OpenFeature.getClient(domain).getBooleanValue('test-flag', false);
217
+
218
+ const expectedAttributes = { email: 'rum@example.com', region: 'us' };
219
+ expect(OpenFeature.getContext(domain)).toStrictEqual({
220
+ targetingKey: 'rum-user',
221
+ ...expectedAttributes
222
+ });
223
+ expect(NativeDdFlags.setEvaluationContext).toHaveBeenCalledWith(
224
+ clientName,
225
+ 'rum-user',
226
+ expectedAttributes
227
+ );
228
+ expect(NativeDdFlags.trackEvaluation).toHaveBeenCalledWith(
229
+ clientName,
230
+ 'test-flag',
231
+ expect.any(Object),
232
+ 'rum-user',
233
+ expectedAttributes
234
+ );
235
+ });
236
+
237
+ it('uses the latest RUM user when the application reapplies its original context', async () => {
238
+ const applicationContext = { region: 'us' };
239
+ UserInfoSingleton.getInstance().setUserInfo({
240
+ id: 'rum-user-a',
241
+ email: 'a@example.com'
242
+ });
243
+ const { clientName, domain } = await setupProvider(
244
+ enrichWithRumUser(applicationContext)
245
+ );
246
+
247
+ UserInfoSingleton.getInstance().setUserInfo({
248
+ id: 'rum-user-b',
249
+ email: 'b@example.com',
250
+ extraInfo: { plan: 'pro' }
251
+ });
252
+ await OpenFeature.setContext(
253
+ domain,
254
+ enrichWithRumUser(applicationContext)
255
+ );
256
+ await OpenFeature.getClient(domain).getBooleanValue('test-flag', false);
257
+
258
+ const expectedContext = {
259
+ targetingKey: 'rum-user-b',
260
+ email: 'b@example.com',
261
+ plan: 'pro',
262
+ region: 'us'
263
+ };
264
+ expect(OpenFeature.getContext(domain)).toStrictEqual(expectedContext);
265
+ expect(NativeDdFlags.setEvaluationContext).toHaveBeenLastCalledWith(
266
+ clientName,
267
+ 'rum-user-b',
268
+ {
269
+ email: 'b@example.com',
270
+ plan: 'pro',
271
+ region: 'us'
272
+ }
273
+ );
274
+ expect(
275
+ NativeDdFlags.trackEvaluation
276
+ ).toHaveBeenLastCalledWith(
277
+ clientName,
278
+ 'test-flag',
279
+ expect.any(Object),
280
+ 'rum-user-b',
281
+ { email: 'b@example.com', plan: 'pro', region: 'us' }
282
+ );
283
+ expect(applicationContext).toStrictEqual({ region: 'us' });
284
+ });
285
+
286
+ it('uses the anonymous subject after clearing the RUM user and reapplying application context', async () => {
287
+ const applicationContext = { region: 'us' };
288
+ await DdSdkReactNative.setUserInfo({
289
+ id: 'rum-user',
290
+ email: 'user@example.com',
291
+ extraInfo: { plan: 'pro' }
292
+ });
293
+ const { clientName, domain } = await setupProvider(
294
+ enrichWithRumUser(applicationContext)
295
+ );
296
+
297
+ await DdSdkReactNative.setUserInfo({ id: '' });
298
+ expect(enrichWithRumUser(applicationContext)).toStrictEqual({
299
+ targetingKey: 'rum-user',
300
+ email: 'user@example.com',
301
+ plan: 'pro',
302
+ region: 'us'
303
+ });
304
+
305
+ await DdSdkReactNative.clearUserInfo();
306
+ await OpenFeature.setContext(
307
+ domain,
308
+ enrichWithRumUser(applicationContext)
309
+ );
310
+ await OpenFeature.getClient(domain).getBooleanValue('test-flag', false);
311
+
312
+ expect(OpenFeature.getContext(domain)).toStrictEqual(
313
+ applicationContext
314
+ );
315
+ expect(
316
+ NativeDdFlags.setEvaluationContext
317
+ ).toHaveBeenLastCalledWith(clientName, '', { region: 'us' });
318
+ expect(
319
+ NativeDdFlags.trackEvaluation
320
+ ).toHaveBeenLastCalledWith(
321
+ clientName,
322
+ 'test-flag',
323
+ expect.any(Object),
324
+ '',
325
+ { region: 'us' }
326
+ );
327
+ });
328
+
329
+ it('isolates RUM login and logout updates from providers inheriting the global context', async () => {
330
+ const globalContext = { region: 'shared-region' };
331
+ await OpenFeature.setContext(globalContext);
332
+ const otherProvider = Object.assign(new InMemoryProvider({}), {
333
+ onContextChange: jest.fn(() => Promise.resolve())
334
+ });
335
+ await OpenFeature.setProviderAndWait('other-provider', otherProvider);
336
+
337
+ const applicationContext = { region: 'datadog-region' };
338
+ const { domain } = await setupProvider(
339
+ enrichWithRumUser(applicationContext)
340
+ );
341
+ await DdSdkReactNative.setUserInfo({
342
+ id: 'rum-user',
343
+ email: 'user@example.com'
344
+ });
345
+ await OpenFeature.setContext(
346
+ domain,
347
+ enrichWithRumUser(applicationContext)
348
+ );
349
+ expect(OpenFeature.getContext(domain)).toStrictEqual({
350
+ targetingKey: 'rum-user',
351
+ email: 'user@example.com',
352
+ region: 'datadog-region'
353
+ });
354
+ expect(
355
+ OpenFeature.getClient(domain).getBooleanValue('test-flag', false)
356
+ ).toBe(true);
357
+
358
+ await DdSdkReactNative.clearUserInfo();
359
+ await OpenFeature.setContext(
360
+ domain,
361
+ enrichWithRumUser(applicationContext)
362
+ );
363
+ expect(OpenFeature.getContext(domain)).toStrictEqual(
364
+ applicationContext
365
+ );
366
+ expect(OpenFeature.getContext()).toStrictEqual(globalContext);
367
+ expect(OpenFeature.getContext('other-provider')).toStrictEqual(
368
+ globalContext
369
+ );
370
+ expect(otherProvider.onContextChange).not.toHaveBeenCalled();
371
+ });
372
+
373
+ it('is independent of RUM feature flag evaluation tracking', async () => {
374
+ await DdFlags.enable({ rumIntegrationEnabled: false });
375
+ UserInfoSingleton.getInstance().setUserInfo({ id: 'rum-user' });
376
+
377
+ const { clientName } = await setupProvider(enrichWithRumUser({}));
378
+
379
+ expect(NativeDdFlags.setEvaluationContext).toHaveBeenLastCalledWith(
380
+ clientName,
381
+ 'rum-user',
382
+ {}
383
+ );
384
+ });
385
+
386
+ it('uses undefined application fields as tombstones for RUM attributes', async () => {
387
+ UserInfoSingleton.getInstance().setUserInfo({
388
+ id: 'rum-user',
389
+ email: 'rum@example.com',
390
+ extraInfo: { plan: 'pro' }
391
+ });
392
+
393
+ const { clientName, domain } = await setupProvider(
394
+ enrichWithRumUser({ email: undefined, plan: undefined })
395
+ );
396
+ await OpenFeature.getClient(domain).getBooleanValue('test-flag', false);
397
+
398
+ expect(OpenFeature.getContext(domain)).toStrictEqual({
399
+ targetingKey: 'rum-user'
400
+ });
401
+ expect(NativeDdFlags.setEvaluationContext).toHaveBeenCalledWith(
402
+ clientName,
403
+ 'rum-user',
404
+ {}
405
+ );
406
+ expect(NativeDdFlags.trackEvaluation).toHaveBeenCalledWith(
407
+ clientName,
408
+ 'test-flag',
409
+ expect.any(Object),
410
+ 'rum-user',
411
+ {}
412
+ );
413
+ });
414
+ });
@@ -0,0 +1,100 @@
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 { UserInfoSingleton } from '../../../core/src/sdk/UserInfoSingleton/UserInfoSingleton';
8
+ import type { EnrichableEvaluationContext } from '../index';
9
+ import { enrichWithRumUser } from '../index';
10
+
11
+ describe('enrichWithRumUser', () => {
12
+ beforeEach(() => {
13
+ UserInfoSingleton.reset();
14
+ });
15
+
16
+ it('explicitly adds the current RUM user to application context', () => {
17
+ UserInfoSingleton.getInstance().setUserInfo({
18
+ id: 'rum-user',
19
+ name: 'RUM Name',
20
+ email: 'rum@example.com',
21
+ extraInfo: {
22
+ plan: 'pro',
23
+ profile: { tier: 'enterprise' }
24
+ }
25
+ });
26
+
27
+ expect(
28
+ enrichWithRumUser({
29
+ targetingKey: 'device-subject',
30
+ email: 'application@example.com',
31
+ region: 'us-east-1'
32
+ })
33
+ ).toStrictEqual({
34
+ targetingKey: 'device-subject',
35
+ name: 'RUM Name',
36
+ email: 'application@example.com',
37
+ plan: 'pro',
38
+ region: 'us-east-1'
39
+ });
40
+ });
41
+
42
+ it('uses undefined application fields as tombstones', () => {
43
+ UserInfoSingleton.getInstance().setUserInfo({
44
+ id: 'rum-user',
45
+ email: 'rum@example.com',
46
+ extraInfo: { plan: 'pro' }
47
+ });
48
+
49
+ expect(
50
+ enrichWithRumUser({
51
+ email: undefined,
52
+ plan: undefined
53
+ })
54
+ ).toStrictEqual({ targetingKey: 'rum-user' });
55
+ });
56
+
57
+ it.each([undefined, 'application-user'])(
58
+ 'uses an optional application targeting key of %s without falling back to the RUM user',
59
+ targetingKey => {
60
+ UserInfoSingleton.getInstance().setUserInfo({ id: 'rum-user' });
61
+ const context: EnrichableEvaluationContext = {
62
+ targetingKey,
63
+ region: 'us'
64
+ };
65
+
66
+ expect(enrichWithRumUser(context)).toStrictEqual(
67
+ targetingKey === undefined
68
+ ? { region: 'us' }
69
+ : { targetingKey, region: 'us' }
70
+ );
71
+ expect(context).toHaveProperty('targetingKey', targetingKey);
72
+ }
73
+ );
74
+
75
+ it('normalizes undefined fields when no RUM user is available', () => {
76
+ expect(
77
+ enrichWithRumUser({
78
+ targetingKey: 'application-subject',
79
+ email: undefined
80
+ })
81
+ ).toStrictEqual({ targetingKey: 'application-subject' });
82
+ });
83
+
84
+ it('reads the latest RUM user without mutating the application context', () => {
85
+ const applicationContext = { region: 'us-east-1' };
86
+
87
+ UserInfoSingleton.getInstance().setUserInfo({ id: 'rum-user-a' });
88
+ expect(enrichWithRumUser(applicationContext)).toStrictEqual({
89
+ targetingKey: 'rum-user-a',
90
+ region: 'us-east-1'
91
+ });
92
+
93
+ UserInfoSingleton.getInstance().setUserInfo({ id: 'rum-user-b' });
94
+ expect(enrichWithRumUser(applicationContext)).toStrictEqual({
95
+ targetingKey: 'rum-user-b',
96
+ region: 'us-east-1'
97
+ });
98
+ expect(applicationContext).toStrictEqual({ region: 'us-east-1' });
99
+ });
100
+ });
@@ -0,0 +1,27 @@
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 { spawnSync } from 'child_process';
8
+ import { resolve } from 'path';
9
+
10
+ it('typechecks RUM context inputs and outputs against the public API', () => {
11
+ // Run tsc separately so Jest's React Native transforms do not process the compiler itself.
12
+ const result = spawnSync(
13
+ process.execPath,
14
+ [
15
+ require.resolve('typescript/bin/tsc'),
16
+ '--project',
17
+ resolve(__dirname, '__utils__/tsconfig.json'),
18
+ '--pretty',
19
+ 'false'
20
+ ],
21
+ { encoding: 'utf8', timeout: 120000 }
22
+ );
23
+
24
+ expect(result.error).toBeUndefined();
25
+ expect(result.stdout + result.stderr).toBe('');
26
+ expect(result.status).toBe(0);
27
+ });