core tests
This commit is contained in:
319
core/notifications/application/ports/NotificationGateway.test.ts
Normal file
319
core/notifications/application/ports/NotificationGateway.test.ts
Normal file
@@ -0,0 +1,319 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { Notification } from '../../domain/entities/Notification';
|
||||
import {
|
||||
NotificationGateway,
|
||||
NotificationGatewayRegistry,
|
||||
NotificationDeliveryResult,
|
||||
} from './NotificationGateway';
|
||||
|
||||
describe('NotificationGateway - Interface Contract', () => {
|
||||
it('NotificationGateway interface defines send method', () => {
|
||||
const mockGateway: NotificationGateway = {
|
||||
send: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
channel: 'in_app',
|
||||
attemptedAt: new Date(),
|
||||
}),
|
||||
supportsChannel: vi.fn().mockReturnValue(true),
|
||||
isConfigured: vi.fn().mockReturnValue(true),
|
||||
getChannel: vi.fn().mockReturnValue('in_app'),
|
||||
};
|
||||
|
||||
const notification = Notification.create({
|
||||
id: 'test-id',
|
||||
recipientId: 'driver-1',
|
||||
type: 'system_announcement',
|
||||
title: 'Test',
|
||||
body: 'Test body',
|
||||
channel: 'in_app',
|
||||
});
|
||||
|
||||
expect(mockGateway.send).toBeDefined();
|
||||
expect(typeof mockGateway.send).toBe('function');
|
||||
});
|
||||
|
||||
it('NotificationGateway interface defines supportsChannel method', () => {
|
||||
const mockGateway: NotificationGateway = {
|
||||
send: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
channel: 'in_app',
|
||||
attemptedAt: new Date(),
|
||||
}),
|
||||
supportsChannel: vi.fn().mockReturnValue(true),
|
||||
isConfigured: vi.fn().mockReturnValue(true),
|
||||
getChannel: vi.fn().mockReturnValue('in_app'),
|
||||
};
|
||||
|
||||
expect(mockGateway.supportsChannel).toBeDefined();
|
||||
expect(typeof mockGateway.supportsChannel).toBe('function');
|
||||
});
|
||||
|
||||
it('NotificationGateway interface defines isConfigured method', () => {
|
||||
const mockGateway: NotificationGateway = {
|
||||
send: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
channel: 'in_app',
|
||||
attemptedAt: new Date(),
|
||||
}),
|
||||
supportsChannel: vi.fn().mockReturnValue(true),
|
||||
isConfigured: vi.fn().mockReturnValue(true),
|
||||
getChannel: vi.fn().mockReturnValue('in_app'),
|
||||
};
|
||||
|
||||
expect(mockGateway.isConfigured).toBeDefined();
|
||||
expect(typeof mockGateway.isConfigured).toBe('function');
|
||||
});
|
||||
|
||||
it('NotificationGateway interface defines getChannel method', () => {
|
||||
const mockGateway: NotificationGateway = {
|
||||
send: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
channel: 'in_app',
|
||||
attemptedAt: new Date(),
|
||||
}),
|
||||
supportsChannel: vi.fn().mockReturnValue(true),
|
||||
isConfigured: vi.fn().mockReturnValue(true),
|
||||
getChannel: vi.fn().mockReturnValue('in_app'),
|
||||
};
|
||||
|
||||
expect(mockGateway.getChannel).toBeDefined();
|
||||
expect(typeof mockGateway.getChannel).toBe('function');
|
||||
});
|
||||
|
||||
it('NotificationDeliveryResult has required properties', () => {
|
||||
const result: NotificationDeliveryResult = {
|
||||
success: true,
|
||||
channel: 'in_app',
|
||||
attemptedAt: new Date(),
|
||||
};
|
||||
|
||||
expect(result).toHaveProperty('success');
|
||||
expect(result).toHaveProperty('channel');
|
||||
expect(result).toHaveProperty('attemptedAt');
|
||||
});
|
||||
|
||||
it('NotificationDeliveryResult can have optional externalId', () => {
|
||||
const result: NotificationDeliveryResult = {
|
||||
success: true,
|
||||
channel: 'email',
|
||||
externalId: 'email-123',
|
||||
attemptedAt: new Date(),
|
||||
};
|
||||
|
||||
expect(result.externalId).toBe('email-123');
|
||||
});
|
||||
|
||||
it('NotificationDeliveryResult can have optional error', () => {
|
||||
const result: NotificationDeliveryResult = {
|
||||
success: false,
|
||||
channel: 'discord',
|
||||
error: 'Failed to send to Discord',
|
||||
attemptedAt: new Date(),
|
||||
};
|
||||
|
||||
expect(result.error).toBe('Failed to send to Discord');
|
||||
});
|
||||
});
|
||||
|
||||
describe('NotificationGatewayRegistry - Interface Contract', () => {
|
||||
it('NotificationGatewayRegistry interface defines register method', () => {
|
||||
const mockRegistry: NotificationGatewayRegistry = {
|
||||
register: vi.fn(),
|
||||
getGateway: vi.fn().mockReturnValue(null),
|
||||
getAllGateways: vi.fn().mockReturnValue([]),
|
||||
send: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
channel: 'in_app',
|
||||
attemptedAt: new Date(),
|
||||
}),
|
||||
};
|
||||
|
||||
expect(mockRegistry.register).toBeDefined();
|
||||
expect(typeof mockRegistry.register).toBe('function');
|
||||
});
|
||||
|
||||
it('NotificationGatewayRegistry interface defines getGateway method', () => {
|
||||
const mockRegistry: NotificationGatewayRegistry = {
|
||||
register: vi.fn(),
|
||||
getGateway: vi.fn().mockReturnValue(null),
|
||||
getAllGateways: vi.fn().mockReturnValue([]),
|
||||
send: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
channel: 'in_app',
|
||||
attemptedAt: new Date(),
|
||||
}),
|
||||
};
|
||||
|
||||
expect(mockRegistry.getGateway).toBeDefined();
|
||||
expect(typeof mockRegistry.getGateway).toBe('function');
|
||||
});
|
||||
|
||||
it('NotificationGatewayRegistry interface defines getAllGateways method', () => {
|
||||
const mockRegistry: NotificationGatewayRegistry = {
|
||||
register: vi.fn(),
|
||||
getGateway: vi.fn().mockReturnValue(null),
|
||||
getAllGateways: vi.fn().mockReturnValue([]),
|
||||
send: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
channel: 'in_app',
|
||||
attemptedAt: new Date(),
|
||||
}),
|
||||
};
|
||||
|
||||
expect(mockRegistry.getAllGateways).toBeDefined();
|
||||
expect(typeof mockRegistry.getAllGateways).toBe('function');
|
||||
});
|
||||
|
||||
it('NotificationGatewayRegistry interface defines send method', () => {
|
||||
const mockRegistry: NotificationGatewayRegistry = {
|
||||
register: vi.fn(),
|
||||
getGateway: vi.fn().mockReturnValue(null),
|
||||
getAllGateways: vi.fn().mockReturnValue([]),
|
||||
send: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
channel: 'in_app',
|
||||
attemptedAt: new Date(),
|
||||
}),
|
||||
};
|
||||
|
||||
expect(mockRegistry.send).toBeDefined();
|
||||
expect(typeof mockRegistry.send).toBe('function');
|
||||
});
|
||||
});
|
||||
|
||||
describe('NotificationGateway - Integration with Notification', () => {
|
||||
it('gateway can send notification and return delivery result', async () => {
|
||||
const mockGateway: NotificationGateway = {
|
||||
send: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
channel: 'in_app',
|
||||
externalId: 'msg-123',
|
||||
attemptedAt: new Date(),
|
||||
}),
|
||||
supportsChannel: vi.fn().mockReturnValue(true),
|
||||
isConfigured: vi.fn().mockReturnValue(true),
|
||||
getChannel: vi.fn().mockReturnValue('in_app'),
|
||||
};
|
||||
|
||||
const notification = Notification.create({
|
||||
id: 'test-id',
|
||||
recipientId: 'driver-1',
|
||||
type: 'system_announcement',
|
||||
title: 'Test',
|
||||
body: 'Test body',
|
||||
channel: 'in_app',
|
||||
});
|
||||
|
||||
const result = await mockGateway.send(notification);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.channel).toBe('in_app');
|
||||
expect(result.externalId).toBe('msg-123');
|
||||
expect(mockGateway.send).toHaveBeenCalledWith(notification);
|
||||
});
|
||||
|
||||
it('gateway can handle failed delivery', async () => {
|
||||
const mockGateway: NotificationGateway = {
|
||||
send: vi.fn().mockResolvedValue({
|
||||
success: false,
|
||||
channel: 'email',
|
||||
error: 'SMTP server unavailable',
|
||||
attemptedAt: new Date(),
|
||||
}),
|
||||
supportsChannel: vi.fn().mockReturnValue(true),
|
||||
isConfigured: vi.fn().mockReturnValue(true),
|
||||
getChannel: vi.fn().mockReturnValue('email'),
|
||||
};
|
||||
|
||||
const notification = Notification.create({
|
||||
id: 'test-id',
|
||||
recipientId: 'driver-1',
|
||||
type: 'race_registration_open',
|
||||
title: 'Test',
|
||||
body: 'Test body',
|
||||
channel: 'email',
|
||||
});
|
||||
|
||||
const result = await mockGateway.send(notification);
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.channel).toBe('email');
|
||||
expect(result.error).toBe('SMTP server unavailable');
|
||||
});
|
||||
});
|
||||
|
||||
describe('NotificationGatewayRegistry - Integration', () => {
|
||||
it('registry can route notification to appropriate gateway', async () => {
|
||||
const inAppGateway: NotificationGateway = {
|
||||
send: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
channel: 'in_app',
|
||||
attemptedAt: new Date(),
|
||||
}),
|
||||
supportsChannel: vi.fn().mockReturnValue(true),
|
||||
isConfigured: vi.fn().mockReturnValue(true),
|
||||
getChannel: vi.fn().mockReturnValue('in_app'),
|
||||
};
|
||||
|
||||
const emailGateway: NotificationGateway = {
|
||||
send: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
channel: 'email',
|
||||
externalId: 'email-456',
|
||||
attemptedAt: new Date(),
|
||||
}),
|
||||
supportsChannel: vi.fn().mockReturnValue(true),
|
||||
isConfigured: vi.fn().mockReturnValue(true),
|
||||
getChannel: vi.fn().mockReturnValue('email'),
|
||||
};
|
||||
|
||||
const mockRegistry: NotificationGatewayRegistry = {
|
||||
register: vi.fn(),
|
||||
getGateway: vi.fn().mockImplementation((channel) => {
|
||||
if (channel === 'in_app') return inAppGateway;
|
||||
if (channel === 'email') return emailGateway;
|
||||
return null;
|
||||
}),
|
||||
getAllGateways: vi.fn().mockReturnValue([inAppGateway, emailGateway]),
|
||||
send: vi.fn().mockImplementation(async (notification) => {
|
||||
const gateway = mockRegistry.getGateway(notification.channel);
|
||||
if (gateway) {
|
||||
return gateway.send(notification);
|
||||
}
|
||||
return {
|
||||
success: false,
|
||||
channel: notification.channel,
|
||||
error: 'No gateway found',
|
||||
attemptedAt: new Date(),
|
||||
};
|
||||
}),
|
||||
};
|
||||
|
||||
const inAppNotification = Notification.create({
|
||||
id: 'test-1',
|
||||
recipientId: 'driver-1',
|
||||
type: 'system_announcement',
|
||||
title: 'Test',
|
||||
body: 'Test body',
|
||||
channel: 'in_app',
|
||||
});
|
||||
|
||||
const emailNotification = Notification.create({
|
||||
id: 'test-2',
|
||||
recipientId: 'driver-1',
|
||||
type: 'race_registration_open',
|
||||
title: 'Test',
|
||||
body: 'Test body',
|
||||
channel: 'email',
|
||||
});
|
||||
|
||||
const inAppResult = await mockRegistry.send(inAppNotification);
|
||||
expect(inAppResult.success).toBe(true);
|
||||
expect(inAppResult.channel).toBe('in_app');
|
||||
|
||||
const emailResult = await mockRegistry.send(emailNotification);
|
||||
expect(emailResult.success).toBe(true);
|
||||
expect(emailResult.channel).toBe('email');
|
||||
expect(emailResult.externalId).toBe('email-456');
|
||||
});
|
||||
});
|
||||
346
core/notifications/application/ports/NotificationService.test.ts
Normal file
346
core/notifications/application/ports/NotificationService.test.ts
Normal file
@@ -0,0 +1,346 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import {
|
||||
NotificationService,
|
||||
SendNotificationCommand,
|
||||
NotificationData,
|
||||
NotificationAction,
|
||||
} from './NotificationService';
|
||||
|
||||
describe('NotificationService - Interface Contract', () => {
|
||||
it('NotificationService interface defines sendNotification method', () => {
|
||||
const mockService: NotificationService = {
|
||||
sendNotification: vi.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
|
||||
expect(mockService.sendNotification).toBeDefined();
|
||||
expect(typeof mockService.sendNotification).toBe('function');
|
||||
});
|
||||
|
||||
it('SendNotificationCommand has required properties', () => {
|
||||
const command: SendNotificationCommand = {
|
||||
recipientId: 'driver-1',
|
||||
type: 'system_announcement',
|
||||
title: 'Test Notification',
|
||||
body: 'This is a test notification',
|
||||
channel: 'in_app',
|
||||
urgency: 'toast',
|
||||
};
|
||||
|
||||
expect(command).toHaveProperty('recipientId');
|
||||
expect(command).toHaveProperty('type');
|
||||
expect(command).toHaveProperty('title');
|
||||
expect(command).toHaveProperty('body');
|
||||
expect(command).toHaveProperty('channel');
|
||||
expect(command).toHaveProperty('urgency');
|
||||
});
|
||||
|
||||
it('SendNotificationCommand can have optional data', () => {
|
||||
const command: SendNotificationCommand = {
|
||||
recipientId: 'driver-1',
|
||||
type: 'race_results_posted',
|
||||
title: 'Race Results',
|
||||
body: 'Your race results are available',
|
||||
channel: 'email',
|
||||
urgency: 'toast',
|
||||
data: {
|
||||
raceEventId: 'event-123',
|
||||
sessionId: 'session-456',
|
||||
position: 5,
|
||||
positionChange: 2,
|
||||
},
|
||||
};
|
||||
|
||||
expect(command.data).toBeDefined();
|
||||
expect(command.data?.raceEventId).toBe('event-123');
|
||||
expect(command.data?.position).toBe(5);
|
||||
});
|
||||
|
||||
it('SendNotificationCommand can have optional actionUrl', () => {
|
||||
const command: SendNotificationCommand = {
|
||||
recipientId: 'driver-1',
|
||||
type: 'protest_vote_required',
|
||||
title: 'Vote Required',
|
||||
body: 'You need to vote on a protest',
|
||||
channel: 'in_app',
|
||||
urgency: 'modal',
|
||||
actionUrl: '/protests/vote/123',
|
||||
};
|
||||
|
||||
expect(command.actionUrl).toBe('/protests/vote/123');
|
||||
});
|
||||
|
||||
it('SendNotificationCommand can have optional actions array', () => {
|
||||
const actions: NotificationAction[] = [
|
||||
{
|
||||
label: 'View Details',
|
||||
type: 'primary',
|
||||
href: '/protests/123',
|
||||
},
|
||||
{
|
||||
label: 'Dismiss',
|
||||
type: 'secondary',
|
||||
actionId: 'dismiss',
|
||||
},
|
||||
];
|
||||
|
||||
const command: SendNotificationCommand = {
|
||||
recipientId: 'driver-1',
|
||||
type: 'protest_filed',
|
||||
title: 'Protest Filed',
|
||||
body: 'A protest has been filed against you',
|
||||
channel: 'in_app',
|
||||
urgency: 'modal',
|
||||
actions,
|
||||
};
|
||||
|
||||
expect(command.actions).toBeDefined();
|
||||
expect(command.actions?.length).toBe(2);
|
||||
expect(command.actions?.[0].label).toBe('View Details');
|
||||
expect(command.actions?.[1].type).toBe('secondary');
|
||||
});
|
||||
|
||||
it('SendNotificationCommand can have optional requiresResponse', () => {
|
||||
const command: SendNotificationCommand = {
|
||||
recipientId: 'driver-1',
|
||||
type: 'protest_vote_required',
|
||||
title: 'Vote Required',
|
||||
body: 'You need to vote on a protest',
|
||||
channel: 'in_app',
|
||||
urgency: 'modal',
|
||||
requiresResponse: true,
|
||||
};
|
||||
|
||||
expect(command.requiresResponse).toBe(true);
|
||||
});
|
||||
|
||||
it('NotificationData can have various optional fields', () => {
|
||||
const data: NotificationData = {
|
||||
raceEventId: 'event-123',
|
||||
sessionId: 'session-456',
|
||||
leagueId: 'league-789',
|
||||
position: 3,
|
||||
positionChange: 1,
|
||||
incidents: 2,
|
||||
provisionalRatingChange: 15,
|
||||
finalRatingChange: 10,
|
||||
hadPenaltiesApplied: true,
|
||||
deadline: new Date('2024-01-01'),
|
||||
protestId: 'protest-999',
|
||||
customField: 'custom value',
|
||||
};
|
||||
|
||||
expect(data.raceEventId).toBe('event-123');
|
||||
expect(data.sessionId).toBe('session-456');
|
||||
expect(data.leagueId).toBe('league-789');
|
||||
expect(data.position).toBe(3);
|
||||
expect(data.positionChange).toBe(1);
|
||||
expect(data.incidents).toBe(2);
|
||||
expect(data.provisionalRatingChange).toBe(15);
|
||||
expect(data.finalRatingChange).toBe(10);
|
||||
expect(data.hadPenaltiesApplied).toBe(true);
|
||||
expect(data.deadline).toBeInstanceOf(Date);
|
||||
expect(data.protestId).toBe('protest-999');
|
||||
expect(data.customField).toBe('custom value');
|
||||
});
|
||||
|
||||
it('NotificationData can have minimal fields', () => {
|
||||
const data: NotificationData = {
|
||||
raceEventId: 'event-123',
|
||||
};
|
||||
|
||||
expect(data.raceEventId).toBe('event-123');
|
||||
});
|
||||
|
||||
it('NotificationAction has required properties', () => {
|
||||
const action: NotificationAction = {
|
||||
label: 'View Details',
|
||||
type: 'primary',
|
||||
};
|
||||
|
||||
expect(action).toHaveProperty('label');
|
||||
expect(action).toHaveProperty('type');
|
||||
});
|
||||
|
||||
it('NotificationAction can have optional href', () => {
|
||||
const action: NotificationAction = {
|
||||
label: 'View Details',
|
||||
type: 'primary',
|
||||
href: '/protests/123',
|
||||
};
|
||||
|
||||
expect(action.href).toBe('/protests/123');
|
||||
});
|
||||
|
||||
it('NotificationAction can have optional actionId', () => {
|
||||
const action: NotificationAction = {
|
||||
label: 'Dismiss',
|
||||
type: 'secondary',
|
||||
actionId: 'dismiss',
|
||||
};
|
||||
|
||||
expect(action.actionId).toBe('dismiss');
|
||||
});
|
||||
|
||||
it('NotificationAction type can be primary, secondary, or danger', () => {
|
||||
const primaryAction: NotificationAction = {
|
||||
label: 'Accept',
|
||||
type: 'primary',
|
||||
};
|
||||
|
||||
const secondaryAction: NotificationAction = {
|
||||
label: 'Cancel',
|
||||
type: 'secondary',
|
||||
};
|
||||
|
||||
const dangerAction: NotificationAction = {
|
||||
label: 'Delete',
|
||||
type: 'danger',
|
||||
};
|
||||
|
||||
expect(primaryAction.type).toBe('primary');
|
||||
expect(secondaryAction.type).toBe('secondary');
|
||||
expect(dangerAction.type).toBe('danger');
|
||||
});
|
||||
});
|
||||
|
||||
describe('NotificationService - Integration', () => {
|
||||
it('service can send notification with all optional fields', async () => {
|
||||
const mockService: NotificationService = {
|
||||
sendNotification: vi.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
|
||||
const command: SendNotificationCommand = {
|
||||
recipientId: 'driver-1',
|
||||
type: 'race_performance_summary',
|
||||
title: 'Performance Summary',
|
||||
body: 'Your performance summary is ready',
|
||||
channel: 'email',
|
||||
urgency: 'toast',
|
||||
data: {
|
||||
raceEventId: 'event-123',
|
||||
sessionId: 'session-456',
|
||||
position: 5,
|
||||
positionChange: 2,
|
||||
incidents: 1,
|
||||
provisionalRatingChange: 10,
|
||||
finalRatingChange: 8,
|
||||
hadPenaltiesApplied: false,
|
||||
},
|
||||
actionUrl: '/performance/summary/123',
|
||||
actions: [
|
||||
{
|
||||
label: 'View Details',
|
||||
type: 'primary',
|
||||
href: '/performance/summary/123',
|
||||
},
|
||||
{
|
||||
label: 'Dismiss',
|
||||
type: 'secondary',
|
||||
actionId: 'dismiss',
|
||||
},
|
||||
],
|
||||
requiresResponse: false,
|
||||
};
|
||||
|
||||
await mockService.sendNotification(command);
|
||||
|
||||
expect(mockService.sendNotification).toHaveBeenCalledWith(command);
|
||||
});
|
||||
|
||||
it('service can send notification with minimal fields', async () => {
|
||||
const mockService: NotificationService = {
|
||||
sendNotification: vi.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
|
||||
const command: SendNotificationCommand = {
|
||||
recipientId: 'driver-1',
|
||||
type: 'system_announcement',
|
||||
title: 'System Update',
|
||||
body: 'System will be down for maintenance',
|
||||
channel: 'in_app',
|
||||
urgency: 'toast',
|
||||
};
|
||||
|
||||
await mockService.sendNotification(command);
|
||||
|
||||
expect(mockService.sendNotification).toHaveBeenCalledWith(command);
|
||||
});
|
||||
|
||||
it('service can send notification with different urgency levels', async () => {
|
||||
const mockService: NotificationService = {
|
||||
sendNotification: vi.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
|
||||
const silentCommand: SendNotificationCommand = {
|
||||
recipientId: 'driver-1',
|
||||
type: 'race_reminder',
|
||||
title: 'Race Reminder',
|
||||
body: 'Your race starts in 30 minutes',
|
||||
channel: 'in_app',
|
||||
urgency: 'silent',
|
||||
};
|
||||
|
||||
const toastCommand: SendNotificationCommand = {
|
||||
recipientId: 'driver-1',
|
||||
type: 'league_invite',
|
||||
title: 'League Invite',
|
||||
body: 'You have been invited to a league',
|
||||
channel: 'in_app',
|
||||
urgency: 'toast',
|
||||
};
|
||||
|
||||
const modalCommand: SendNotificationCommand = {
|
||||
recipientId: 'driver-1',
|
||||
type: 'protest_vote_required',
|
||||
title: 'Vote Required',
|
||||
body: 'You need to vote on a protest',
|
||||
channel: 'in_app',
|
||||
urgency: 'modal',
|
||||
};
|
||||
|
||||
await mockService.sendNotification(silentCommand);
|
||||
await mockService.sendNotification(toastCommand);
|
||||
await mockService.sendNotification(modalCommand);
|
||||
|
||||
expect(mockService.sendNotification).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
it('service can send notification through different channels', async () => {
|
||||
const mockService: NotificationService = {
|
||||
sendNotification: vi.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
|
||||
const inAppCommand: SendNotificationCommand = {
|
||||
recipientId: 'driver-1',
|
||||
type: 'system_announcement',
|
||||
title: 'System Update',
|
||||
body: 'System will be down for maintenance',
|
||||
channel: 'in_app',
|
||||
urgency: 'toast',
|
||||
};
|
||||
|
||||
const emailCommand: SendNotificationCommand = {
|
||||
recipientId: 'driver-1',
|
||||
type: 'race_results_posted',
|
||||
title: 'Race Results',
|
||||
body: 'Your race results are available',
|
||||
channel: 'email',
|
||||
urgency: 'toast',
|
||||
};
|
||||
|
||||
const discordCommand: SendNotificationCommand = {
|
||||
recipientId: 'driver-1',
|
||||
type: 'sponsorship_request_received',
|
||||
title: 'Sponsorship Request',
|
||||
body: 'A sponsor wants to sponsor you',
|
||||
channel: 'discord',
|
||||
urgency: 'toast',
|
||||
};
|
||||
|
||||
await mockService.sendNotification(inAppCommand);
|
||||
await mockService.sendNotification(emailCommand);
|
||||
await mockService.sendNotification(discordCommand);
|
||||
|
||||
expect(mockService.sendNotification).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,143 @@
|
||||
import type { Logger } from '@core/shared/domain/Logger';
|
||||
import { Result } from '@core/shared/domain/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
|
||||
import { Notification } from '../../domain/entities/Notification';
|
||||
import { NotificationRepository } from '../../domain/repositories/NotificationRepository';
|
||||
import {
|
||||
GetAllNotificationsUseCase,
|
||||
type GetAllNotificationsInput,
|
||||
} from './GetAllNotificationsUseCase';
|
||||
|
||||
interface NotificationRepositoryMock {
|
||||
findByRecipientId: Mock;
|
||||
}
|
||||
|
||||
describe('GetAllNotificationsUseCase', () => {
|
||||
let notificationRepository: NotificationRepositoryMock;
|
||||
let logger: Logger;
|
||||
let useCase: GetAllNotificationsUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
notificationRepository = {
|
||||
findByRecipientId: vi.fn(),
|
||||
};
|
||||
|
||||
logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
|
||||
useCase = new GetAllNotificationsUseCase(
|
||||
notificationRepository as unknown as NotificationRepository,
|
||||
logger,
|
||||
);
|
||||
});
|
||||
|
||||
it('returns all notifications and total count for recipient', async () => {
|
||||
const recipientId = 'driver-1';
|
||||
const notifications: Notification[] = [
|
||||
Notification.create({
|
||||
id: 'n1',
|
||||
recipientId,
|
||||
type: 'system_announcement',
|
||||
title: 'Test 1',
|
||||
body: 'Body 1',
|
||||
channel: 'in_app',
|
||||
}),
|
||||
Notification.create({
|
||||
id: 'n2',
|
||||
recipientId,
|
||||
type: 'race_registration_open',
|
||||
title: 'Test 2',
|
||||
body: 'Body 2',
|
||||
channel: 'email',
|
||||
}),
|
||||
];
|
||||
|
||||
notificationRepository.findByRecipientId.mockResolvedValue(notifications);
|
||||
|
||||
const input: GetAllNotificationsInput = { recipientId };
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(notificationRepository.findByRecipientId).toHaveBeenCalledWith(recipientId);
|
||||
expect(result).toBeInstanceOf(Result);
|
||||
expect(result.isOk()).toBe(true);
|
||||
|
||||
const successResult = result.unwrap();
|
||||
expect(successResult.notifications).toEqual(notifications);
|
||||
expect(successResult.totalCount).toBe(2);
|
||||
});
|
||||
|
||||
it('returns empty array when no notifications exist', async () => {
|
||||
const recipientId = 'driver-1';
|
||||
notificationRepository.findByRecipientId.mockResolvedValue([]);
|
||||
|
||||
const input: GetAllNotificationsInput = { recipientId };
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(notificationRepository.findByRecipientId).toHaveBeenCalledWith(recipientId);
|
||||
expect(result.isOk()).toBe(true);
|
||||
|
||||
const successResult = result.unwrap();
|
||||
expect(successResult.notifications).toEqual([]);
|
||||
expect(successResult.totalCount).toBe(0);
|
||||
});
|
||||
|
||||
it('handles repository errors by logging and returning error result', async () => {
|
||||
const recipientId = 'driver-1';
|
||||
const error = new Error('DB error');
|
||||
notificationRepository.findByRecipientId.mockRejectedValue(error);
|
||||
|
||||
const input: GetAllNotificationsInput = { recipientId };
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<'REPOSITORY_ERROR', { message: string }>;
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details.message).toBe('DB error');
|
||||
expect((logger.error as unknown as Mock)).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('logs debug message when starting execution', async () => {
|
||||
const recipientId = 'driver-1';
|
||||
notificationRepository.findByRecipientId.mockResolvedValue([]);
|
||||
|
||||
const input: GetAllNotificationsInput = { recipientId };
|
||||
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(logger.debug).toHaveBeenCalledWith(
|
||||
`Attempting to retrieve all notifications for recipient ID: ${recipientId}`,
|
||||
);
|
||||
});
|
||||
|
||||
it('logs info message on successful retrieval', async () => {
|
||||
const recipientId = 'driver-1';
|
||||
const notifications: Notification[] = [
|
||||
Notification.create({
|
||||
id: 'n1',
|
||||
recipientId,
|
||||
type: 'system_announcement',
|
||||
title: 'Test',
|
||||
body: 'Body',
|
||||
channel: 'in_app',
|
||||
}),
|
||||
];
|
||||
|
||||
notificationRepository.findByRecipientId.mockResolvedValue(notifications);
|
||||
|
||||
const input: GetAllNotificationsInput = { recipientId };
|
||||
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(logger.info).toHaveBeenCalledWith(
|
||||
`Successfully retrieved 1 notifications for recipient ID: ${recipientId}`,
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user