test(backend): add diagnostics route integration tests

- 3 tests: GET /diagnostics/flags, GET /diagnostics/telemetry, POST /diagnostics/telemetry/flush
- All tests pass
This commit is contained in:
saravanakumardb1 2026-03-20 19:28:09 -07:00
parent d1f2587cc3
commit 95c2f9426a

View File

@ -0,0 +1,45 @@
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import Fastify from 'fastify';
import type { FastifyInstance } from 'fastify';
import { getAllFlags } from './lib/feature-flags.js';
import { getBufferedEvents, flushEvents } from './lib/telemetry.js';
let app: FastifyInstance;
beforeAll(async () => {
app = Fastify({ logger: false });
app.get('/api/diagnostics/flags', async () => getAllFlags());
app.get('/api/diagnostics/telemetry', async () => ({ events: getBufferedEvents() }));
app.post('/api/diagnostics/telemetry/flush', async () => ({ flushed: flushEvents().length }));
await app.ready();
});
afterAll(async () => {
await app.close();
});
describe('diagnostics routes', () => {
it('GET /api/diagnostics/flags returns feature flags', async () => {
const res = await app.inject({ method: 'GET', url: '/api/diagnostics/flags' });
expect(res.statusCode).toBe(200);
const body = res.json();
expect(typeof body).toBe('object');
expect(body).not.toBeNull();
});
it('GET /api/diagnostics/telemetry returns buffered events', async () => {
const res = await app.inject({ method: 'GET', url: '/api/diagnostics/telemetry' });
expect(res.statusCode).toBe(200);
const body = res.json();
expect(body).toHaveProperty('events');
expect(Array.isArray(body.events)).toBe(true);
});
it('POST /api/diagnostics/telemetry/flush returns flushed count', async () => {
const res = await app.inject({ method: 'POST', url: '/api/diagnostics/telemetry/flush' });
expect(res.statusCode).toBe(200);
const body = res.json();
expect(body).toHaveProperty('flushed');
expect(typeof body.flushed).toBe('number');
});
});