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

describe('DatabaseMigrationsService — classroom status normalization', () => {
  it('normalizes legacy persisted statuses to available', async () => {
    const runner = {
      connect: jest.fn(),
      release: jest.fn(),
      getTables: jest.fn().mockResolvedValue([{ name: 'classrooms' }]),
      query: 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 & {
      normalizeClassroomStatuses(): Promise<void>;
    };

    await service.normalizeClassroomStatuses();

    expect(runner.query).toHaveBeenCalledWith(
      expect.stringContaining("status NOT IN ('available', 'maintenance', 'archived')"),
    );
    expect(runner.release).toHaveBeenCalled();
  });
});
