@@ -6,6 +6,7 @@ import OpenAI from "openai"
66import { type FireworksModelId , fireworksDefaultModelId , fireworksModels } from "@roo-code/types"
77
88import { FireworksHandler } from "../fireworks"
9+ import { asyncStreamFrom , collectStream } from "../../../test-utils/stream"
910
1011// Create mock functions
1112const mockCreate = vi . fn ( )
@@ -29,18 +30,18 @@ describe("FireworksHandler", () => {
2930 beforeEach ( ( ) => {
3031 vi . clearAllMocks ( )
3132 // Set up default mock implementation
32- mockCreate . mockImplementation ( async ( ) => ( {
33- [ Symbol . asyncIterator ] : async function * ( ) {
34- yield {
33+ mockCreate . mockImplementation ( async ( ) =>
34+ asyncStreamFrom ( [
35+ {
3536 choices : [
3637 {
3738 delta : { content : "Test response" } ,
3839 index : 0 ,
3940 } ,
4041 ] ,
4142 usage : null ,
42- }
43- yield {
43+ } ,
44+ {
4445 choices : [
4546 {
4647 delta : { } ,
@@ -52,9 +53,9 @@ describe("FireworksHandler", () => {
5253 completion_tokens : 5 ,
5354 total_tokens : 15 ,
5455 } ,
55- }
56- } ,
57- } ) )
56+ } ,
57+ ] ) ,
58+ )
5859 handler = new FireworksHandler ( { fireworksApiKey : "test-key" } )
5960 } )
6061
@@ -436,19 +437,7 @@ describe("FireworksHandler", () => {
436437 it ( "createMessage should yield text content from stream" , async ( ) => {
437438 const testContent = "This is test content from Fireworks stream"
438439
439- mockCreate . mockImplementationOnce ( ( ) => {
440- return {
441- [ Symbol . asyncIterator ] : ( ) => ( {
442- next : vi
443- . fn ( )
444- . mockResolvedValueOnce ( {
445- done : false ,
446- value : { choices : [ { delta : { content : testContent } } ] } ,
447- } )
448- . mockResolvedValueOnce ( { done : true } ) ,
449- } ) ,
450- }
451- } )
440+ mockCreate . mockImplementationOnce ( ( ) => asyncStreamFrom ( [ { choices : [ { delta : { content : testContent } } ] } ] ) )
452441
453442 const stream = handler . createMessage ( "system prompt" , [ ] )
454443 const firstChunk = await stream . next ( )
@@ -458,19 +447,9 @@ describe("FireworksHandler", () => {
458447 } )
459448
460449 it ( "createMessage should yield usage data from stream" , async ( ) => {
461- mockCreate . mockImplementationOnce ( ( ) => {
462- return {
463- [ Symbol . asyncIterator ] : ( ) => ( {
464- next : vi
465- . fn ( )
466- . mockResolvedValueOnce ( {
467- done : false ,
468- value : { choices : [ { delta : { } } ] , usage : { prompt_tokens : 10 , completion_tokens : 20 } } ,
469- } )
470- . mockResolvedValueOnce ( { done : true } ) ,
471- } ) ,
472- }
473- } )
450+ mockCreate . mockImplementationOnce ( ( ) =>
451+ asyncStreamFrom ( [ { choices : [ { delta : { } } ] , usage : { prompt_tokens : 10 , completion_tokens : 20 } } ] ) ,
452+ )
474453
475454 const stream = handler . createMessage ( "system prompt" , [ ] )
476455 const firstChunk = await stream . next ( )
@@ -487,15 +466,7 @@ describe("FireworksHandler", () => {
487466 fireworksApiKey : "test-fireworks-api-key" ,
488467 } )
489468
490- mockCreate . mockImplementationOnce ( ( ) => {
491- return {
492- [ Symbol . asyncIterator ] : ( ) => ( {
493- async next ( ) {
494- return { done : true }
495- } ,
496- } ) ,
497- }
498- } )
469+ mockCreate . mockImplementationOnce ( ( ) => asyncStreamFrom ( [ ] ) )
499470
500471 const systemPrompt = "Test system prompt for Fireworks"
501472 const messages : Anthropic . Messages . MessageParam [ ] = [ { role : "user" , content : "Test message for Fireworks" } ]
@@ -523,13 +494,7 @@ describe("FireworksHandler", () => {
523494 fireworksApiKey : "test-fireworks-api-key" ,
524495 } )
525496
526- mockCreate . mockImplementationOnce ( ( ) => ( {
527- [ Symbol . asyncIterator ] : ( ) => ( {
528- async next ( ) {
529- return { done : true }
530- } ,
531- } ) ,
532- } ) )
497+ mockCreate . mockImplementationOnce ( ( ) => asyncStreamFrom ( [ ] ) )
533498
534499 const messageGenerator = handlerWithModel . createMessage ( "system" , [ ] )
535500 await messageGenerator . next ( )
@@ -549,13 +514,7 @@ describe("FireworksHandler", () => {
549514 fireworksApiKey : "test-fireworks-api-key" ,
550515 } )
551516
552- mockCreate . mockImplementationOnce ( ( ) => ( {
553- [ Symbol . asyncIterator ] : ( ) => ( {
554- async next ( ) {
555- return { done : true }
556- } ,
557- } ) ,
558- } ) )
517+ mockCreate . mockImplementationOnce ( ( ) => asyncStreamFrom ( [ ] ) )
559518
560519 const messageGenerator = handlerWithModel . createMessage ( "system" , [ ] )
561520 await messageGenerator . next ( )
@@ -577,13 +536,7 @@ describe("FireworksHandler", () => {
577536 modelTemperature : 0.7 ,
578537 } )
579538
580- mockCreate . mockImplementationOnce ( ( ) => ( {
581- [ Symbol . asyncIterator ] : ( ) => ( {
582- async next ( ) {
583- return { done : true }
584- } ,
585- } ) ,
586- } ) )
539+ mockCreate . mockImplementationOnce ( ( ) => asyncStreamFrom ( [ ] ) )
587540
588541 const messageGenerator = handlerWithModel . createMessage ( "system" , [ ] )
589542 await messageGenerator . next ( )
@@ -610,27 +563,27 @@ describe("FireworksHandler", () => {
610563 } )
611564
612565 it ( "createMessage should handle stream with multiple chunks" , async ( ) => {
613- mockCreate . mockImplementationOnce ( async ( ) => ( {
614- [ Symbol . asyncIterator ] : async function * ( ) {
615- yield {
566+ mockCreate . mockImplementationOnce ( async ( ) =>
567+ asyncStreamFrom ( [
568+ {
616569 choices : [
617570 {
618571 delta : { content : "Hello" } ,
619572 index : 0 ,
620573 } ,
621574 ] ,
622575 usage : null ,
623- }
624- yield {
576+ } ,
577+ {
625578 choices : [
626579 {
627580 delta : { content : " world" } ,
628581 index : 0 ,
629582 } ,
630583 ] ,
631584 usage : null ,
632- }
633- yield {
585+ } ,
586+ {
634587 choices : [
635588 {
636589 delta : { } ,
@@ -642,18 +595,15 @@ describe("FireworksHandler", () => {
642595 completion_tokens : 10 ,
643596 total_tokens : 15 ,
644597 } ,
645- }
646- } ,
647- } ) )
598+ } ,
599+ ] ) ,
600+ )
648601
649602 const systemPrompt = "You are a helpful assistant."
650603 const messages : Anthropic . Messages . MessageParam [ ] = [ { role : "user" , content : "Hi" } ]
651604
652605 const stream = handler . createMessage ( systemPrompt , messages )
653- const chunks = [ ]
654- for await ( const chunk of stream ) {
655- chunks . push ( chunk )
656- }
606+ const chunks = await collectStream ( stream )
657607
658608 expect ( chunks [ 0 ] ) . toEqual ( { type : "text" , text : "Hello" } )
659609 expect ( chunks [ 1 ] ) . toEqual ( { type : "text" , text : " world" } )
0 commit comments