import { describe, it, expect } from 'vitest';
import Fastify from 'fastify';
import { serializerCompiler, validatorCompiler } from 'fastify-type-provider-zod';
import healthRoutes from '../../../src/modules/health/health.routes.js';

describe('Health Routes', () => {
  async function buildTestApp() {
    const app = Fastify({ logger: false });
    app.setValidatorCompiler(validatorCompiler);
    app.setSerializerCompiler(serializerCompiler);
    await app.register(healthRoutes);
    return app;
  }

  it('GET /api/v1/health returns 200 with status ok', async () => {
    const app = await buildTestApp();

    const response = await app.inject({
      method: 'GET',
      url: '/api/v1/health',
    });

    expect(response.statusCode).toBe(200);
    const body = response.json();
    expect(body).toMatchObject({
      status: 'ok',
      version: expect.any(String),
      uptime: expect.any(Number),
    });
  });

  it('GET /api/v1/health returns increasing uptime', async () => {
    const app = await buildTestApp();

    const first = await app.inject({ method: 'GET', url: '/api/v1/health' });
    const second = await app.inject({ method: 'GET', url: '/api/v1/health' });

    expect(second.json().uptime).toBeGreaterThanOrEqual(first.json().uptime);
  });
});
