|
| 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://github.com/DataDog). |
| 4 | + * Copyright 2016-Present Datadog, Inc. |
| 5 | + */ |
| 6 | + |
| 7 | +import { |
| 8 | + flaggingCoreRulesEngine, |
| 9 | + getNoopRulesLogger, |
| 10 | + prepareRulesConfiguration, |
| 11 | + toRulesEvaluationContext |
| 12 | +} from '../rules'; |
| 13 | + |
| 14 | +import { |
| 15 | + buildRulesConfiguration, |
| 16 | + createFakeRulesEngine |
| 17 | +} from './__utils__/rulesTestUtils'; |
| 18 | + |
| 19 | +describe('rules configuration', () => { |
| 20 | + it('converts an SDK context to a flat rules context and reserves identifiers', () => { |
| 21 | + expect( |
| 22 | + toRulesEvaluationContext({ |
| 23 | + targetingKey: 'user-1', |
| 24 | + attributes: { |
| 25 | + country: 'US', |
| 26 | + id: 'customer-id', |
| 27 | + targetingKey: 'attribute-key', |
| 28 | + enabled: true |
| 29 | + } |
| 30 | + }) |
| 31 | + ).toEqual({ |
| 32 | + targetingKey: 'user-1', |
| 33 | + country: 'US', |
| 34 | + enabled: true |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('clones and freezes a valid rules configuration', () => { |
| 39 | + const source = buildRulesConfiguration(); |
| 40 | + const prepared = prepareRulesConfiguration(source); |
| 41 | + |
| 42 | + expect(prepared.status).toBe('ready'); |
| 43 | + if (prepared.status !== 'ready') { |
| 44 | + throw new Error(prepared.errorMessage); |
| 45 | + } |
| 46 | + |
| 47 | + source.flags['dynamic-flag'].enabled = false; |
| 48 | + |
| 49 | + expect(prepared.configuration.flags['dynamic-flag'].enabled).toBe(true); |
| 50 | + expect(Object.isFrozen(prepared.configuration)).toBe(true); |
| 51 | + expect( |
| 52 | + Object.isFrozen( |
| 53 | + prepared.configuration.flags['dynamic-flag'].allocations[0] |
| 54 | + ) |
| 55 | + ).toBe(true); |
| 56 | + }); |
| 57 | + |
| 58 | + it('rejects an unsupported operator', () => { |
| 59 | + const source = buildRulesConfiguration(); |
| 60 | + const condition = |
| 61 | + source.flags['dynamic-flag'].allocations[0].rules?.[0] |
| 62 | + .conditions[0]; |
| 63 | + |
| 64 | + if (!condition) { |
| 65 | + throw new Error('The fixture has no condition.'); |
| 66 | + } |
| 67 | + (condition as { operator: string }).operator = 'ONE_OF_SHA256'; |
| 68 | + |
| 69 | + expect(prepareRulesConfiguration(source)).toEqual({ |
| 70 | + status: 'error', |
| 71 | + errorMessage: |
| 72 | + 'The rules configuration uses the unsupported operator "ONE_OF_SHA256".' |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('rejects an invalid regular expression', () => { |
| 77 | + const source = buildRulesConfiguration(); |
| 78 | + const conditions = |
| 79 | + source.flags['dynamic-flag'].allocations[0].rules?.[0].conditions; |
| 80 | + if (!conditions) { |
| 81 | + throw new Error('The fixture has no conditions.'); |
| 82 | + } |
| 83 | + conditions[0] = { |
| 84 | + operator: 'MATCHES', |
| 85 | + attribute: 'country', |
| 86 | + value: '[' |
| 87 | + } as typeof conditions[number]; |
| 88 | + |
| 89 | + expect(prepareRulesConfiguration(source)).toEqual({ |
| 90 | + status: 'error', |
| 91 | + errorMessage: 'A regular expression condition is not valid.' |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('rejects a split that points to an absent variation', () => { |
| 96 | + const source = buildRulesConfiguration(); |
| 97 | + source.flags['dynamic-flag'].allocations[0].splits[0].variationKey = |
| 98 | + 'absent'; |
| 99 | + |
| 100 | + expect(prepareRulesConfiguration(source)).toEqual({ |
| 101 | + status: 'error', |
| 102 | + errorMessage: 'A split has an invalid variation key.' |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('normalizes a real flagging-core evaluation', () => { |
| 107 | + const configuration = buildRulesConfiguration(); |
| 108 | + |
| 109 | + const result = flaggingCoreRulesEngine.evaluate({ |
| 110 | + configuration, |
| 111 | + type: 'boolean', |
| 112 | + flagKey: 'dynamic-flag', |
| 113 | + defaultValue: false, |
| 114 | + context: { |
| 115 | + targetingKey: 'user-1', |
| 116 | + country: 'US' |
| 117 | + }, |
| 118 | + logger: getNoopRulesLogger() |
| 119 | + }); |
| 120 | + |
| 121 | + expect(result).toMatchObject({ |
| 122 | + value: true, |
| 123 | + variant: 'enabled', |
| 124 | + reason: 'TARGETING_MATCH', |
| 125 | + metadata: { |
| 126 | + allocationKey: 'allocation-1', |
| 127 | + variationType: 'boolean', |
| 128 | + doLog: false, |
| 129 | + extraLogging: { experiment: 'checkout' }, |
| 130 | + splitSerialId: 7 |
| 131 | + } |
| 132 | + }); |
| 133 | + expect(result.metadata.evaluationTimestampMs).toEqual( |
| 134 | + expect.any(Number) |
| 135 | + ); |
| 136 | + }); |
| 137 | + |
| 138 | + it('checks own properties before it calls flagging-core', () => { |
| 139 | + const result = flaggingCoreRulesEngine.evaluate({ |
| 140 | + configuration: buildRulesConfiguration(), |
| 141 | + type: 'boolean', |
| 142 | + flagKey: 'toString', |
| 143 | + defaultValue: false, |
| 144 | + context: { targetingKey: 'user-1' }, |
| 145 | + logger: getNoopRulesLogger() |
| 146 | + }); |
| 147 | + |
| 148 | + expect(result).toEqual({ |
| 149 | + value: false, |
| 150 | + reason: 'ERROR', |
| 151 | + errorCode: 'FLAG_NOT_FOUND', |
| 152 | + metadata: {} |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + it('provides a deterministic fake engine for client tests', () => { |
| 157 | + const fake = createFakeRulesEngine({ |
| 158 | + value: true, |
| 159 | + variant: 'fake', |
| 160 | + reason: 'TARGETING_MATCH', |
| 161 | + metadata: {} |
| 162 | + }); |
| 163 | + |
| 164 | + expect( |
| 165 | + fake.evaluate({ |
| 166 | + configuration: buildRulesConfiguration(), |
| 167 | + type: 'boolean', |
| 168 | + flagKey: 'dynamic-flag', |
| 169 | + defaultValue: false, |
| 170 | + context: { targetingKey: 'user-1' }, |
| 171 | + logger: getNoopRulesLogger() |
| 172 | + }) |
| 173 | + ).toMatchObject({ value: true, variant: 'fake' }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments