import { getMetadataArgsStorage } from 'typeorm';
import { Player } from '../../players/entities/player.entity';
import { AddPlayerLookupIndexes1785517200000 } from './1785517200000-AddPlayerLookupIndexes';

describe('AddPlayerLookupIndexes1785517200000', () => {
  it('adds reversible indexes for team and user foreign-key lookups', async () => {
    const queryRunner = { query: jest.fn() } as any;
    const migration = new AddPlayerLookupIndexes1785517200000();

    await migration.up(queryRunner);
    expect(queryRunner.query.mock.calls.map(([sql]) => sql)).toEqual([
      'CREATE INDEX "IDX_player_team_id" ON "player" ("teamId")',
      'CREATE INDEX "IDX_player_user_id" ON "player" ("userId")',
    ]);

    queryRunner.query.mockClear();
    await migration.down(queryRunner);
    expect(queryRunner.query.mock.calls.map(([sql]) => sql)).toEqual([
      'DROP INDEX "IDX_player_user_id"',
      'DROP INDEX "IDX_player_team_id"',
    ]);
  });

  it('keeps entity index metadata aligned with the migration', () => {
    const playerIndexes = getMetadataArgsStorage()
      .indices.filter((index) => index.target === Player)
      .map((index) => index.name);

    expect(playerIndexes).toEqual(
      expect.arrayContaining(['IDX_player_team_id', 'IDX_player_user_id']),
    );
  });
});
