|
| 1 | +import { matcherTypes } from '../matcherTypes'; |
| 2 | +import { matcherFactory } from '..'; |
| 3 | +import { evaluateFeature } from '../../index'; |
| 4 | +import { IMatcherDto } from '../../types'; |
| 5 | +import { loggerMock } from '../../../logger/__tests__/sdkLogger.mock'; |
| 6 | +import { IRBSegment, ISplit } from '../../../dtos/types'; |
| 7 | +import { IStorageAsync, IStorageSync } from '../../../storages/types'; |
| 8 | +import { thenable } from '../../../utils/promise/thenable'; |
| 9 | +import { ALWAYS_ON_SPLIT } from '../../../storages/__tests__/testUtils'; |
| 10 | + |
| 11 | +const STORED_SPLITS: Record<string, ISplit> = { |
| 12 | + 'always-on': ALWAYS_ON_SPLIT |
| 13 | +}; |
| 14 | + |
| 15 | +const STORED_SEGMENTS: Record<string, Set<string>> = { |
| 16 | + 'segment_test': new Set(['[email protected]']), |
| 17 | + 'regular_segment': new Set(['[email protected]']) |
| 18 | +}; |
| 19 | + |
| 20 | +const STORED_RBSEGMENTS: Record<string, IRBSegment> = { |
| 21 | + 'mauro_rule_based_segment': { |
| 22 | + changeNumber: 5, |
| 23 | + name: 'mauro_rule_based_segment', |
| 24 | + status: 'ACTIVE', |
| 25 | + excluded: { |
| 26 | + |
| 27 | + segments: ['segment_test'] |
| 28 | + }, |
| 29 | + conditions: [ |
| 30 | + { |
| 31 | + matcherGroup: { |
| 32 | + combiner: 'AND', |
| 33 | + matchers: [ |
| 34 | + { |
| 35 | + keySelector: { |
| 36 | + trafficType: 'user', |
| 37 | + attribute: 'location', |
| 38 | + }, |
| 39 | + matcherType: 'WHITELIST', |
| 40 | + negate: false, |
| 41 | + whitelistMatcherData: { |
| 42 | + whitelist: [ |
| 43 | + 'mdp', |
| 44 | + 'tandil', |
| 45 | + 'bsas' |
| 46 | + ] |
| 47 | + } |
| 48 | + }, |
| 49 | + { |
| 50 | + keySelector: { |
| 51 | + trafficType: 'user', |
| 52 | + attribute: null |
| 53 | + }, |
| 54 | + matcherType: 'ENDS_WITH', |
| 55 | + negate: false, |
| 56 | + whitelistMatcherData: { |
| 57 | + whitelist: [ |
| 58 | + '@split.io' |
| 59 | + ] |
| 60 | + } |
| 61 | + } |
| 62 | + ] |
| 63 | + } |
| 64 | + }, |
| 65 | + { |
| 66 | + matcherGroup: { |
| 67 | + combiner: 'AND', |
| 68 | + matchers: [ |
| 69 | + { |
| 70 | + keySelector: { |
| 71 | + trafficType: 'user', |
| 72 | + attribute: null |
| 73 | + }, |
| 74 | + matcherType: 'IN_SEGMENT', |
| 75 | + negate: false, |
| 76 | + userDefinedSegmentMatcherData: { |
| 77 | + segmentName: 'regular_segment' |
| 78 | + } |
| 79 | + } |
| 80 | + ] |
| 81 | + } |
| 82 | + } |
| 83 | + ] |
| 84 | + }, |
| 85 | + 'depend_on_always_on': { |
| 86 | + name: 'depend_on_always_on', |
| 87 | + changeNumber: 123, |
| 88 | + status: 'ACTIVE', |
| 89 | + excluded: { |
| 90 | + keys: [], |
| 91 | + segments: [] |
| 92 | + }, |
| 93 | + conditions: [{ |
| 94 | + matcherGroup: { |
| 95 | + combiner: 'AND', |
| 96 | + matchers: [{ |
| 97 | + matcherType: 'IN_SPLIT_TREATMENT', |
| 98 | + keySelector: { |
| 99 | + trafficType: 'user', |
| 100 | + attribute: null |
| 101 | + }, |
| 102 | + negate: false, |
| 103 | + dependencyMatcherData: { |
| 104 | + split: 'always-on', |
| 105 | + treatments: [ |
| 106 | + 'on', |
| 107 | + ] |
| 108 | + } |
| 109 | + }] |
| 110 | + } |
| 111 | + }] |
| 112 | + }, |
| 113 | + 'depend_on_mauro_rule_based_segment': { |
| 114 | + name: 'depend_on_mauro_rule_based_segment', |
| 115 | + changeNumber: 123, |
| 116 | + status: 'ACTIVE', |
| 117 | + excluded: { |
| 118 | + keys: [], |
| 119 | + segments: [] |
| 120 | + }, |
| 121 | + conditions: [{ |
| 122 | + matcherGroup: { |
| 123 | + combiner: 'AND', |
| 124 | + matchers: [{ |
| 125 | + matcherType: 'IN_RULE_BASED_SEGMENT', |
| 126 | + keySelector: { |
| 127 | + trafficType: 'user', |
| 128 | + attribute: null |
| 129 | + }, |
| 130 | + negate: false, |
| 131 | + userDefinedSegmentMatcherData: { |
| 132 | + segmentName: 'mauro_rule_based_segment' |
| 133 | + } |
| 134 | + }] |
| 135 | + } |
| 136 | + }] |
| 137 | + }, |
| 138 | +}; |
| 139 | + |
| 140 | +const mockStorageSync = { |
| 141 | + isSync: true, |
| 142 | + splits: { |
| 143 | + getSplit(name: string) { |
| 144 | + return STORED_SPLITS[name]; |
| 145 | + } |
| 146 | + }, |
| 147 | + segments: { |
| 148 | + isInSegment(segmentName: string, matchingKey: string) { |
| 149 | + return STORED_SEGMENTS[segmentName] ? STORED_SEGMENTS[segmentName].has(matchingKey) : false; |
| 150 | + } |
| 151 | + }, |
| 152 | + rbSegments: { |
| 153 | + get(rbsegmentName: string) { |
| 154 | + return STORED_RBSEGMENTS[rbsegmentName]; |
| 155 | + } |
| 156 | + } |
| 157 | +} as unknown as IStorageSync; |
| 158 | + |
| 159 | +const mockStorageAsync = { |
| 160 | + isSync: false, |
| 161 | + splits: { |
| 162 | + getSplit(name: string) { |
| 163 | + return Promise.resolve(STORED_SPLITS[name]); |
| 164 | + } |
| 165 | + }, |
| 166 | + segments: { |
| 167 | + isInSegment(segmentName: string, matchingKey: string) { |
| 168 | + return Promise.resolve(STORED_SEGMENTS[segmentName] ? STORED_SEGMENTS[segmentName].has(matchingKey) : false); |
| 169 | + } |
| 170 | + }, |
| 171 | + rbSegments: { |
| 172 | + get(rbsegmentName: string) { |
| 173 | + return Promise.resolve(STORED_RBSEGMENTS[rbsegmentName]); |
| 174 | + } |
| 175 | + } |
| 176 | +} as unknown as IStorageAsync; |
| 177 | + |
| 178 | +describe.each([ |
| 179 | + { mockStorage: mockStorageSync, isAsync: false }, |
| 180 | + { mockStorage: mockStorageAsync, isAsync: true } |
| 181 | +])('MATCHER IN_RULE_BASED_SEGMENT', ({ mockStorage, isAsync }) => { |
| 182 | + test('should support excluded keys, excluded segments, and multiple conditions', async () => { |
| 183 | + const matcher = matcherFactory(loggerMock, { |
| 184 | + type: matcherTypes.IN_RULE_BASED_SEGMENT, |
| 185 | + value: 'mauro_rule_based_segment' |
| 186 | + } as IMatcherDto, mockStorage)!; |
| 187 | + |
| 188 | + const dependentMatcher = matcherFactory(loggerMock, { |
| 189 | + type: matcherTypes.IN_RULE_BASED_SEGMENT, |
| 190 | + value: 'depend_on_mauro_rule_based_segment' |
| 191 | + } as IMatcherDto, mockStorage)!; |
| 192 | + |
| 193 | + [matcher, dependentMatcher].forEach(async matcher => { |
| 194 | + |
| 195 | + // should return false if the provided key is excluded (even if some condition is met) |
| 196 | + let match = matcher({ key: '[email protected]', attributes: { location: 'mdp' } }, evaluateFeature); |
| 197 | + expect(thenable(match)).toBe(isAsync); |
| 198 | + expect(await match).toBe(false); |
| 199 | + |
| 200 | + // should return false if the provided key is in some excluded segment (even if some condition is met) |
| 201 | + match = matcher({ key: '[email protected]', attributes: { location: 'tandil' } }, evaluateFeature); |
| 202 | + expect(thenable(match)).toBe(isAsync); |
| 203 | + expect(await match).toBe(false); |
| 204 | + |
| 205 | + // should return false if doesn't match any condition |
| 206 | + match = matcher({ key: '[email protected]' }, evaluateFeature); |
| 207 | + expect(thenable(match)).toBe(isAsync); |
| 208 | + expect(await match).toBe(false); |
| 209 | + match = matcher({ key: { matchingKey: '[email protected]', bucketingKey: '123' }, attributes: { location: 'italy' } }, evaluateFeature); |
| 210 | + expect(thenable(match)).toBe(isAsync); |
| 211 | + expect(await match).toBe(false); |
| 212 | + |
| 213 | + // should return true if match the first condition: location attribute in whitelist and key ends with '@split.io' |
| 214 | + match = matcher({ key: '[email protected]', attributes: { location: 'tandil' } }, evaluateFeature); |
| 215 | + expect(thenable(match)).toBe(isAsync); |
| 216 | + expect(await match).toBe(true); |
| 217 | + |
| 218 | + // should return true if match the second condition: key in regular_segment |
| 219 | + match = matcher({ key: { matchingKey: '[email protected]', bucketingKey: '123' }, attributes: { location: 'mdp' } }, evaluateFeature); |
| 220 | + expect(thenable(match)).toBe(isAsync); |
| 221 | + expect(await match).toBe(true); |
| 222 | + }); |
| 223 | + }); |
| 224 | + |
| 225 | + test('edge cases', async () => { |
| 226 | + const matcherNotExist = matcherFactory(loggerMock, { |
| 227 | + type: matcherTypes.IN_RULE_BASED_SEGMENT, |
| 228 | + value: 'non_existent_segment' |
| 229 | + } as IMatcherDto, mockStorageSync)!; |
| 230 | + |
| 231 | + // should return false if the provided segment does not exist |
| 232 | + expect(await matcherNotExist({ key: 'a-key' }, evaluateFeature)).toBe(false); |
| 233 | + |
| 234 | + const matcherTrueAlwaysOn = matcherFactory(loggerMock, { |
| 235 | + type: matcherTypes.IN_RULE_BASED_SEGMENT, |
| 236 | + value: 'depend_on_always_on' |
| 237 | + } as IMatcherDto, mockStorageSync)!; |
| 238 | + |
| 239 | + // should support feature flag dependency matcher |
| 240 | + expect(await matcherTrueAlwaysOn({ key: 'a-key' }, evaluateFeature)).toBe(true); // Parent split returns one of the expected treatments, so the matcher returns true |
| 241 | + }); |
| 242 | + |
| 243 | +}); |
0 commit comments