@@ -5,7 +5,14 @@ import {
5
5
MockRequest ,
6
6
MockResponse ,
7
7
} from "node-mocks-http" ;
8
- import { CalendarEvent , Contact , Controller } from "." ;
8
+ import {
9
+ CalendarEvent ,
10
+ CallDirection ,
11
+ CallParticipantType ,
12
+ CallState ,
13
+ Contact ,
14
+ Controller ,
15
+ } from "." ;
9
16
import { StorageCache } from "../cache" ;
10
17
import { MemoryStorageAdapter } from "../cache/storage" ;
11
18
import { APIContact } from "./api-contact.model" ;
@@ -595,3 +602,253 @@ describe("getHealth", () => {
595
602
expect ( next ) . toBeCalled ( ) ;
596
603
} ) ;
597
604
} ) ;
605
+
606
+ describe ( "handleCallEvent" , ( ) => {
607
+ let request : MockRequest < BridgeRequest > ;
608
+ let response : MockResponse < Response > ;
609
+ let next : jest . Mock ;
610
+
611
+ beforeEach ( ( ) => {
612
+ response = createResponse ( ) ;
613
+ next = jest . fn ( ) ;
614
+ } ) ;
615
+
616
+ it ( "should ignore a call event with a remote direct dial" , async ( ) => {
617
+ request = createRequest ( {
618
+ providerConfig : {
619
+ apiKey : "a1b2c3" ,
620
+ apiUrl : "http://example.com" ,
621
+ locale : "de_DE" ,
622
+ } ,
623
+ body : {
624
+ participants : [
625
+ {
626
+ type : CallParticipantType . LOCAL ,
627
+ phoneNumber : "1234567890" ,
628
+ } ,
629
+ {
630
+ type : CallParticipantType . REMOTE ,
631
+ phoneNumber : "13" ,
632
+ } ,
633
+ ] ,
634
+ id : "" ,
635
+ startTime : 0 ,
636
+ endTime : 0 ,
637
+ direction : CallDirection . IN ,
638
+ note : "" ,
639
+ state : CallState . BUSY ,
640
+ } ,
641
+ } ) ;
642
+ const controller : Controller = new Controller (
643
+ {
644
+ handleCallEvent : ( config , event ) => Promise . resolve ( "" ) ,
645
+ } ,
646
+ new StorageCache ( new MemoryStorageAdapter ( ) )
647
+ ) ;
648
+
649
+ await controller . handleCallEvent ( request , response , next ) ;
650
+
651
+ const data : string = response . _getData ( ) ;
652
+
653
+ expect ( next ) . not . toBeCalled ( ) ;
654
+ expect ( data ) . toEqual ( "Skipping call event" ) ;
655
+ } ) ;
656
+
657
+ it ( "should handle a call event" , async ( ) => {
658
+ request = createRequest ( {
659
+ providerConfig : {
660
+ apiKey : "a1b2c3" ,
661
+ apiUrl : "http://example.com" ,
662
+ locale : "de_DE" ,
663
+ } ,
664
+ body : {
665
+ participants : [
666
+ {
667
+ type : CallParticipantType . LOCAL ,
668
+ phoneNumber : "1234567890" ,
669
+ } ,
670
+ {
671
+ type : CallParticipantType . REMOTE ,
672
+ phoneNumber : "0123456789" ,
673
+ } ,
674
+ ] ,
675
+ id : "" ,
676
+ startTime : 0 ,
677
+ endTime : 0 ,
678
+ direction : CallDirection . IN ,
679
+ note : "" ,
680
+ state : CallState . BUSY ,
681
+ } ,
682
+ } ) ;
683
+ const controller : Controller = new Controller (
684
+ {
685
+ handleCallEvent : ( config , event ) => Promise . resolve ( "callRef" ) ,
686
+ } ,
687
+ new StorageCache ( new MemoryStorageAdapter ( ) )
688
+ ) ;
689
+
690
+ await controller . handleCallEvent ( request , response , next ) ;
691
+
692
+ const data : string = response . _getData ( ) ;
693
+
694
+ expect ( next ) . not . toBeCalled ( ) ;
695
+ expect ( data ) . toEqual ( "callRef" ) ;
696
+ } ) ;
697
+
698
+ it ( "should handle adapter not implementing the feature" , async ( ) => {
699
+ request = createRequest ( {
700
+ providerConfig : {
701
+ apiKey : "a1b2c3" ,
702
+ apiUrl : "http://example.com" ,
703
+ locale : "de_DE" ,
704
+ } ,
705
+ } ) ;
706
+ const controller : Controller = new Controller (
707
+ { } ,
708
+ new StorageCache ( new MemoryStorageAdapter ( ) )
709
+ ) ;
710
+
711
+ await controller . handleCallEvent ( request , response , next ) ;
712
+
713
+ expect ( next ) . toBeCalled ( ) ;
714
+ } ) ;
715
+
716
+ it ( "should handle config being missing" , async ( ) => {
717
+ request = createRequest ( { } ) ;
718
+ const controller : Controller = new Controller (
719
+ {
720
+ handleCallEvent : ( config , event ) => Promise . resolve ( "callRef" ) ,
721
+ } ,
722
+ new StorageCache ( new MemoryStorageAdapter ( ) )
723
+ ) ;
724
+
725
+ await controller . handleCallEvent ( request , response , next ) ;
726
+
727
+ expect ( next ) . toBeCalled ( ) ;
728
+ } ) ;
729
+ } ) ;
730
+
731
+ describe ( "updateCallEvent" , ( ) => {
732
+ let request : MockRequest < BridgeRequest > ;
733
+ let response : MockResponse < Response > ;
734
+ let next : jest . Mock ;
735
+
736
+ beforeEach ( ( ) => {
737
+ response = createResponse ( ) ;
738
+ next = jest . fn ( ) ;
739
+ } ) ;
740
+
741
+ it ( "should ignore a call event with a remote direct dial" , async ( ) => {
742
+ request = createRequest ( {
743
+ providerConfig : {
744
+ apiKey : "a1b2c3" ,
745
+ apiUrl : "http://example.com" ,
746
+ locale : "de_DE" ,
747
+ } ,
748
+ body : {
749
+ participants : [
750
+ {
751
+ type : CallParticipantType . LOCAL ,
752
+ phoneNumber : "1234567890" ,
753
+ } ,
754
+ {
755
+ type : CallParticipantType . REMOTE ,
756
+ phoneNumber : "13" ,
757
+ } ,
758
+ ] ,
759
+ id : "" ,
760
+ startTime : 0 ,
761
+ endTime : 0 ,
762
+ direction : CallDirection . IN ,
763
+ note : "" ,
764
+ state : CallState . BUSY ,
765
+ } ,
766
+ } ) ;
767
+ const controller : Controller = new Controller (
768
+ {
769
+ updateCallEvent : ( config , id , event ) => Promise . resolve ( ) ,
770
+ } ,
771
+ new StorageCache ( new MemoryStorageAdapter ( ) )
772
+ ) ;
773
+
774
+ await controller . updateCallEvent ( request , response , next ) ;
775
+
776
+ const data : string = response . _getData ( ) ;
777
+
778
+ expect ( next ) . not . toBeCalled ( ) ;
779
+ expect ( data ) . toEqual ( "Skipping call event" ) ;
780
+ } ) ;
781
+
782
+ it ( "should handle a call event" , async ( ) => {
783
+ request = createRequest ( {
784
+ providerConfig : {
785
+ apiKey : "a1b2c3" ,
786
+ apiUrl : "http://example.com" ,
787
+ locale : "de_DE" ,
788
+ } ,
789
+ body : {
790
+ participants : [
791
+ {
792
+ type : CallParticipantType . LOCAL ,
793
+ phoneNumber : "1234567890" ,
794
+ } ,
795
+ {
796
+ type : CallParticipantType . REMOTE ,
797
+ phoneNumber : "0123456789" ,
798
+ } ,
799
+ ] ,
800
+ id : "" ,
801
+ startTime : 0 ,
802
+ endTime : 0 ,
803
+ direction : CallDirection . IN ,
804
+ note : "" ,
805
+ state : CallState . BUSY ,
806
+ } ,
807
+ } ) ;
808
+ const controller : Controller = new Controller (
809
+ {
810
+ updateCallEvent : ( config , id , event ) => Promise . resolve ( ) ,
811
+ } ,
812
+ new StorageCache ( new MemoryStorageAdapter ( ) )
813
+ ) ;
814
+
815
+ await controller . updateCallEvent ( request , response , next ) ;
816
+
817
+ const data : string = response . _getData ( ) ;
818
+
819
+ expect ( next ) . not . toBeCalled ( ) ;
820
+ expect ( data ) . toEqual ( "" ) ;
821
+ } ) ;
822
+
823
+ it ( "should handle adapter not implementing the feature" , async ( ) => {
824
+ request = createRequest ( {
825
+ providerConfig : {
826
+ apiKey : "a1b2c3" ,
827
+ apiUrl : "http://example.com" ,
828
+ locale : "de_DE" ,
829
+ } ,
830
+ } ) ;
831
+ const controller : Controller = new Controller (
832
+ { } ,
833
+ new StorageCache ( new MemoryStorageAdapter ( ) )
834
+ ) ;
835
+
836
+ await controller . updateCallEvent ( request , response , next ) ;
837
+
838
+ expect ( next ) . toBeCalled ( ) ;
839
+ } ) ;
840
+
841
+ it ( "should handle config being missing" , async ( ) => {
842
+ request = createRequest ( { } ) ;
843
+ const controller : Controller = new Controller (
844
+ {
845
+ updateCallEvent : ( config , id , event ) => Promise . resolve ( ) ,
846
+ } ,
847
+ new StorageCache ( new MemoryStorageAdapter ( ) )
848
+ ) ;
849
+
850
+ await controller . updateCallEvent ( request , response , next ) ;
851
+
852
+ expect ( next ) . toBeCalled ( ) ;
853
+ } ) ;
854
+ } ) ;
0 commit comments