import { DataSource } from 'typeorm';
import { AddAiChat1784780000000 } from '../migrations/1784780000000-AddAiChat';
import { EnhanceAiChatForAntDesignX1784860000000 } from '../migrations/1784860000000-EnhanceAiChatForAntDesignX';

describe('EnhanceAiChatForAntDesignX1784860000000', () => {
  let dataSource: DataSource;

  beforeEach(async () => {
    dataSource = new DataSource({
      type: 'better-sqlite3',
      database: ':memory:',
      migrations: [AddAiChat1784780000000, EnhanceAiChatForAntDesignX1784860000000],
    });
    await dataSource.initialize();
    await dataSource.query(
      'CREATE TABLE users (id integer PRIMARY KEY AUTOINCREMENT, username varchar(100) NOT NULL)',
    );
    await dataSource.query(
      'CREATE TABLE ai_config (id integer PRIMARY KEY AUTOINCREMENT, singleton_key varchar(20) NOT NULL)',
    );
  });

  afterEach(async () => {
    if (dataSource.isInitialized) await dataSource.destroy();
  });

  it('adds Ant Design X chat fields and attachment relations', async () => {
    await dataSource.runMigrations();
    const runner = dataSource.createQueryRunner();
    for (const table of ['ai_attachments', 'ai_message_attachments']) {
      expect(await runner.hasTable(table)).toBe(true);
    }
    expect(await runner.hasColumn('ai_config', 'supports_vision')).toBe(true);
    expect(await runner.hasColumn('ai_conversations', 'locked_skill_key')).toBe(true);
    expect(await runner.hasColumn('ai_messages', 'feedback')).toBe(true);
    expect(await runner.hasColumn('ai_tool_runs', 'skill_key')).toBe(true);
    await runner.release();
  });
});
