|
| 1 | +import { |
| 2 | + expect, |
| 3 | + before, |
| 4 | + describe, |
| 5 | + deps, |
| 6 | + fsExtra, |
| 7 | + getMainCheckFn, |
| 8 | +} from '../deps'; |
| 9 | +import { constants } from '../../helpers/constants'; |
| 10 | +import * as defaultConfig from '../../../config/features-config.json'; |
| 11 | +import { getRepository, repositories } from '../../helpers/local-db'; |
| 12 | + |
| 13 | +const { server, request } = deps; |
| 14 | + |
| 15 | +// endpoint to test |
| 16 | +const endpoint = () => request(server).post('/features/sync'); |
| 17 | + |
| 18 | +const mainCheckFn = getMainCheckFn(endpoint); |
| 19 | + |
| 20 | +let featureConfigRepository; |
| 21 | +let featureRepository; |
| 22 | +describe('POST /features/sync', () => { |
| 23 | + before(async () => { |
| 24 | + featureConfigRepository = await getRepository(repositories.FEATURES_CONFIG); |
| 25 | + featureRepository = await getRepository(repositories.FEATURE); |
| 26 | + }); |
| 27 | + |
| 28 | + [ |
| 29 | + { |
| 30 | + name: 'Should sync with default config when db:null and remote:fail', |
| 31 | + before: async () => { |
| 32 | + // remove remote config so BE will get an error during fetching |
| 33 | + await fsExtra.remove(constants.TEST_FEATURE_FLAG_REMOTE_CONFIG_PATH).catch(console.error); |
| 34 | + // remove all configs |
| 35 | + await featureConfigRepository.delete({}); |
| 36 | + |
| 37 | + const [config] = await featureConfigRepository.find(); |
| 38 | + expect(config).to.eq(undefined); |
| 39 | + }, |
| 40 | + statusCode: 200, |
| 41 | + checkFn: async () => { |
| 42 | + const [config, empty] = await featureConfigRepository.find(); |
| 43 | + |
| 44 | + expect(empty).to.eq(undefined); |
| 45 | + expect(config.controlNumber).to.gte(0).lt(100); |
| 46 | + expect(config.data).to.eq(JSON.stringify(defaultConfig)); |
| 47 | + } |
| 48 | + }, |
| 49 | + { |
| 50 | + name: 'Should sync with default config when db:version < default.version and remote:fail', |
| 51 | + before: async () => { |
| 52 | + await fsExtra.remove(constants.TEST_FEATURE_FLAG_REMOTE_CONFIG_PATH).catch(console.error); |
| 53 | + await featureConfigRepository.update({}, { |
| 54 | + data: JSON.stringify({ |
| 55 | + ...defaultConfig, |
| 56 | + version: defaultConfig.version - 0.1, |
| 57 | + }), |
| 58 | + }); |
| 59 | + |
| 60 | + const [config, empty] = await featureConfigRepository.find(); |
| 61 | + |
| 62 | + expect(empty).to.eq(undefined); |
| 63 | + expect(config.data).to.eq(JSON.stringify({ |
| 64 | + ...defaultConfig, |
| 65 | + version: defaultConfig.version - 0.1, |
| 66 | + })); |
| 67 | + }, |
| 68 | + statusCode: 200, |
| 69 | + checkFn: async () => { |
| 70 | + const [config, empty] = await featureConfigRepository.find(); |
| 71 | + |
| 72 | + expect(empty).to.eq(undefined); |
| 73 | + expect(config.controlNumber).to.gte(0).lt(100); |
| 74 | + expect(config.data).to.eq(JSON.stringify(defaultConfig)); |
| 75 | + } |
| 76 | + }, |
| 77 | + { |
| 78 | + name: 'Should sync with remote config when db:null and remote:version > default.version', |
| 79 | + before: async () => { |
| 80 | + await fsExtra.writeFile(constants.TEST_FEATURE_FLAG_REMOTE_CONFIG_PATH, JSON.stringify({ |
| 81 | + ...defaultConfig, |
| 82 | + version: defaultConfig.version + 3.33, |
| 83 | + })).catch(console.error); |
| 84 | + |
| 85 | + // remove all configs |
| 86 | + await featureConfigRepository.delete({}); |
| 87 | + |
| 88 | + const [config] = await featureConfigRepository.find(); |
| 89 | + |
| 90 | + expect(config).to.eq(undefined); |
| 91 | + }, |
| 92 | + statusCode: 200, |
| 93 | + checkFn: async () => { |
| 94 | + const [config, empty] = await featureConfigRepository.find(); |
| 95 | + |
| 96 | + expect(empty).to.eq(undefined); |
| 97 | + expect(config.controlNumber).to.gte(0).lt(100); |
| 98 | + expect(config.data).to.eq(JSON.stringify({ |
| 99 | + ...defaultConfig, |
| 100 | + version: defaultConfig.version + 3.33, |
| 101 | + })); |
| 102 | + } |
| 103 | + }, |
| 104 | + { |
| 105 | + name: 'Should sync with remote config when db:version < default and remote:version > default', |
| 106 | + before: async () => { |
| 107 | + await fsExtra.writeFile(constants.TEST_FEATURE_FLAG_REMOTE_CONFIG_PATH, JSON.stringify({ |
| 108 | + ...defaultConfig, |
| 109 | + version: defaultConfig.version + 1.11, |
| 110 | + })).catch(console.error); |
| 111 | + // remove all configs |
| 112 | + await featureConfigRepository.update({}, { |
| 113 | + data: JSON.stringify({ |
| 114 | + ...defaultConfig, |
| 115 | + version: defaultConfig.version - 0.1, |
| 116 | + }), |
| 117 | + }); |
| 118 | + |
| 119 | + const [config, empty] = await featureConfigRepository.find(); |
| 120 | + |
| 121 | + expect(empty).to.eq(undefined); |
| 122 | + expect(config.data).to.eq(JSON.stringify({ |
| 123 | + ...defaultConfig, |
| 124 | + version: defaultConfig.version - 0.1, |
| 125 | + })); |
| 126 | + }, |
| 127 | + statusCode: 200, |
| 128 | + checkFn: async () => { |
| 129 | + const [config, empty] = await featureConfigRepository.find(); |
| 130 | + |
| 131 | + expect(empty).to.eq(undefined); |
| 132 | + expect(config.controlNumber).to.gte(0).lt(100); |
| 133 | + expect(config.data).to.eq(JSON.stringify({ |
| 134 | + ...defaultConfig, |
| 135 | + version: defaultConfig.version + 1.11, |
| 136 | + })); |
| 137 | + } |
| 138 | + }, |
| 139 | + ].map(mainCheckFn); |
| 140 | +}); |
0 commit comments