@@ -586,130 +586,126 @@ describe('API integration', { timeout: 60000 }, () => {
586586 } )
587587 } )
588588
589- describe (
590- 'assembly notification' ,
591- { retry : 2 } ,
592- ( ) => {
593- type OnNotification = ( params : {
594- path ?: string
595- client : Transloadit
596- assemblyId : string
597- } ) => void
598-
599- let server : VirtualTestServer
600- afterEach ( ( ) => {
601- server ?. close ( )
602- } )
603-
604- // helper function
605- const streamToString = ( stream : IncomingMessage ) =>
606- new Promise < string > ( ( resolve , reject ) => {
607- const chunks : string [ ] = [ ]
608- stream . on ( 'data' , ( chunk ) => chunks . push ( chunk ) )
609- stream . on ( 'error' , ( err ) => reject ( err ) )
610- stream . on ( 'end' , ( ) => resolve ( chunks . join ( '' ) ) )
611- } )
612-
613- const runNotificationTest = async (
614- onNotification : OnNotification ,
615- onError : ( error : unknown ) => void ,
616- ) => {
617- const client = createClient ( )
589+ describe ( 'assembly notification' , { retry : 2 } , ( ) => {
590+ type OnNotification = ( params : {
591+ path ?: string
592+ client : Transloadit
593+ assemblyId : string
594+ } ) => void
595+
596+ let server : VirtualTestServer
597+ afterEach ( ( ) => {
598+ server ?. close ( )
599+ } )
618600
619- // listens for notifications
620- const onNotificationRequest : RequestListener = async ( req , res ) => {
621- try {
622- expect ( req . method ) . toBe ( 'POST' )
623- const body = await streamToString ( req )
624- const result = JSON . parse ( ( parse ( body ) as { transloadit : string } ) . transloadit )
625- expect ( result ) . toHaveProperty ( 'ok' )
626- if ( result . ok !== 'ASSEMBLY_COMPLETED' ) {
627- onError ( new Error ( `result.ok was ${ result . ok } ` ) )
628- return
629- }
601+ // helper function
602+ const streamToString = ( stream : IncomingMessage ) =>
603+ new Promise < string > ( ( resolve , reject ) => {
604+ const chunks : string [ ] = [ ]
605+ stream . on ( 'data' , ( chunk ) => chunks . push ( chunk ) )
606+ stream . on ( 'error' , ( err ) => reject ( err ) )
607+ stream . on ( 'end' , ( ) => resolve ( chunks . join ( '' ) ) )
608+ } )
630609
631- res . writeHead ( 200 )
632- res . end ( )
610+ const runNotificationTest = async (
611+ onNotification : OnNotification ,
612+ onError : ( error : unknown ) => void ,
613+ ) => {
614+ const client = createClient ( )
633615
634- onNotification ( { path : req . url , client, assemblyId : result . assembly_id } )
635- } catch ( err ) {
636- onError ( err )
616+ // listens for notifications
617+ const onNotificationRequest : RequestListener = async ( req , res ) => {
618+ try {
619+ expect ( req . method ) . toBe ( 'POST' )
620+ const body = await streamToString ( req )
621+ const result = JSON . parse ( ( parse ( body ) as { transloadit : string } ) . transloadit )
622+ expect ( result ) . toHaveProperty ( 'ok' )
623+ if ( result . ok !== 'ASSEMBLY_COMPLETED' ) {
624+ onError ( new Error ( `result.ok was ${ result . ok } ` ) )
625+ return
637626 }
638- }
639627
640- try {
641- server = await createVirtualTestServer ( onNotificationRequest )
642- await createAssembly ( client , { params : { ...genericParams , notify_url : server . url } } )
628+ res . writeHead ( 200 )
629+ res . end ( )
630+
631+ onNotification ( { path : req . url , client, assemblyId : result . assembly_id } )
643632 } catch ( err ) {
644633 onError ( err )
645634 }
646635 }
647636
648- it ( 'should send a notification upon assembly completion' , async ( ) => {
649- await new Promise < void > ( ( resolve , reject ) => {
650- const onNotification : OnNotification = async ( { path } ) => {
651- try {
652- expect ( path ) . toBe ( '/' )
653- resolve ( )
654- } catch ( err ) {
655- reject ( err )
656- }
637+ try {
638+ server = await createVirtualTestServer ( onNotificationRequest )
639+ await createAssembly ( client , { params : { ...genericParams , notify_url : server . url } } )
640+ } catch ( err ) {
641+ onError ( err )
642+ }
643+ }
644+
645+ it ( 'should send a notification upon assembly completion' , async ( ) => {
646+ await new Promise < void > ( ( resolve , reject ) => {
647+ const onNotification : OnNotification = async ( { path } ) => {
648+ try {
649+ expect ( path ) . toBe ( '/' )
650+ resolve ( )
651+ } catch ( err ) {
652+ reject ( err )
657653 }
658- runNotificationTest ( onNotification , reject )
659- } )
654+ }
655+ runNotificationTest ( onNotification , reject )
660656 } )
657+ } )
661658
662- it ( 'should replay the notification when requested' , async ( ) => {
663- let secondNotification = false
664-
665- await new Promise < void > ( ( resolve , reject ) => {
666- const onNotification : OnNotification = async ( { path, client, assemblyId } ) => {
667- const newPath = '/newPath'
668- const newUrl = `${ server . url } ${ newPath } `
669-
670- // I think there are some eventual consistency issues here
671- await setTimeout ( 1000 )
672-
673- const result = await client . getAssembly ( assemblyId )
659+ it ( 'should replay the notification when requested' , async ( ) => {
660+ let secondNotification = false
674661
675- expect ( [ 'successful' , 'processing' ] ) . toContain ( result . notify_status )
676- expect ( result . notify_response_code ) . toBe ( 200 )
662+ await new Promise < void > ( ( resolve , reject ) => {
663+ const onNotification : OnNotification = async ( { path, client, assemblyId } ) => {
664+ const newPath = '/newPath'
665+ const newUrl = `${ server . url } ${ newPath } `
677666
678- if ( secondNotification ) {
679- expect ( path ) . toBe ( newPath )
667+ // I think there are some eventual consistency issues here
668+ await setTimeout ( 1000 )
680669
681- // notify_url will not get updated to new URL
682- expect ( result . notify_url ) . toBe ( server . url )
670+ const result = await client . getAssembly ( assemblyId )
683671
684- try {
685- // If we quit immediately, things will not get cleaned up and jest will hang
686- await setTimeout ( 2000 )
687- resolve ( )
688- } catch ( err ) {
689- reject ( err )
690- }
672+ expect ( [ 'successful' , 'processing' ] ) . toContain ( result . notify_status )
673+ expect ( result . notify_response_code ) . toBe ( 200 )
691674
692- return
693- }
675+ if ( secondNotification ) {
676+ expect ( path ) . toBe ( newPath )
694677
695- secondNotification = true
678+ // notify_url will not get updated to new URL
679+ expect ( result . notify_url ) . toBe ( server . url )
696680
697681 try {
698- expect ( path ) . toBe ( '/' )
699- expect ( result . notify_url ) . toBe ( server . url )
700-
682+ // If we quit immediately, things will not get cleaned up and jest will hang
701683 await setTimeout ( 2000 )
702- await client . replayAssemblyNotification ( assemblyId , { notify_url : newUrl } )
684+ resolve ( )
703685 } catch ( err ) {
704686 reject ( err )
705687 }
688+
689+ return
706690 }
707691
708- runNotificationTest ( onNotification , reject )
709- } )
692+ secondNotification = true
693+
694+ try {
695+ expect ( path ) . toBe ( '/' )
696+ expect ( result . notify_url ) . toBe ( server . url )
697+
698+ await setTimeout ( 2000 )
699+ await client . replayAssemblyNotification ( assemblyId , { notify_url : newUrl } )
700+ } catch ( err ) {
701+ reject ( err )
702+ }
703+ }
704+
705+ runNotificationTest ( onNotification , reject )
710706 } )
711- } ,
712- )
707+ } )
708+ } )
713709
714710 describe ( 'template methods' , ( ) => {
715711 // can contain only lowercase latin letters, numbers, and dashes.
0 commit comments