@@ -46,6 +46,7 @@ import {
4646 installBlockFixtures ,
4747} from '@/utils/test/fixtures/block' ;
4848import { installContentFixtures } from '@/utils/test/fixtures/content' ;
49+ import { installNlpValueFixtures } from '@/utils/test/fixtures/nlpvalue' ;
4950import {
5051 blockEmpty ,
5152 blockGetStarted ,
@@ -64,7 +65,6 @@ import {
6465 subscriberContextBlankInstance ,
6566} from '@/utils/test/mocks/conversation' ;
6667import {
67- mockNlpCacheMap ,
6868 mockNlpEntitiesSetOne ,
6969 nlpEntitiesGreeting ,
7070} from '@/utils/test/mocks/nlp' ;
@@ -102,6 +102,7 @@ describe('BlockService', () => {
102102 rootMongooseTestModule ( async ( ) => {
103103 await installContentFixtures ( ) ;
104104 await installBlockFixtures ( ) ;
105+ await installNlpValueFixtures ( ) ;
105106 } ) ,
106107 MongooseModule . forFeature ( [
107108 BlockModel ,
@@ -357,10 +358,7 @@ describe('BlockService', () => {
357358
358359 describe ( 'matchBestNLP' , ( ) => {
359360 it ( 'should return the block with the highest NLP score' , async ( ) => {
360- jest
361- . spyOn ( nlpEntityService , 'getNlpMap' )
362- . mockResolvedValue ( mockNlpCacheMap ) ;
363- const blocks = [ mockNlpBlock , blockGetStarted ] ; // You can add more blocks with different patterns and scores
361+ const blocks = [ mockNlpBlock , blockGetStarted ] ;
364362 const nlp = mockNlpEntitiesSetOne ;
365363 // Spy on calculateBlockScore to check if it's called
366364 const calculateBlockScoreSpy = jest . spyOn (
@@ -379,10 +377,7 @@ describe('BlockService', () => {
379377 } ) ;
380378
381379 it ( 'should return the block with the highest NLP score applying penalties' , async ( ) => {
382- jest
383- . spyOn ( nlpEntityService , 'getNlpMap' )
384- . mockResolvedValue ( mockNlpCacheMap ) ;
385- const blocks = [ mockNlpBlock , mockModifiedNlpBlock ] ; // You can add more blocks with different patterns and scores
380+ const blocks = [ mockNlpBlock , mockModifiedNlpBlock ] ;
386381 const nlp = mockNlpEntitiesSetOne ;
387382 // Spy on calculateBlockScore to check if it's called
388383 const calculateBlockScoreSpy = jest . spyOn (
@@ -401,9 +396,6 @@ describe('BlockService', () => {
401396 } ) ;
402397
403398 it ( 'another case where it should return the block with the highest NLP score applying penalties' , async ( ) => {
404- jest
405- . spyOn ( nlpEntityService , 'getNlpMap' )
406- . mockResolvedValue ( mockNlpCacheMap ) ;
407399 const blocks = [ mockModifiedNlpBlockOne , mockModifiedNlpBlockTwo ] ; // You can add more blocks with different patterns and scores
408400 const nlp = mockNlpEntitiesSetOne ;
409401 // Spy on calculateBlockScore to check if it's called
@@ -423,10 +415,7 @@ describe('BlockService', () => {
423415 } ) ;
424416
425417 it ( 'should return undefined if no blocks match or the list is empty' , async ( ) => {
426- jest
427- . spyOn ( nlpEntityService , 'getNlpMap' )
428- . mockResolvedValue ( mockNlpCacheMap ) ;
429- const blocks : BlockFull [ ] = [ ] ; // Empty block array
418+ const blocks : BlockFull [ ] = [ ] ;
430419 const nlp = mockNlpEntitiesSetOne ;
431420
432421 const bestBlock = await blockService . matchBestNLP ( blocks , nlp ) ;
@@ -438,9 +427,7 @@ describe('BlockService', () => {
438427
439428 describe ( 'calculateBlockScore' , ( ) => {
440429 it ( 'should calculate the correct NLP score for a block' , async ( ) => {
441- jest
442- . spyOn ( nlpEntityService , 'getNlpMap' )
443- . mockResolvedValue ( mockNlpCacheMap ) ;
430+ const getNlpCacheMapSpy = jest . spyOn ( nlpEntityService , 'getNlpMap' ) ;
444431 const score = await blockService . calculateBlockScore (
445432 mockNlpPatternsSetOne ,
446433 mockNlpEntitiesSetOne ,
@@ -449,16 +436,15 @@ describe('BlockService', () => {
449436 mockNlpPatternsSetTwo ,
450437 mockNlpEntitiesSetOne ,
451438 ) ;
452-
439+ expect ( getNlpCacheMapSpy ) . toHaveBeenCalledTimes ( 2 ) ;
440+ getNlpCacheMapSpy . mockRestore ( ) ;
453441 expect ( score ) . toBeGreaterThan ( 0 ) ;
454442 expect ( score2 ) . toBe ( 0 ) ;
455443 expect ( score ) . toBeGreaterThan ( score2 ) ;
456444 } ) ;
457445
458446 it ( 'should calculate the correct NLP score for a block and apply penalties ' , async ( ) => {
459- jest
460- . spyOn ( nlpEntityService , 'getNlpMap' )
461- . mockResolvedValue ( mockNlpCacheMap ) ;
447+ const getNlpCacheMapSpy = jest . spyOn ( nlpEntityService , 'getNlpMap' ) ;
462448 const score = await blockService . calculateBlockScore (
463449 mockNlpPatternsSetOne ,
464450 mockNlpEntitiesSetOne ,
@@ -468,19 +454,25 @@ describe('BlockService', () => {
468454 mockNlpEntitiesSetOne ,
469455 ) ;
470456
457+ expect ( getNlpCacheMapSpy ) . toHaveBeenCalledTimes ( 2 ) ;
458+ getNlpCacheMapSpy . mockRestore ( ) ;
459+
471460 expect ( score ) . toBeGreaterThan ( 0 ) ;
472461 expect ( score2 ) . toBeGreaterThan ( 0 ) ;
473462 expect ( score ) . toBeGreaterThan ( score2 ) ;
474463 } ) ;
475464
476465 it ( 'should return 0 if no matching entities are found' , async ( ) => {
477- jest . spyOn ( nlpEntityService , 'getNlpMap' ) . mockResolvedValue ( new Map ( ) ) ;
466+ const getNlpCacheMapSpy = jest . spyOn ( nlpEntityService , 'getNlpMap' ) ;
467+
478468 const score = await blockService . calculateBlockScore (
479469 mockNlpPatternsSetTwo ,
480470 mockNlpEntitiesSetOne ,
481471 ) ;
472+ expect ( getNlpCacheMapSpy ) . toHaveBeenCalledTimes ( 1 ) ;
473+ getNlpCacheMapSpy . mockRestore ( ) ;
482474
483- expect ( score ) . toBe ( 0 ) ; // No matching entity, so score should be 0
475+ expect ( score ) . toBe ( 0 ) ; // No matching entity
484476 } ) ;
485477 } ) ;
486478
0 commit comments