|
| 1 | +import { ILogsEmitter } from 'src/modules/profiler/interfaces/logs-emitter.interface'; |
| 2 | +import { ProfilerClient } from 'src/modules/profiler/models/profiler.client'; |
| 3 | +import { RedisObserver } from 'src/modules/profiler/models/redis.observer'; |
| 4 | +import { IShardObserver } from 'src/modules/profiler/interfaces/shard-observer.interface'; |
| 5 | +import { LogFile } from 'src/modules/profiler/models/log-file'; |
| 6 | +import { ProfilerAnalyticsService } from 'src/modules/profiler/profiler-analytics.service'; |
| 7 | +import { MockType } from 'src/__mocks__/common'; |
| 8 | +import { TelemetryEvents } from 'src/constants'; |
| 9 | +import * as MockedSocket from 'socket.io-mock'; |
| 10 | +import { IMonitorData } from 'src/modules/profiler/interfaces/monitor-data.interface'; |
| 11 | + |
| 12 | +export const mockMonitorDataItemEmitted = { |
| 13 | + time: '14239988881.12341', |
| 14 | + source: '127.0.0.1', |
| 15 | + database: 0, |
| 16 | + args: ['set', 'foo', 'bar'], |
| 17 | +}; |
| 18 | + |
| 19 | +export const mockMonitorDataItem: IMonitorData = { |
| 20 | + ...mockMonitorDataItemEmitted, |
| 21 | + shardOptions: null, |
| 22 | +}; |
| 23 | + |
| 24 | +export const mockSocket = new MockedSocket(); |
| 25 | +mockSocket['emit'] = jest.fn(); |
| 26 | +// |
| 27 | +// export const mockProfilerClient: MockType<ProfilerClient> = { |
| 28 | +// id: uuidv4(), |
| 29 | +// handleOnData: jest.fn(), |
| 30 | +// handleOnDisconnect: jest.fn(), |
| 31 | +// addLogsEmitter: jest.fn(), |
| 32 | +// flushLogs: jest.fn(), |
| 33 | +// destroy: jest.fn(), |
| 34 | +// }; |
| 35 | + |
| 36 | +export const mockRedisObserver: Partial<RedisObserver> = { |
| 37 | + // status: RedisObserverStatus.Wait, |
| 38 | + init: jest.fn(), |
| 39 | + subscribe: jest.fn(), |
| 40 | + unsubscribe: jest.fn(), |
| 41 | + getProfilerClientsSize: jest.fn(), |
| 42 | + clear: jest.fn(), |
| 43 | + removeShardsListeners: jest.fn(), |
| 44 | +}; |
| 45 | + |
| 46 | +export const mockRedisShardObserver: IShardObserver = { |
| 47 | + addListener: jest.fn(), |
| 48 | + eventNames: jest.fn(), |
| 49 | + getMaxListeners: jest.fn(), |
| 50 | + listenerCount: jest.fn(), |
| 51 | + listeners: jest.fn(), |
| 52 | + prependListener: jest.fn(), |
| 53 | + prependOnceListener: jest.fn(), |
| 54 | + removeAllListeners: jest.fn(), |
| 55 | + removeListener: jest.fn(), |
| 56 | + rawListeners: jest.fn(), |
| 57 | + setMaxListeners: jest.fn(), |
| 58 | + on: jest.fn(), |
| 59 | + emit: jest.fn(), |
| 60 | + off: jest.fn(), |
| 61 | + once: jest.fn(), |
| 62 | + disconnect: jest.fn(), |
| 63 | +}; |
| 64 | + |
| 65 | +export const mockProfilerAnalyticsEvents = new Map(); |
| 66 | +mockProfilerAnalyticsEvents.set(TelemetryEvents.ProfilerLogDownloaded, jest.fn()); |
| 67 | +mockProfilerAnalyticsEvents.set(TelemetryEvents.ProfilerLogDeleted, jest.fn()); |
| 68 | + |
| 69 | +export const mockProfilerAnalyticsService: MockType<ProfilerAnalyticsService> = { |
| 70 | + sendLogDeleted: jest.fn(), |
| 71 | + sendLogDownloaded: jest.fn(), |
| 72 | + getEventsEmitters: jest.fn().mockImplementation(() => mockProfilerAnalyticsEvents), |
| 73 | +}; |
| 74 | + |
| 75 | +export const mockLogEmitter: ILogsEmitter = { |
| 76 | + id: 'test', |
| 77 | + emit: jest.fn(), |
| 78 | + addProfilerClient: jest.fn(), |
| 79 | + removeProfilerClient: jest.fn(), |
| 80 | + flushLogs: jest.fn(), |
| 81 | +}; |
| 82 | + |
| 83 | +export const mockWriteStream = { |
| 84 | + write: jest.fn(), |
| 85 | +}; |
| 86 | +export const testLogFileId = 'test-log-file-id'; |
| 87 | +export const mockLogFile: LogFile = new LogFile('instanceid', testLogFileId, mockProfilerAnalyticsEvents); |
| 88 | +mockLogFile['getWriteStream'] = jest.fn(); |
| 89 | +mockLogFile['addProfilerClient'] = jest.fn(); |
| 90 | +mockLogFile['removeProfilerClient'] = jest.fn(); |
| 91 | +mockLogFile['destroy'] = jest.fn(); |
| 92 | + |
| 93 | +export const mockProfilerClientId = 'profiler-client-id'; |
| 94 | +export const mockProfilerClient: ProfilerClient = new ProfilerClient(mockProfilerClientId, mockSocket); |
0 commit comments