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

import type { DataSource } from 'typeorm';

import { ResultOrmMapper } from '../mappers/ResultOrmMapper';
import { TypeOrmResultRepository } from './TypeOrmResultRepository';

describe('TypeOrmResultRepository', () => {
  it('requires an injected mapper (does not construct one internally)', () => {
    const dataSource = {} as DataSource;
    const mapper = new ResultOrmMapper();

    expect(() => new TypeOrmResultRepository(dataSource, mapper)).not.toThrow();
  });

  it('uses TypeORM DataSource.getRepository (DB-free mocked repository)', async () => {
    const mapper = new ResultOrmMapper();

    const repoMock = {
      findOne: vi.fn(async () => null),
    };

    const dataSource = {
      getRepository: vi.fn(() => repoMock),
    } as unknown as DataSource;

    const repo = new TypeOrmResultRepository(dataSource, mapper);

    await repo.findById('result-1');

    expect(dataSource.getRepository).toHaveBeenCalled();
    expect(repoMock.findOne).toHaveBeenCalled();
  });
});