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

import { TypeOrmSeasonRepository } from './TypeOrmSeasonRepository';

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

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

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

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

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

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

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

    const season = await repo.findById('s1');

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