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

vi.mock('@/lib/prisma', () => ({
    prisma: { $queryRaw: vi.fn() },
}));

import { GET } from '../../../../src/app/api/health/route';
import { prisma } from '@/lib/prisma';

describe('Unit — /api/health/route', () => {
    beforeEach(() => {
        vi.resetAllMocks();
    });

    it('GET returns 200 and database ok when query succeeds', async () => {
        vi.mocked(prisma.$queryRaw).mockResolvedValueOnce({} as never);

        const res = await GET();

        expect(res.status).toBe(200);
        const body = await res.json();
        expect(body.status).toBe('ok');
        expect(body.database).toBe('ok');
        expect(body).toHaveProperty('timestamp');
    });

    it('GET returns 503 and database down when query fails', async () => {
        vi.mocked(prisma.$queryRaw).mockRejectedValueOnce(new Error('db down'));

        const res = await GET();

        expect(res.status).toBe(503);
        const body = await res.json();
        expect(body.status).toBe('degraded');
        expect(body.database).toBe('down');
        expect(body).toHaveProperty('timestamp');
    });
});