@@ -93,6 +93,141 @@ describe(`ConstructorIO - Agent${bundledDescriptionSuffix}`, () => {
9393 expect ( url ) . not . contain ( ' ' ) ;
9494 expect ( url ) . contain ( encodeURIComponentRFC3986 ( intentWithSpaces ) ) ;
9595 } ) ;
96+
97+ it ( 'should include threadId parameter when provided' , ( ) => {
98+ const url = createAgentUrl (
99+ 'running shoes' ,
100+ { ...defaultParameters , threadId : 'test-thread-id' } ,
101+ defaultOptions ,
102+ ) ;
103+ const requestedUrlParams = qs . parse ( url . split ( '?' ) ?. [ 1 ] ) ;
104+
105+ expect ( requestedUrlParams ) . to . have . property ( 'thread_id' ) . to . equal ( 'test-thread-id' ) ;
106+ } ) ;
107+
108+ it ( 'should include guard parameter when provided as true' , ( ) => {
109+ const url = createAgentUrl (
110+ 'running shoes' ,
111+ { ...defaultParameters , guard : true } ,
112+ defaultOptions ,
113+ ) ;
114+ const requestedUrlParams = qs . parse ( url . split ( '?' ) ?. [ 1 ] ) ;
115+
116+ expect ( requestedUrlParams ) . to . have . property ( 'guard' ) . to . equal ( 'true' ) ;
117+ } ) ;
118+
119+ it ( 'should include guard parameter when provided as false' , ( ) => {
120+ const url = createAgentUrl (
121+ 'running shoes' ,
122+ { ...defaultParameters , guard : false } ,
123+ defaultOptions ,
124+ ) ;
125+ const requestedUrlParams = qs . parse ( url . split ( '?' ) ?. [ 1 ] ) ;
126+
127+ expect ( requestedUrlParams ) . to . have . property ( 'guard' ) . to . equal ( 'false' ) ;
128+ } ) ;
129+
130+ it ( 'should include numResultsPerEvent parameter when provided' , ( ) => {
131+ const url = createAgentUrl (
132+ 'running shoes' ,
133+ { ...defaultParameters , numResultsPerEvent : 5 } ,
134+ defaultOptions ,
135+ ) ;
136+ const requestedUrlParams = qs . parse ( url . split ( '?' ) ?. [ 1 ] ) ;
137+
138+ expect ( requestedUrlParams ) . to . have . property ( 'num_results_per_event' ) . to . equal ( '5' ) ;
139+ } ) ;
140+
141+ it ( 'should include numResultEvents parameter when provided' , ( ) => {
142+ const url = createAgentUrl (
143+ 'running shoes' ,
144+ { ...defaultParameters , numResultEvents : 3 } ,
145+ defaultOptions ,
146+ ) ;
147+ const requestedUrlParams = qs . parse ( url . split ( '?' ) ?. [ 1 ] ) ;
148+
149+ expect ( requestedUrlParams ) . to . have . property ( 'num_result_events' ) . to . equal ( '3' ) ;
150+ } ) ;
151+
152+ it ( 'should include numResultsPerPage parameter when provided' , ( ) => {
153+ const url = createAgentUrl (
154+ 'running shoes' ,
155+ { ...defaultParameters , numResultsPerPage : 10 } ,
156+ defaultOptions ,
157+ ) ;
158+ const requestedUrlParams = qs . parse ( url . split ( '?' ) ?. [ 1 ] ) ;
159+
160+ expect ( requestedUrlParams ) . to . have . property ( 'num_results_per_page' ) . to . equal ( '10' ) ;
161+ } ) ;
162+
163+ it ( 'should include preFilterExpression as JSON string when provided as object' , ( ) => {
164+ const preFilterExpression = { and : [ { name : 'brand' , value : 'Nike' } ] } ;
165+ const url = createAgentUrl (
166+ 'running shoes' ,
167+ { ...defaultParameters , preFilterExpression } ,
168+ defaultOptions ,
169+ ) ;
170+ const requestedUrlParams = qs . parse ( url . split ( '?' ) ?. [ 1 ] ) ;
171+
172+ expect ( requestedUrlParams ) . to . have . property ( 'pre_filter_expression' ) . to . equal ( JSON . stringify ( preFilterExpression ) ) ;
173+ } ) ;
174+
175+ it ( 'should include qsParam as JSON string when provided as object' , ( ) => {
176+ const qsParam = { section : 'Products' } ;
177+ const url = createAgentUrl (
178+ 'running shoes' ,
179+ { ...defaultParameters , qsParam } ,
180+ defaultOptions ,
181+ ) ;
182+ const requestedUrlParams = qs . parse ( url . split ( '?' ) ?. [ 1 ] ) ;
183+
184+ expect ( requestedUrlParams ) . to . have . property ( 'qs' ) . to . equal ( JSON . stringify ( qsParam ) ) ;
185+ } ) ;
186+
187+ it ( 'should include fmtOptions when provided' , ( ) => {
188+ const fmtOptions = { fields : [ 'title' , 'image_url' ] , hidden_fields : [ 'price' ] } ;
189+ const url = createAgentUrl (
190+ 'running shoes' ,
191+ { ...defaultParameters , fmtOptions } ,
192+ defaultOptions ,
193+ ) ;
194+ const requestedUrlParams = qs . parse ( url . split ( '?' ) ?. [ 1 ] ) ;
195+
196+ expect ( requestedUrlParams ) . to . have . property ( 'fmt_options' ) ;
197+ expect ( requestedUrlParams . fmt_options ) . to . have . property ( 'fields' ) ;
198+ expect ( requestedUrlParams . fmt_options ) . to . have . property ( 'hidden_fields' ) ;
199+ } ) ;
200+
201+ it ( 'should include user segments when provided in options' , ( ) => {
202+ const url = createAgentUrl ( 'running shoes' , defaultParameters , {
203+ ...defaultOptions ,
204+ segments : [ 'segment1' , 'segment2' ] ,
205+ } ) ;
206+ const requestedUrlParams = qs . parse ( url . split ( '?' ) ?. [ 1 ] ) ;
207+
208+ expect ( requestedUrlParams ) . to . have . property ( 'us' ) ;
209+ } ) ;
210+
211+ it ( 'should include userId when provided in options' , ( ) => {
212+ const url = createAgentUrl ( 'running shoes' , defaultParameters , {
213+ ...defaultOptions ,
214+ userId : 'user-123' ,
215+ } ) ;
216+ const requestedUrlParams = qs . parse ( url . split ( '?' ) ?. [ 1 ] ) ;
217+
218+ expect ( requestedUrlParams ) . to . have . property ( 'ui' ) . to . equal ( 'user-123' ) ;
219+ } ) ;
220+
221+ it ( 'should include testCells when provided in options' , ( ) => {
222+ const url = createAgentUrl ( 'running shoes' , defaultParameters , {
223+ ...defaultOptions ,
224+ testCells : { cellA : 'variant1' , cellB : 'variant2' } ,
225+ } ) ;
226+ const requestedUrlParams = qs . parse ( url . split ( '?' ) ?. [ 1 ] ) ;
227+
228+ expect ( requestedUrlParams ) . to . have . property ( 'ef-cellA' ) . to . equal ( 'variant1' ) ;
229+ expect ( requestedUrlParams ) . to . have . property ( 'ef-cellB' ) . to . equal ( 'variant2' ) ;
230+ } ) ;
96231 } ) ;
97232
98233 // setupEventListeners util Tests
@@ -196,6 +331,42 @@ describe(`ConstructorIO - Agent${bundledDescriptionSuffix}`, () => {
196331 done ( ) ;
197332 } ) ;
198333 } ) ;
334+
335+ it ( 'should enqueue MESSAGE event data into the stream' , ( done ) => {
336+ const eventType = Agent . EventTypes . MESSAGE ;
337+ const eventData = { text : 'Here are some recommendations' } ;
338+
339+ setupEventListeners ( mockEventSource , mockStreamController , Agent . EventTypes ) ;
340+
341+ const messageCallback = mockEventSource . addEventListener
342+ . getCalls ( )
343+ . find ( ( call ) => call . args [ 0 ] === eventType ) . args [ 1 ] ;
344+
345+ messageCallback ( { data : JSON . stringify ( eventData ) } ) ;
346+
347+ setImmediate ( ( ) => {
348+ expect ( mockStreamController . enqueue . calledWith ( { type : eventType , data : eventData } ) ) . to . be . true ;
349+ done ( ) ;
350+ } ) ;
351+ } ) ;
352+
353+ it ( 'should enqueue FOLLOW_UP_QUESTIONS event data into the stream' , ( done ) => {
354+ const eventType = Agent . EventTypes . FOLLOW_UP_QUESTIONS ;
355+ const eventData = { questions : [ 'What size?' , 'What color?' ] } ;
356+
357+ setupEventListeners ( mockEventSource , mockStreamController , Agent . EventTypes ) ;
358+
359+ const followUpCallback = mockEventSource . addEventListener
360+ . getCalls ( )
361+ . find ( ( call ) => call . args [ 0 ] === eventType ) . args [ 1 ] ;
362+
363+ followUpCallback ( { data : JSON . stringify ( eventData ) } ) ;
364+
365+ setImmediate ( ( ) => {
366+ expect ( mockStreamController . enqueue . calledWith ( { type : eventType , data : eventData } ) ) . to . be . true ;
367+ done ( ) ;
368+ } ) ;
369+ } ) ;
199370 } ) ;
200371
201372 describe ( 'getAgentResultsStream' , ( ) => {
@@ -265,4 +436,5 @@ describe(`ConstructorIO - Agent${bundledDescriptionSuffix}`, () => {
265436 reader . cancel ( ) ;
266437 } ) ;
267438 } ) ;
439+
268440} ) ;
0 commit comments