124 lines
3.6 KiB
TypeScript
124 lines
3.6 KiB
TypeScript
import { Test, TestingModule } from '@nestjs/testing';
|
|
import { NotificationsQueueService } from './notifications-queue.service';
|
|
import { EmailService } from './email.service';
|
|
import { SmsService } from '../common/services/sms.service';
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
|
|
describe('NotificationsQueueService', () => {
|
|
let service: NotificationsQueueService;
|
|
let prisma: any;
|
|
let emailService: any;
|
|
let smsService: any;
|
|
|
|
beforeEach(async () => {
|
|
prisma = {
|
|
notificationQueue: {
|
|
findUnique: jest.fn(),
|
|
create: jest.fn(),
|
|
update: jest.fn(),
|
|
findMany: jest.fn(),
|
|
count: jest.fn(),
|
|
},
|
|
};
|
|
|
|
emailService = {
|
|
sendMail: jest.fn().mockResolvedValue({ success: true, messageId: 'msg-123' }),
|
|
};
|
|
|
|
smsService = {
|
|
sendPatternSms: jest.fn().mockResolvedValue(true),
|
|
sendSms: jest.fn().mockResolvedValue(true),
|
|
};
|
|
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [
|
|
NotificationsQueueService,
|
|
{ provide: PrismaService, useValue: prisma },
|
|
{ provide: EmailService, useValue: emailService },
|
|
{ provide: SmsService, useValue: smsService },
|
|
],
|
|
}).compile();
|
|
|
|
service = module.get<NotificationsQueueService>(NotificationsQueueService);
|
|
});
|
|
|
|
it('should enqueue notification and preserve idempotency', async () => {
|
|
prisma.notificationQueue.findUnique.mockResolvedValueOnce(null);
|
|
prisma.notificationQueue.create.mockResolvedValueOnce({
|
|
id: 'queue-1',
|
|
channel: 'EMAIL',
|
|
recipient: 'user@example.com',
|
|
event: 'ORDER_CREATED',
|
|
content: 'Hello World',
|
|
idempotencyKey: 'idemp-1',
|
|
status: 'QUEUED',
|
|
attempts: 0,
|
|
});
|
|
|
|
const res = await service.enqueue({
|
|
channel: 'EMAIL',
|
|
recipient: 'user@example.com',
|
|
event: 'ORDER_CREATED',
|
|
content: 'Hello World',
|
|
idempotencyKey: 'idemp-1',
|
|
});
|
|
|
|
expect(prisma.notificationQueue.findUnique).toHaveBeenCalledWith({ where: { idempotencyKey: 'idemp-1' } });
|
|
expect(prisma.notificationQueue.create).toHaveBeenCalled();
|
|
expect(res.id).toBe('queue-1');
|
|
});
|
|
|
|
it('should return existing notification if idempotency key exists', async () => {
|
|
const existing = {
|
|
id: 'existing-1',
|
|
channel: 'SMS',
|
|
recipient: '09121111111',
|
|
event: 'OTP',
|
|
content: '12345',
|
|
idempotencyKey: 'idemp-2',
|
|
status: 'SENT',
|
|
};
|
|
prisma.notificationQueue.findUnique.mockResolvedValueOnce(existing);
|
|
|
|
const res = await service.enqueue({
|
|
channel: 'SMS',
|
|
recipient: '09121111111',
|
|
event: 'OTP',
|
|
content: '12345',
|
|
idempotencyKey: 'idemp-2',
|
|
});
|
|
|
|
expect(prisma.notificationQueue.create).not.toHaveBeenCalled();
|
|
expect(res.id).toBe('existing-1');
|
|
});
|
|
|
|
it('should process email queue item and mark as SENT', async () => {
|
|
const item = {
|
|
id: 'item-1',
|
|
channel: 'EMAIL',
|
|
recipient: 'user@example.com',
|
|
subject: 'Test Subject',
|
|
content: 'Test Body',
|
|
payload: {},
|
|
status: 'QUEUED',
|
|
attempts: 0,
|
|
maxAttempts: 3,
|
|
};
|
|
prisma.notificationQueue.findUnique.mockResolvedValueOnce(item);
|
|
prisma.notificationQueue.update.mockResolvedValue({});
|
|
|
|
await service.processItem('item-1');
|
|
|
|
expect(emailService.sendMail).toHaveBeenCalledWith({
|
|
to: 'user@example.com',
|
|
subject: 'Test Subject',
|
|
text: 'Test Body',
|
|
html: expect.stringContaining('Test Body'),
|
|
});
|
|
expect(prisma.notificationQueue.update).toHaveBeenCalledWith({
|
|
where: { id: 'item-1' },
|
|
data: expect.objectContaining({ status: 'SENT' }),
|
|
});
|
|
});
|
|
});
|