|
| 1 | +import { createEvaluationEvent } from '../src/telemetry/evaluation-event'; |
| 2 | +import { ErrorCode, StandardResolutionReasons, type EvaluationDetails } from '../src/evaluation/evaluation'; |
| 3 | +import type { HookContext } from '../src/hooks/hooks'; |
| 4 | +import { TelemetryAttribute, TelemetryFlagMetadata, TelemetryEvaluationData } from '../src/telemetry'; |
| 5 | + |
| 6 | +describe('evaluationEvent', () => { |
| 7 | + const flagKey = 'test-flag'; |
| 8 | + const providerMetadata = { |
| 9 | + name: 'test-provider', |
| 10 | + }; |
| 11 | + const mockHookContext: HookContext<boolean> = { |
| 12 | + flagKey, |
| 13 | + providerMetadata: providerMetadata, |
| 14 | + context: { |
| 15 | + targetingKey: 'test-target', |
| 16 | + }, |
| 17 | + clientMetadata: { |
| 18 | + providerMetadata, |
| 19 | + }, |
| 20 | + defaultValue: false, |
| 21 | + flagValueType: 'boolean', |
| 22 | + logger: { |
| 23 | + debug: jest.fn(), |
| 24 | + info: jest.fn(), |
| 25 | + error: jest.fn(), |
| 26 | + warn: jest.fn(), |
| 27 | + }, |
| 28 | + }; |
| 29 | + |
| 30 | + it('should return basic event body with mandatory fields', () => { |
| 31 | + const details: EvaluationDetails<boolean> = { |
| 32 | + value: true, |
| 33 | + reason: StandardResolutionReasons.STATIC, |
| 34 | + flagMetadata: {}, |
| 35 | + flagKey, |
| 36 | + }; |
| 37 | + |
| 38 | + const result = createEvaluationEvent(mockHookContext, details); |
| 39 | + |
| 40 | + expect(result.name).toBe('feature_flag.evaluation'); |
| 41 | + expect(result.attributes).toEqual({ |
| 42 | + [TelemetryAttribute.KEY]: 'test-flag', |
| 43 | + [TelemetryAttribute.PROVIDER]: 'test-provider', |
| 44 | + [TelemetryAttribute.REASON]: StandardResolutionReasons.STATIC.toLowerCase(), |
| 45 | + [TelemetryAttribute.CONTEXT_ID]: 'test-target', |
| 46 | + }); |
| 47 | + expect(result.data).toEqual({ |
| 48 | + [TelemetryEvaluationData.VALUE]: true, |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should include variant when provided', () => { |
| 53 | + const details: EvaluationDetails<boolean> = { |
| 54 | + flagKey, |
| 55 | + value: true, |
| 56 | + variant: 'test-variant', |
| 57 | + reason: StandardResolutionReasons.STATIC, |
| 58 | + flagMetadata: {}, |
| 59 | + }; |
| 60 | + |
| 61 | + const result = createEvaluationEvent(mockHookContext, details); |
| 62 | + |
| 63 | + expect(result.attributes[TelemetryAttribute.VARIANT]).toBe('test-variant'); |
| 64 | + expect(result.attributes[TelemetryEvaluationData.VALUE]).toBeUndefined(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should include flag metadata when provided', () => { |
| 68 | + const details: EvaluationDetails<boolean> = { |
| 69 | + flagKey, |
| 70 | + value: true, |
| 71 | + reason: StandardResolutionReasons.STATIC, |
| 72 | + flagMetadata: { |
| 73 | + [TelemetryFlagMetadata.FLAG_SET_ID]: 'test-set', |
| 74 | + [TelemetryFlagMetadata.VERSION]: 'v1.0', |
| 75 | + [TelemetryFlagMetadata.CONTEXT_ID]: 'metadata-context', |
| 76 | + }, |
| 77 | + }; |
| 78 | + |
| 79 | + const result = createEvaluationEvent(mockHookContext, details); |
| 80 | + |
| 81 | + expect(result.attributes[TelemetryAttribute.FLAG_SET_ID]).toBe('test-set'); |
| 82 | + expect(result.attributes[TelemetryAttribute.VERSION]).toBe('v1.0'); |
| 83 | + expect(result.attributes[TelemetryAttribute.CONTEXT_ID]).toBe('metadata-context'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should handle error cases', () => { |
| 87 | + const details: EvaluationDetails<boolean> = { |
| 88 | + flagKey, |
| 89 | + value: false, |
| 90 | + reason: StandardResolutionReasons.ERROR, |
| 91 | + errorCode: ErrorCode.GENERAL, |
| 92 | + errorMessage: 'test error', |
| 93 | + flagMetadata: {}, |
| 94 | + }; |
| 95 | + |
| 96 | + const result = createEvaluationEvent(mockHookContext, details); |
| 97 | + |
| 98 | + expect(result.attributes[TelemetryAttribute.ERROR_CODE]).toBe(ErrorCode.GENERAL.toLowerCase()); |
| 99 | + expect(result.attributes[TelemetryAttribute.ERROR_MESSAGE]).toBe('test error'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should use unknown reason when reason is not provided', () => { |
| 103 | + const details: EvaluationDetails<boolean> = { |
| 104 | + flagKey, |
| 105 | + value: true, |
| 106 | + flagMetadata: {}, |
| 107 | + }; |
| 108 | + |
| 109 | + const result = createEvaluationEvent(mockHookContext, details); |
| 110 | + |
| 111 | + expect(result.attributes[TelemetryAttribute.REASON]).toBe(StandardResolutionReasons.UNKNOWN.toLowerCase()); |
| 112 | + }); |
| 113 | +}); |
0 commit comments