"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const server_js_1 = require("./server.js");
(0, vitest_1.describe)('buildServer', () => {
    let app = null;
    (0, vitest_1.afterEach)(async () => {
        if (app) {
            await app.close();
            app = null;
        }
    });
    (0, vitest_1.it)('should create a Fastify instance', async () => {
        app = await (0, server_js_1.buildServer)();
        (0, vitest_1.expect)(app).toBeDefined();
    });
    (0, vitest_1.it)('should respond to /health with status ok', async () => {
        app = await (0, server_js_1.buildServer)();
        const response = await app.inject({ method: 'GET', url: '/health' });
        (0, vitest_1.expect)(response.statusCode).toBe(200);
        const body = JSON.parse(response.body);
        (0, vitest_1.expect)(body.status).toBe('ok');
        (0, vitest_1.expect)(body.timestamp).toBeDefined();
    });
    (0, vitest_1.it)('should return 403 for non-localhost requests', async () => {
        app = await (0, server_js_1.buildServer)();
        const response = await app.inject({
            method: 'GET',
            url: '/health',
            headers: { host: 'example.com:3001' },
        });
        (0, vitest_1.expect)(response.statusCode).toBe(403);
        const body = JSON.parse(response.body);
        (0, vitest_1.expect)(body.error).toBe('Access restricted to localhost');
    });
    (0, vitest_1.it)('should allow localhost requests', async () => {
        app = await (0, server_js_1.buildServer)();
        const response = await app.inject({
            method: 'GET',
            url: '/health',
            headers: { host: 'localhost:3001' },
        });
        (0, vitest_1.expect)(response.statusCode).toBe(200);
    });
    (0, vitest_1.it)('should allow 127.0.0.1 requests', async () => {
        app = await (0, server_js_1.buildServer)();
        const response = await app.inject({
            method: 'GET',
            url: '/health',
            headers: { host: '127.0.0.1:3001' },
        });
        (0, vitest_1.expect)(response.statusCode).toBe(200);
    });
    (0, vitest_1.it)('should handle errors with structured response', async () => {
        app = await (0, server_js_1.buildServer)();
        // Register a route that throws
        app.get('/test-error', async () => {
            throw new Error('Test error');
        });
        const response = await app.inject({ method: 'GET', url: '/test-error' });
        (0, vitest_1.expect)(response.statusCode).toBe(500);
        const body = JSON.parse(response.body);
        (0, vitest_1.expect)(body.error).toBe('Internal Server Error');
        (0, vitest_1.expect)(body.statusCode).toBe(500);
    });
});
//# sourceMappingURL=server.test.js.map