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}`, ); }); });