import { AmqpConnection } from '@golevelup/nestjs-rabbitmq'; import { Test, TestingModule } from '@nestjs/testing'; import { RedisService } from 'nestjs-redis'; import { PipelineTaskFlushService } from './pipeline-task-flush.service'; import { PipelineTaskEvent } from './models/pipeline-task-event'; import { TaskStatuses } from './enums/task-statuses.enum'; import { EXCHANGE_PIPELINE_TASK_TOPIC, ROUTE_PIPELINE_TASK_DONE, } from './pipeline-tasks.constants'; describe('PipelineTaskFlushService', () => { let service: PipelineTaskFlushService; let redisService: RedisService; let amqpConnection: AmqpConnection; beforeEach(async () => { const redisClient = { rpush: jest.fn(() => Promise.resolve()), lrange: jest.fn(() => Promise.resolve()), expire: jest.fn(() => Promise.resolve()), }; const module: TestingModule = await Test.createTestingModule({ providers: [ PipelineTaskFlushService, { provide: RedisService, useValue: { getClient() { return redisClient; }, }, }, { provide: AmqpConnection, useValue: { request: jest.fn(() => Promise.resolve()), }, }, ], }).compile(); service = module.get(PipelineTaskFlushService); redisService = module.get(RedisService); amqpConnection = module.get(AmqpConnection); }); it('should be defined', () => { expect(service).toBeDefined(); }); describe('write', () => { const amqpMsg = { properties: { headers: { sender: 'test' } }, } as any; it('normal', async () => { const testEvent = new PipelineTaskEvent(); testEvent.taskId = 'test'; testEvent.status = TaskStatuses.working; const rpush = jest.spyOn(redisService.getClient(), 'rpush'); const request = jest.spyOn(amqpConnection, 'request'); await service.write(testEvent, amqpMsg); expect(rpush).toBeCalledTimes(1); expect(rpush.mock.calls[0][0]).toEqual('p-task:log:test'); expect(rpush.mock.calls[0][1]).toEqual(JSON.stringify(testEvent)); expect(request).toBeCalledTimes(1); }); it('event for which task done', async () => { const testEvent = new PipelineTaskEvent(); testEvent.taskId = 'test'; testEvent.status = TaskStatuses.success; const rpush = jest.spyOn(redisService.getClient(), 'rpush'); const request = jest.spyOn(amqpConnection, 'request'); await service.write(testEvent, amqpMsg); expect(rpush).toBeCalledTimes(1); expect(request).toBeCalledTimes(1); expect(request.mock.calls[0][0]).toMatchObject({ exchange: EXCHANGE_PIPELINE_TASK_TOPIC, routingKey: ROUTE_PIPELINE_TASK_DONE, payload: { taskId: 'test', status: TaskStatuses.success, runOn: 'test', }, }); }); }); });