@@ -20,7 +20,7 @@ describe('checkDomainHealth', () => {
2020 } ) ;
2121
2222 test ( 'returns error when domain is not configured' , async ( ) => {
23- const result = await checkDomainHealth ( 'VIDSRC_DOMAIN' ) ;
23+ const result = await checkDomainHealth ( 'VIDSRC_DOMAIN' , undefined ) ;
2424 expect ( result ) . toEqual ( {
2525 name : 'VIDSRC_DOMAIN' ,
2626 status : 'error' ,
@@ -67,6 +67,12 @@ describe('checkDomainHealth', () => {
6767describe ( 'healthController' , ( ) => {
6868 const createRes = ( ) => {
6969 const res : Partial < Response > = { } ;
70+ res . statusCode = 200 ;
71+ res . status = jest . fn ( ) . mockImplementation ( ( code : number ) => {
72+ res . statusCode = code ;
73+ return res ;
74+ } ) ;
75+ res . set = jest . fn ( ) . mockReturnValue ( res ) ;
7076 res . json = jest . fn ( ) . mockReturnValue ( res ) ;
7177 return res as Response ;
7278 } ;
@@ -84,9 +90,16 @@ describe('healthController', () => {
8490 . mockResolvedValueOnce ( { status : 503 } as any ) ;
8591
8692 const res = createRes ( ) ;
87- await healthController . getEmbedDomains ( { } as Request , res ) ;
88-
93+ await healthController . getEmbedDomains ( { query : { } } as unknown as Request , res ) ;
94+ expect ( res . set ) . toHaveBeenCalledWith ( {
95+ 'Cache-Control' : 'no-store, no-cache, must-revalidate, proxy-revalidate' ,
96+ Pragma : 'no-cache' ,
97+ Expires : '0' ,
98+ 'Surrogate-Control' : 'no-store' ,
99+ } ) ;
100+ expect ( res . status ) . toHaveBeenCalledWith ( 503 ) ;
89101 expect ( res . json ) . toHaveBeenCalledWith ( {
102+ status : 'error' ,
90103 domains : [
91104 {
92105 name : 'VIDSRC_DOMAIN' ,
@@ -109,6 +122,13 @@ describe('healthController', () => {
109122 mockedHttpClient . get . mockResolvedValue ( { status : 301 } as any ) ;
110123 const res = createRes ( ) ;
111124 await healthController . getAppUrl ( { } as Request , res ) ;
125+ expect ( res . set ) . toHaveBeenCalledWith ( {
126+ 'Cache-Control' : 'no-store, no-cache, must-revalidate, proxy-revalidate' ,
127+ Pragma : 'no-cache' ,
128+ Expires : '0' ,
129+ 'Surrogate-Control' : 'no-store' ,
130+ } ) ;
131+ expect ( res . status ) . toHaveBeenCalledWith ( 200 ) ;
112132 expect ( res . json ) . toHaveBeenCalledWith ( {
113133 name : 'APP_URL' ,
114134 domain : 'https://app.example' ,
@@ -122,22 +142,82 @@ describe('healthController', () => {
122142 mockedHttpClient . get . mockResolvedValue ( { status : 200 } as any ) ;
123143
124144 const res = createRes ( ) ;
125- await healthController . getEmbedDomains ( { } as Request , res ) ;
145+ await healthController . getEmbedDomains ( { query : { } } as unknown as Request , res ) ;
126146
147+ expect ( res . status ) . toHaveBeenCalledWith ( 200 ) ;
127148 expect ( res . json ) . toHaveBeenCalledWith ( {
149+ status : 'success' ,
128150 domains : [
129151 {
130152 name : 'VIDSRC_DOMAIN' ,
131153 domain : 'vidsrc.example' ,
132154 status : 'success' ,
133155 httpStatus : 200 ,
134156 } ,
157+ ] ,
158+ } ) ;
159+ } ) ;
160+
161+ test ( 'getEmbedDomains rejects multi target when MULTI_DOMAIN is not configured' , async ( ) => {
162+ ( appConfig as any ) . MULTI_DOMAIN = undefined ;
163+ const res = createRes ( ) ;
164+
165+ await healthController . getEmbedDomains ( { query : { target : 'multi' } } as unknown as Request , res ) ;
166+
167+ expect ( res . status ) . toHaveBeenCalledWith ( 400 ) ;
168+ expect ( res . json ) . toHaveBeenCalledWith ( {
169+ status : 'error' ,
170+ message : 'Target not configured' ,
171+ domains : [ ] ,
172+ } ) ;
173+ } ) ;
174+
175+ test ( 'getEmbedDomains filters by target query and returns 200 when healthy' , async ( ) => {
176+ mockedHttpClient . get . mockResolvedValue ( { status : 200 } as any ) ;
177+
178+ const res = createRes ( ) ;
179+ await healthController . getEmbedDomains ( { query : { target : 'vidsrc' } } as unknown as Request , res ) ;
180+
181+ expect ( res . status ) . toHaveBeenCalledWith ( 200 ) ;
182+ expect ( res . json ) . toHaveBeenCalledWith ( {
183+ status : 'success' ,
184+ domains : [
135185 {
136- name : 'MULTI_DOMAIN' ,
137- status : 'error' ,
138- message : 'Domain not configured' ,
186+ name : 'VIDSRC_DOMAIN' ,
187+ domain : 'vidsrc.example' ,
188+ status : 'success' ,
189+ httpStatus : 200 ,
139190 } ,
140191 ] ,
141192 } ) ;
142193 } ) ;
194+
195+ test ( 'getEmbedDomains rejects invalid target' , async ( ) => {
196+ const res = createRes ( ) ;
197+
198+ await healthController . getEmbedDomains ( { query : { target : 'unknown' } } as unknown as Request , res ) ;
199+
200+ expect ( res . status ) . toHaveBeenCalledWith ( 400 ) ;
201+ expect ( res . json ) . toHaveBeenCalledWith ( {
202+ status : 'error' ,
203+ message : 'Invalid target' ,
204+ domains : [ ] ,
205+ } ) ;
206+ } ) ;
207+
208+ test ( 'getAppUrl returns 503 when app url is unhealthy' , async ( ) => {
209+ mockedHttpClient . get . mockResolvedValue ( { status : 500 } as any ) ;
210+ const res = createRes ( ) ;
211+
212+ await healthController . getAppUrl ( { } as Request , res ) ;
213+
214+ expect ( res . status ) . toHaveBeenCalledWith ( 503 ) ;
215+ expect ( res . json ) . toHaveBeenCalledWith ( {
216+ name : 'APP_URL' ,
217+ domain : 'https://app.example' ,
218+ status : 'error' ,
219+ httpStatus : 500 ,
220+ message : 'Received status code 500' ,
221+ } ) ;
222+ } ) ;
143223} ) ;
0 commit comments