|
| 1 | +import type { EvaluationContext } from '@openfeature/web-sdk'; |
| 2 | +import { OpenFeature, StandardResolutionReasons, ErrorCode } from '@openfeature/web-sdk'; |
| 3 | +import type { FlagStatus, UserContext } from './rocketflag-provider'; |
| 4 | +import { RocketFlagProvider } from './rocketflag-provider'; |
| 5 | + |
| 6 | +// Create a mock RocketFlag client for testing |
| 7 | +const mockClient = { |
| 8 | + getFlag: jest.fn<Promise<FlagStatus>, [string, UserContext]>(), |
| 9 | +}; |
| 10 | + |
| 11 | +// Mock Logger |
| 12 | +const mockLogger = { |
| 13 | + info: jest.fn(), |
| 14 | + warn: jest.fn(), |
| 15 | + error: jest.fn(), |
| 16 | + debug: jest.fn(), |
| 17 | +}; |
| 18 | + |
| 19 | +describe('RocketFlagProvider', () => { |
| 20 | + beforeEach(() => jest.clearAllMocks()); |
| 21 | + |
| 22 | + it('should have the correct metadata name', () => { |
| 23 | + const provider = new RocketFlagProvider(mockClient); |
| 24 | + expect(provider.metadata.name).toBe('RocketFlagProvider'); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('resolveBooleanEvaluation', () => { |
| 28 | + it('should return STALE initially, then resolve to the correct value with TARGETING_MATCH', async () => { |
| 29 | + const provider = new RocketFlagProvider(mockClient); |
| 30 | + const flagKey = 'test-flag-targeting'; |
| 31 | + const targetingContext: EvaluationContext = { targetingKey: '[email protected]' }; |
| 32 | + |
| 33 | + mockClient.getFlag.mockResolvedValue({ enabled: true }); |
| 34 | + |
| 35 | + const initialDetails = provider.resolveBooleanEvaluation(flagKey, false, targetingContext, mockLogger); |
| 36 | + expect(initialDetails.reason).toBe(StandardResolutionReasons.STALE); |
| 37 | + expect(initialDetails.value).toBe(false); |
| 38 | + |
| 39 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 40 | + |
| 41 | + const finalDetails = provider.resolveBooleanEvaluation(flagKey, false, targetingContext, mockLogger); |
| 42 | + |
| 43 | + expect(finalDetails.value).toBe(true); |
| 44 | + expect(finalDetails.reason).toBe(StandardResolutionReasons.TARGETING_MATCH); |
| 45 | + expect(mockClient.getFlag).toHaveBeenCalledWith(flagKey, { cohort: '[email protected]' }); |
| 46 | + expect(mockClient.getFlag).toHaveBeenCalledTimes(2); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should return STALE initially, then resolve with DEFAULT reason when no targetingKey is provided', async () => { |
| 50 | + const provider = new RocketFlagProvider(mockClient); |
| 51 | + const flagKey = 'test-flag-default'; |
| 52 | + |
| 53 | + mockClient.getFlag.mockResolvedValue({ enabled: true }); |
| 54 | + |
| 55 | + const initialDetails = provider.resolveBooleanEvaluation(flagKey, false, {}, mockLogger); |
| 56 | + expect(initialDetails.reason).toBe(StandardResolutionReasons.STALE); |
| 57 | + |
| 58 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 59 | + |
| 60 | + const finalDetails = provider.resolveBooleanEvaluation(flagKey, false, {}, mockLogger); |
| 61 | + |
| 62 | + expect(finalDetails.value).toBe(true); |
| 63 | + expect(finalDetails.reason).toBe(StandardResolutionReasons.DEFAULT); |
| 64 | + expect(mockClient.getFlag).toHaveBeenCalledWith(flagKey, {}); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should return STALE initially, then resolve with an ERROR if the client rejects', async () => { |
| 68 | + const provider = new RocketFlagProvider(mockClient); |
| 69 | + OpenFeature.setProvider(provider); |
| 70 | + const client = OpenFeature.getClient(); |
| 71 | + const flagKey = 'test-flag-error'; |
| 72 | + const errorMessage = 'Network error'; |
| 73 | + |
| 74 | + mockClient.getFlag.mockRejectedValue(new Error(errorMessage)); |
| 75 | + |
| 76 | + const initialDetails = provider.resolveBooleanEvaluation(flagKey, false, {}, mockLogger); |
| 77 | + expect(initialDetails.reason).toBe(StandardResolutionReasons.STALE); |
| 78 | + |
| 79 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 80 | + |
| 81 | + const finalDetails = client.getBooleanDetails(flagKey, false); |
| 82 | + |
| 83 | + expect(finalDetails.value).toBe(false); // Default value |
| 84 | + expect(finalDetails.reason).toBe(StandardResolutionReasons.ERROR); |
| 85 | + expect(finalDetails.errorCode).toBe(ErrorCode.GENERAL); |
| 86 | + expect(finalDetails.errorMessage).toBe(errorMessage); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should return from cache on subsequent calls for the same context', () => { |
| 90 | + const provider = new RocketFlagProvider(mockClient); |
| 91 | + const flagKey = 'cached-flag'; |
| 92 | + const targetingContext: EvaluationContext = { targetingKey: 'cached-user' }; |
| 93 | + const cacheKey = JSON.stringify({ flagKey, context: targetingContext }); |
| 94 | + const cachedDetails = { |
| 95 | + value: true, |
| 96 | + reason: StandardResolutionReasons.TARGETING_MATCH, |
| 97 | + }; |
| 98 | + |
| 99 | + // @ts-expect-error - setting private property for test purposes |
| 100 | + provider.cache.set(cacheKey, cachedDetails); |
| 101 | + |
| 102 | + const result = provider.resolveBooleanEvaluation(flagKey, false, targetingContext, mockLogger); |
| 103 | + |
| 104 | + expect(result).toEqual(cachedDetails); |
| 105 | + expect(mockClient.getFlag).toHaveBeenCalledTimes(1); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + // Tests for other evaluation types to ensure they return TYPE_MISMATCH |
| 110 | + describe('Unsupported Evaluations', () => { |
| 111 | + const provider = new RocketFlagProvider(mockClient); |
| 112 | + |
| 113 | + it('resolveStringEvaluation should return TYPE_MISMATCH error', () => { |
| 114 | + const details = provider.resolveStringEvaluation('flag', 'default'); |
| 115 | + expect(details.reason).toBe(StandardResolutionReasons.ERROR); |
| 116 | + expect(details.errorCode).toBe(ErrorCode.TYPE_MISMATCH); |
| 117 | + expect(details.value).toBe('default'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('resolveNumberEvaluation should return TYPE_MISMATCH error', () => { |
| 121 | + const details = provider.resolveNumberEvaluation('flag', 123); |
| 122 | + expect(details.reason).toBe(StandardResolutionReasons.ERROR); |
| 123 | + expect(details.errorCode).toBe(ErrorCode.TYPE_MISMATCH); |
| 124 | + expect(details.value).toBe(123); |
| 125 | + }); |
| 126 | + |
| 127 | + it('resolveObjectEvaluation should return TYPE_MISMATCH error', () => { |
| 128 | + const defaultValue = { key: 'value' }; |
| 129 | + const details = provider.resolveObjectEvaluation('flag', defaultValue); |
| 130 | + expect(details.reason).toBe(StandardResolutionReasons.ERROR); |
| 131 | + expect(details.errorCode).toBe(ErrorCode.TYPE_MISMATCH); |
| 132 | + expect(details.value).toEqual(defaultValue); |
| 133 | + }); |
| 134 | + }); |
| 135 | +}); |
0 commit comments