@datadog/mobile-react-native-openfeature 3.9.0 → 3.11.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.
- package/README.md +118 -10
- package/lib/commonjs/datadogCoreProvider.js +129 -0
- package/lib/commonjs/datadogCoreProvider.js.map +1 -0
- package/lib/commonjs/offlineEvaluation.js +198 -0
- package/lib/commonjs/offlineEvaluation.js.map +1 -0
- package/lib/commonjs/offlineProvider.js +44 -8
- package/lib/commonjs/offlineProvider.js.map +1 -1
- package/lib/commonjs/rules-based.js +62 -0
- package/lib/commonjs/rules-based.js.map +1 -0
- package/lib/module/datadogCoreProvider.js +122 -0
- package/lib/module/datadogCoreProvider.js.map +1 -0
- package/lib/module/offlineEvaluation.js +192 -0
- package/lib/module/offlineEvaluation.js.map +1 -0
- package/lib/module/offlineProvider.js +45 -9
- package/lib/module/offlineProvider.js.map +1 -1
- package/lib/module/rules-based.js +50 -0
- package/lib/module/rules-based.js.map +1 -0
- package/lib/typescript/core/src/config/FileBasedConfiguration.d.ts.map +1 -1
- package/lib/typescript/core/src/config/FileBasedConfiguration.type.d.ts +2 -0
- package/lib/typescript/core/src/config/FileBasedConfiguration.type.d.ts.map +1 -1
- package/lib/typescript/core/src/config/async/AutoInstrumentationConfiguration.d.ts +4 -0
- package/lib/typescript/core/src/config/async/AutoInstrumentationConfiguration.d.ts.map +1 -1
- package/lib/typescript/core/src/config/async/PartialInitializationConfiguration.d.ts +2 -0
- package/lib/typescript/core/src/config/async/PartialInitializationConfiguration.d.ts.map +1 -1
- package/lib/typescript/core/src/config/features/RumConfiguration.d.ts +4 -0
- package/lib/typescript/core/src/config/features/RumConfiguration.d.ts.map +1 -1
- package/lib/typescript/core/src/config/features/RumConfiguration.type.d.ts +21 -0
- package/lib/typescript/core/src/config/features/RumConfiguration.type.d.ts.map +1 -1
- package/lib/typescript/core/src/config/features/RumConfigurationNative.d.ts +2 -0
- package/lib/typescript/core/src/config/features/RumConfigurationNative.d.ts.map +1 -1
- package/lib/typescript/core/src/flags/FlagsClient.d.ts +38 -1
- package/lib/typescript/core/src/flags/FlagsClient.d.ts.map +1 -1
- package/lib/typescript/core/src/flags/internal.d.ts +1 -0
- package/lib/typescript/core/src/flags/internal.d.ts.map +1 -1
- package/lib/typescript/core/src/flags/types.d.ts +1 -1
- package/lib/typescript/core/src/flags/types.d.ts.map +1 -1
- package/lib/typescript/core/src/rum/DdRum.d.ts +1 -0
- package/lib/typescript/core/src/rum/DdRum.d.ts.map +1 -1
- package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/common/RequestContext.d.ts +2 -0
- package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/common/RequestContext.d.ts.map +1 -1
- package/lib/typescript/core/src/rum/types.d.ts +6 -0
- package/lib/typescript/core/src/rum/types.d.ts.map +1 -1
- package/lib/typescript/core/src/sdk/AccountInfoSingleton/AccountInfoSingleton.d.ts.map +1 -1
- package/lib/typescript/core/src/sdk/UserInfoSingleton/UserInfoSingleton.d.ts.map +1 -1
- package/lib/typescript/core/src/specs/NativeDdRum.d.ts +6 -0
- package/lib/typescript/core/src/specs/NativeDdRum.d.ts.map +1 -1
- package/lib/typescript/core/src/version.d.ts +1 -1
- package/lib/typescript/core/src/version.d.ts.map +1 -1
- package/lib/typescript/react-native-openfeature/src/datadogCoreProvider.d.ts +31 -0
- package/lib/typescript/react-native-openfeature/src/datadogCoreProvider.d.ts.map +1 -0
- package/lib/typescript/react-native-openfeature/src/index.d.ts +1 -0
- package/lib/typescript/react-native-openfeature/src/index.d.ts.map +1 -1
- package/lib/typescript/react-native-openfeature/src/offlineEvaluation.d.ts +33 -0
- package/lib/typescript/react-native-openfeature/src/offlineEvaluation.d.ts.map +1 -0
- package/lib/typescript/react-native-openfeature/src/offlineProvider.d.ts +14 -8
- package/lib/typescript/react-native-openfeature/src/offlineProvider.d.ts.map +1 -1
- package/lib/typescript/react-native-openfeature/src/rules-based.d.ts +17 -0
- package/lib/typescript/react-native-openfeature/src/rules-based.d.ts.map +1 -0
- package/package.json +8 -4
- package/rules-based/package.json +7 -0
- package/src/__tests__/__utils__/coreConfiguration.ts +84 -0
- package/src/__tests__/datadogCoreProvider.integration.test.ts +230 -0
- package/src/__tests__/datadogCoreProvider.test.ts +587 -0
- package/src/__tests__/entrypoints.test.ts +35 -0
- package/src/__tests__/offlineProvider.delegation.test.ts +795 -0
- package/src/__tests__/offlineProvider.test.ts +74 -10
- package/src/datadogCoreProvider.ts +223 -0
- package/src/index.ts +1 -0
- package/src/offlineEvaluation.ts +295 -0
- package/src/offlineProvider.ts +64 -13
- package/src/rules-based.ts +58 -0
|
@@ -0,0 +1,587 @@
|
|
|
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 { configurationFromString } from '@datadog/flagging-core/rules-based';
|
|
8
|
+
import * as flaggingCore from '@datadog/flagging-core';
|
|
9
|
+
import type { FlagsConfiguration } from '@datadog/flagging-core';
|
|
10
|
+
import {
|
|
11
|
+
ErrorCode,
|
|
12
|
+
InvalidContextError,
|
|
13
|
+
ParseError,
|
|
14
|
+
ProviderEvents,
|
|
15
|
+
ProviderNotReadyError
|
|
16
|
+
} from '@openfeature/web-sdk';
|
|
17
|
+
import type { EvaluationContext, Logger } from '@openfeature/web-sdk';
|
|
18
|
+
|
|
19
|
+
import packageJson from '../../package.json';
|
|
20
|
+
import { DatadogCoreProvider } from '../rules-based';
|
|
21
|
+
|
|
22
|
+
import {
|
|
23
|
+
matchingContext,
|
|
24
|
+
precomputedConfiguration,
|
|
25
|
+
rulesConfiguration
|
|
26
|
+
} from './__utils__/coreConfiguration';
|
|
27
|
+
|
|
28
|
+
// Make the CommonJS barrel's re-exports spyable while retaining the real implementations.
|
|
29
|
+
jest.mock('@datadog/flagging-core', () => ({
|
|
30
|
+
__esModule: true,
|
|
31
|
+
...jest.requireActual('@datadog/flagging-core')
|
|
32
|
+
}));
|
|
33
|
+
|
|
34
|
+
// No native SDK initialization, networking, or tracking is needed for this provider.
|
|
35
|
+
jest.mock('@datadog/mobile-react-native', () => {
|
|
36
|
+
throw new Error('The core provider must not import the native SDK');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const logger: Logger = {
|
|
40
|
+
debug: jest.fn(),
|
|
41
|
+
info: jest.fn(),
|
|
42
|
+
warn: jest.fn(),
|
|
43
|
+
error: jest.fn()
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
function configuredProvider(
|
|
47
|
+
configuration: FlagsConfiguration
|
|
48
|
+
): DatadogCoreProvider {
|
|
49
|
+
const provider = new DatadogCoreProvider();
|
|
50
|
+
provider.setConfiguration(configuration);
|
|
51
|
+
return provider;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const configurationId = (provider: DatadogCoreProvider): unknown =>
|
|
55
|
+
provider.resolveBooleanEvaluation(
|
|
56
|
+
'boolean-flag',
|
|
57
|
+
false,
|
|
58
|
+
matchingContext,
|
|
59
|
+
logger
|
|
60
|
+
).flagMetadata?.__dd_core_configuration_id;
|
|
61
|
+
|
|
62
|
+
const rulesConfigurationId = (provider: DatadogCoreProvider): unknown =>
|
|
63
|
+
provider.resolveBooleanEvaluation(
|
|
64
|
+
'test-flag',
|
|
65
|
+
false,
|
|
66
|
+
matchingContext,
|
|
67
|
+
logger
|
|
68
|
+
).flagMetadata?.__dd_core_configuration_id;
|
|
69
|
+
|
|
70
|
+
describe('DatadogCoreProvider', () => {
|
|
71
|
+
afterEach(() => {
|
|
72
|
+
jest.restoreAllMocks();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('pins the evaluator to the React Native-tested version without a semver range', () => {
|
|
76
|
+
expect(packageJson.dependencies['@datadog/flagging-core']).toBe(
|
|
77
|
+
'3.1.1'
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('has browser-compatible metadata and stores the supplied configuration', () => {
|
|
82
|
+
const provider = new DatadogCoreProvider();
|
|
83
|
+
expect(provider.metadata).toEqual({ name: 'datadog-core' });
|
|
84
|
+
expect(provider.runsOn).toBe('client');
|
|
85
|
+
expect(provider.getConfiguration()).toBeUndefined();
|
|
86
|
+
|
|
87
|
+
const configuration = precomputedConfiguration();
|
|
88
|
+
provider.setConfiguration(configuration);
|
|
89
|
+
expect(provider.getConfiguration()).toBe(configuration);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('delegates all four evaluation types to the published flagging-core evaluator', () => {
|
|
93
|
+
const configuration = precomputedConfiguration();
|
|
94
|
+
const provider = configuredProvider(configuration);
|
|
95
|
+
const evaluate = jest.spyOn(flaggingCore, 'evaluate');
|
|
96
|
+
const defaultObject = { nested: { enabled: false }, list: [] };
|
|
97
|
+
|
|
98
|
+
expect(
|
|
99
|
+
provider.resolveBooleanEvaluation(
|
|
100
|
+
'boolean-flag',
|
|
101
|
+
false,
|
|
102
|
+
matchingContext,
|
|
103
|
+
logger
|
|
104
|
+
)
|
|
105
|
+
).toMatchObject({
|
|
106
|
+
value: true,
|
|
107
|
+
variant: 'on',
|
|
108
|
+
reason: 'TARGETING_MATCH'
|
|
109
|
+
});
|
|
110
|
+
expect(
|
|
111
|
+
provider.resolveStringEvaluation(
|
|
112
|
+
'string-flag',
|
|
113
|
+
'default',
|
|
114
|
+
matchingContext,
|
|
115
|
+
logger
|
|
116
|
+
)
|
|
117
|
+
).toMatchObject({
|
|
118
|
+
value: 'hello',
|
|
119
|
+
variant: 'greeting',
|
|
120
|
+
reason: 'STATIC'
|
|
121
|
+
});
|
|
122
|
+
expect(
|
|
123
|
+
provider.resolveNumberEvaluation(
|
|
124
|
+
'number-flag',
|
|
125
|
+
0,
|
|
126
|
+
matchingContext,
|
|
127
|
+
logger
|
|
128
|
+
)
|
|
129
|
+
).toMatchObject({ value: 3.14, variant: 'amount', reason: 'STATIC' });
|
|
130
|
+
expect(
|
|
131
|
+
provider.resolveObjectEvaluation(
|
|
132
|
+
'object-flag',
|
|
133
|
+
defaultObject,
|
|
134
|
+
matchingContext,
|
|
135
|
+
logger
|
|
136
|
+
)
|
|
137
|
+
).toMatchObject({
|
|
138
|
+
value: { nested: { enabled: true }, list: [1, 2] },
|
|
139
|
+
variant: 'settings'
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
expect(evaluate.mock.calls).toEqual([
|
|
143
|
+
[
|
|
144
|
+
configuration,
|
|
145
|
+
'boolean',
|
|
146
|
+
'boolean-flag',
|
|
147
|
+
false,
|
|
148
|
+
matchingContext,
|
|
149
|
+
logger
|
|
150
|
+
],
|
|
151
|
+
[
|
|
152
|
+
configuration,
|
|
153
|
+
'string',
|
|
154
|
+
'string-flag',
|
|
155
|
+
'default',
|
|
156
|
+
matchingContext,
|
|
157
|
+
logger
|
|
158
|
+
],
|
|
159
|
+
[
|
|
160
|
+
configuration,
|
|
161
|
+
'number',
|
|
162
|
+
'number-flag',
|
|
163
|
+
0,
|
|
164
|
+
matchingContext,
|
|
165
|
+
logger
|
|
166
|
+
],
|
|
167
|
+
[
|
|
168
|
+
configuration,
|
|
169
|
+
'object',
|
|
170
|
+
'object-flag',
|
|
171
|
+
defaultObject,
|
|
172
|
+
matchingContext,
|
|
173
|
+
logger
|
|
174
|
+
]
|
|
175
|
+
]);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('preserves flag-not-found and type-mismatch defaults and error details', () => {
|
|
179
|
+
const provider = configuredProvider(precomputedConfiguration());
|
|
180
|
+
expect(
|
|
181
|
+
provider.resolveBooleanEvaluation(
|
|
182
|
+
'missing',
|
|
183
|
+
false,
|
|
184
|
+
matchingContext,
|
|
185
|
+
logger
|
|
186
|
+
)
|
|
187
|
+
).toMatchObject({
|
|
188
|
+
value: false,
|
|
189
|
+
reason: 'ERROR',
|
|
190
|
+
errorCode: ErrorCode.FLAG_NOT_FOUND
|
|
191
|
+
});
|
|
192
|
+
expect(
|
|
193
|
+
provider.resolveNumberEvaluation(
|
|
194
|
+
'boolean-flag',
|
|
195
|
+
42,
|
|
196
|
+
matchingContext,
|
|
197
|
+
logger
|
|
198
|
+
)
|
|
199
|
+
).toMatchObject({
|
|
200
|
+
value: 42,
|
|
201
|
+
reason: 'ERROR',
|
|
202
|
+
errorCode: ErrorCode.TYPE_MISMATCH
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('evaluates rules locally with the supplied context', async () => {
|
|
207
|
+
const provider = configuredProvider(rulesConfiguration());
|
|
208
|
+
const otherContext = { targetingKey: 'user-1', country: 'CA' };
|
|
209
|
+
await provider.initialize(otherContext);
|
|
210
|
+
expect(
|
|
211
|
+
provider.resolveBooleanEvaluation(
|
|
212
|
+
'test-flag',
|
|
213
|
+
false,
|
|
214
|
+
otherContext,
|
|
215
|
+
logger
|
|
216
|
+
)
|
|
217
|
+
).toMatchObject({
|
|
218
|
+
value: false,
|
|
219
|
+
variant: 'off',
|
|
220
|
+
reason: 'SPLIT',
|
|
221
|
+
flagMetadata: { doLog: false }
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
provider.onContextChange(otherContext, matchingContext);
|
|
225
|
+
expect(
|
|
226
|
+
provider.resolveBooleanEvaluation(
|
|
227
|
+
'test-flag',
|
|
228
|
+
false,
|
|
229
|
+
matchingContext,
|
|
230
|
+
logger
|
|
231
|
+
)
|
|
232
|
+
).toMatchObject({
|
|
233
|
+
value: true,
|
|
234
|
+
variant: 'on',
|
|
235
|
+
reason: 'TARGETING_MATCH',
|
|
236
|
+
flagMetadata: { allocationKey: 'allocation', doLog: true }
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it('prefers matching precomputed data and falls back to rules for a different context', async () => {
|
|
241
|
+
const provider = configuredProvider({
|
|
242
|
+
...rulesConfiguration(),
|
|
243
|
+
...precomputedConfiguration()
|
|
244
|
+
});
|
|
245
|
+
await provider.initialize(matchingContext);
|
|
246
|
+
expect(
|
|
247
|
+
provider.resolveStringEvaluation(
|
|
248
|
+
'string-flag',
|
|
249
|
+
'default',
|
|
250
|
+
matchingContext,
|
|
251
|
+
logger
|
|
252
|
+
).value
|
|
253
|
+
).toBe('hello');
|
|
254
|
+
// No per-flag fallback to rules when the precomputed capability matches.
|
|
255
|
+
expect(
|
|
256
|
+
provider.resolveBooleanEvaluation(
|
|
257
|
+
'test-flag',
|
|
258
|
+
false,
|
|
259
|
+
matchingContext,
|
|
260
|
+
logger
|
|
261
|
+
).errorCode
|
|
262
|
+
).toBe(ErrorCode.FLAG_NOT_FOUND);
|
|
263
|
+
|
|
264
|
+
const otherContext = { targetingKey: 'user-2', country: 'US' };
|
|
265
|
+
provider.onContextChange(matchingContext, otherContext);
|
|
266
|
+
expect(
|
|
267
|
+
provider.resolveBooleanEvaluation(
|
|
268
|
+
'test-flag',
|
|
269
|
+
false,
|
|
270
|
+
otherContext,
|
|
271
|
+
logger
|
|
272
|
+
).value
|
|
273
|
+
).toBe(true);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it('can use a valid capability even if the other capability failed to parse', async () => {
|
|
277
|
+
const rules = configuredProvider({
|
|
278
|
+
...rulesConfiguration(),
|
|
279
|
+
precomputedError: 'Malformed precomputed data'
|
|
280
|
+
});
|
|
281
|
+
await expect(
|
|
282
|
+
rules.initialize(matchingContext)
|
|
283
|
+
).resolves.toBeUndefined();
|
|
284
|
+
expect(
|
|
285
|
+
rules.resolveBooleanEvaluation(
|
|
286
|
+
'test-flag',
|
|
287
|
+
false,
|
|
288
|
+
matchingContext,
|
|
289
|
+
logger
|
|
290
|
+
).value
|
|
291
|
+
).toBe(true);
|
|
292
|
+
|
|
293
|
+
const precomputed = configuredProvider({
|
|
294
|
+
...precomputedConfiguration(),
|
|
295
|
+
rulesError: 'Malformed rules data'
|
|
296
|
+
});
|
|
297
|
+
await expect(
|
|
298
|
+
precomputed.initialize(matchingContext)
|
|
299
|
+
).resolves.toBeUndefined();
|
|
300
|
+
expect(
|
|
301
|
+
precomputed.resolveBooleanEvaluation(
|
|
302
|
+
'boolean-flag',
|
|
303
|
+
false,
|
|
304
|
+
matchingContext,
|
|
305
|
+
logger
|
|
306
|
+
).value
|
|
307
|
+
).toBe(true);
|
|
308
|
+
expect(() => precomputed.onContextChange(matchingContext, {})).toThrow(
|
|
309
|
+
ParseError
|
|
310
|
+
);
|
|
311
|
+
expect(
|
|
312
|
+
precomputed.resolveBooleanEvaluation(
|
|
313
|
+
'boolean-flag',
|
|
314
|
+
false,
|
|
315
|
+
{},
|
|
316
|
+
logger
|
|
317
|
+
)
|
|
318
|
+
).toMatchObject({
|
|
319
|
+
value: false,
|
|
320
|
+
reason: 'ERROR',
|
|
321
|
+
errorCode: ErrorCode.PARSE_ERROR,
|
|
322
|
+
errorMessage: 'Malformed rules data'
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it.each([
|
|
327
|
+
{},
|
|
328
|
+
({ targetingKey: undefined } as unknown) as EvaluationContext
|
|
329
|
+
])(
|
|
330
|
+
'does not adopt the embedded precomputed context when initialized with %p',
|
|
331
|
+
async context => {
|
|
332
|
+
const provider = configuredProvider(precomputedConfiguration());
|
|
333
|
+
await expect(provider.initialize(context)).rejects.toBeInstanceOf(
|
|
334
|
+
InvalidContextError
|
|
335
|
+
);
|
|
336
|
+
expect(
|
|
337
|
+
provider.resolveBooleanEvaluation(
|
|
338
|
+
'boolean-flag',
|
|
339
|
+
false,
|
|
340
|
+
context,
|
|
341
|
+
logger
|
|
342
|
+
)
|
|
343
|
+
).toMatchObject({
|
|
344
|
+
value: false,
|
|
345
|
+
reason: 'ERROR',
|
|
346
|
+
errorCode: ErrorCode.INVALID_CONTEXT
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
it('rejects missing configuration and returns provider-not-ready evaluation details', async () => {
|
|
352
|
+
const provider = new DatadogCoreProvider();
|
|
353
|
+
await expect(provider.initialize()).rejects.toBeInstanceOf(
|
|
354
|
+
ProviderNotReadyError
|
|
355
|
+
);
|
|
356
|
+
expect(
|
|
357
|
+
provider.resolveBooleanEvaluation('missing', true, {}, logger)
|
|
358
|
+
).toEqual({
|
|
359
|
+
value: true,
|
|
360
|
+
reason: 'ERROR',
|
|
361
|
+
errorCode: ErrorCode.PROVIDER_NOT_READY,
|
|
362
|
+
errorMessage: 'No flags configuration has been set'
|
|
363
|
+
});
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
it.each([
|
|
367
|
+
{},
|
|
368
|
+
configurationFromString('not json'),
|
|
369
|
+
{ rulesError: 'Malformed rules data' }
|
|
370
|
+
])(
|
|
371
|
+
'rejects an unusable configuration with a parse error',
|
|
372
|
+
async configuration => {
|
|
373
|
+
const provider = configuredProvider(configuration);
|
|
374
|
+
await expect(provider.initialize()).rejects.toBeInstanceOf(
|
|
375
|
+
ParseError
|
|
376
|
+
);
|
|
377
|
+
expect(
|
|
378
|
+
provider.resolveBooleanEvaluation('missing', true, {}, logger)
|
|
379
|
+
).toMatchObject({
|
|
380
|
+
value: true,
|
|
381
|
+
reason: 'ERROR',
|
|
382
|
+
errorCode: ErrorCode.PARSE_ERROR
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
);
|
|
386
|
+
|
|
387
|
+
it.each([null, undefined, 'not a configuration', 42])(
|
|
388
|
+
'rejects a non-object runtime configuration (%p) with a parse error',
|
|
389
|
+
async configuration => {
|
|
390
|
+
const provider = configuredProvider(
|
|
391
|
+
(configuration as unknown) as FlagsConfiguration
|
|
392
|
+
);
|
|
393
|
+
await expect(provider.initialize()).rejects.toBeInstanceOf(
|
|
394
|
+
ParseError
|
|
395
|
+
);
|
|
396
|
+
expect(
|
|
397
|
+
provider.resolveBooleanEvaluation('missing', true, {}, logger)
|
|
398
|
+
).toEqual({
|
|
399
|
+
value: true,
|
|
400
|
+
reason: 'ERROR',
|
|
401
|
+
errorCode: ErrorCode.PARSE_ERROR,
|
|
402
|
+
errorMessage: 'Flags configuration must be an object'
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
);
|
|
406
|
+
|
|
407
|
+
it('replaces a usable configuration with a parse error when given null after initialization', async () => {
|
|
408
|
+
const provider = configuredProvider(rulesConfiguration());
|
|
409
|
+
await provider.initialize({});
|
|
410
|
+
const errorHandler = jest.fn();
|
|
411
|
+
provider.events.addHandler(ProviderEvents.Error, errorHandler);
|
|
412
|
+
|
|
413
|
+
expect(() =>
|
|
414
|
+
provider.setConfiguration((null as unknown) as FlagsConfiguration)
|
|
415
|
+
).not.toThrow();
|
|
416
|
+
expect(errorHandler).toHaveBeenLastCalledWith({
|
|
417
|
+
error: expect.any(ParseError),
|
|
418
|
+
message: 'Flags configuration must be an object',
|
|
419
|
+
errorCode: ErrorCode.PARSE_ERROR
|
|
420
|
+
});
|
|
421
|
+
expect(
|
|
422
|
+
provider.resolveBooleanEvaluation('test-flag', false, {}, logger)
|
|
423
|
+
).toMatchObject({
|
|
424
|
+
value: false,
|
|
425
|
+
reason: 'ERROR',
|
|
426
|
+
errorCode: ErrorCode.PARSE_ERROR
|
|
427
|
+
});
|
|
428
|
+
expect(rulesConfigurationId(provider)).toBeUndefined();
|
|
429
|
+
|
|
430
|
+
// A later valid configuration recovers.
|
|
431
|
+
const readyHandler = jest.fn();
|
|
432
|
+
provider.events.addHandler(ProviderEvents.Ready, readyHandler);
|
|
433
|
+
provider.setConfiguration(rulesConfiguration());
|
|
434
|
+
expect(readyHandler).toHaveBeenCalledTimes(1);
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
it('defers configuration events and validation until initialization supplies the context', async () => {
|
|
438
|
+
const provider = new DatadogCoreProvider();
|
|
439
|
+
const handler = jest.fn();
|
|
440
|
+
provider.events.addHandler(ProviderEvents.Ready, handler);
|
|
441
|
+
provider.events.addHandler(
|
|
442
|
+
ProviderEvents.ConfigurationChanged,
|
|
443
|
+
handler
|
|
444
|
+
);
|
|
445
|
+
provider.events.addHandler(ProviderEvents.Error, handler);
|
|
446
|
+
provider.setConfiguration({});
|
|
447
|
+
provider.setConfiguration(precomputedConfiguration());
|
|
448
|
+
expect(handler).not.toHaveBeenCalled();
|
|
449
|
+
await expect(
|
|
450
|
+
provider.initialize(matchingContext)
|
|
451
|
+
).resolves.toBeUndefined();
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
it('emits Ready then ConfigurationChanged when recovering, and only ConfigurationChanged on replacement', async () => {
|
|
455
|
+
const provider = new DatadogCoreProvider();
|
|
456
|
+
const events: string[] = [];
|
|
457
|
+
provider.events.addHandler(ProviderEvents.Ready, () =>
|
|
458
|
+
events.push('ready')
|
|
459
|
+
);
|
|
460
|
+
provider.events.addHandler(ProviderEvents.ConfigurationChanged, () =>
|
|
461
|
+
events.push('changed')
|
|
462
|
+
);
|
|
463
|
+
await expect(provider.initialize(matchingContext)).rejects.toThrow();
|
|
464
|
+
|
|
465
|
+
provider.setConfiguration(precomputedConfiguration());
|
|
466
|
+
expect(events).toEqual(['ready', 'changed']);
|
|
467
|
+
provider.setConfiguration(rulesConfiguration());
|
|
468
|
+
expect(events).toEqual(['ready', 'changed', 'changed']);
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
it('emits precise error events for invalid replacement configurations and context mismatches', async () => {
|
|
472
|
+
const provider = configuredProvider(rulesConfiguration());
|
|
473
|
+
const errorHandler = jest.fn();
|
|
474
|
+
const changedHandler = jest.fn();
|
|
475
|
+
await provider.initialize({});
|
|
476
|
+
provider.events.addHandler(ProviderEvents.Error, errorHandler);
|
|
477
|
+
provider.events.addHandler(
|
|
478
|
+
ProviderEvents.ConfigurationChanged,
|
|
479
|
+
changedHandler
|
|
480
|
+
);
|
|
481
|
+
|
|
482
|
+
provider.setConfiguration({});
|
|
483
|
+
expect(errorHandler).toHaveBeenLastCalledWith({
|
|
484
|
+
error: expect.any(ParseError),
|
|
485
|
+
message: 'Flags configuration contains no usable capability',
|
|
486
|
+
errorCode: ErrorCode.PARSE_ERROR
|
|
487
|
+
});
|
|
488
|
+
provider.setConfiguration(precomputedConfiguration());
|
|
489
|
+
expect(errorHandler).toHaveBeenLastCalledWith({
|
|
490
|
+
error: expect.any(InvalidContextError),
|
|
491
|
+
message:
|
|
492
|
+
'Precomputed flags configuration does not match the current context',
|
|
493
|
+
errorCode: ErrorCode.INVALID_CONTEXT
|
|
494
|
+
});
|
|
495
|
+
expect(changedHandler).not.toHaveBeenCalled();
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
it('retains the new context even when reconciliation throws', async () => {
|
|
499
|
+
const provider = configuredProvider(precomputedConfiguration());
|
|
500
|
+
const readyHandler = jest.fn();
|
|
501
|
+
provider.events.addHandler(ProviderEvents.Ready, readyHandler);
|
|
502
|
+
await provider.initialize(matchingContext);
|
|
503
|
+
expect(() =>
|
|
504
|
+
provider.onContextChange(matchingContext, { targetingKey: 'other' })
|
|
505
|
+
).toThrow(InvalidContextError);
|
|
506
|
+
const replacement = precomputedConfiguration();
|
|
507
|
+
replacement.precomputed.context = { targetingKey: 'other' };
|
|
508
|
+
provider.setConfiguration(replacement);
|
|
509
|
+
expect(readyHandler).toHaveBeenCalledTimes(1);
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
it('adds configuration identity only to trackable evaluations', () => {
|
|
513
|
+
const provider = configuredProvider(precomputedConfiguration());
|
|
514
|
+
expect(configurationId(provider)).toEqual(expect.any(String));
|
|
515
|
+
expect(
|
|
516
|
+
provider.resolveStringEvaluation(
|
|
517
|
+
'string-flag',
|
|
518
|
+
'',
|
|
519
|
+
matchingContext,
|
|
520
|
+
logger
|
|
521
|
+
).flagMetadata?.__dd_core_configuration_id
|
|
522
|
+
).toBeUndefined();
|
|
523
|
+
expect(
|
|
524
|
+
provider.resolveBooleanEvaluation(
|
|
525
|
+
'missing',
|
|
526
|
+
false,
|
|
527
|
+
matchingContext,
|
|
528
|
+
logger
|
|
529
|
+
).flagMetadata?.__dd_core_configuration_id
|
|
530
|
+
).toBeUndefined();
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
it('keeps configuration identity stable across retrieval metadata changes, but not value changes', () => {
|
|
534
|
+
const configuration = precomputedConfiguration();
|
|
535
|
+
const provider = configuredProvider(configuration);
|
|
536
|
+
const original = configurationId(provider);
|
|
537
|
+
provider.setConfiguration({
|
|
538
|
+
precomputed: {
|
|
539
|
+
...configuration.precomputed,
|
|
540
|
+
etag: 'new',
|
|
541
|
+
fetchedAt: flaggingCore.timeStampNow()
|
|
542
|
+
}
|
|
543
|
+
});
|
|
544
|
+
expect(configurationId(provider)).toBe(original);
|
|
545
|
+
const replacement = precomputedConfiguration();
|
|
546
|
+
replacement.precomputed.response.data.attributes.flags[
|
|
547
|
+
'boolean-flag'
|
|
548
|
+
].variationValue = false;
|
|
549
|
+
provider.setConfiguration(replacement);
|
|
550
|
+
expect(configurationId(provider)).not.toBe(original);
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
it('keeps rules identity stable across retrieval metadata and build timestamp changes', () => {
|
|
554
|
+
const configuration = rulesConfiguration();
|
|
555
|
+
const provider = configuredProvider(configuration);
|
|
556
|
+
const original = rulesConfigurationId(provider);
|
|
557
|
+
const createdAt = configuration.rules.response.createdAt;
|
|
558
|
+
if (!createdAt) {
|
|
559
|
+
throw new Error('Rules fixture must have a build timestamp');
|
|
560
|
+
}
|
|
561
|
+
provider.setConfiguration({
|
|
562
|
+
rules: {
|
|
563
|
+
...configuration.rules,
|
|
564
|
+
response: {
|
|
565
|
+
...configuration.rules.response,
|
|
566
|
+
createdAt: {
|
|
567
|
+
...createdAt,
|
|
568
|
+
nanos: 123
|
|
569
|
+
}
|
|
570
|
+
},
|
|
571
|
+
etag: 'new',
|
|
572
|
+
fetchedAt: flaggingCore.timeStampNow()
|
|
573
|
+
}
|
|
574
|
+
});
|
|
575
|
+
expect(rulesConfigurationId(provider)).toBe(original);
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
it('still loads configuration and assigns a distinct fallback identity if hashing fails', () => {
|
|
579
|
+
jest.spyOn(flaggingCore, 'getMD5Hash').mockImplementation(() => {
|
|
580
|
+
throw new Error('hash failed');
|
|
581
|
+
});
|
|
582
|
+
const provider = configuredProvider(precomputedConfiguration());
|
|
583
|
+
expect(configurationId(provider)).toBe('core-configuration-1');
|
|
584
|
+
provider.setConfiguration(precomputedConfiguration());
|
|
585
|
+
expect(configurationId(provider)).toBe('core-configuration-2');
|
|
586
|
+
});
|
|
587
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
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
|
+
/* eslint-disable global-require */
|
|
8
|
+
|
|
9
|
+
jest.mock('../../../core/src/specs/NativeDdFlags', () => ({
|
|
10
|
+
__esModule: true,
|
|
11
|
+
default: {
|
|
12
|
+
enable: jest.fn(() => Promise.resolve()),
|
|
13
|
+
setEvaluationContext: jest.fn(() => Promise.resolve({})),
|
|
14
|
+
trackEvaluation: jest.fn(() => Promise.resolve())
|
|
15
|
+
}
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
// The rules parser loads Protobuf-ES. Only the `/rules-based` entry may load it.
|
|
19
|
+
jest.mock('@datadog/flagging-core/rules-based', () => {
|
|
20
|
+
throw new Error('rules-based parser loaded');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe('package entry points', () => {
|
|
24
|
+
it('keeps the rules-based parser out of the main entry', () => {
|
|
25
|
+
expect(() =>
|
|
26
|
+
jest.isolateModules(() => require('../index'))
|
|
27
|
+
).not.toThrow();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('loads the rules-based parser from the rules-based entry', () => {
|
|
31
|
+
expect(() =>
|
|
32
|
+
jest.isolateModules(() => require('../rules-based'))
|
|
33
|
+
).toThrow('rules-based parser loaded');
|
|
34
|
+
});
|
|
35
|
+
});
|