|
| 1 | +import { FallbackTreatmentsCalculator } from '../'; |
| 2 | +import type { FallbackTreatmentConfiguration } from '../../../../types/splitio'; |
| 3 | +import { loggerMock } from '../../../logger/__tests__/sdkLogger.mock'; |
| 4 | +import { CONTROL } from '../../../utils/constants'; |
| 5 | + |
| 6 | +describe('FallbackTreatmentsCalculator' , () => { |
| 7 | + const longName = 'a'.repeat(101); |
| 8 | + |
| 9 | + test('logs an error if flag name is invalid - by Flag', () => { |
| 10 | + let config: FallbackTreatmentConfiguration = { |
| 11 | + byFlag: { |
| 12 | + 'feature A': { treatment: 'TREATMENT_A', config: '{ value: 1 }' }, |
| 13 | + }, |
| 14 | + }; |
| 15 | + new FallbackTreatmentsCalculator(loggerMock, config); |
| 16 | + expect(loggerMock.error.mock.calls[0][0]).toBe( |
| 17 | + 'Fallback treatments - Discarded flag \'feature A\': Invalid flag name (max 100 chars, no spaces)' |
| 18 | + ); |
| 19 | + config = { |
| 20 | + byFlag: { |
| 21 | + [longName]: { treatment: 'TREATMENT_A', config: '{ value: 1 }' }, |
| 22 | + }, |
| 23 | + }; |
| 24 | + new FallbackTreatmentsCalculator(loggerMock, config); |
| 25 | + expect(loggerMock.error.mock.calls[1][0]).toBe( |
| 26 | + `Fallback treatments - Discarded flag '${longName}': Invalid flag name (max 100 chars, no spaces)` |
| 27 | + ); |
| 28 | + |
| 29 | + config = { |
| 30 | + byFlag: { |
| 31 | + 'featureB': { treatment: longName, config: '{ value: 1 }' }, |
| 32 | + }, |
| 33 | + }; |
| 34 | + new FallbackTreatmentsCalculator(loggerMock, config); |
| 35 | + expect(loggerMock.error.mock.calls[2][0]).toBe( |
| 36 | + 'Fallback treatments - Discarded treatment for flag \'featureB\': Invalid treatment (max 100 chars and must match pattern)' |
| 37 | + ); |
| 38 | + |
| 39 | + config = { |
| 40 | + byFlag: { |
| 41 | + // @ts-ignore |
| 42 | + 'featureC': { config: '{ global: true }' }, |
| 43 | + }, |
| 44 | + }; |
| 45 | + new FallbackTreatmentsCalculator(loggerMock, config); |
| 46 | + expect(loggerMock.error.mock.calls[3][0]).toBe( |
| 47 | + 'Fallback treatments - Discarded treatment for flag \'featureC\': Invalid treatment (max 100 chars and must match pattern)' |
| 48 | + ); |
| 49 | + |
| 50 | + config = { |
| 51 | + byFlag: { |
| 52 | + // @ts-ignore |
| 53 | + 'featureC': { treatment: 'invalid treatment!', config: '{ global: true }' }, |
| 54 | + }, |
| 55 | + }; |
| 56 | + new FallbackTreatmentsCalculator(loggerMock, config); |
| 57 | + expect(loggerMock.error.mock.calls[4][0]).toBe( |
| 58 | + 'Fallback treatments - Discarded treatment for flag \'featureC\': Invalid treatment (max 100 chars and must match pattern)' |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + test('logs an error if flag name is invalid - global', () => { |
| 63 | + let config: FallbackTreatmentConfiguration = { |
| 64 | + global: { treatment: longName, config: '{ value: 1 }' }, |
| 65 | + }; |
| 66 | + new FallbackTreatmentsCalculator(loggerMock, config); |
| 67 | + expect(loggerMock.error.mock.calls[2][0]).toBe( |
| 68 | + 'Fallback treatments - Discarded treatment for flag \'featureB\': Invalid treatment (max 100 chars and must match pattern)' |
| 69 | + ); |
| 70 | + |
| 71 | + config = { |
| 72 | + // @ts-ignore |
| 73 | + global: { config: '{ global: true }' }, |
| 74 | + }; |
| 75 | + new FallbackTreatmentsCalculator(loggerMock, config); |
| 76 | + expect(loggerMock.error.mock.calls[3][0]).toBe( |
| 77 | + 'Fallback treatments - Discarded treatment for flag \'featureC\': Invalid treatment (max 100 chars and must match pattern)' |
| 78 | + ); |
| 79 | + |
| 80 | + config = { |
| 81 | + // @ts-ignore |
| 82 | + global: { treatment: 'invalid treatment!', config: '{ global: true }' }, |
| 83 | + }; |
| 84 | + new FallbackTreatmentsCalculator(loggerMock, config); |
| 85 | + expect(loggerMock.error.mock.calls[4][0]).toBe( |
| 86 | + 'Fallback treatments - Discarded treatment for flag \'featureC\': Invalid treatment (max 100 chars and must match pattern)' |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + test('returns specific fallback if flag exists', () => { |
| 91 | + const config: FallbackTreatmentConfiguration = { |
| 92 | + byFlag: { |
| 93 | + 'featureA': { treatment: 'TREATMENT_A', config: '{ value: 1 }' }, |
| 94 | + }, |
| 95 | + }; |
| 96 | + const calculator = new FallbackTreatmentsCalculator(loggerMock, config); |
| 97 | + const result = calculator.resolve('featureA', 'label by flag'); |
| 98 | + |
| 99 | + expect(result).toEqual({ |
| 100 | + treatment: 'TREATMENT_A', |
| 101 | + config: '{ value: 1 }', |
| 102 | + label: 'fallback - label by flag', |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + test('returns global fallback if flag is missing and global exists', () => { |
| 107 | + const config: FallbackTreatmentConfiguration = { |
| 108 | + byFlag: {}, |
| 109 | + global: { treatment: 'GLOBAL_TREATMENT', config: '{ global: true }' }, |
| 110 | + }; |
| 111 | + const calculator = new FallbackTreatmentsCalculator(loggerMock, config); |
| 112 | + const result = calculator.resolve('missingFlag', 'label by global'); |
| 113 | + |
| 114 | + expect(result).toEqual({ |
| 115 | + treatment: 'GLOBAL_TREATMENT', |
| 116 | + config: '{ global: true }', |
| 117 | + label: 'fallback - label by global', |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + test('returns control fallback if flag and global are missing', () => { |
| 122 | + const config: FallbackTreatmentConfiguration = { |
| 123 | + byFlag: {}, |
| 124 | + }; |
| 125 | + const calculator = new FallbackTreatmentsCalculator(loggerMock, config); |
| 126 | + const result = calculator.resolve('missingFlag', 'label by noFallback'); |
| 127 | + |
| 128 | + expect(result).toEqual({ |
| 129 | + treatment: CONTROL, |
| 130 | + config: null, |
| 131 | + label: 'label by noFallback', |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments