import { Pool } from 'pg';
import { runMigrations } from '../../../apps/api/src/migration';

describe('runMigrations (integration)', () => {
  it('applies all pending migrations idempotently against a real database', async () => {
    await runMigrations();
    await runMigrations(); // must be safe to run twice

    const pool = new Pool({ connectionString: process.env.DATABASE_URL });
    try {
      const tables = await pool.query<{ table_name: string }>(
        `select table_name from information_schema.tables where table_schema = 'public'`,
      );
      const names = tables.rows.map((r) => r.table_name);
      for (const expected of [
        'users',
        'user_preferences',
        'trips',
        'trip_settings',
        'trip_members',
        'trip_invitations',
        'travelers',
        'trip_preference_overrides',
      ]) {
        expect(names).toContain(expected);
      }
    } finally {
      await pool.end();
    }
  });
});
