|
| 1 | +import type { FlagsConfiguration } from './configuration' |
| 2 | +import { configurationFromString, configurationToString } from './wire' |
| 3 | + |
| 4 | +const configuration: FlagsConfiguration = { |
| 5 | + precomputed: { |
| 6 | + response: { |
| 7 | + data: { |
| 8 | + attributes: { |
| 9 | + createdAt: '2026-07-06T23:01:56.822Z', |
| 10 | + flags: { |
| 11 | + 'my-flag': { |
| 12 | + allocationKey: 'alloc-1', |
| 13 | + variationKey: 'true', |
| 14 | + variationType: 'boolean', |
| 15 | + variationValue: true, |
| 16 | + reason: 'STATIC', |
| 17 | + doLog: true, |
| 18 | + extraLogging: {}, |
| 19 | + }, |
| 20 | + }, |
| 21 | + }, |
| 22 | + }, |
| 23 | + }, |
| 24 | + context: { targetingKey: 'user-1', country: 'US' }, |
| 25 | + }, |
| 26 | +} |
| 27 | + |
| 28 | +describe('configuration wire', () => { |
| 29 | + it('round-trips a precomputed configuration', () => { |
| 30 | + const restored = configurationFromString(configurationToString(configuration)) |
| 31 | + |
| 32 | + expect(restored).toEqual(configuration) |
| 33 | + }) |
| 34 | + |
| 35 | + it('keeps flags readable after a round-trip', () => { |
| 36 | + const restored = configurationFromString(configurationToString(configuration)) |
| 37 | + |
| 38 | + expect(restored.precomputed?.response.data.attributes.flags['my-flag'].variationValue).toBe(true) |
| 39 | + }) |
| 40 | + |
| 41 | + it('serializes precomputed.response as a stringified response object, not the whole precomputed', () => { |
| 42 | + const wire = JSON.parse(configurationToString(configuration)) |
| 43 | + |
| 44 | + // `precomputed.response` on the wire must be the response only, so parsing it yields |
| 45 | + // the response object (regression guard: it previously stringified the whole |
| 46 | + // precomputed object, double-nesting the response and losing the flags). |
| 47 | + expect(JSON.parse(wire.precomputed.response)).toEqual(configuration.precomputed?.response) |
| 48 | + }) |
| 49 | + |
| 50 | + it('returns an empty configuration for an unknown version', () => { |
| 51 | + expect(configurationFromString(JSON.stringify({ version: 2 }))).toEqual({}) |
| 52 | + }) |
| 53 | + |
| 54 | + it('returns an empty configuration for malformed input', () => { |
| 55 | + expect(configurationFromString('not json')).toEqual({}) |
| 56 | + }) |
| 57 | +}) |
0 commit comments