adapter tests
Some checks failed
CI / lint-typecheck (pull_request) Failing after 4m51s
CI / tests (pull_request) Has been skipped
CI / contract-tests (pull_request) Has been skipped
CI / e2e-tests (pull_request) Has been skipped
CI / comment-pr (pull_request) Has been skipped
CI / commit-types (pull_request) Has been skipped
Some checks failed
CI / lint-typecheck (pull_request) Failing after 4m51s
CI / tests (pull_request) Has been skipped
CI / contract-tests (pull_request) Has been skipped
CI / e2e-tests (pull_request) Has been skipped
CI / comment-pr (pull_request) Has been skipped
CI / commit-types (pull_request) Has been skipped
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { NotificationGatewayRegistry } from './NotificationGatewayRegistry';
|
||||
import { Notification } from '@core/notifications/domain/entities/Notification';
|
||||
import type { NotificationGateway, NotificationDeliveryResult } from '@core/notifications/application/ports/NotificationGateway';
|
||||
import type { NotificationChannel } from '@core/notifications/domain/types/NotificationTypes';
|
||||
|
||||
describe('NotificationGatewayRegistry', () => {
|
||||
let registry: NotificationGatewayRegistry;
|
||||
let mockGateway: NotificationGateway;
|
||||
|
||||
beforeEach(() => {
|
||||
mockGateway = {
|
||||
send: vi.fn(),
|
||||
supportsChannel: vi.fn().mockReturnValue(true),
|
||||
isConfigured: vi.fn().mockReturnValue(true),
|
||||
getChannel: vi.fn().mockReturnValue('email'),
|
||||
};
|
||||
registry = new NotificationGatewayRegistry([mockGateway]);
|
||||
});
|
||||
|
||||
const createNotification = (overrides: any = {}) => {
|
||||
return Notification.create({
|
||||
id: 'notif-123',
|
||||
recipientId: 'driver-456',
|
||||
type: 'protest_filed',
|
||||
title: 'New Protest',
|
||||
body: 'A new protest has been filed against you.',
|
||||
channel: 'email',
|
||||
...overrides,
|
||||
});
|
||||
};
|
||||
|
||||
describe('register and get', () => {
|
||||
it('should register and retrieve a gateway', () => {
|
||||
const discordGateway = {
|
||||
...mockGateway,
|
||||
getChannel: vi.fn().mockReturnValue('discord'),
|
||||
} as any;
|
||||
|
||||
registry.register(discordGateway);
|
||||
expect(registry.getGateway('discord')).toBe(discordGateway);
|
||||
});
|
||||
|
||||
it('should return null for unregistered channel', () => {
|
||||
expect(registry.getGateway('discord')).toBeNull();
|
||||
});
|
||||
|
||||
it('should return all registered gateways', () => {
|
||||
expect(registry.getAllGateways()).toHaveLength(1);
|
||||
expect(registry.getAllGateways()[0]).toBe(mockGateway);
|
||||
});
|
||||
});
|
||||
|
||||
describe('send', () => {
|
||||
it('should route notification to the correct gateway', async () => {
|
||||
// Given
|
||||
const notification = createNotification();
|
||||
const expectedResult: NotificationDeliveryResult = {
|
||||
success: true,
|
||||
channel: 'email',
|
||||
externalId: 'ext-123',
|
||||
attemptedAt: new Date(),
|
||||
};
|
||||
vi.mocked(mockGateway.send).mockResolvedValue(expectedResult);
|
||||
|
||||
// When
|
||||
const result = await registry.send(notification);
|
||||
|
||||
// Then
|
||||
expect(mockGateway.send).toHaveBeenCalledWith(notification);
|
||||
expect(result).toBe(expectedResult);
|
||||
});
|
||||
|
||||
it('should return failure if no gateway is registered for channel', async () => {
|
||||
// Given
|
||||
const notification = createNotification({ channel: 'discord' });
|
||||
|
||||
// When
|
||||
const result = await registry.send(notification);
|
||||
|
||||
// Then
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('No gateway registered for channel: discord');
|
||||
});
|
||||
|
||||
it('should return failure if gateway is not configured', async () => {
|
||||
// Given
|
||||
const notification = createNotification();
|
||||
vi.mocked(mockGateway.isConfigured).mockReturnValue(false);
|
||||
|
||||
// When
|
||||
const result = await registry.send(notification);
|
||||
|
||||
// Then
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('Gateway for channel email is not configured');
|
||||
});
|
||||
|
||||
it('should catch and return errors from gateway.send', async () => {
|
||||
// Given
|
||||
const notification = createNotification();
|
||||
vi.mocked(mockGateway.send).mockRejectedValue(new Error('Network error'));
|
||||
|
||||
// When
|
||||
const result = await registry.send(notification);
|
||||
|
||||
// Then
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe('Network error');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user