|
| 1 | +/** |
| 2 | + * Integration tests for /api/platform/* — read-only telemetry endpoints. |
| 3 | + * |
| 4 | + * Uses Fastify inject (no real HTTP) with MockOtaAdapter so tests don't |
| 5 | + * pull in the live Duffel client at boot. |
| 6 | + */ |
| 7 | + |
| 8 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 9 | +import type { FastifyInstance } from 'fastify'; |
| 10 | +import { MockOtaAdapter } from '../mock-ota-adapter.js'; |
| 11 | +import { buildApp } from '../server.js'; |
| 12 | + |
| 13 | +let app: FastifyInstance; |
| 14 | + |
| 15 | +beforeAll(async () => { |
| 16 | + app = await buildApp({ |
| 17 | + adapter: new MockOtaAdapter(), |
| 18 | + initResolver: false, |
| 19 | + // The platform routes opt out of rate limiting via per-route config; |
| 20 | + // this guard makes sure tests don't accidentally trip the global cap. |
| 21 | + security: { rateLimit: false, helmet: false, cors: false }, |
| 22 | + }); |
| 23 | + await app.ready(); |
| 24 | +}); |
| 25 | + |
| 26 | +afterAll(async () => { |
| 27 | + await app.close(); |
| 28 | +}); |
| 29 | + |
| 30 | +describe('GET /api/platform/agents', () => { |
| 31 | + it('returns the discovered agent list with domain rollups', async () => { |
| 32 | + const res = await app.inject({ method: 'GET', url: '/api/platform/agents' }); |
| 33 | + expect(res.statusCode).toBe(200); |
| 34 | + const body = res.json() as { |
| 35 | + agents: Array<{ id: string; stage: string; contract_status: string; version: string }>; |
| 36 | + domain_groups: Record<string, { total: number; active: number; stub: number }>; |
| 37 | + totals: { total: number; active: number; stub: number }; |
| 38 | + }; |
| 39 | + expect(body.agents.length).toBeGreaterThan(0); |
| 40 | + expect(body.totals.total).toBe(body.agents.length); |
| 41 | + expect(body.totals.active + body.totals.stub).toBe(body.totals.total); |
| 42 | + // Every domain bucket totals match its members |
| 43 | + for (const [stage, bucket] of Object.entries(body.domain_groups)) { |
| 44 | + const members = body.agents.filter((a) => a.stage === stage); |
| 45 | + expect(members.length).toBe(bucket.total); |
| 46 | + expect(members.filter((a) => a.contract_status === 'active').length).toBe(bucket.active); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + it('marks v0.0.0 agents as stubs', async () => { |
| 51 | + const res = await app.inject({ method: 'GET', url: '/api/platform/agents' }); |
| 52 | + const body = res.json() as { agents: Array<{ version: string; contract_status: string }> }; |
| 53 | + for (const a of body.agents) { |
| 54 | + if (a.version === '0.0.0') expect(a.contract_status).toBe('stub'); |
| 55 | + else expect(a.contract_status).toBe('active'); |
| 56 | + } |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +describe('GET /api/platform/adapters', () => { |
| 61 | + it('lists every documented adapter with env-derived configured flags', async () => { |
| 62 | + const res = await app.inject({ method: 'GET', url: '/api/platform/adapters' }); |
| 63 | + expect(res.statusCode).toBe(200); |
| 64 | + const body = res.json() as { |
| 65 | + adapters: Array<{ id: string; name: string; configured: boolean; env_vars: string[] }>; |
| 66 | + }; |
| 67 | + const ids = body.adapters.map((a) => a.id); |
| 68 | + expect(ids).toEqual( |
| 69 | + expect.arrayContaining(['amadeus', 'sabre', 'navitaire', 'trippro', 'duffel', 'haip', 'hotelbeds']), |
| 70 | + ); |
| 71 | + // The hotelbeds adapter is "configured" iff both API_KEY and SECRET are set |
| 72 | + const hb = body.adapters.find((a) => a.id === 'hotelbeds')!; |
| 73 | + const hbExpected = |
| 74 | + Boolean(process.env['HOTELBEDS_API_KEY']) && Boolean(process.env['HOTELBEDS_SECRET']); |
| 75 | + expect(hb.configured).toBe(hbExpected); |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +describe('GET /api/platform/health', () => { |
| 80 | + it('returns uptime, node version, and a request count after the call lands', async () => { |
| 81 | + const res = await app.inject({ method: 'GET', url: '/api/platform/health' }); |
| 82 | + expect(res.statusCode).toBe(200); |
| 83 | + const body = res.json() as { |
| 84 | + status: string; |
| 85 | + uptime_seconds: number; |
| 86 | + node_version: string; |
| 87 | + otaip_version: string; |
| 88 | + last_request_at: string | null; |
| 89 | + request_count: number; |
| 90 | + }; |
| 91 | + expect(body.status).toBe('ok'); |
| 92 | + expect(body.uptime_seconds).toBeGreaterThanOrEqual(0); |
| 93 | + expect(body.node_version).toMatch(/^v\d+\.\d+\.\d+/); |
| 94 | + expect(body.request_count).toBeGreaterThan(0); |
| 95 | + expect(body.last_request_at).not.toBeNull(); |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +describe('GET /api/platform/stats', () => { |
| 100 | + it('returns aggregate counts that agree with the agent + adapter endpoints', async () => { |
| 101 | + const [statsRes, agentsRes, adaptersRes] = await Promise.all([ |
| 102 | + app.inject({ method: 'GET', url: '/api/platform/stats' }), |
| 103 | + app.inject({ method: 'GET', url: '/api/platform/agents' }), |
| 104 | + app.inject({ method: 'GET', url: '/api/platform/adapters' }), |
| 105 | + ]); |
| 106 | + expect(statsRes.statusCode).toBe(200); |
| 107 | + const stats = statsRes.json() as { |
| 108 | + agents: { total: number; active: number; stub: number }; |
| 109 | + adapters: { total: number; configured: number }; |
| 110 | + }; |
| 111 | + const agents = (agentsRes.json() as { totals: { total: number } }).totals; |
| 112 | + const adapters = (adaptersRes.json() as { adapters: Array<{ configured: boolean }> }).adapters; |
| 113 | + expect(stats.agents.total).toBe(agents.total); |
| 114 | + expect(stats.adapters.total).toBe(adapters.length); |
| 115 | + expect(stats.adapters.configured).toBe(adapters.filter((a) => a.configured).length); |
| 116 | + }); |
| 117 | +}); |
0 commit comments