import { describe, it, expect, vi } from 'vitest'; import { SubmitProtestDefenseUseCase } from './SubmitProtestDefenseUseCase'; import type { IProtestRepository } from '../../domain/repositories/IProtestRepository'; describe('SubmitProtestDefenseUseCase', () => { it('submits defense successfully', async () => { const mockProtest = { id: 'protest-1', accusedDriverId: 'driver-1', canSubmitDefense: vi.fn().mockReturnValue(true), submitDefense: vi.fn().mockReturnValue({}), }; const mockProtestRepository = { findById: vi.fn().mockResolvedValue(mockProtest), update: vi.fn().mockResolvedValue(undefined), } as unknown as IProtestRepository; const useCase = new SubmitProtestDefenseUseCase(mockProtestRepository); const command = { protestId: 'protest-1', driverId: 'driver-1', statement: 'My defense', videoUrl: 'http://video.com', }; const result = await useCase.execute(command); expect(result.isOk()).toBe(true); expect(result.unwrap()).toEqual({ protestId: 'protest-1' }); expect(mockProtestRepository.findById).toHaveBeenCalledWith('protest-1'); expect(mockProtest.canSubmitDefense).toHaveBeenCalled(); expect(mockProtest.submitDefense).toHaveBeenCalledWith('My defense', 'http://video.com'); expect(mockProtestRepository.update).toHaveBeenCalledWith({}); }); it('returns error when protest not found', async () => { const mockProtestRepository = { findById: vi.fn().mockResolvedValue(null), } as unknown as IProtestRepository; const useCase = new SubmitProtestDefenseUseCase(mockProtestRepository); const command = { protestId: 'protest-1', driverId: 'driver-1', statement: 'My defense', }; const result = await useCase.execute(command); expect(result.isErr()).toBe(true); expect(result.unwrapErr()).toEqual({ code: 'PROTEST_NOT_FOUND' }); }); it('returns error when driver is not the accused', async () => { const mockProtest = { id: 'protest-1', accusedDriverId: 'driver-2', }; const mockProtestRepository = { findById: vi.fn().mockResolvedValue(mockProtest), } as unknown as IProtestRepository; const useCase = new SubmitProtestDefenseUseCase(mockProtestRepository); const command = { protestId: 'protest-1', driverId: 'driver-1', statement: 'My defense', }; const result = await useCase.execute(command); expect(result.isErr()).toBe(true); expect(result.unwrapErr()).toEqual({ code: 'NOT_ACCUSED_DRIVER' }); }); it('returns error when defense cannot be submitted', async () => { const mockProtest = { id: 'protest-1', accusedDriverId: 'driver-1', canSubmitDefense: vi.fn().mockReturnValue(false), }; const mockProtestRepository = { findById: vi.fn().mockResolvedValue(mockProtest), } as unknown as IProtestRepository; const useCase = new SubmitProtestDefenseUseCase(mockProtestRepository); const command = { protestId: 'protest-1', driverId: 'driver-1', statement: 'My defense', }; const result = await useCase.execute(command); expect(result.isErr()).toBe(true); expect(result.unwrapErr()).toEqual({ code: 'DEFENSE_CANNOT_BE_SUBMITTED' }); }); });