@@ -169,17 +169,39 @@ describe("provider adapters", () => {
169169 } ) ;
170170 } ) ;
171171
172- it ( "fails closed on non-200 provider responses" , async ( ) => {
173- const guard = createOpenAiGuard ( {
174- apiKey : "test" ,
175- pinnedModel : model ,
176- fetch : async ( ) => jsonResponse ( { error : "no" } , 500 ) ,
177- } ) ;
178- await expect ( guard . classify ( request ) ) . resolves . toMatchObject ( {
179- decision : "deny" ,
180- category : "guard_failure" ,
181- } ) ;
182- } ) ;
172+ it . each ( [
173+ [
174+ "OpenAI" ,
175+ ( fetch : FetchLike ) => createOpenAiGuard ( { apiKey : "test" , pinnedModel : model , fetch } ) ,
176+ ] ,
177+ [
178+ "Anthropic" ,
179+ ( fetch : FetchLike ) => createAnthropicGuard ( { apiKey : "test" , pinnedModel : model , fetch } ) ,
180+ ] ,
181+ ] ) (
182+ "cancels %s non-200 provider response bodies before failing closed" ,
183+ async ( _name , createGuard ) => {
184+ let cancelled = false ;
185+ const response = new Response (
186+ new ReadableStream < Uint8Array > ( {
187+ start ( controller ) {
188+ controller . enqueue ( new TextEncoder ( ) . encode ( "partial error body" ) ) ;
189+ } ,
190+ cancel ( ) {
191+ cancelled = true ;
192+ } ,
193+ } ) ,
194+ { status : 503 } ,
195+ ) ;
196+ const guard = createGuard ( async ( ) => response ) ;
197+
198+ await expect ( guard . classify ( request ) ) . resolves . toMatchObject ( {
199+ decision : "deny" ,
200+ category : "guard_failure" ,
201+ } ) ;
202+ expect ( cancelled ) . toBe ( true ) ;
203+ } ,
204+ ) ;
183205
184206 it ( "cancels oversized provider response streams before buffering them fully" , async ( ) => {
185207 const maxBytes = 256 * 1024 ;
0 commit comments