@@ -74,6 +74,7 @@ vitest.mock("../fetchers/modelCache", () => ({
7474import { poeDefaultModelId } from "@roo-code/types"
7575import { PoeHandler } from "../poe"
7676
77+ import { makeCreateMessageMetadata } from "../../../test-utils/api"
7778import { clearAllMocks } from "../../../test-utils/reset"
7879
7980describe ( "PoeHandler" , ( ) => {
@@ -166,6 +167,69 @@ describe("PoeHandler", () => {
166167 expect ( chunks ) . toContainEqual ( { type : "text" , text : "world!" } )
167168 expect ( chunks ) . toContainEqual ( expect . objectContaining ( { type : "usage" , inputTokens : 10 , outputTokens : 5 } ) )
168169 } )
170+ it ( "rejects with AbortError when the external signal is pre-aborted" , async ( ) => {
171+ const handler = new PoeHandler ( { poeApiKey : "key" , apiModelId : "openai/gpt-4o" } )
172+ mockStreamText . mockReturnValue ( {
173+ fullStream : ( async function * ( ) { } ) ( ) ,
174+ usage : Promise . resolve ( undefined ) ,
175+ } )
176+
177+ const controller = new AbortController ( )
178+ controller . abort ( )
179+ const metadata = makeCreateMessageMetadata ( { abortSignal : controller . signal } )
180+
181+ await expect (
182+ handler . createMessage ( "system" , [ { role : "user" as const , content : "hi" } ] , metadata ) . next ( ) ,
183+ ) . rejects . toMatchObject ( {
184+ name : "AbortError" ,
185+ } )
186+ } )
187+
188+ it ( "aborts the in-flight stream and rejects with AbortError when the external signal aborts" , async ( ) => {
189+ const handler = new PoeHandler ( { poeApiKey : "key" , apiModelId : "openai/gpt-4o" } )
190+
191+ let requestSignal : AbortSignal | undefined
192+ mockStreamText . mockImplementationOnce ( ( args : { abortSignal ?: AbortSignal } ) => {
193+ requestSignal = args . abortSignal
194+ // Emulate the AI SDK: the first chunk arrives, then the stream errors once
195+ // the abort signal fires.
196+ const fullStream = ( async function * ( ) {
197+ yield { type : "text-delta" , text : "Hello " }
198+ await new Promise < void > ( ( resolve ) => {
199+ if ( requestSignal ?. aborted ) {
200+ resolve ( )
201+ } else {
202+ requestSignal ?. addEventListener ( "abort" , ( ) => resolve ( ) , { once : true } )
203+ }
204+ } )
205+ const abortError = new Error ( "The operation was aborted" )
206+ abortError . name = "AbortError"
207+ throw abortError
208+ } ) ( )
209+ return { fullStream, usage : Promise . resolve ( undefined ) }
210+ } )
211+
212+ const controller = new AbortController ( )
213+ const metadata = makeCreateMessageMetadata ( { abortSignal : controller . signal } )
214+
215+ const chunks : unknown [ ] = [ ]
216+ const iteration = ( async ( ) => {
217+ for await ( const chunk of handler . createMessage (
218+ "system" ,
219+ [ { role : "user" as const , content : "hi" } ] ,
220+ metadata ,
221+ ) ) {
222+ chunks . push ( chunk )
223+ if ( chunk . type === "text" ) {
224+ // Abort while the stream is still in flight.
225+ controller . abort ( )
226+ }
227+ }
228+ } ) ( )
229+
230+ await expect ( iteration ) . rejects . toMatchObject ( { name : "AbortError" } )
231+ expect ( chunks ) . toContainEqual ( { type : "text" , text : "Hello " } )
232+ } )
169233 } )
170234
171235 describe ( "reasoning" , ( ) => {
@@ -311,5 +375,132 @@ describe("PoeHandler", () => {
311375 } ) ,
312376 )
313377 } )
378+ it ( "completePrompt should pass abort signal through to generateText" , async ( ) => {
379+ const handler = new PoeHandler ( { poeApiKey : "key" , apiModelId : "openai/gpt-4o" } )
380+ const controller = new AbortController ( )
381+ mockGenerateText . mockResolvedValueOnce ( { text : "response" } )
382+
383+ await handler . completePrompt ( "test prompt" , { abortSignal : controller . signal } )
384+ expect ( mockGenerateText ) . toHaveBeenCalledWith (
385+ expect . objectContaining ( {
386+ model : mockLanguageModel ,
387+ prompt : "test prompt" ,
388+ abortSignal : controller . signal ,
389+ } ) ,
390+ )
391+ } )
392+
393+ it ( "completePrompt should work without options (backward compatible)" , async ( ) => {
394+ const handler = new PoeHandler ( { poeApiKey : "key" , apiModelId : "openai/gpt-4o" } )
395+ mockGenerateText . mockResolvedValueOnce ( { text : "response" } )
396+
397+ const result = await handler . completePrompt ( "test prompt" )
398+ expect ( result ) . toBe ( "response" )
399+ expect ( mockGenerateText ) . toHaveBeenCalledWith (
400+ expect . objectContaining ( {
401+ model : mockLanguageModel ,
402+ prompt : "test prompt" ,
403+ } ) ,
404+ )
405+ } )
406+
407+ it ( "completePrompt should merge signal and timeoutMs into combined abortSignal" , async ( ) => {
408+ const handler = new PoeHandler ( { poeApiKey : "key" , apiModelId : "openai/gpt-4o" } )
409+ const controller = new AbortController ( )
410+ mockGenerateText . mockResolvedValueOnce ( { text : "response" } )
411+
412+ await handler . completePrompt ( "test prompt" , { abortSignal : controller . signal , timeoutMs : 5000 } )
413+ expect ( mockGenerateText ) . toHaveBeenCalledWith (
414+ expect . objectContaining ( {
415+ model : mockLanguageModel ,
416+ prompt : "test prompt" ,
417+ abortSignal : expect . any ( AbortSignal ) ,
418+ } ) ,
419+ )
420+ // The abortSignal should be a merged signal (not the original controller.signal)
421+ const callArgs = mockGenerateText . mock . calls [ 0 ] [ 0 ]
422+ expect ( callArgs . abortSignal ) . toBeDefined ( )
423+ expect ( callArgs . abortSignal ) . toBeInstanceOf ( AbortSignal )
424+ } )
425+
426+ it ( "completePrompt should use AbortSignal.timeout when only timeoutMs is provided" , async ( ) => {
427+ const handler = new PoeHandler ( { poeApiKey : "key" , apiModelId : "openai/gpt-4o" } )
428+ mockGenerateText . mockResolvedValueOnce ( { text : "response" } )
429+
430+ await handler . completePrompt ( "test prompt" , { timeoutMs : 3000 } )
431+ expect ( mockGenerateText ) . toHaveBeenCalledWith (
432+ expect . objectContaining ( {
433+ model : mockLanguageModel ,
434+ prompt : "test prompt" ,
435+ abortSignal : expect . any ( AbortSignal ) ,
436+ } ) ,
437+ )
438+ const callArgs = mockGenerateText . mock . calls [ 0 ] [ 0 ]
439+ expect ( callArgs . abortSignal ) . toBeDefined ( )
440+ expect ( callArgs . abortSignal ) . toBeInstanceOf ( AbortSignal )
441+ } )
442+
443+ it ( "completePrompt should prefer signal over timeoutMs when both are provided" , async ( ) => {
444+ const handler = new PoeHandler ( { poeApiKey : "key" , apiModelId : "openai/gpt-4o" } )
445+ const controller = new AbortController ( )
446+ mockGenerateText . mockResolvedValueOnce ( { text : "response" } )
447+
448+ await handler . completePrompt ( "test prompt" , { abortSignal : controller . signal , timeoutMs : 5000 } )
449+ const callArgs = mockGenerateText . mock . calls [ 0 ] [ 0 ]
450+ // Should have a merged abortSignal (not the original controller.signal)
451+ expect ( callArgs . abortSignal ) . toBeInstanceOf ( AbortSignal )
452+ expect ( callArgs . abortSignal ) . not . toBe ( controller . signal )
453+ } )
454+
455+ it ( "completePrompt rejects with AbortError when the external signal aborts mid-flight" , async ( ) => {
456+ const handler = new PoeHandler ( { poeApiKey : "key" , apiModelId : "openai/gpt-4o" } )
457+ const controller = new AbortController ( )
458+ // Emulate the AI SDK: the in-flight generation rejects when the abort signal fires.
459+ mockGenerateText . mockImplementationOnce ( async ( args : { abortSignal ?: AbortSignal } ) => {
460+ await new Promise < void > ( ( resolve ) => {
461+ if ( args . abortSignal ?. aborted ) {
462+ resolve ( )
463+ } else {
464+ args . abortSignal ?. addEventListener ( "abort" , ( ) => resolve ( ) , { once : true } )
465+ }
466+ } )
467+ const abortError = new Error ( "The operation was aborted" )
468+ abortError . name = "AbortError"
469+ throw abortError
470+ } )
471+
472+ const promise = handler . completePrompt ( "test prompt" , { abortSignal : controller . signal , timeoutMs : 5000 } )
473+ const callArgs = mockGenerateText . mock . calls [ 0 ] [ 0 ]
474+ expect ( callArgs . abortSignal ) . toBeDefined ( )
475+
476+ // Abort the external signal before the generation settles.
477+ controller . abort ( )
478+
479+ await expect ( promise ) . rejects . toMatchObject ( { name : "AbortError" } )
480+ // The merged signal should be aborted once the user signal aborts.
481+ expect ( callArgs . abortSignal . aborted ) . toBe ( true )
482+ } )
483+
484+ it ( "completePrompt should handle timeoutMs=0 as no timeout" , async ( ) => {
485+ const handler = new PoeHandler ( { poeApiKey : "key" , apiModelId : "openai/gpt-4o" } )
486+ mockGenerateText . mockResolvedValueOnce ( { text : "response" } )
487+
488+ await handler . completePrompt ( "test prompt" , { timeoutMs : 0 } )
489+ expect ( mockGenerateText ) . toHaveBeenCalledWith (
490+ expect . objectContaining ( {
491+ model : mockLanguageModel ,
492+ prompt : "test prompt" ,
493+ } ) ,
494+ )
495+ const callArgs = mockGenerateText . mock . calls [ 0 ] [ 0 ]
496+ expect ( callArgs . abortSignal ) . toBeUndefined ( )
497+ } )
498+
499+ it ( "completePrompt should handle non-Error values in catch block" , async ( ) => {
500+ const handler = new PoeHandler ( { poeApiKey : "key" , apiModelId : "openai/gpt-4o" } )
501+ mockGenerateText . mockRejectedValueOnce ( "not an error" )
502+
503+ await expect ( handler . completePrompt ( "test prompt" ) ) . rejects . toThrow ( )
504+ } )
314505 } )
315506} )
0 commit comments