|
| 1 | +import { |
| 2 | + IConfigurationStore, |
| 3 | + ObfuscatedFlag, |
| 4 | + Flag, |
| 5 | + EventDispatcher, |
| 6 | +} from '@eppo/js-client-sdk-common'; |
| 7 | +import * as td from 'testdouble'; |
| 8 | + |
| 9 | +import { clientOptionsToParameters } from './client-options-converter'; |
| 10 | +import { IClientOptions } from './i-client-config'; |
| 11 | +import { sdkName, sdkVersion } from './sdk-data'; |
| 12 | + |
| 13 | +describe('clientOptionsToParameters', () => { |
| 14 | + const mockStore = td.object<IConfigurationStore<Flag | ObfuscatedFlag>>(); |
| 15 | + |
| 16 | + it('converts basic client options', () => { |
| 17 | + const options: IClientOptions = { |
| 18 | + sdkKey: 'test-key', |
| 19 | + baseUrl: 'https://test.eppo.cloud', |
| 20 | + assignmentLogger: { logAssignment: jest.fn() }, |
| 21 | + }; |
| 22 | + |
| 23 | + const result = clientOptionsToParameters(options, mockStore); |
| 24 | + |
| 25 | + expect(result.isObfuscated).toBe(true); |
| 26 | + expect(result.flagConfigurationStore).toBeDefined(); |
| 27 | + expect(result.configurationRequestParameters).toEqual({ |
| 28 | + apiKey: 'test-key', |
| 29 | + baseUrl: 'https://test.eppo.cloud', |
| 30 | + sdkName, |
| 31 | + sdkVersion, |
| 32 | + numInitialRequestRetries: undefined, |
| 33 | + numPollRequestRetries: undefined, |
| 34 | + pollingIntervalMs: undefined, |
| 35 | + requestTimeoutMs: undefined, |
| 36 | + pollAfterFailedInitialization: undefined, |
| 37 | + pollAfterSuccessfulInitialization: undefined, |
| 38 | + throwOnFailedInitialization: undefined, |
| 39 | + skipInitialPoll: undefined, |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('uses provided flag configuration store', () => { |
| 44 | + const options: IClientOptions = { |
| 45 | + sdkKey: 'test-key', |
| 46 | + assignmentLogger: { logAssignment: jest.fn() }, |
| 47 | + }; |
| 48 | + |
| 49 | + const result = clientOptionsToParameters(options, mockStore); |
| 50 | + |
| 51 | + expect(result.flagConfigurationStore).toBe(mockStore); |
| 52 | + }); |
| 53 | + |
| 54 | + it('converts client options with event ingestion config', () => { |
| 55 | + const options: IClientOptions = { |
| 56 | + sdkKey: 'test-key', |
| 57 | + assignmentLogger: { logAssignment: jest.fn() }, |
| 58 | + }; |
| 59 | + const mockDispatcher: EventDispatcher = td.object<EventDispatcher>(); |
| 60 | + |
| 61 | + const result = clientOptionsToParameters(options, mockStore, mockDispatcher); |
| 62 | + |
| 63 | + expect(result.eventDispatcher).toBeDefined(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('converts client options with polling configuration', () => { |
| 67 | + const options: IClientOptions = { |
| 68 | + sdkKey: 'test-key', |
| 69 | + assignmentLogger: { logAssignment: jest.fn() }, |
| 70 | + pollingIntervalMs: 30000, |
| 71 | + pollAfterSuccessfulInitialization: true, |
| 72 | + pollAfterFailedInitialization: true, |
| 73 | + skipInitialRequest: true, |
| 74 | + }; |
| 75 | + |
| 76 | + const result = clientOptionsToParameters(options, mockStore); |
| 77 | + |
| 78 | + expect(result.configurationRequestParameters).toMatchObject({ |
| 79 | + pollingIntervalMs: 30000, |
| 80 | + pollAfterSuccessfulInitialization: true, |
| 81 | + pollAfterFailedInitialization: true, |
| 82 | + skipInitialPoll: true, |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('converts client options with retry configuration', () => { |
| 87 | + const options: IClientOptions = { |
| 88 | + sdkKey: 'test-key', |
| 89 | + assignmentLogger: { logAssignment: jest.fn() }, |
| 90 | + requestTimeoutMs: 5000, |
| 91 | + numInitialRequestRetries: 3, |
| 92 | + numPollRequestRetries: 2, |
| 93 | + }; |
| 94 | + |
| 95 | + const result = clientOptionsToParameters(options, mockStore); |
| 96 | + |
| 97 | + expect(result.configurationRequestParameters).toMatchObject({ |
| 98 | + requestTimeoutMs: 5000, |
| 99 | + numInitialRequestRetries: 3, |
| 100 | + numPollRequestRetries: 2, |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('handles undefined optional parameters', () => { |
| 105 | + const options: IClientOptions = { |
| 106 | + sdkKey: 'test-key', |
| 107 | + assignmentLogger: { logAssignment: jest.fn() }, |
| 108 | + }; |
| 109 | + |
| 110 | + const result = clientOptionsToParameters(options, mockStore); |
| 111 | + |
| 112 | + expect(result.configurationRequestParameters).toMatchObject({ |
| 113 | + baseUrl: undefined, |
| 114 | + pollingIntervalMs: undefined, |
| 115 | + requestTimeoutMs: undefined, |
| 116 | + numInitialRequestRetries: undefined, |
| 117 | + numPollRequestRetries: undefined, |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('includes sdk metadata', () => { |
| 122 | + const options: IClientOptions = { |
| 123 | + sdkKey: 'test-key', |
| 124 | + assignmentLogger: { logAssignment: jest.fn() }, |
| 125 | + }; |
| 126 | + |
| 127 | + const result = clientOptionsToParameters(options, mockStore); |
| 128 | + |
| 129 | + expect(result.configurationRequestParameters).toMatchObject({ |
| 130 | + sdkName, |
| 131 | + sdkVersion, |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments