import { describe, it, expect, beforeEach, vi } from 'vitest'; import { DashboardTestContext } from '../DashboardTestContext'; import { DriverNotFoundError } from '../../../../core/dashboard/domain/errors/DriverNotFoundError'; import { ValidationError } from '../../../../core/shared/errors/ValidationError'; describe('Dashboard Error Handling Integration', () => { const context = DashboardTestContext.create(); beforeEach(() => { context.clear(); }); describe('Driver Not Found Errors', () => { it('should throw DriverNotFoundError when driver does not exist', async () => { const driverId = 'non-existent-driver-id'; await expect(context.getDashboardUseCase.execute({ driverId })) .rejects.toThrow(DriverNotFoundError); expect(context.eventPublisher.getDashboardAccessedEventCount()).toBe(0); }); }); describe('Validation Errors', () => { it('should throw ValidationError when driver ID is empty string', async () => { const driverId = ''; await expect(context.getDashboardUseCase.execute({ driverId })) .rejects.toThrow(ValidationError); expect(context.eventPublisher.getDashboardAccessedEventCount()).toBe(0); }); }); describe('Repository Error Handling', () => { it('should handle driver repository query error', async () => { const driverId = 'driver-repo-error'; const spy = vi.spyOn(context.driverRepository, 'findDriverById').mockRejectedValue(new Error('Driver repo failed')); await expect(context.getDashboardUseCase.execute({ driverId })) .rejects.toThrow('Driver repo failed'); expect(context.eventPublisher.getDashboardAccessedEventCount()).toBe(0); spy.mockRestore(); }); }); describe('Event Publisher Error Handling', () => { it('should handle event publisher error gracefully', async () => { const driverId = 'driver-pub-error'; context.driverRepository.addDriver({ id: driverId, name: 'Pub Error Driver', rating: 1000, rank: 1, starts: 0, wins: 0, podiums: 0, leagues: 0, }); const spy = vi.spyOn(context.eventPublisher, 'publishDashboardAccessed').mockRejectedValue(new Error('Publisher failed')); const result = await context.getDashboardUseCase.execute({ driverId }); expect(result).toBeDefined(); expect(result.driver.id).toBe(driverId); expect(context.loggerMock.error).toHaveBeenCalledWith( 'Failed to publish dashboard accessed event', expect.any(Error), expect.objectContaining({ driverId }) ); spy.mockRestore(); }); }); });