/**
 * Test A — Gateway boot succeeds when federated services are up.
 *
 * Prereq: docker compose -f docker-compose.federated.yml --profile federated up -d
 * Run:    FEDERATED_INTEGRATION=1 pnpm --filter @mosaicstack/gateway test src/__tests__/integration/federated-boot.success.integration.test.ts
 *
 * Skipped when FEDERATED_INTEGRATION !== '1'.
 */

import postgres from 'postgres';
import { afterAll, describe, expect, it } from 'vitest';
import { detectAndAssertTier } from '@mosaicstack/storage';

const run = process.env['FEDERATED_INTEGRATION'] === '1';

const PG_URL = 'postgresql://mosaic:mosaic@localhost:5433/mosaic';
const VALKEY_URL = 'redis://localhost:6380';

const federatedConfig = {
  tier: 'federated' as const,
  storage: {
    type: 'postgres' as const,
    url: PG_URL,
    enableVector: true,
  },
  queue: {
    type: 'bullmq',
    url: VALKEY_URL,
  },
};

describe.skipIf(!run)('federated boot — success path', () => {
  let sql: ReturnType<typeof postgres> | undefined;

  afterAll(async () => {
    if (sql) {
      await sql.end({ timeout: 2 }).catch(() => {});
    }
  });

  it('detectAndAssertTier resolves without throwing when federated services are up', async () => {
    await expect(detectAndAssertTier(federatedConfig)).resolves.toBeUndefined();
  }, 10_000);

  it('pgvector extension is registered (pg_extension row exists)', async () => {
    sql = postgres(PG_URL, { max: 1, connect_timeout: 5, idle_timeout: 5 });
    const rows = await sql`SELECT * FROM pg_extension WHERE extname = 'vector'`;
    expect(rows).toHaveLength(1);
  }, 10_000);
});
