|
| 1 | +import { |
| 2 | + MOCK_BANDIT_MODELS_RESPONSE_FILE, |
| 3 | + MOCK_FLAGS_WITH_BANDITS_RESPONSE_FILE, |
| 4 | + readMockUFCResponse, |
| 5 | +} from '../test/testHelpers'; |
| 6 | + |
| 7 | +import { ConfigurationWireV1, deflateResponse, inflateResponse } from './configuration-wire-types'; |
| 8 | +import { IUniversalFlagConfigResponse, IBanditParametersResponse } from './http-client'; |
| 9 | +import { FormatEnum } from './interfaces'; |
| 10 | + |
| 11 | +describe('Response String Type Safety', () => { |
| 12 | + const mockFlagConfig: IUniversalFlagConfigResponse = readMockUFCResponse( |
| 13 | + MOCK_FLAGS_WITH_BANDITS_RESPONSE_FILE, |
| 14 | + ) as IUniversalFlagConfigResponse; |
| 15 | + const mockBanditConfig: IBanditParametersResponse = readMockUFCResponse( |
| 16 | + MOCK_BANDIT_MODELS_RESPONSE_FILE, |
| 17 | + ) as IBanditParametersResponse; |
| 18 | + |
| 19 | + describe('deflateResponse and inflateResponse', () => { |
| 20 | + it('should correctly serialize and deserialize flag config', () => { |
| 21 | + const serialized = deflateResponse(mockFlagConfig); |
| 22 | + const deserialized = inflateResponse(serialized); |
| 23 | + |
| 24 | + expect(deserialized).toEqual(mockFlagConfig); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should correctly serialize and deserialize bandit config', () => { |
| 28 | + const serialized = deflateResponse(mockBanditConfig); |
| 29 | + const deserialized = inflateResponse(serialized); |
| 30 | + |
| 31 | + expect(deserialized).toEqual(mockBanditConfig); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should maintain type information through serialization', () => { |
| 35 | + const serialized = deflateResponse(mockFlagConfig); |
| 36 | + const deserialized = inflateResponse(serialized); |
| 37 | + |
| 38 | + // TypeScript compilation check: these should work |
| 39 | + expect(deserialized.format).toBe(FormatEnum.SERVER); |
| 40 | + expect(deserialized.environment).toStrictEqual({ name: 'Test' }); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('ConfigurationWireV1', () => { |
| 45 | + it('should create configuration with flag config', () => { |
| 46 | + const wirePacket = ConfigurationWireV1.fromResponses(mockFlagConfig); |
| 47 | + |
| 48 | + expect(wirePacket.version).toBe(1); |
| 49 | + expect(wirePacket.config).toBeDefined(); |
| 50 | + expect(wirePacket.bandits).toBeUndefined(); |
| 51 | + |
| 52 | + // Verify we can deserialize the response |
| 53 | + expect(wirePacket.config).toBeTruthy(); |
| 54 | + if (!wirePacket.config) { |
| 55 | + fail('Flag config not present in ConfigurationWire'); |
| 56 | + } |
| 57 | + const deserializedConfig = inflateResponse(wirePacket.config.response); |
| 58 | + expect(deserializedConfig).toEqual(mockFlagConfig); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should create configuration with both flag and bandit configs', () => { |
| 62 | + const wirePacket = ConfigurationWireV1.fromResponses( |
| 63 | + mockFlagConfig, |
| 64 | + mockBanditConfig, |
| 65 | + 'flag-etag', |
| 66 | + 'bandit-etag', |
| 67 | + ); |
| 68 | + |
| 69 | + if (!wirePacket.config) { |
| 70 | + fail('Flag config not present in ConfigurationWire'); |
| 71 | + } |
| 72 | + if (!wirePacket.bandits) { |
| 73 | + fail('Bandit Model Parameters not present in ConfigurationWire'); |
| 74 | + } |
| 75 | + |
| 76 | + expect(wirePacket.version).toBe(1); |
| 77 | + expect(wirePacket.config).toBeDefined(); |
| 78 | + expect(wirePacket.bandits).toBeDefined(); |
| 79 | + expect(wirePacket.config.etag).toBe('flag-etag'); |
| 80 | + expect(wirePacket.bandits.etag).toBe('bandit-etag'); |
| 81 | + |
| 82 | + // Verify we can deserialize both responses |
| 83 | + const deserializedConfig = inflateResponse(wirePacket.config.response); |
| 84 | + const deserializedBandits = inflateResponse(wirePacket.bandits.response); |
| 85 | + |
| 86 | + expect(deserializedConfig).toEqual(mockFlagConfig); |
| 87 | + expect(deserializedBandits).toEqual(mockBanditConfig); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should create empty configuration', () => { |
| 91 | + const config = ConfigurationWireV1.empty(); |
| 92 | + |
| 93 | + expect(config.version).toBe(1); |
| 94 | + expect(config.config).toBeUndefined(); |
| 95 | + expect(config.bandits).toBeUndefined(); |
| 96 | + expect(config.precomputed).toBeUndefined(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should include fetchedAt timestamps', () => { |
| 100 | + const wirePacket = ConfigurationWireV1.fromResponses(mockFlagConfig, mockBanditConfig); |
| 101 | + |
| 102 | + if (!wirePacket.config) { |
| 103 | + fail('Flag config not present in ConfigurationWire'); |
| 104 | + } |
| 105 | + if (!wirePacket.bandits) { |
| 106 | + fail('Bandit Model Parameters not present in ConfigurationWire'); |
| 107 | + } |
| 108 | + expect(wirePacket.config.fetchedAt).toBeDefined(); |
| 109 | + expect(Date.parse(wirePacket.config.fetchedAt ?? '')).not.toBeNaN(); |
| 110 | + expect(Date.parse(wirePacket.bandits.fetchedAt ?? '')).not.toBeNaN(); |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments