import { eventLog } from './index' import { database } from './database' import { cron } from './cron' const PUBLIC_API_KEY = 'staging_gAAAAABj-NC2WEf3XA4lHnN6RfGbjETf6qL27jlU7c1tm01ERV-Y-E6ddee1QPyV_CA8cxjVlGy-qDNA2-mqa9msQlTJit-Fmbvv6f2AZdfbFd_CUo1stvixnF4_8MzM_IYVWYz7KK86VfEBvxNpjRCyp7BDef-QzrT1-yqa8HAYTuA1GU3Hjjo=' const eventArgs = { source: 'test', event: 'testEvent', description: 'Test event', original_created_at: new Date().toISOString(), } const logArgs = { sdk_version: '', url: '', method: '', country: '', original_created_at: new Date().toISOString() } describe('EventLog', () => { beforeEach(() => { // Reset the database before each test // to ensure a clean slate eventLog.init({ publicApiKey: PUBLIC_API_KEY, batchTime: 500, enableEvents: true, enableLogs: true, }) database.reset() }) afterEach(async () => { await new Promise((resolve) => setTimeout(resolve, 1000)) cron.stop() }) it('should add an event to the database', async () => { eventLog.event(eventArgs) const events = await database.getAllItems() expect(events).toHaveLength(1) expect(events[0]).toMatchObject(eventArgs) }) it('should not add an event to the database if events are disabled', async () => { eventLog.init({ publicApiKey: PUBLIC_API_KEY, batchTime: 1000, enableEvents: false, enableLogs: true, }) eventLog.event(eventArgs) const events = await database.getAllItems() expect(events).toHaveLength(0) }) it('should log an error', async () => { eventLog.logger.error(logArgs) const events = await database.getAllItems() expect(events).toHaveLength(1) expect(events[0]).toMatchObject(logArgs) }) it('should not log an error if logs are disabled', async () => { eventLog.init({ publicApiKey: PUBLIC_API_KEY, batchTime: 1000, enableEvents: false, enableLogs: false, }) eventLog.logger.error(logArgs) const events = await database.getAllItems() expect(events).toHaveLength(0) }) it('should log a debug message', async () => { eventLog.logger.debug(logArgs) const events = await database.getAllItems() expect(events).toHaveLength(1) expect(events[0]).toMatchObject(logArgs) }) it('should not log a debug message if logs are disabled', async () => { eventLog.init({ publicApiKey: PUBLIC_API_KEY, batchTime: 1000, enableEvents: false, enableLogs: false, }) eventLog.logger.debug(logArgs) const events = await database.getAllItems() expect(events).toHaveLength(0) }) it('should log an info message', async () => { eventLog.logger.info(logArgs) const events = await database.getAllItems() expect(events).toHaveLength(1) expect(events[0]).toMatchObject(logArgs) }) it('should not log an info message if logs are disabled', async () => { eventLog.init({ publicApiKey: PUBLIC_API_KEY, batchTime: 1000, enableEvents: false, enableLogs: false, }) eventLog.logger.info(logArgs) const events = await database.getAllItems() expect(events).toHaveLength(0) }) })