refactor use cases

This commit is contained in:
2026-01-08 15:34:51 +01:00
parent d984ab24a8
commit 52e9a2f6a7
362 changed files with 5192 additions and 8409 deletions

View File

@@ -1,5 +1,5 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import type { Logger } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
@@ -19,17 +19,11 @@ vi.mock('uuid', () => ({
v4: () => 'notif-1',
}));
interface TestOutputPort extends UseCaseOutputPort<SendNotificationResult> {
present: Mock;
result?: SendNotificationResult;
}
describe('SendNotificationUseCase', () => {
let notificationRepository: { create: Mock };
let preferenceRepository: { getOrCreateDefault: Mock };
let gatewayRegistry: { send: Mock };
let logger: Logger;
let output: TestOutputPort;
let useCase: SendNotificationUseCase;
beforeEach(() => {
@@ -52,17 +46,10 @@ describe('SendNotificationUseCase', () => {
error: vi.fn(),
} as unknown as Logger;
output = {
present: vi.fn((result: SendNotificationResult) => {
output.result = result;
}),
} as unknown as TestOutputPort;
useCase = new SendNotificationUseCase(
notificationRepository as unknown as INotificationRepository,
preferenceRepository as unknown as INotificationPreferenceRepository,
gatewayRegistry as unknown as NotificationGatewayRegistry,
output,
logger,
);
});
@@ -92,10 +79,10 @@ describe('SendNotificationUseCase', () => {
expect(notificationRepository.create).toHaveBeenCalledTimes(1);
expect(gatewayRegistry.send).not.toHaveBeenCalled();
expect(output.present).toHaveBeenCalledTimes(1);
expect(output.result?.deliveryResults).toEqual([]);
expect(output.result?.notification.channel).toBe('in_app');
expect(output.result?.notification.status).toBe('dismissed');
const successResult = result.unwrap();
expect(successResult.deliveryResults).toEqual([]);
expect(successResult.notification.channel).toBe('in_app');
expect(successResult.notification.status).toBe('dismissed');
});
it('ensures in_app is used and sends external channels when enabled', async () => {
@@ -128,11 +115,11 @@ describe('SendNotificationUseCase', () => {
expect(notificationRepository.create).toHaveBeenCalledTimes(1);
expect(gatewayRegistry.send).toHaveBeenCalledTimes(1);
expect(output.present).toHaveBeenCalledTimes(1);
expect(output.result?.notification.channel).toBe('in_app');
expect(output.result?.deliveryResults.length).toBe(2);
const successResult = result.unwrap();
expect(successResult.notification.channel).toBe('in_app');
expect(successResult.deliveryResults.length).toBe(2);
const channels = output.result!.deliveryResults.map(r => r.channel).sort();
const channels = successResult.deliveryResults.map(r => r.channel).sort();
expect(channels).toEqual(['email', 'in_app']);
});
@@ -158,8 +145,9 @@ describe('SendNotificationUseCase', () => {
expect(notificationRepository.create).toHaveBeenCalledTimes(1);
expect(gatewayRegistry.send).not.toHaveBeenCalled();
expect(output.result?.deliveryResults.length).toBe(1);
expect(output.result?.deliveryResults[0]?.channel).toBe('in_app');
const successResult = result.unwrap();
expect(successResult.deliveryResults.length).toBe(1);
expect(successResult.deliveryResults[0]?.channel).toBe('in_app');
});
it('returns REPOSITORY_ERROR when preference repository throws', async () => {
@@ -172,13 +160,12 @@ describe('SendNotificationUseCase', () => {
body: 'World',
};
const result: Result<void, ApplicationErrorCode<SendNotificationErrorCode, { message: string }>> =
const result: Result<SendNotificationResult, ApplicationErrorCode<SendNotificationErrorCode, { message: string }>> =
await useCase.execute(command);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr();
expect(err.code).toBe('REPOSITORY_ERROR');
expect(err.details?.message).toBe('DB error');
expect(output.present).not.toHaveBeenCalled();
});
});
});