import { describe, it, expect, beforeEach, vi, Mock } from 'vitest'; import { GetTotalDriversUseCase, GetTotalDriversInput, GetTotalDriversResult, GetTotalDriversErrorCode, } from './GetTotalDriversUseCase'; import { IDriverRepository } from '../../domain/repositories/IDriverRepository'; import type { UseCaseOutputPort } from '@core/shared/application'; import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode'; describe('GetTotalDriversUseCase', () => { let useCase: GetTotalDriversUseCase; let driverRepository: { findAll: Mock; }; let output: UseCaseOutputPort & { present: Mock }; beforeEach(() => { driverRepository = { findAll: vi.fn(), }; output = { present: vi.fn(), } as unknown as UseCaseOutputPort & { present: Mock }; useCase = new GetTotalDriversUseCase( driverRepository as unknown as IDriverRepository, output, ); }); it('should return total number of drivers', async () => { const drivers = [{ id: '1' }, { id: '2' }]; driverRepository.findAll.mockResolvedValue(drivers); const input: GetTotalDriversInput = {}; const result = await useCase.execute(input); expect(result.isOk()).toBe(true); expect(result.unwrap()).toBeUndefined(); expect(output.present).toHaveBeenCalledTimes(1); expect(output.present).toHaveBeenCalledWith<[{ totalDrivers: number }]>( expect.objectContaining({ totalDrivers: 2 }), ); }); it('should return error on repository failure', async () => { const error = new Error('Repository error'); driverRepository.findAll.mockRejectedValue(error); const input: GetTotalDriversInput = {}; const result = await useCase.execute(input); expect(result.isErr()).toBe(true); const unwrappedError = result.unwrapErr() as ApplicationErrorCode< GetTotalDriversErrorCode, { message: string } >; expect(unwrappedError.code).toBe('REPOSITORY_ERROR'); expect(unwrappedError.details.message).toBe(error.message); expect(output.present).not.toHaveBeenCalled(); }); });