|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { buildImpressionEvent, buildConversionEvent } from './user_event'; |
| 3 | +import { createProjectConfig, ProjectConfig } from '../../project_config/project_config'; |
| 4 | +import { DecisionObj } from '../../core/decision_service'; |
| 5 | +import testData from '../../tests/test_data'; |
| 6 | + |
| 7 | +describe('buildImpressionEvent', () => { |
| 8 | + it('should use correct region from projectConfig in event context', () => { |
| 9 | + const projectConfig = createProjectConfig( |
| 10 | + testData.getTestProjectConfig(), |
| 11 | + ) |
| 12 | + |
| 13 | + const experiment = projectConfig.experiments[0]; |
| 14 | + const variation = experiment.variations[0]; |
| 15 | + |
| 16 | + const decisionObj = { |
| 17 | + experiment, |
| 18 | + variation, |
| 19 | + decisionSource: 'experiment', |
| 20 | + } as DecisionObj; |
| 21 | + |
| 22 | + |
| 23 | + const impressionEvent = buildImpressionEvent({ |
| 24 | + configObj: projectConfig, |
| 25 | + decisionObj, |
| 26 | + userId: 'test_user', |
| 27 | + flagKey: 'test_flag', |
| 28 | + enabled: true, |
| 29 | + clientEngine: 'node-sdk', |
| 30 | + clientVersion: '1.0.0', |
| 31 | + }); |
| 32 | + |
| 33 | + expect(impressionEvent.context.region).toBe('US'); |
| 34 | + |
| 35 | + projectConfig.region = 'EU'; |
| 36 | + |
| 37 | + const impressionEventEU = buildImpressionEvent({ |
| 38 | + configObj: projectConfig, |
| 39 | + decisionObj, |
| 40 | + userId: 'test_user', |
| 41 | + flagKey: 'test_flag', |
| 42 | + enabled: true, |
| 43 | + clientEngine: 'node-sdk', |
| 44 | + clientVersion: '1.0.0', |
| 45 | + }); |
| 46 | + |
| 47 | + expect(impressionEventEU.context.region).toBe('EU'); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +describe('buildConversionEvent', () => { |
| 52 | + it('should use correct region from projectConfig in event context', () => { |
| 53 | + const projectConfig = createProjectConfig( |
| 54 | + testData.getTestProjectConfig(), |
| 55 | + ) |
| 56 | + |
| 57 | + const conversionEvent = buildConversionEvent({ |
| 58 | + configObj: projectConfig, |
| 59 | + userId: 'test_user', |
| 60 | + eventKey: 'test_event', |
| 61 | + eventTags: { revenue: 1000 }, |
| 62 | + clientEngine: 'node-sdk', |
| 63 | + clientVersion: '1.0.0', |
| 64 | + }); |
| 65 | + |
| 66 | + expect(conversionEvent.context.region).toBe('US'); |
| 67 | + |
| 68 | + projectConfig.region = 'EU'; |
| 69 | + |
| 70 | + const conversionEventEU = buildConversionEvent({ |
| 71 | + configObj: projectConfig, |
| 72 | + userId: 'test_user', |
| 73 | + eventKey: 'test_event', |
| 74 | + eventTags: { revenue: 1000 }, |
| 75 | + clientEngine: 'node-sdk', |
| 76 | + clientVersion: '1.0.0', |
| 77 | + }); |
| 78 | + |
| 79 | + expect(conversionEventEU.context.region).toBe('EU'); |
| 80 | + }); |
| 81 | +}); |
0 commit comments