@@ -2,6 +2,21 @@ import { expect, jest, test } from '@jest/globals';
22import Replicate , { Prediction } from 'replicate' ;
33import nock from 'nock' ;
44import fetch from 'cross-fetch' ;
5+ import { SetupServer , setupServer } from 'msw/node' ;
6+ import { rest } from 'msw' ;
7+
8+ type ServerOptions = Parameters < typeof setupServer > ;
9+
10+ const withRequestInterception =
11+ ( handlers : ServerOptions , test : ( server : SetupServer ) => any ) => async ( ) => {
12+ const server = setupServer ( ...handlers ) ;
13+ server . listen ( ) ;
14+
15+ return Promise . resolve ( test ( server ) ) . finally ( ( ) => {
16+ server . resetHandlers ( ) ;
17+ server . close ( ) ;
18+ } ) ;
19+ } ;
520
621describe ( 'Replicate client' , ( ) => {
722 let client : Replicate ;
@@ -483,7 +498,7 @@ describe('Replicate client', () => {
483498 status : 'processing' ,
484499 } )
485500 . get ( '/predictions/ufawqhfynnddngldkgtslldrkq' )
486- . reply ( 200 , {
501+ . reply ( 201 , {
487502 id : 'ufawqhfynnddngldkgtslldrkq' ,
488503 status : 'succeeded' ,
489504 output : 'foobar' ,
@@ -498,6 +513,29 @@ describe('Replicate client', () => {
498513 expect ( output ) . toBe ( 'foobar' ) ;
499514 } ) ;
500515
516+ test ( 'Sends an Accept: text/event-stream header when stream is true' , async ( ) => {
517+ withRequestInterception (
518+ [
519+ rest . post ( 'https://api.replicate.com/v1/predictions/' , ( req , res , ctx ) => {
520+ expect ( req . headers . get ( 'Accept' ) ) . toBe ( 'text/event-stream' ) ;
521+ return res ( ctx . status ( 201 ) , ctx . text ( 'data: Once upon a time\n\n' ) ) ;
522+ } ) ,
523+ ] ,
524+ async ( ) => {
525+ const stream = await client . run (
526+ 'owner/model:5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa' ,
527+ {
528+ input : { prompt : 'Tell me a story' } ,
529+ stream : true
530+ }
531+ ) ;
532+
533+ for await ( const event of await stream ) {
534+ expect ( event ) . toBe ( 'Once upon a time' ) ;
535+ }
536+ } ) ;
537+ } ) ;
538+
501539 test ( 'Does not throw an error for identifier containing hyphen and full stop' , async ( ) => {
502540 nock ( BASE_URL )
503541 . post ( '/predictions' )
@@ -520,12 +558,10 @@ describe('Replicate client', () => {
520558
521559 await expect ( client . run ( 'owner/model:invalid' , options ) ) . rejects . toThrow ( ) ;
522560
523- // @ts -expect-error
524561 await expect ( client . run ( 'owner:abc123' , options ) ) . rejects . toThrow ( ) ;
525562
526563 await expect ( client . run ( '/model:abc123' , options ) ) . rejects . toThrow ( ) ;
527564
528- // @ts -expect-error
529565 await expect ( client . run ( ':abc123' , options ) ) . rejects . toThrow ( ) ;
530566 } ) ;
531567
0 commit comments