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

import type { DataSource } from 'typeorm';

import { TypeOrmLeagueMembershipRepository } from './TypeOrmLeagueMembershipRepository';
import { LeagueMembershipOrmMapper } from '../mappers/LeagueMembershipOrmMapper';

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

    const repo = new TypeOrmLeagueMembershipRepository(dataSource, mapper);

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

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

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

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

    const repo = new TypeOrmLeagueMembershipRepository(dataSource, mapper);

    await expect(repo.getMembership('league-1', 'driver-1')).resolves.toBeNull();
  });
});