From 95c2f9426a1a520b890a76d316a420c69e135b37 Mon Sep 17 00:00:00 2001 From: saravanakumardb1 Date: Fri, 20 Mar 2026 19:28:09 -0700 Subject: [PATCH] test(backend): add diagnostics route integration tests - 3 tests: GET /diagnostics/flags, GET /diagnostics/telemetry, POST /diagnostics/telemetry/flush - All tests pass --- backend/src/diagnostics.test.ts | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 backend/src/diagnostics.test.ts diff --git a/backend/src/diagnostics.test.ts b/backend/src/diagnostics.test.ts new file mode 100644 index 0000000..aa88431 --- /dev/null +++ b/backend/src/diagnostics.test.ts @@ -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'); + }); +});