|
| 1 | +import { isValidConfigName, isValidConfig, sanitizeFallbacks } from '../fallbackSanitizer'; |
| 2 | +import SplitIO from '../../../../types/splitio'; |
| 3 | +import { loggerMock } from '../../../logger/__tests__/sdkLogger.mock'; |
| 4 | + |
| 5 | +describe('FallbackConfigsSanitizer', () => { |
| 6 | + const validConfig: SplitIO.Config = { variant: 'on', value: { color: 'blue' } }; |
| 7 | + const invalidVariantConfig: SplitIO.Config = { variant: ' ', value: { color: 'blue' } }; |
| 8 | + const invalidValueConfig = { variant: 'on', value: 'not_an_object' } as unknown as SplitIO.Config; |
| 9 | + const fallbackMock = { |
| 10 | + global: undefined, |
| 11 | + byName: {} |
| 12 | + }; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + loggerMock.mockClear(); |
| 16 | + }); |
| 17 | + |
| 18 | + describe('isValidConfigName', () => { |
| 19 | + test('returns true for a valid config name', () => { |
| 20 | + expect(isValidConfigName('my_config')).toBe(true); |
| 21 | + }); |
| 22 | + |
| 23 | + test('returns false for a name longer than 100 chars', () => { |
| 24 | + const longName = 'a'.repeat(101); |
| 25 | + expect(isValidConfigName(longName)).toBe(false); |
| 26 | + }); |
| 27 | + |
| 28 | + test('returns false if the name contains spaces', () => { |
| 29 | + expect(isValidConfigName('invalid config')).toBe(false); |
| 30 | + }); |
| 31 | + |
| 32 | + test('returns false if the name is not a string', () => { |
| 33 | + // @ts-ignore |
| 34 | + expect(isValidConfigName(true)).toBe(false); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('isValidConfig', () => { |
| 39 | + test('returns true for a valid config', () => { |
| 40 | + expect(isValidConfig(validConfig)).toBe(true); |
| 41 | + }); |
| 42 | + |
| 43 | + test('returns false for null or undefined', () => { |
| 44 | + expect(isValidConfig()).toBe(false); |
| 45 | + expect(isValidConfig(undefined)).toBe(false); |
| 46 | + }); |
| 47 | + |
| 48 | + test('returns false for a variant longer than 100 chars', () => { |
| 49 | + const long: SplitIO.Config = { variant: 'a'.repeat(101), value: {} }; |
| 50 | + expect(isValidConfig(long)).toBe(false); |
| 51 | + }); |
| 52 | + |
| 53 | + test('returns false if variant does not match regex pattern', () => { |
| 54 | + const invalid: SplitIO.Config = { variant: 'invalid variant!', value: {} }; |
| 55 | + expect(isValidConfig(invalid)).toBe(false); |
| 56 | + }); |
| 57 | + |
| 58 | + test('returns false if value is not an object', () => { |
| 59 | + expect(isValidConfig(invalidValueConfig)).toBe(false); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('sanitizeGlobal', () => { |
| 64 | + test('returns the config if valid', () => { |
| 65 | + expect(sanitizeFallbacks(loggerMock, { ...fallbackMock, global: validConfig })).toEqual({ ...fallbackMock, global: validConfig }); |
| 66 | + expect(loggerMock.error).not.toHaveBeenCalled(); |
| 67 | + }); |
| 68 | + |
| 69 | + test('returns undefined and logs error if variant is invalid', () => { |
| 70 | + const result = sanitizeFallbacks(loggerMock, { ...fallbackMock, global: invalidVariantConfig }); |
| 71 | + expect(result).toEqual(fallbackMock); |
| 72 | + expect(loggerMock.error).toHaveBeenCalledWith( |
| 73 | + expect.stringContaining('Fallback configs - Discarded fallback') |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + test('returns undefined and logs error if value is invalid', () => { |
| 78 | + const result = sanitizeFallbacks(loggerMock, { ...fallbackMock, global: invalidValueConfig }); |
| 79 | + expect(result).toEqual(fallbackMock); |
| 80 | + expect(loggerMock.error).toHaveBeenCalledWith( |
| 81 | + expect.stringContaining('Fallback configs - Discarded fallback') |
| 82 | + ); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('sanitizeByName', () => { |
| 87 | + test('returns a sanitized map with valid entries only', () => { |
| 88 | + const input = { |
| 89 | + valid_config: validConfig, |
| 90 | + 'invalid config': validConfig, |
| 91 | + bad_variant: invalidVariantConfig, |
| 92 | + }; |
| 93 | + |
| 94 | + const result = sanitizeFallbacks(loggerMock, { ...fallbackMock, byName: input }); |
| 95 | + |
| 96 | + expect(result).toEqual({ ...fallbackMock, byName: { valid_config: validConfig } }); |
| 97 | + expect(loggerMock.error).toHaveBeenCalledTimes(2); // invalid config name + bad_variant |
| 98 | + }); |
| 99 | + |
| 100 | + test('returns empty object if all invalid', () => { |
| 101 | + const input = { |
| 102 | + 'invalid config': invalidVariantConfig, |
| 103 | + }; |
| 104 | + |
| 105 | + const result = sanitizeFallbacks(loggerMock, { ...fallbackMock, byName: input }); |
| 106 | + expect(result).toEqual(fallbackMock); |
| 107 | + expect(loggerMock.error).toHaveBeenCalled(); |
| 108 | + }); |
| 109 | + |
| 110 | + test('returns same object if all valid', () => { |
| 111 | + const input = { |
| 112 | + ...fallbackMock, |
| 113 | + byName: { |
| 114 | + config_one: validConfig, |
| 115 | + config_two: { variant: 'valid_2', value: { key: 'val' } }, |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + const result = sanitizeFallbacks(loggerMock, input); |
| 120 | + expect(result).toEqual(input); |
| 121 | + expect(loggerMock.error).not.toHaveBeenCalled(); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + describe('sanitizeFallbacks', () => { |
| 126 | + test('returns undefined and logs error if fallbacks is not an object', () => { // @ts-expect-error |
| 127 | + const result = sanitizeFallbacks(loggerMock, 'invalid_fallbacks'); |
| 128 | + expect(result).toBeUndefined(); |
| 129 | + expect(loggerMock.error).toHaveBeenCalledWith( |
| 130 | + 'Fallback configs - Discarded configuration: it must be an object with optional `global` and `byName` properties' |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + test('returns undefined and logs error if fallbacks is not an object', () => { // @ts-expect-error |
| 135 | + const result = sanitizeFallbacks(loggerMock, true); |
| 136 | + expect(result).toBeUndefined(); |
| 137 | + expect(loggerMock.error).toHaveBeenCalledWith( |
| 138 | + 'Fallback configs - Discarded configuration: it must be an object with optional `global` and `byName` properties' |
| 139 | + ); |
| 140 | + }); |
| 141 | + |
| 142 | + test('sanitizes both global and byName fallbacks for empty object', () => { // @ts-expect-error |
| 143 | + const result = sanitizeFallbacks(loggerMock, { global: {} }); |
| 144 | + expect(result).toEqual({ global: undefined, byName: {} }); |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
0 commit comments