|
| 1 | +import { IBanditParametersResponse, IUniversalFlagConfigResponse } from '../http-client'; |
| 2 | +import { FormatEnum } from '../interfaces'; |
| 3 | +import { getMD5Hash } from '../obfuscation'; |
| 4 | + |
| 5 | +import { ConfigurationWireHelper } from './configuration-wire-helper'; |
| 6 | + |
| 7 | +const TEST_BASE_URL = 'https://us-central1-eppo-qa.cloudfunctions.net/serveGitHubRacTestFile'; |
| 8 | +const DUMMY_SDK_KEY = 'dummy-sdk-key'; |
| 9 | + |
| 10 | +// This SDK causes the cloud endpoint below to serve the UFC test file with bandit flags. |
| 11 | +const BANDIT_SDK_KEY = 'this-key-serves-bandits'; |
| 12 | + |
| 13 | +describe('ConfigurationWireHelper', () => { |
| 14 | + describe('getBootstrapConfigurationFromApi', () => { |
| 15 | + it('should fetch obfuscated flags with android SDK', async () => { |
| 16 | + const helper = ConfigurationWireHelper.build(DUMMY_SDK_KEY, { |
| 17 | + sdkName: 'android', |
| 18 | + sdkVersion: '4.0.0', |
| 19 | + baseUrl: TEST_BASE_URL, |
| 20 | + }); |
| 21 | + |
| 22 | + const wirePacket = await helper.fetchBootstrapConfiguration(); |
| 23 | + |
| 24 | + expect(wirePacket.version).toBe(1); |
| 25 | + expect(wirePacket.config).toBeDefined(); |
| 26 | + |
| 27 | + if (!wirePacket.config) { |
| 28 | + throw new Error('Flag config not present in ConfigurationWire'); |
| 29 | + } |
| 30 | + |
| 31 | + const configResponse = JSON.parse(wirePacket.config.response) as IUniversalFlagConfigResponse; |
| 32 | + expect(configResponse.format).toBe(FormatEnum.CLIENT); |
| 33 | + expect(configResponse.flags).toBeDefined(); |
| 34 | + expect(Object.keys(configResponse.flags).length).toBeGreaterThan(1); |
| 35 | + expect(Object.keys(configResponse.flags)).toHaveLength(19); |
| 36 | + |
| 37 | + const testFlagKey = getMD5Hash('numeric_flag'); |
| 38 | + expect(Object.keys(configResponse.flags)).toContain(testFlagKey); |
| 39 | + |
| 40 | + // No bandits. |
| 41 | + expect(configResponse.banditReferences).toBeUndefined(); |
| 42 | + expect(wirePacket.bandits).toBeUndefined(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should fetch flags and bandits for node-server SDK', async () => { |
| 46 | + const helper = ConfigurationWireHelper.build(BANDIT_SDK_KEY, { |
| 47 | + sdkName: 'node-server', |
| 48 | + sdkVersion: '4.0.0', |
| 49 | + baseUrl: TEST_BASE_URL, |
| 50 | + }); |
| 51 | + |
| 52 | + const wirePacket = await helper.fetchBootstrapConfiguration(); |
| 53 | + |
| 54 | + expect(wirePacket.version).toBe(1); |
| 55 | + expect(wirePacket.config).toBeDefined(); |
| 56 | + |
| 57 | + if (!wirePacket.config) { |
| 58 | + throw new Error('Flag config not present in ConfigurationWire'); |
| 59 | + } |
| 60 | + |
| 61 | + const configResponse = JSON.parse(wirePacket.config.response) as IUniversalFlagConfigResponse; |
| 62 | + expect(configResponse.format).toBe(FormatEnum.SERVER); |
| 63 | + expect(configResponse.flags).toBeDefined(); |
| 64 | + expect(configResponse.banditReferences).toBeDefined(); |
| 65 | + expect(Object.keys(configResponse.flags)).toContain('banner_bandit_flag'); |
| 66 | + expect(Object.keys(configResponse.flags)).toContain('car_bandit_flag'); |
| 67 | + |
| 68 | + expect(wirePacket.bandits).toBeDefined(); |
| 69 | + const banditResponse = JSON.parse( |
| 70 | + wirePacket.bandits?.response ?? '', |
| 71 | + ) as IBanditParametersResponse; |
| 72 | + expect(Object.keys(banditResponse.bandits).length).toBeGreaterThan(1); |
| 73 | + expect(Object.keys(banditResponse.bandits)).toContain('banner_bandit'); |
| 74 | + expect(Object.keys(banditResponse.bandits)).toContain('car_bandit'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should include fetchedAt timestamps', async () => { |
| 78 | + const helper = ConfigurationWireHelper.build(BANDIT_SDK_KEY, { |
| 79 | + sdkName: 'android', |
| 80 | + sdkVersion: '4.0.0', |
| 81 | + baseUrl: TEST_BASE_URL, |
| 82 | + }); |
| 83 | + |
| 84 | + const wirePacket = await helper.fetchBootstrapConfiguration(); |
| 85 | + |
| 86 | + if (!wirePacket.config) { |
| 87 | + throw new Error('Flag config not present in ConfigurationWire'); |
| 88 | + } |
| 89 | + if (!wirePacket.bandits) { |
| 90 | + throw new Error('Bandit config not present in ConfigurationWire'); |
| 91 | + } |
| 92 | + |
| 93 | + expect(wirePacket.config.fetchedAt).toBeDefined(); |
| 94 | + expect(Date.parse(wirePacket.config.fetchedAt ?? '')).not.toBeNaN(); |
| 95 | + expect(Date.parse(wirePacket.bandits.fetchedAt ?? '')).not.toBeNaN(); |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments