import { describe, expect, it } from 'vitest';

import type { DataSource } from 'typeorm';

import { RaceRegistrationOrmMapper } from '../mappers/RaceRegistrationOrmMapper';
import { TypeOrmRaceRegistrationRepository } from './TypeOrmRaceRegistrationRepository';

describe('TypeOrmRaceRegistrationRepository', () => {
  it('constructor requires injected mapper (no internal mapper instantiation)', () => {
    const dataSource = {} as unknown as DataSource;
    const mapper = {} as unknown as RaceRegistrationOrmMapper;

    const repo = new TypeOrmRaceRegistrationRepository(dataSource, mapper);

    expect(repo).toBeInstanceOf(TypeOrmRaceRegistrationRepository);
    expect((repo as unknown as { mapper: unknown }).mapper).toBe(mapper);
  });

  it('works with mocked TypeORM DataSource (no DB required)', async () => {
    const count = async () => 0;

    const dataSource = {
      getRepository: () => ({ count }),
    } as unknown as DataSource;

    const mapper = {
      toDomain: () => {
        throw new Error('should-not-be-called');
      },
    } as unknown as RaceRegistrationOrmMapper;

    const repo = new TypeOrmRaceRegistrationRepository(dataSource, mapper);

    await expect(repo.getRegistrationCount('race-1')).resolves.toBe(0);
  });
});