|
| 1 | +import type { EvaluationContext, EvaluationDetails, FlagMetadata, FlagValue } from '@openfeature/core' |
| 2 | +import { createExposureEvent } from '../../src/configuration/exposureEvent' |
| 3 | + |
| 4 | +const context: EvaluationContext = { targetingKey: 'user-123', country: 'US' } |
| 5 | + |
| 6 | +function detailsWith(flagMetadata: FlagMetadata): EvaluationDetails<FlagValue> { |
| 7 | + return { |
| 8 | + flagKey: 'checkout-redesign', |
| 9 | + value: 'red', |
| 10 | + variant: 'treatment', |
| 11 | + reason: 'SPLIT', |
| 12 | + flagMetadata, |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +const baseMetadata: FlagMetadata = { doLog: true, allocationKey: 'allocation-123' } |
| 17 | + |
| 18 | +describe('createExposureEvent', () => { |
| 19 | + it('should build the whole event, including serial_id, from the evaluation metadata', () => { |
| 20 | + const event = createExposureEvent(context, detailsWith({ ...baseMetadata, __dd_split_serial_id: 340132 })) |
| 21 | + |
| 22 | + expect(event).toEqual({ |
| 23 | + allocation: { key: 'allocation-123' }, |
| 24 | + flag: { key: 'checkout-redesign' }, |
| 25 | + variant: { key: 'treatment' }, |
| 26 | + serial_id: 340132, |
| 27 | + subject: { |
| 28 | + id: 'user-123', |
| 29 | + attributes: { country: 'US' }, |
| 30 | + }, |
| 31 | + }) |
| 32 | + }) |
| 33 | + |
| 34 | + it('should not include serial_id when the metadata carries none', () => { |
| 35 | + const event = createExposureEvent(context, detailsWith(baseMetadata)) |
| 36 | + |
| 37 | + expect(event).not.toHaveProperty('serial_id') |
| 38 | + expect(event?.allocation.key).toBe('allocation-123') |
| 39 | + }) |
| 40 | + |
| 41 | + it('should include a serial id of zero', () => { |
| 42 | + const event = createExposureEvent(context, detailsWith({ ...baseMetadata, __dd_split_serial_id: 0 })) |
| 43 | + |
| 44 | + expect(event).toHaveProperty('serial_id') |
| 45 | + expect(event?.serial_id).toBe(0) |
| 46 | + }) |
| 47 | + |
| 48 | + it.each<[number, string]>([ |
| 49 | + [-1, 'negative'], |
| 50 | + [1.5, 'not an integer'], |
| 51 | + ])('should send a serial id of %p (%s) without validating it', (serialId) => { |
| 52 | + const event = createExposureEvent(context, detailsWith({ ...baseMetadata, __dd_split_serial_id: serialId })) |
| 53 | + |
| 54 | + expect(event?.serial_id).toBe(serialId) |
| 55 | + }) |
| 56 | + |
| 57 | + it.each<[string | boolean, string]>([ |
| 58 | + ['340132', 'a string'], |
| 59 | + [true, 'a boolean'], |
| 60 | + ])('should not include serial_id when the metadata value is %p (%s)', (serialId) => { |
| 61 | + const event = createExposureEvent(context, detailsWith({ ...baseMetadata, __dd_split_serial_id: serialId })) |
| 62 | + |
| 63 | + expect(event).not.toHaveProperty('serial_id') |
| 64 | + expect(event?.flag.key).toBe('checkout-redesign') |
| 65 | + }) |
| 66 | + |
| 67 | + it('should return undefined when doLog is false, whatever the serial id', () => { |
| 68 | + const event = createExposureEvent( |
| 69 | + context, |
| 70 | + detailsWith({ doLog: false, allocationKey: 'allocation-123', __dd_split_serial_id: 340132 }) |
| 71 | + ) |
| 72 | + |
| 73 | + expect(event).toBeUndefined() |
| 74 | + }) |
| 75 | +}) |
0 commit comments