1
- import { parseSseLine } from "./stream" ;
2
-
3
- // Jest unit tests
4
- describe ( 'parseSseLine' , ( ) => {
5
- it ( 'should return done:true and data:undefined for line starting with "data:[DONE]"' , ( ) => {
6
- const result = parseSseLine ( 'data:[DONE]' ) ;
7
- expect ( result ) . toEqual ( { done : true , data : undefined } ) ;
8
- } ) ;
9
-
10
- it ( 'should return done:true and data:undefined for line starting with "data: [DONE]"' , ( ) => {
11
- const result = parseSseLine ( 'data: [DONE]' ) ;
12
- expect ( result ) . toEqual ( { done : true , data : undefined } ) ;
13
- } ) ;
14
-
15
- it ( 'should return done:false and parsed data for line starting with "data:"' , ( ) => {
16
- const result = parseSseLine ( 'data:Hello World' ) ;
17
- expect ( result ) . toEqual ( { done : false , data : 'Hello World' } ) ;
18
- } ) ;
19
-
20
- it ( 'should return done:true and data:undefined for line starting with ": ping"' , ( ) => {
21
- const result = parseSseLine ( ': ping' ) ;
22
- expect ( result ) . toEqual ( { done : true , data : undefined } ) ;
23
- } ) ;
24
-
25
- it ( 'should return done:false and data:undefined for line not starting with specific prefixes' , ( ) => {
26
- const result = parseSseLine ( 'random line' ) ;
27
- expect ( result ) . toEqual ( { done : false , data : undefined } ) ;
28
- } ) ;
29
- } ) ;
1
+ import { Readable } from "stream" ;
2
+ import { streamSse } from "./stream" ;
3
+
4
+ function createMockResponse ( sseLines : string [ ] ) : Response {
5
+ // Create a Readable stream that emits the SSE lines
6
+ const stream = new Readable ( {
7
+ read ( ) {
8
+ for ( const line of sseLines ) {
9
+ this . push ( line + "\n\n" ) ;
10
+ }
11
+ this . push ( null ) ; // End of stream
12
+ }
13
+ } ) as any ;
14
+
15
+ // Minimal Response mock
16
+ return {
17
+ status : 200 ,
18
+ body : stream ,
19
+ text : async ( ) => "" ,
20
+ } as unknown as Response ;
21
+ }
22
+
23
+ describe ( "streamSse" , ( ) => {
24
+ it ( "yields parsed SSE data objects that ends with `data:[DONE]`" , async ( ) => {
25
+ const sseLines = [
26
+ 'data: {"foo": "bar"}' ,
27
+ 'data: {"baz": 42}' ,
28
+ 'data:[DONE]'
29
+ ] ;
30
+ const response = createMockResponse ( sseLines ) ;
31
+
32
+ const results = [ ] ;
33
+ for await ( const data of streamSse ( response ) ) {
34
+ results . push ( data ) ;
35
+ }
36
+
37
+ expect ( results ) . toEqual ( [
38
+ { foo : "bar" } ,
39
+ { baz : 42 }
40
+ ] ) ;
41
+ } ) ;
42
+
43
+ it ( "yields parsed SSE data objects that ends with `data: [DONE]` (with a space before [DONE]" , async ( ) => {
44
+ const sseLines = [
45
+ 'data: {"foo": "bar"}' ,
46
+ 'data: {"baz": 42}' ,
47
+ 'data: [DONE]'
48
+ ] ;
49
+ const response = createMockResponse ( sseLines ) ;
50
+
51
+ const results = [ ] ;
52
+ for await ( const data of streamSse ( response ) ) {
53
+ results . push ( data ) ;
54
+ }
55
+
56
+ expect ( results ) . toEqual ( [
57
+ { foo : "bar" } ,
58
+ { baz : 42 }
59
+ ] ) ;
60
+ } ) ;
61
+
62
+ it ( "throws on malformed JSON" , async ( ) => {
63
+ const sseLines = [
64
+ 'data: {"foo": "bar"' ,
65
+ 'data:[DONE]'
66
+ ] ;
67
+ const response = createMockResponse ( sseLines ) ;
68
+
69
+ const iterator = streamSse ( response ) [ Symbol . asyncIterator ] ( ) ;
70
+ await expect ( iterator . next ( ) ) . rejects . toThrow ( / M a l f o r m e d J S O N / ) ;
71
+ } ) ;
72
+ } ) ;
0 commit comments