|
1 | | -import { DatadogProvider } from '@datadog/openfeature-browser' |
2 | | -import { OpenFeature } from '@openfeature/web-sdk' |
3 | | - |
4 | | -// Initialize the Datadog provider |
5 | | -const provider = new DatadogProvider({ |
6 | | - applicationId: 'test-app-id', |
7 | | - clientToken: 'test-token', |
8 | | - site: 'datadoghq.com', |
9 | | - service: 'test-service', |
| 1 | +import { evaluateRulesBasedConfiguration, matchesRule, OperatorType } from '@datadog/flagging-core' |
| 2 | +import { configurationFromString, configurationToString } from '@datadog/openfeature-browser' |
| 3 | +import { assert, reportSuccess } from './smoke' |
| 4 | + |
| 5 | +const rulesResponse = |
| 6 | + 'EgRwcm9kGigKDGJyb3dzZXItZmxhZxIYEAQaAigBIhAKCmFsbG9jYXRpb24iAiADGigKDGludGVnZXItZmxhZxIYEAIaAhgqIhAKCmFsbG9jYXRpb24iAiADKgJvbg==' |
| 7 | +const configuration = configurationFromString(JSON.stringify({ version: 1, rules: { response: rulesResponse } })) |
| 8 | + |
| 9 | +assert(configuration.rules, 'rules configuration was not parsed') |
| 10 | + |
| 11 | +const context = { targetingKey: 'browser-user' } |
| 12 | +const booleanDetails = evaluateRulesBasedConfiguration( |
| 13 | + configuration.rules.response, |
| 14 | + 'boolean', |
| 15 | + 'browser-flag', |
| 16 | + false, |
| 17 | + context, |
| 18 | + console |
| 19 | +) |
| 20 | +const integerDetails = evaluateRulesBasedConfiguration( |
| 21 | + configuration.rules.response, |
| 22 | + 'number', |
| 23 | + 'integer-flag', |
| 24 | + 0, |
| 25 | + context, |
| 26 | + console |
| 27 | +) |
| 28 | +const restored = configurationFromString(configurationToString(configuration)) |
| 29 | +const sha256Matched = matchesRule( |
| 30 | + { |
| 31 | + conditions: [ |
| 32 | + { |
| 33 | + attribute: 'name', |
| 34 | + operator: OperatorType.ONE_OF_SHA256, |
| 35 | + value: { |
| 36 | + salt: [1, 2], |
| 37 | + hashes: ['c0e551d80aa1e2cb1eaf5be7edbb04e51eb1823e562e2ce5dfeda0ecba76c744'], |
| 38 | + }, |
| 39 | + }, |
| 40 | + ], |
| 41 | + }, |
| 42 | + { name: 'hello' } |
| 43 | +) |
| 44 | + |
| 45 | +assert(booleanDetails.value === true, 'boolean rule evaluation returned the wrong value') |
| 46 | +assert(integerDetails.value === 42, 'integer rule evaluation returned the wrong value') |
| 47 | +assert(restored.rules?.response.flags['browser-flag'], 'rules configuration did not survive a round trip') |
| 48 | +assert(sha256Matched, 'SHA-256 condition did not match') |
| 49 | + |
| 50 | +reportSuccess({ |
| 51 | + entrypoint: 'full', |
| 52 | + booleanValue: booleanDetails.value, |
| 53 | + integerValue: integerDetails.value, |
| 54 | + sha256Matched, |
10 | 55 | }) |
11 | | - |
12 | | -// Set the provider |
13 | | -OpenFeature.setProvider(provider) |
14 | | - |
15 | | -// Get a client |
16 | | -const client = OpenFeature.getClient() |
17 | | - |
18 | | -// Evaluate a flag using getBooleanDetails to get full evaluation details |
19 | | -const details = client.getBooleanDetails('test-flag', false) |
20 | | - |
21 | | -console.log('✓ Successfully imported and initialized @datadog/openfeature-browser') |
22 | | -console.log('Flag evaluation details:', details) |
23 | | - |
24 | | -// Format the details for display |
25 | | -const detailsJson = JSON.stringify(details, null, 2) |
26 | | - |
27 | | -// Update the DOM to show success |
28 | | -document.querySelector<HTMLDivElement>('#app')!.innerHTML = ` |
29 | | - <div> |
30 | | - <h1>DataDog OpenFeature Browser Test App</h1> |
31 | | - <p class="success">✓ Successfully imported and initialized @datadog/openfeature-browser</p> |
32 | | -
|
33 | | - <div class="details"> |
34 | | - <h2>Flag Evaluation Details</h2> |
35 | | - <p><strong>Flag Key:</strong> ${details.flagKey}</p> |
36 | | - <p><strong>Value:</strong> ${details.value}</p> |
37 | | - <p><strong>Reason:</strong> ${details.reason}</p> |
38 | | - <p><strong>Variant:</strong> ${details.variant || 'N/A'}</p> |
39 | | - <p><strong>Error Code:</strong> ${details.errorCode || 'N/A'}</p> |
40 | | -
|
41 | | - <h3>Full Details (JSON):</h3> |
42 | | - <pre>${detailsJson}</pre> |
43 | | - </div> |
44 | | - </div> |
45 | | -` |
|
0 commit comments