refactor use cases

This commit is contained in:
2025-12-21 01:20:27 +01:00
parent c12656d671
commit 8ecd638396
39 changed files with 2523 additions and 686 deletions

View File

@@ -1,16 +1,27 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import { GetUnreadNotificationsUseCase } from './GetUnreadNotificationsUseCase';
import {
GetUnreadNotificationsUseCase,
type GetUnreadNotificationsInput,
type GetUnreadNotificationsResult,
} from './GetUnreadNotificationsUseCase';
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
import type { Logger } from '@core/shared/application';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { Notification } from '../../domain/entities/Notification';
interface NotificationRepositoryMock {
findUnreadByRecipientId: Mock;
}
interface OutputPortMock extends UseCaseOutputPort<GetUnreadNotificationsResult> {
present: Mock;
}
describe('GetUnreadNotificationsUseCase', () => {
let notificationRepository: NotificationRepositoryMock;
let logger: Logger;
let output: OutputPortMock;
let useCase: GetUnreadNotificationsUseCase;
beforeEach(() => {
@@ -25,8 +36,13 @@ describe('GetUnreadNotificationsUseCase', () => {
error: vi.fn(),
} as unknown as Logger;
output = {
present: vi.fn(),
} as unknown as OutputPortMock;
useCase = new GetUnreadNotificationsUseCase(
notificationRepository as unknown as INotificationRepository,
output,
logger,
);
});
@@ -37,7 +53,7 @@ describe('GetUnreadNotificationsUseCase', () => {
Notification.create({
id: 'n1',
recipientId,
type: 'info',
type: 'system_announcement',
title: 'Test',
body: 'Body',
channel: 'in_app',
@@ -46,19 +62,33 @@ describe('GetUnreadNotificationsUseCase', () => {
notificationRepository.findUnreadByRecipientId.mockResolvedValue(notifications);
const result = await useCase.execute(recipientId);
const input: GetUnreadNotificationsInput = { recipientId };
const result = await useCase.execute(input);
expect(notificationRepository.findUnreadByRecipientId).toHaveBeenCalledWith(recipientId);
expect(result.notifications).toEqual(notifications);
expect(result.totalCount).toBe(1);
expect(result).toBeInstanceOf(Result);
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalledWith({
notifications,
totalCount: 1,
});
});
it('handles repository errors by logging and rethrowing', async () => {
it('handles repository errors by logging and returning error result', async () => {
const recipientId = 'driver-1';
const error = new Error('DB error');
notificationRepository.findUnreadByRecipientId.mockRejectedValue(error);
await expect(useCase.execute(recipientId)).rejects.toThrow('DB error');
const input: GetUnreadNotificationsInput = { 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();
expect(output.present).not.toHaveBeenCalled();
});
});