@@ -973,7 +973,42 @@ describe("OpenRouterHandler", () => {
973973 } )
974974 } )
975975
976+ // Installs a typed completion double without widening the client to `any`.
977+ const installMockClient = ( create : ReturnType < typeof vitest . fn > ) => {
978+ ; ( OpenAI as unknown as { prototype : { chat : unknown } } ) . prototype . chat = {
979+ completions : { create } ,
980+ }
981+ }
982+
976983 describe ( "completePrompt" , ( ) => {
984+ it ( "forwards the abort signal and deadline to the completion request" , async ( ) => {
985+ const handler = new OpenRouterHandler ( mockOptions )
986+ const { signal } = new AbortController ( )
987+ const mockCreate = vitest . fn ( ) . mockResolvedValue ( { choices : [ { message : { content : "ok" } } ] } )
988+ installMockClient ( mockCreate )
989+
990+ await handler . completePrompt ( "test prompt" , { abortSignal : signal , timeoutMs : 5_000 } )
991+
992+ expect ( mockCreate ) . toHaveBeenCalledWith ( expect . objectContaining ( { model : mockOptions . openRouterModelId } ) , {
993+ headers : { "x-anthropic-beta" : "fine-grained-tool-streaming-2025-05-14" } ,
994+ signal,
995+ timeout : 5_000 ,
996+ } )
997+ } )
998+
999+ it ( "does not start a request when the caller already aborted" , async ( ) => {
1000+ const handler = new OpenRouterHandler ( mockOptions )
1001+ const mockCreate = vitest . fn ( )
1002+ installMockClient ( mockCreate )
1003+ const controller = new AbortController ( )
1004+ controller . abort ( )
1005+
1006+ await expect (
1007+ handler . completePrompt ( "test prompt" , { abortSignal : controller . signal } ) ,
1008+ ) . rejects . toMatchObject ( { name : "AbortError" } )
1009+ expect ( mockCreate ) . not . toHaveBeenCalled ( )
1010+ } )
1011+
9771012 it ( "returns correct response" , async ( ) => {
9781013 const handler = new OpenRouterHandler ( mockOptions )
9791014 const mockResponse = { choices : [ { message : { content : "test completion" } } ] }
0 commit comments