11import { describe , it , expect , vi , beforeEach } from 'vitest' ;
22import { healthRouter } from '../health.js' ;
3- import { Request , Response } from 'express' ;
3+ import { Request , Response , Router , RequestHandler } from 'express' ;
44import { server as stellarServer } from '../../services/stellar.js' ;
55import { getJobScheduler } from '../../jobs/index.js' ;
66
@@ -20,6 +20,17 @@ describe('Health Router', () => {
2020 let resJson : any ;
2121 let resStatus : any ;
2222
23+ const getRouteHandler = ( router : Router , path : string ) : RequestHandler => {
24+ const layer = router . stack . find ( ( entry ) => entry . route ?. path === path ) ;
25+ const handler = layer ?. route ?. stack [ 0 ] ?. handle ;
26+
27+ if ( ! handler ) {
28+ throw new Error ( `Route handler not found for ${ path } ` ) ;
29+ }
30+
31+ return handler ;
32+ } ;
33+
2334 beforeEach ( ( ) => {
2435 vi . clearAllMocks ( ) ;
2536 resJson = vi . fn ( ) ;
@@ -32,13 +43,16 @@ describe('Health Router', () => {
3243
3344 describe ( 'GET /health' , ( ) => {
3445 it ( 'returns 200 and healthy status when all dependencies are up' , async ( ) => {
35- vi . mocked ( stellarServer . root ) . mockResolvedValue ( { } as any ) ;
46+ const mockedStellarRoot = vi . mocked (
47+ ( stellarServer as unknown as { root : ( ) => Promise < unknown > } ) . root
48+ ) ;
49+
50+ mockedStellarRoot . mockResolvedValue ( { } ) ;
3651 vi . mocked ( getJobScheduler ) . mockReturnValue ( { } as any ) ;
3752 process . env . OPENAI_API_KEY = 'test-key' ;
3853
39- // Access the private handler (for testing purposes in vitest)
40- const handler = ( healthRouter . stack . find ( s => s . route . path === '/health' ) ?. route . stack [ 0 ] . handle ) ;
41- await handler ( mockReq as Request , mockRes as Response ) ;
54+ const handler = getRouteHandler ( healthRouter , '/health' ) ;
55+ await handler ( mockReq as Request , mockRes as Response , vi . fn ( ) ) ;
4256
4357 expect ( resStatus ) . toHaveBeenCalledWith ( 200 ) ;
4458 expect ( resJson ) . toHaveBeenCalledWith ( expect . objectContaining ( {
@@ -52,13 +66,17 @@ describe('Health Router', () => {
5266 } ) ;
5367
5468 it ( 'returns 200 and degraded status when OpenAI is missing' , async ( ) => {
55- vi . mocked ( stellarServer . root ) . mockResolvedValue ( { } as any ) ;
69+ const mockedStellarRoot = vi . mocked (
70+ ( stellarServer as unknown as { root : ( ) => Promise < unknown > } ) . root
71+ ) ;
72+
73+ mockedStellarRoot . mockResolvedValue ( { } ) ;
5674 vi . mocked ( getJobScheduler ) . mockReturnValue ( { } as any ) ;
5775 const originalKey = process . env . OPENAI_API_KEY ;
5876 delete process . env . OPENAI_API_KEY ;
5977
60- const handler = ( healthRouter . stack . find ( s => s . route . path === '/health' ) ?. route . stack [ 0 ] . handle ) ;
61- await handler ( mockReq as Request , mockRes as Response ) ;
78+ const handler = getRouteHandler ( healthRouter , '/health' ) ;
79+ await handler ( mockReq as Request , mockRes as Response , vi . fn ( ) ) ;
6280
6381 expect ( resStatus ) . toHaveBeenCalledWith ( 200 ) ;
6482 expect ( resJson ) . toHaveBeenCalledWith ( expect . objectContaining ( {
@@ -74,12 +92,16 @@ describe('Health Router', () => {
7492 } ) ;
7593
7694 it ( 'returns 503 and unhealthy status when Stellar Horizon is down' , async ( ) => {
77- vi . mocked ( stellarServer . root ) . mockRejectedValue ( new Error ( 'Horizon Down' ) ) ;
95+ const mockedStellarRoot = vi . mocked (
96+ ( stellarServer as unknown as { root : ( ) => Promise < unknown > } ) . root
97+ ) ;
98+
99+ mockedStellarRoot . mockRejectedValue ( new Error ( 'Horizon Down' ) ) ;
78100 vi . mocked ( getJobScheduler ) . mockReturnValue ( { } as any ) ;
79101 process . env . OPENAI_API_KEY = 'test-key' ;
80102
81- const handler = ( healthRouter . stack . find ( s => s . route . path === '/health' ) ?. route . stack [ 0 ] . handle ) ;
82- await handler ( mockReq as Request , mockRes as Response ) ;
103+ const handler = getRouteHandler ( healthRouter , '/health' ) ;
104+ await handler ( mockReq as Request , mockRes as Response , vi . fn ( ) ) ;
83105
84106 expect ( resStatus ) . toHaveBeenCalledWith ( 503 ) ;
85107 expect ( resJson ) . toHaveBeenCalledWith ( expect . objectContaining ( {
@@ -95,8 +117,8 @@ describe('Health Router', () => {
95117
96118 describe ( 'GET /ready' , ( ) => {
97119 it ( 'returns 200 and ready status' , async ( ) => {
98- const handler = ( healthRouter . stack . find ( s => s . route . path === '/ready' ) ?. route . stack [ 0 ] . handle ) ;
99- await handler ( mockReq as Request , mockRes as Response ) ;
120+ const handler = getRouteHandler ( healthRouter , '/ready' ) ;
121+ await handler ( mockReq as Request , mockRes as Response , vi . fn ( ) ) ;
100122
101123 expect ( resStatus ) . toHaveBeenCalledWith ( 200 ) ;
102124 expect ( resJson ) . toHaveBeenCalledWith ( expect . objectContaining ( {
0 commit comments