import { Test, TestingModule } from '@nestjs/testing';
import { getDataSourceToken } from '@nestjs/typeorm';
import { DatabaseMigrationsService } from './database-migrations.service';

describe('DatabaseMigrationsService — room gender cleanup', () => {
  it('drops the retired rooms.gender column', async () => {
    const runner = {
      connect: jest.fn(),
      release: jest.fn(),
      getTables: jest.fn().mockResolvedValue([{ name: 'rooms' }]),
      getTable: jest.fn().mockResolvedValue({
        name: 'rooms',
        columns: [{ name: 'id' }, { name: 'gender' }],
      }),
      dropColumn: jest.fn().mockResolvedValue(undefined),
    };
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        DatabaseMigrationsService,
        {
          provide: getDataSourceToken(),
          useValue: {
            options: { type: 'better-sqlite3' },
            createQueryRunner: jest.fn().mockReturnValue(runner),
          },
        },
      ],
    }).compile();
    const service = module.get(DatabaseMigrationsService) as DatabaseMigrationsService & {
      removeUnusedRoomColumns(): Promise<void>;
    };

    await service.removeUnusedRoomColumns();

    expect(runner.dropColumn).toHaveBeenCalledWith('rooms', 'gender');
    expect(runner.release).toHaveBeenCalled();
  });
});
