|
| 1 | +const { initialize } = require('../index'); |
| 2 | +const stubPlatform = require('./stubPlatform'); |
| 3 | +const { respondJson } = require('./mockHttp'); |
| 4 | + |
| 5 | +// Mock the logger functions |
| 6 | +const mockLogger = () => ({ |
| 7 | + error: jest.fn(), |
| 8 | + warn: jest.fn(), |
| 9 | + info: jest.fn(), |
| 10 | + debug: jest.fn(), |
| 11 | +}); |
| 12 | + |
| 13 | +// Define a basic Hook structure for tests |
| 14 | +const createTestHook = (name = 'Test Hook') => ({ |
| 15 | + getMetadata: jest.fn().mockReturnValue({ name }), |
| 16 | + beforeEvaluation: jest.fn().mockImplementation((_ctx, data) => data), |
| 17 | + afterEvaluation: jest.fn().mockImplementation((_ctx, data) => data), |
| 18 | + beforeIdentify: jest.fn().mockImplementation((_ctx, data) => data), |
| 19 | + afterIdentify: jest.fn().mockImplementation((_ctx, data) => data), |
| 20 | + afterTrack: jest.fn().mockImplementation((_ctx, data) => data), |
| 21 | +}); |
| 22 | + |
| 23 | +// Define a basic Plugin structure for tests |
| 24 | +const createTestPlugin = (name = 'Test Plugin', hooks = []) => ({ |
| 25 | + getMetadata: jest.fn().mockReturnValue({ name }), |
| 26 | + register: jest.fn(), |
| 27 | + getHooks: jest.fn().mockReturnValue(hooks), |
| 28 | +}); |
| 29 | + |
| 30 | +// Helper to initialize the client for tests |
| 31 | +async function withClient(initialContext, configOverrides = {}, plugins = [], testFn) { |
| 32 | + const platform = stubPlatform.defaults(); |
| 33 | + const server = platform.testing.http.newServer(); |
| 34 | + const logger = mockLogger(); |
| 35 | + |
| 36 | + // Disable streaming and event sending unless overridden |
| 37 | + const defaults = { |
| 38 | + baseUrl: server.url, |
| 39 | + streaming: false, |
| 40 | + sendEvents: false, |
| 41 | + useLdd: false, |
| 42 | + logger: logger, |
| 43 | + plugins: plugins, |
| 44 | + }; |
| 45 | + const config = { ...defaults, ...configOverrides }; |
| 46 | + const { client, start } = initialize('env', initialContext, config, platform); |
| 47 | + |
| 48 | + server.byDefault(respondJson({})); |
| 49 | + start(); |
| 50 | + |
| 51 | + try { |
| 52 | + await client.waitForInitialization(10); |
| 53 | + await testFn(client, logger, platform); |
| 54 | + } finally { |
| 55 | + await client.close(); |
| 56 | + server.close(); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +it('registers plugins and executes hooks during initialization', async () => { |
| 61 | + const mockHook = createTestHook('test-hook'); |
| 62 | + const mockPlugin = createTestPlugin('test-plugin', [mockHook]); |
| 63 | + |
| 64 | + await withClient({ key: 'user-key', kind: 'user' }, {}, [mockPlugin], async client => { |
| 65 | + // Verify the plugin was registered |
| 66 | + expect(mockPlugin.register).toHaveBeenCalled(); |
| 67 | + |
| 68 | + // Test identify hook |
| 69 | + await client.identify({ key: 'user-key', kind: 'user' }); |
| 70 | + expect(mockHook.beforeIdentify).toHaveBeenCalledWith( |
| 71 | + { context: { key: 'user-key', kind: 'user' }, timeout: undefined }, |
| 72 | + {} |
| 73 | + ); |
| 74 | + expect(mockHook.afterIdentify).toHaveBeenCalledWith( |
| 75 | + { context: { key: 'user-key', kind: 'user' }, timeout: undefined }, |
| 76 | + {}, |
| 77 | + { status: 'completed' } |
| 78 | + ); |
| 79 | + |
| 80 | + // Test variation hook |
| 81 | + client.variation('flag-key', false); |
| 82 | + expect(mockHook.beforeEvaluation).toHaveBeenCalledWith( |
| 83 | + { |
| 84 | + context: { key: 'user-key', kind: 'user' }, |
| 85 | + defaultValue: false, |
| 86 | + flagKey: 'flag-key', |
| 87 | + }, |
| 88 | + {} |
| 89 | + ); |
| 90 | + expect(mockHook.afterEvaluation).toHaveBeenCalled(); |
| 91 | + |
| 92 | + // Test track hook |
| 93 | + client.track('event-key', { data: true }, 42); |
| 94 | + expect(mockHook.afterTrack).toHaveBeenCalledWith({ |
| 95 | + context: { key: 'user-key', kind: 'user' }, |
| 96 | + key: 'event-key', |
| 97 | + data: { data: true }, |
| 98 | + metricValue: 42, |
| 99 | + }); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +it('registers multiple plugins and executes all hooks', async () => { |
| 104 | + const mockHook1 = createTestHook('test-hook-1'); |
| 105 | + const mockHook2 = createTestHook('test-hook-2'); |
| 106 | + const mockPlugin1 = createTestPlugin('test-plugin-1', [mockHook1]); |
| 107 | + const mockPlugin2 = createTestPlugin('test-plugin-2', [mockHook2]); |
| 108 | + |
| 109 | + await withClient({ key: 'user-key', kind: 'user' }, {}, [mockPlugin1, mockPlugin2], async client => { |
| 110 | + // Verify plugins were registered |
| 111 | + expect(mockPlugin1.register).toHaveBeenCalled(); |
| 112 | + expect(mockPlugin2.register).toHaveBeenCalled(); |
| 113 | + |
| 114 | + // Test that both hooks work |
| 115 | + await client.identify({ key: 'user-key', kind: 'user' }); |
| 116 | + client.variation('flag-key', false); |
| 117 | + client.track('event-key', { data: true }, 42); |
| 118 | + |
| 119 | + expect(mockHook1.beforeEvaluation).toHaveBeenCalled(); |
| 120 | + expect(mockHook1.afterEvaluation).toHaveBeenCalled(); |
| 121 | + expect(mockHook2.beforeEvaluation).toHaveBeenCalled(); |
| 122 | + expect(mockHook2.afterEvaluation).toHaveBeenCalled(); |
| 123 | + expect(mockHook1.afterTrack).toHaveBeenCalled(); |
| 124 | + expect(mockHook2.afterTrack).toHaveBeenCalled(); |
| 125 | + }); |
| 126 | +}); |
| 127 | + |
| 128 | +it('passes correct environmentMetadata to plugin getHooks and register functions', async () => { |
| 129 | + const mockPlugin = createTestPlugin('test-plugin'); |
| 130 | + const options = { |
| 131 | + wrapperName: 'test-wrapper', |
| 132 | + wrapperVersion: '2.0.0', |
| 133 | + application: { |
| 134 | + name: 'test-app', |
| 135 | + version: '3.0.0', |
| 136 | + }, |
| 137 | + }; |
| 138 | + |
| 139 | + await withClient( |
| 140 | + { key: 'user-key', kind: 'user' }, |
| 141 | + { ...options, plugins: [mockPlugin] }, |
| 142 | + [mockPlugin], |
| 143 | + async (client, logger, testPlatform) => { |
| 144 | + expect(testPlatform.userAgent).toBeDefined(); |
| 145 | + expect(testPlatform.version).toBeDefined(); |
| 146 | + // Verify getHooks was called with correct environmentMetadata |
| 147 | + expect(mockPlugin.getHooks).toHaveBeenCalledWith({ |
| 148 | + sdk: { |
| 149 | + name: testPlatform.userAgent, |
| 150 | + version: testPlatform.version, |
| 151 | + wrapperName: options.wrapperName, |
| 152 | + wrapperVersion: options.wrapperVersion, |
| 153 | + }, |
| 154 | + application: { |
| 155 | + id: options.application.id, |
| 156 | + version: options.application.version, |
| 157 | + }, |
| 158 | + clientSideId: 'env', |
| 159 | + }); |
| 160 | + |
| 161 | + // Verify register was called with correct environmentMetadata |
| 162 | + expect(mockPlugin.register).toHaveBeenCalledWith( |
| 163 | + expect.any(Object), // client |
| 164 | + { |
| 165 | + sdk: { |
| 166 | + name: testPlatform.userAgent, |
| 167 | + version: testPlatform.version, |
| 168 | + wrapperName: options.wrapperName, |
| 169 | + wrapperVersion: options.wrapperVersion, |
| 170 | + }, |
| 171 | + application: { |
| 172 | + id: options.application.id, |
| 173 | + version: options.application.version, |
| 174 | + }, |
| 175 | + clientSideId: 'env', |
| 176 | + } |
| 177 | + ); |
| 178 | + } |
| 179 | + ); |
| 180 | +}); |
| 181 | + |
| 182 | +it('passes correct environmentMetadata without optional fields', async () => { |
| 183 | + const mockPlugin = createTestPlugin('test-plugin'); |
| 184 | + |
| 185 | + await withClient( |
| 186 | + { key: 'user-key', kind: 'user' }, |
| 187 | + { plugins: [mockPlugin] }, |
| 188 | + [mockPlugin], |
| 189 | + async (client, logger, testPlatform) => { |
| 190 | + expect(testPlatform.userAgent).toBeDefined(); |
| 191 | + expect(testPlatform.version).toBeDefined(); |
| 192 | + // Verify getHooks was called with correct environmentMetadata |
| 193 | + expect(mockPlugin.getHooks).toHaveBeenCalledWith({ |
| 194 | + sdk: { |
| 195 | + name: testPlatform.userAgent, |
| 196 | + version: testPlatform.version, |
| 197 | + }, |
| 198 | + clientSideId: 'env', |
| 199 | + }); |
| 200 | + |
| 201 | + // Verify register was called with correct environmentMetadata |
| 202 | + expect(mockPlugin.register).toHaveBeenCalledWith( |
| 203 | + expect.any(Object), // client |
| 204 | + { |
| 205 | + sdk: { |
| 206 | + name: testPlatform.userAgent, |
| 207 | + version: testPlatform.version, |
| 208 | + }, |
| 209 | + clientSideId: 'env', |
| 210 | + } |
| 211 | + ); |
| 212 | + } |
| 213 | + ); |
| 214 | +}); |
0 commit comments