@@ -29,6 +29,8 @@ describe('parseComplexResponse function', () => {
29
29
reader : createTestReader ( [ '0:"Hello"\n' ] ) ,
30
30
abortControllerRef : { current : new AbortController ( ) } ,
31
31
update : mockUpdate ,
32
+ generateId : ( ) => 'test-id' ,
33
+ getCurrentDate : ( ) => new Date ( 0 ) ,
32
34
} ) ;
33
35
34
36
const expectedMessage = assistantTextMessage ( 'Hello' ) ;
@@ -37,9 +39,18 @@ describe('parseComplexResponse function', () => {
37
39
expect ( mockUpdate ) . toHaveBeenCalledTimes ( 1 ) ;
38
40
expect ( mockUpdate . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ expectedMessage ] ) ;
39
41
40
- // check the prefix map:
41
- expect ( result ) . toHaveProperty ( 'text' ) ;
42
- expect ( result . text ) . toEqual ( expectedMessage ) ;
42
+ // check the result
43
+ expect ( result ) . toEqual ( {
44
+ messages : [
45
+ {
46
+ content : 'Hello' ,
47
+ createdAt : new Date ( 0 ) ,
48
+ id : 'test-id' ,
49
+ role : 'assistant' ,
50
+ } ,
51
+ ] ,
52
+ data : [ ] ,
53
+ } ) ;
43
54
} ) ;
44
55
45
56
it ( 'should parse a sequence of text messages' , async ( ) => {
@@ -54,6 +65,8 @@ describe('parseComplexResponse function', () => {
54
65
] ) ,
55
66
abortControllerRef : { current : new AbortController ( ) } ,
56
67
update : mockUpdate ,
68
+ generateId : ( ) => 'test-id' ,
69
+ getCurrentDate : ( ) => new Date ( 0 ) ,
57
70
} ) ;
58
71
59
72
// check the mockUpdate call:
@@ -71,20 +84,31 @@ describe('parseComplexResponse function', () => {
71
84
assistantTextMessage ( 'Hello, world.' ) ,
72
85
] ) ;
73
86
74
- // check the prefix map:
75
- expect ( result ) . toHaveProperty ( 'text' ) ;
76
- expect ( result . text ) . toEqual ( assistantTextMessage ( 'Hello, world.' ) ) ;
87
+ // check the result
88
+ expect ( result ) . toEqual ( {
89
+ messages : [
90
+ {
91
+ content : 'Hello, world.' ,
92
+ createdAt : new Date ( 0 ) ,
93
+ id : 'test-id' ,
94
+ role : 'assistant' ,
95
+ } ,
96
+ ] ,
97
+ data : [ ] ,
98
+ } ) ;
77
99
} ) ;
78
100
79
101
it ( 'should parse a function call' , async ( ) => {
80
102
const mockUpdate = jest . fn ( ) ;
81
103
82
104
const result = await parseComplexResponse ( {
83
105
reader : createTestReader ( [
84
- '1:"{\\ "function_call\\": {\\ "name\\": \\ "get_current_weather\\", \\ "arguments\\": \\ "{\\\\ n\\\\\\ "location\\\\\\ ": \\\\\\ "Charlottesville, Virginia\\\\\\ ",\\\\ n\\\\\\ "format\\\\\\ ": \\\\\\ "celsius\\\\\\ "\\\\n}\\ "}}" \n' ,
106
+ '1:{ "function_call":{ "name": "get_current_weather", "arguments": "{\\n\\"location\\": \\"Charlottesville, Virginia\\",\\n\\"format\\": \\"celsius\\"\\n} "}}\n' ,
85
107
] ) ,
86
108
abortControllerRef : { current : new AbortController ( ) } ,
87
109
update : mockUpdate ,
110
+ generateId : ( ) => 'test-id' ,
111
+ getCurrentDate : ( ) => new Date ( 0 ) ,
88
112
} ) ;
89
113
90
114
// check the mockUpdate call:
@@ -104,21 +128,24 @@ describe('parseComplexResponse function', () => {
104
128
} ,
105
129
] ) ;
106
130
107
- // check the prefix map:
108
- expect ( result . function_call ) . toEqual ( {
109
- id : expect . any ( String ) ,
110
- role : 'assistant' ,
111
- content : '' ,
112
- name : 'get_current_weather' ,
113
- function_call : {
114
- name : 'get_current_weather' ,
115
- arguments :
116
- '{\n"location": "Charlottesville, Virginia",\n"format": "celsius"\n}' ,
117
- } ,
118
- createdAt : expect . any ( Date ) ,
131
+ // check the result
132
+ expect ( result ) . toEqual ( {
133
+ messages : [
134
+ {
135
+ content : '' ,
136
+ createdAt : new Date ( 0 ) ,
137
+ id : 'test-id' ,
138
+ role : 'assistant' ,
139
+ function_call : {
140
+ name : 'get_current_weather' ,
141
+ arguments :
142
+ '{\n"location": "Charlottesville, Virginia",\n"format": "celsius"\n}' ,
143
+ } ,
144
+ name : 'get_current_weather' ,
145
+ } ,
146
+ ] ,
147
+ data : [ ] ,
119
148
} ) ;
120
- expect ( result ) . not . toHaveProperty ( 'text' ) ;
121
- expect ( result ) . not . toHaveProperty ( 'data' ) ;
122
149
} ) ;
123
150
124
151
it ( 'should parse a combination of a data and a text message' , async ( ) => {
@@ -127,31 +154,74 @@ describe('parseComplexResponse function', () => {
127
154
// Execute the parser function
128
155
const result = await parseComplexResponse ( {
129
156
reader : createTestReader ( [
130
- '2:"[{\\ "t1\\":\\ "v1\\ "}]" \n' ,
157
+ '2:[{ "t1": "v1"}]\n' ,
131
158
'0:"Sample text message."\n' ,
132
159
] ) ,
133
160
abortControllerRef : { current : new AbortController ( ) } ,
134
161
update : mockUpdate ,
162
+ generateId : ( ) => 'test-id' ,
163
+ getCurrentDate : ( ) => new Date ( 0 ) ,
135
164
} ) ;
136
165
137
- const expectedData = [ { t1 : 'v1' } ] ;
138
-
139
166
// check the mockUpdate call:
140
167
expect ( mockUpdate ) . toHaveBeenCalledTimes ( 2 ) ;
141
168
142
169
expect ( mockUpdate . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ ] ) ;
143
- expect ( mockUpdate . mock . calls [ 0 ] [ 1 ] ) . toEqual ( expectedData ) ;
170
+ expect ( mockUpdate . mock . calls [ 0 ] [ 1 ] ) . toEqual ( [ { t1 : 'v1' } ] ) ;
144
171
145
172
expect ( mockUpdate . mock . calls [ 1 ] [ 0 ] ) . toEqual ( [
146
173
assistantTextMessage ( 'Sample text message.' ) ,
147
174
] ) ;
148
- expect ( mockUpdate . mock . calls [ 1 ] [ 1 ] ) . toEqual ( expectedData ) ;
175
+ expect ( mockUpdate . mock . calls [ 1 ] [ 1 ] ) . toEqual ( [ { t1 : 'v1' } ] ) ;
176
+
177
+ // check the result
178
+ expect ( result ) . toEqual ( {
179
+ messages : [
180
+ {
181
+ content : 'Sample text message.' ,
182
+ createdAt : new Date ( 0 ) ,
183
+ id : 'test-id' ,
184
+ role : 'assistant' ,
185
+ } ,
186
+ ] ,
187
+ data : [ { t1 : 'v1' } ] ,
188
+ } ) ;
189
+ } ) ;
149
190
150
- // check the prefix map:
151
- expect ( result ) . toHaveProperty ( 'data' ) ;
152
- expect ( result . data ) . toEqual ( expectedData ) ;
191
+ it ( 'should parse multiple data messages incl. primitive values' , async ( ) => {
192
+ const mockUpdate = jest . fn ( ) ;
153
193
154
- expect ( result ) . toHaveProperty ( 'text' ) ;
155
- expect ( result . text ) . toEqual ( assistantTextMessage ( 'Sample text message.' ) ) ;
194
+ // Execute the parser function
195
+ const result = await parseComplexResponse ( {
196
+ reader : createTestReader ( [
197
+ '2:[{"t1":"v1"}, 3]\n' ,
198
+ '2:[null,false,"text"]\n' ,
199
+ ] ) ,
200
+ abortControllerRef : { current : new AbortController ( ) } ,
201
+ update : mockUpdate ,
202
+ generateId : ( ) => 'test-id' ,
203
+ getCurrentDate : ( ) => new Date ( 0 ) ,
204
+ } ) ;
205
+
206
+ // check the mockUpdate call:
207
+ expect ( mockUpdate ) . toHaveBeenCalledTimes ( 2 ) ;
208
+
209
+ expect ( mockUpdate . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ ] ) ;
210
+ expect ( mockUpdate . mock . calls [ 0 ] [ 1 ] ) . toEqual ( [ { t1 : 'v1' } , 3 ] ) ;
211
+
212
+ expect ( mockUpdate . mock . calls [ 1 ] [ 0 ] ) . toEqual ( [ ] ) ;
213
+ expect ( mockUpdate . mock . calls [ 1 ] [ 1 ] ) . toEqual ( [
214
+ { t1 : 'v1' } ,
215
+ 3 ,
216
+ null ,
217
+ false ,
218
+ 'text' ,
219
+ ] ) ;
220
+
221
+ // check the result
222
+ expect ( result ) . toEqual ( {
223
+ messages : [ ] ,
224
+ data : [ { t1 : 'v1' } , 3 , null , false , 'text' ] ,
225
+ } ) ;
156
226
} ) ;
157
227
} ) ;
0 commit comments