@@ -44,8 +44,31 @@ const MockPrismaService = {
44
44
}
45
45
} ,
46
46
count : ( ) => 10 ,
47
- update : jest . fn ( )
48
- }
47
+ update : jest . fn ( ) ,
48
+ deleteMany : ( filter ) => {
49
+ deletedIds . push ( { 'bot' : filter . where . id . in } ) ;
50
+ }
51
+ } ,
52
+ service : {
53
+ deleteMany : ( filter ) => {
54
+ deletedIds . push ( { 'service' : filter . where . id . in } ) ;
55
+ }
56
+ } ,
57
+ userSegment : {
58
+ deleteMany : ( filter ) => {
59
+ deletedIds . push ( { 'userSegment' : filter . where . id . in } ) ;
60
+ }
61
+ } ,
62
+ transformerConfig : {
63
+ deleteMany : ( filter ) => {
64
+ deletedIds . push ( { 'transformerConfig' : filter . where . id . in } ) ;
65
+ }
66
+ } ,
67
+ conversationLogic : {
68
+ deleteMany : ( filter ) => {
69
+ deletedIds . push ( { 'conversationLogic' : filter . where . id . in } ) ;
70
+ }
71
+ } ,
49
72
}
50
73
51
74
class MockConfigService {
@@ -320,6 +343,9 @@ const mockConfig = {
320
343
"totalRecords" : 1
321
344
} ;
322
345
346
+ // Used for delete bot testing
347
+ let deletedIds : any [ ] = [ ]
348
+
323
349
describe ( 'BotService' , ( ) => {
324
350
let botService : BotService ;
325
351
let configService : ConfigService ;
@@ -367,6 +393,17 @@ describe('BotService', () => {
367
393
. toThrowError ( new ConflictException ( "Bot already exists with the following name or starting message!" ) ) ;
368
394
} ) ;
369
395
396
+ it ( 'create bot trims bot name properly' , async ( ) => {
397
+ const mockCreateBotDtoCopy : CreateBotDto & { ownerID : string ; ownerOrgID : string } = JSON . parse ( JSON . stringify ( mockCreateBotDto ) ) ;
398
+ mockCreateBotDtoCopy . name = ' testBotExistingName ' ;
399
+ expect ( botService . create ( mockCreateBotDtoCopy , mockFile ) ) . rejects
400
+ . toThrowError ( new ConflictException ( "Bot already exists with the following name or starting message!" ) ) ;
401
+ const mockCreateBotDtoCopy2 : CreateBotDto & { ownerID : string ; ownerOrgID : string } = JSON . parse ( JSON . stringify ( mockCreateBotDto ) ) ;
402
+ mockCreateBotDtoCopy2 . startingMessage = ' testBotExistingStartingMessage' ;
403
+ expect ( botService . create ( mockCreateBotDtoCopy2 , mockFile ) ) . rejects
404
+ . toThrowError ( new ConflictException ( "Bot already exists with the following name or starting message!" ) ) ;
405
+ } ) ;
406
+
370
407
it ( 'get bot all data test' , async ( ) => {
371
408
fetchMock . getOnce ( `${ configService . get < string > ( 'MINIO_GET_SIGNED_FILE_URL' ) } ?fileName=testImageFile` ,
372
409
'testImageUrl'
@@ -506,7 +543,7 @@ describe('BotService', () => {
506
543
} ) ;
507
544
508
545
it ( 'bot update throws NotFoundException when non existent bot is updated' , async ( ) => {
509
- fetchMock . getOnce ( `${ configService . get < string > ( 'UCI_CORE_BASE_URL' ) } ${ configService . get < string > ( 'CAFFINE_INVALIDATE_ENDPOINT' ) } ` ,
546
+ fetchMock . deleteOnce ( `${ configService . get < string > ( 'UCI_CORE_BASE_URL' ) } ${ configService . get < string > ( 'CAFFINE_INVALIDATE_ENDPOINT' ) } ` ,
510
547
true
511
548
) ;
512
549
expect ( botService . update ( 'testBotIdNotExisting' , {
@@ -605,4 +642,97 @@ describe('BotService', () => {
605
642
) . toBe ( true ) ;
606
643
fetchMock . restore ( ) ;
607
644
} ) ;
645
+
646
+ it ( 'bot delete with bot id list works as expected' , async ( ) => {
647
+ fetchMock . delete ( `${ configService . get < string > ( 'UCI_CORE_BASE_URL' ) } ${ configService . get < string > ( 'CAFFINE_INVALIDATE_ENDPOINT' ) } ` ,
648
+ true
649
+ ) ;
650
+ mockBotsDb [ 0 ] . status = BotStatus . DISABLED ;
651
+ await botService . remove ( { ids : [ 'testId' ] , endDate : null } ) ;
652
+ expect ( deletedIds ) . toEqual (
653
+ [
654
+ { 'service' : [ 'testId' ] } ,
655
+ { 'userSegment' : [ 'testUserId' ] } ,
656
+ { 'transformerConfig' : [ 'testTransformerId' ] } ,
657
+ { 'conversationLogic' : [ 'testLogicId' ] } ,
658
+ { 'bot' : [ 'testId' ] } ,
659
+ ]
660
+ ) ;
661
+ deletedIds = [ ] ;
662
+ await botService . remove ( { ids : [ 'nonExisting' ] , endDate : null } ) ;
663
+ expect ( deletedIds ) . toEqual (
664
+ [
665
+ { 'service' : [ ] } ,
666
+ { 'userSegment' : [ ] } ,
667
+ { 'transformerConfig' : [ ] } ,
668
+ { 'conversationLogic' : [ ] } ,
669
+ { 'bot' : [ ] } ,
670
+ ]
671
+ ) ;
672
+ deletedIds = [ ] ;
673
+ expect ( fetchMock . called (
674
+ `${ configService . get < string > ( 'UCI_CORE_BASE_URL' ) } ${ configService . get < string > ( 'CAFFINE_INVALIDATE_ENDPOINT' ) } `
675
+ ) )
676
+ . toBe ( true ) ;
677
+ mockBotsDb [ 0 ] . status = BotStatus . ENABLED ;
678
+ fetchMock . restore ( ) ;
679
+ } ) ;
680
+
681
+ it ( 'bot delete with endDate works as expected' , async ( ) => {
682
+ fetchMock . delete ( `${ configService . get < string > ( 'UCI_CORE_BASE_URL' ) } ${ configService . get < string > ( 'CAFFINE_INVALIDATE_ENDPOINT' ) } ` ,
683
+ true
684
+ ) ;
685
+ mockBotsDb [ 0 ] . status = BotStatus . DISABLED ;
686
+ await botService . remove ( { ids : null , endDate : '2025-12-01' } ) ;
687
+ expect ( deletedIds ) . toEqual (
688
+ [
689
+ { 'service' : [ 'testId' ] } ,
690
+ { 'userSegment' : [ 'testUserId' ] } ,
691
+ { 'transformerConfig' : [ 'testTransformerId' ] } ,
692
+ { 'conversationLogic' : [ 'testLogicId' ] } ,
693
+ { 'bot' : [ 'testId' ] } ,
694
+ ]
695
+ ) ;
696
+ deletedIds = [ ] ;
697
+ await botService . remove ( { ids : null , endDate : '2023-12-01' } ) ;
698
+ expect ( deletedIds ) . toEqual (
699
+ [
700
+ { 'service' : [ ] } ,
701
+ { 'userSegment' : [ ] } ,
702
+ { 'transformerConfig' : [ ] } ,
703
+ { 'conversationLogic' : [ ] } ,
704
+ { 'bot' : [ ] } ,
705
+ ]
706
+ ) ;
707
+ expect ( fetchMock . called (
708
+ `${ configService . get < string > ( 'UCI_CORE_BASE_URL' ) } ${ configService . get < string > ( 'CAFFINE_INVALIDATE_ENDPOINT' ) } `
709
+ ) )
710
+ . toBe ( true ) ;
711
+ deletedIds = [ ] ;
712
+ mockBotsDb [ 0 ] . status = BotStatus . ENABLED ;
713
+ fetchMock . restore ( ) ;
714
+ } ) ;
715
+
716
+ it ( 'bot delete only deletes disabled bots' , async ( ) => {
717
+ fetchMock . delete ( `${ configService . get < string > ( 'UCI_CORE_BASE_URL' ) } ${ configService . get < string > ( 'CAFFINE_INVALIDATE_ENDPOINT' ) } ` ,
718
+ true
719
+ ) ;
720
+ mockBotsDb [ 0 ] . status = BotStatus . ENABLED ;
721
+ await botService . remove ( { ids : [ 'testId' ] , endDate : null } ) ;
722
+ expect ( deletedIds ) . toEqual (
723
+ [
724
+ { 'service' : [ ] } ,
725
+ { 'userSegment' : [ ] } ,
726
+ { 'transformerConfig' : [ ] } ,
727
+ { 'conversationLogic' : [ ] } ,
728
+ { 'bot' : [ ] } ,
729
+ ]
730
+ ) ;
731
+ expect ( fetchMock . called (
732
+ `${ configService . get < string > ( 'UCI_CORE_BASE_URL' ) } ${ configService . get < string > ( 'CAFFINE_INVALIDATE_ENDPOINT' ) } `
733
+ ) )
734
+ . toBe ( true ) ;
735
+ deletedIds = [ ] ;
736
+ fetchMock . restore ( ) ;
737
+ } ) ;
608
738
} ) ;
0 commit comments