11// Runtime model preflight tests cover provider/model checks before cron execution.
22import { beforeEach , describe , expect , it , vi } from "vitest" ;
33
4- const { fetchWithSsrFGuardMock } = vi . hoisted ( ( ) => ( {
4+ const {
5+ fetchWithSsrFGuardMock,
6+ shouldUseEnvHttpProxyForUrlMock,
7+ withTrustedEnvProxyGuardedFetchModeMock,
8+ } = vi . hoisted ( ( ) => ( {
59 fetchWithSsrFGuardMock : vi . fn ( ) ,
10+ shouldUseEnvHttpProxyForUrlMock : vi . fn ( ( ) => false ) ,
11+ withTrustedEnvProxyGuardedFetchModeMock : vi . fn ( ( params : Record < string , unknown > ) => ( {
12+ ...params ,
13+ mode : "trusted_env_proxy" ,
14+ } ) ) ,
615} ) ) ;
716
817vi . mock ( "../../infra/net/fetch-guard.js" , ( ) => ( {
918 fetchWithSsrFGuard : fetchWithSsrFGuardMock ,
19+ withTrustedEnvProxyGuardedFetchMode : withTrustedEnvProxyGuardedFetchModeMock ,
20+ } ) ) ;
21+
22+ vi . mock ( "../../infra/net/proxy-env.js" , ( ) => ( {
23+ shouldUseEnvHttpProxyForUrl : shouldUseEnvHttpProxyForUrlMock ,
1024} ) ) ;
1125
1226import {
@@ -25,9 +39,10 @@ function requireFetchPreflightRequest(): {
2539 url ?: string ;
2640 timeoutMs ?: number ;
2741 auditContext ?: string ;
42+ mode ?: string ;
2843} {
2944 const request = fetchWithSsrFGuardMock . mock . calls [ 0 ] ?. [ 0 ] as
30- | { url ?: string ; timeoutMs ?: number ; auditContext ?: string }
45+ | { url ?: string ; timeoutMs ?: number ; auditContext ?: string ; mode ?: string }
3146 | undefined ;
3247 if ( ! request ) {
3348 throw new Error ( "Expected cron model preflight fetch request" ) ;
@@ -38,6 +53,9 @@ function requireFetchPreflightRequest(): {
3853describe ( "preflightCronModelProvider" , ( ) => {
3954 beforeEach ( ( ) => {
4055 fetchWithSsrFGuardMock . mockReset ( ) ;
56+ shouldUseEnvHttpProxyForUrlMock . mockReset ( ) ;
57+ shouldUseEnvHttpProxyForUrlMock . mockReturnValue ( false ) ;
58+ withTrustedEnvProxyGuardedFetchModeMock . mockClear ( ) ;
4159 resetCronModelProviderPreflightCacheForTest ( ) ;
4260 } ) ;
4361
@@ -171,4 +189,59 @@ describe("preflightCronModelProvider", () => {
171189 expect ( second ) . toEqual ( { status : "available" } ) ;
172190 expect ( fetchWithSsrFGuardMock ) . toHaveBeenCalledTimes ( 2 ) ;
173191 } ) ;
192+
193+ it ( "uses trusted-env-proxy mode when the env proxy applies, mirroring the runner" , async ( ) => {
194+ // Proxy-routed local vhosts (e.g. `inference.local`) need the env proxy;
195+ // STRICT pinning would EAI_AGAIN. The runner uses env proxy, so the
196+ // preflight must mirror it or it false-negatives and skips every run.
197+ shouldUseEnvHttpProxyForUrlMock . mockReturnValue ( true ) ;
198+ mockReachableResponse ( 200 ) ;
199+
200+ const result = await preflightCronModelProvider ( {
201+ cfg : {
202+ models : {
203+ providers : {
204+ openshell : {
205+ api : "openai-completions" ,
206+ baseUrl : "http://inference.local/v1" ,
207+ models : [ ] ,
208+ } ,
209+ } ,
210+ } ,
211+ } ,
212+ provider : "openshell" ,
213+ model : "qwen3" ,
214+ } ) ;
215+
216+ expect ( result ) . toEqual ( { status : "available" } ) ;
217+ expect ( withTrustedEnvProxyGuardedFetchModeMock ) . toHaveBeenCalledTimes ( 1 ) ;
218+ const request = requireFetchPreflightRequest ( ) ;
219+ expect ( request . url ) . toBe ( "http://inference.local/v1/models" ) ;
220+ expect ( request . mode ) . toBe ( "trusted_env_proxy" ) ;
221+ } ) ;
222+
223+ it ( "keeps strict mode when the env proxy does not apply" , async ( ) => {
224+ mockReachableResponse ( 200 ) ;
225+
226+ const result = await preflightCronModelProvider ( {
227+ cfg : {
228+ models : {
229+ providers : {
230+ vllm : {
231+ api : "openai-completions" ,
232+ baseUrl : "http://127.0.0.1:8000/v1" ,
233+ models : [ ] ,
234+ } ,
235+ } ,
236+ } ,
237+ } ,
238+ provider : "vllm" ,
239+ model : "llama" ,
240+ } ) ;
241+
242+ expect ( result ) . toEqual ( { status : "available" } ) ;
243+ expect ( withTrustedEnvProxyGuardedFetchModeMock ) . not . toHaveBeenCalled ( ) ;
244+ const request = requireFetchPreflightRequest ( ) ;
245+ expect ( request . mode ) . toBeUndefined ( ) ;
246+ } ) ;
174247} ) ;
0 commit comments