import * as fs from 'node:fs';
import * as path from 'node:path';
import { describe, expect, it, vi } from 'vitest';

import { TypeOrmLeagueRepository } from './TypeOrmLeagueRepository';

describe('TypeOrmLeagueRepository', () => {
  it('does not construct its own mapper dependencies', () => {
    const sourcePath = path.resolve(__dirname, 'TypeOrmLeagueRepository.ts');
    const source = fs.readFileSync(sourcePath, 'utf8');

    expect(source).not.toMatch(/new\s+LeagueOrmMapper\s*\(/);
    expect(source).not.toMatch(/=\s*new\s+LeagueOrmMapper\s*\(/);
  });

  it('requires mapper injection via constructor (no default mapper)', () => {
    expect(TypeOrmLeagueRepository.length).toBe(2);
  });

  it('uses the injected mapper at runtime (DB-free)', async () => {
    const ormRepo = {
      findOne: vi.fn().mockResolvedValue({ id: 'l1' }),
    };

    const dataSource = {
      getRepository: vi.fn().mockReturnValue(ormRepo),
    };

    const mapper = {
      toDomain: vi.fn().mockReturnValue({ id: 'domain' }),
      toOrmEntity: vi.fn(),
    };

    const repo = new TypeOrmLeagueRepository(dataSource as any, mapper as any);

    const league = await repo.findById('l1');

    expect(dataSource.getRepository).toHaveBeenCalledTimes(1);
    expect(ormRepo.findOne).toHaveBeenCalledTimes(1);
    expect(mapper.toDomain).toHaveBeenCalledTimes(1);
    expect(league).toEqual({ id: 'domain' });
  });
});