import { describe, it, expect, vi } from 'vitest';
import { WithdrawFromRaceUseCase } from './WithdrawFromRaceUseCase';
import type { IRaceRegistrationRepository } from '../../domain/repositories/IRaceRegistrationRepository';

describe('WithdrawFromRaceUseCase', () => {
  it('withdraws from race successfully', async () => {
    const mockRegistrationRepository = {
      withdraw: vi.fn().mockResolvedValue(undefined),
    } as unknown as IRaceRegistrationRepository;

    const useCase = new WithdrawFromRaceUseCase(mockRegistrationRepository);

    const command = {
      raceId: 'race-1',
      driverId: 'driver-1',
    };

    const result = await useCase.execute(command);

    expect(result.isOk()).toBe(true);
    expect(mockRegistrationRepository.withdraw).toHaveBeenCalledWith('race-1', 'driver-1');
  });

  it('returns error when not registered', async () => {
    const mockRegistrationRepository = {
      withdraw: vi.fn().mockRejectedValue(new Error('not registered')),
    } as unknown as IRaceRegistrationRepository;

    const useCase = new WithdrawFromRaceUseCase(mockRegistrationRepository);

    const command = {
      raceId: 'race-1',
      driverId: 'driver-1',
    };

    const result = await useCase.execute(command);

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({ code: 'NOT_REGISTERED' });
  });
});