import { describe, it, expect } from 'vitest';

import { GET as healthGet } from '@/app/api/health/route';
import { GET as readyGet } from '@/app/api/ready/route';

describe('GET /api/health (liveness)', () => {
  it('returns 200 + status=ok regardless of downstream dependency state', async () => {
    const res = await healthGet();
    expect(res.status).toBe(200);
    const body = (await res.json()) as any;
    expect(body.status).toBe('ok');
    expect(body.timestamp).toMatch(/^\d{4}-\d{2}-\d{2}T/);
  });
});

describe('GET /api/ready (readiness)', () => {
  it('returns ready=200 when postgres + redis + storage all answer', async () => {
    // The dev/test environment has all three reachable; this covers the
    // happy path. The degraded path is not exercised here because
    // simulating a down dep without leaking into other tests is awkward;
    // the route's logic is intentionally trivial (Promise.allSettled +
    // every-ok check) and worth covering at the unit level only.
    //
    // `storage` (renamed from `minio`) probes whichever backend the
    // active port is configured for, via getStorageBackend().head().
    const res = await readyGet();
    expect(res.status).toBe(200);
    const body = (await res.json()) as any;
    expect(body.status).toBe('ready');
    expect(body.checks).toEqual({
      postgres: 'ok',
      redis: 'ok',
      storage: 'ok',
    });
  });
});
