@@ -243,4 +243,151 @@ describe('MockSuite', () => {
243243 expect ( actualError . status ) . toStrictEqual ( 429 ) ;
244244 expect ( endTime - startTime ) . toStrictEqual ( 10 ) ;
245245 } ) ;
246+
247+ describe ( 'StreamingMode' , ( ) => {
248+
249+ test ( 'Setting streaming adds deliveryMode=stream to outgoing activity' , ( ) => {
250+ // override directline with streaming enabled
251+ directline = new DirectLineExport . DirectLine ( { ...services , streaming : true } ) ;
252+
253+ const streamingActivity = DirectLineMock . mockActivity ( 'streaming-test' ) ;
254+ const scenario = function * ( ) : IterableIterator < Observable < unknown > > {
255+ yield Observable . timer ( 200 , scheduler ) ;
256+ yield directline . postActivity ( streamingActivity ) ;
257+ } ;
258+
259+ subscriptions . push ( lazyConcat ( scenario ( ) ) . observeOn ( scheduler ) . subscribe ( ) ) ;
260+
261+ const actual : Array < DirectLineExport . Activity > = [ ] ;
262+ subscriptions . push ( directline . activity$ . subscribe ( a => actual . push ( a ) ) ) ;
263+
264+ scheduler . flush ( ) ;
265+
266+ expect ( streamingActivity . deliveryMode ) . toStrictEqual ( 'stream' ) ;
267+ expect ( actual [ 0 ] . deliveryMode ) . toStrictEqual ( 'stream' ) ;
268+ } ) ;
269+
270+ test ( 'Not setting streaming does not add deliveryMode at all to outgoing activity' , ( ) => {
271+ const normalActivity = DirectLineMock . mockActivity ( 'normal-test' ) ;
272+ const scenario = function * ( ) : IterableIterator < Observable < unknown > > {
273+ yield Observable . timer ( 200 , scheduler ) ;
274+ yield directline . postActivity ( normalActivity ) ;
275+ } ;
276+
277+ subscriptions . push ( lazyConcat ( scenario ( ) ) . observeOn ( scheduler ) . subscribe ( ) ) ;
278+
279+ const actual : Array < DirectLineExport . Activity > = [ ] ;
280+ subscriptions . push ( directline . activity$ . subscribe ( a => actual . push ( a ) ) ) ;
281+
282+ scheduler . flush ( ) ;
283+
284+ expect ( normalActivity . deliveryMode ) . toBeUndefined ( ) ;
285+ expect ( actual [ 0 ] . deliveryMode ) . toBeUndefined ( ) ;
286+ } ) ;
287+
288+ test ( 'Setting streaming overrides passed deliveryMode "normal" in activity to "stream"' , ( ) => {
289+ directline = new DirectLineExport . DirectLine ( { ...services , streaming : true } ) ;
290+
291+ const presetActivity : DirectLineExport . Message = {
292+ type : 'message' ,
293+ from : { id : 'sender' } ,
294+ text : 'preset' ,
295+ deliveryMode : 'normal'
296+ } ;
297+
298+ const scenario = function * ( ) : IterableIterator < Observable < unknown > > {
299+ yield Observable . timer ( 200 , scheduler ) ;
300+ yield directline . postActivity ( presetActivity ) ;
301+ } ;
302+
303+ subscriptions . push ( lazyConcat ( scenario ( ) ) . observeOn ( scheduler ) . subscribe ( ) ) ;
304+
305+ const actual : Array < DirectLineExport . Activity > = [ ] ;
306+ subscriptions . push ( directline . activity$ . subscribe ( a => actual . push ( a ) ) ) ;
307+
308+ scheduler . flush ( ) ;
309+
310+ expect ( presetActivity . deliveryMode ) . toStrictEqual ( 'stream' ) ;
311+ expect ( actual [ 0 ] . deliveryMode ) . toStrictEqual ( 'stream' ) ;
312+ } ) ;
313+
314+ test ( 'Not setting streaming preserves passed deliveryMode "normal" in activity' , ( ) => {
315+ const presetActivity : DirectLineExport . Message = {
316+ type : 'message' ,
317+ from : { id : 'sender' } ,
318+ text : 'preset-nonstream' ,
319+ deliveryMode : 'normal'
320+ } ;
321+
322+ const scenario = function * ( ) : IterableIterator < Observable < unknown > > {
323+ yield Observable . timer ( 200 , scheduler ) ;
324+ yield directline . postActivity ( presetActivity ) ;
325+ } ;
326+
327+ subscriptions . push ( lazyConcat ( scenario ( ) ) . observeOn ( scheduler ) . subscribe ( ) ) ;
328+
329+ const actual : Array < DirectLineExport . Activity > = [ ] ;
330+ subscriptions . push ( directline . activity$ . subscribe ( a => actual . push ( a ) ) ) ;
331+
332+ scheduler . flush ( ) ;
333+
334+ expect ( presetActivity . deliveryMode ) . toStrictEqual ( 'normal' ) ;
335+ expect ( actual [ 0 ] . deliveryMode ) . toStrictEqual ( 'normal' ) ;
336+ } ) ;
337+
338+ test . each ( [
339+ { streaming : true , expectedDeliveryMode : 'stream' , testName : 'Streaming' } ,
340+ { streaming : false , expectedDeliveryMode : undefined , testName : 'Non-streaming' }
341+ ] ) ( '$testName + 403 post returns retry and preserves deliveryMode' , ( { streaming, expectedDeliveryMode } ) => {
342+ services . ajax = DirectLineMock . mockAjax ( server , ( urlOrRequest ) => {
343+ if ( typeof urlOrRequest === 'string' ) {
344+ throw new Error ( ) ;
345+ }
346+
347+ if ( urlOrRequest . url && urlOrRequest . url . indexOf ( '/conversations' ) > 0 && ! / a c t i v i t i e s / u. test ( urlOrRequest . url ) ) {
348+ // start conversation
349+ const response : Partial < AjaxResponse > = {
350+ response : server . conversation ,
351+ status : 201 ,
352+ xhr : { getResponseHeader : ( ) => 'n/a' } as unknown as XMLHttpRequest
353+ } ;
354+ return response as AjaxResponse ;
355+ }
356+
357+ if ( urlOrRequest . url && / a c t i v i t i e s / u. test ( urlOrRequest . url ) ) {
358+ const response : Partial < AjaxResponse > = {
359+ status : 403 ,
360+ xhr : { getResponseHeader : ( ) => 'n/a' } as unknown as XMLHttpRequest
361+ } ;
362+ const error = new Error ( 'Forbidden' ) ;
363+ throw Object . assign ( error , response ) ;
364+ }
365+
366+ throw new Error ( ) ;
367+ } ) ;
368+
369+ directline = new DirectLineExport . DirectLine ( {
370+ ...services ,
371+ ...( streaming ? { streaming : true } : { } )
372+ } ) ;
373+
374+ const retryActivity = DirectLineMock . mockActivity ( 'will-retry' ) ;
375+ const scenario = function * ( ) : IterableIterator < Observable < unknown > > {
376+ yield Observable . timer ( 200 , scheduler ) ;
377+ yield directline . postActivity ( retryActivity ) ;
378+ } ;
379+
380+ let postResult : string | undefined ;
381+ subscriptions . push ( lazyConcat ( scenario ( ) ) . observeOn ( scheduler ) . subscribe ( {
382+ next : v => { postResult = v as string ; } ,
383+ error : ( ) => { } ,
384+ complete : ( ) => { }
385+ } ) ) ;
386+
387+ scheduler . flush ( ) ;
388+
389+ expect ( retryActivity . deliveryMode ) . toStrictEqual ( expectedDeliveryMode ) ;
390+ expect ( postResult ) . toStrictEqual ( 'retry' ) ;
391+ } ) ;
392+ } ) ;
246393} ) ;
0 commit comments