@@ -37,6 +37,7 @@ import { NlpValueRepository } from '@/nlp/repositories/nlp-value.repository';
37
37
import { NlpEntityModel } from '@/nlp/schemas/nlp-entity.schema' ;
38
38
import { NlpSampleEntityModel } from '@/nlp/schemas/nlp-sample-entity.schema' ;
39
39
import { NlpValueModel } from '@/nlp/schemas/nlp-value.schema' ;
40
+ import { NlpCacheMap } from '@/nlp/schemas/types' ;
40
41
import { NlpEntityService } from '@/nlp/services/nlp-entity.service' ;
41
42
import { NlpValueService } from '@/nlp/services/nlp-value.service' ;
42
43
import { PluginService } from '@/plugins/plugins.service' ;
@@ -51,8 +52,11 @@ import {
51
52
blockGetStarted ,
52
53
blockProductListMock ,
53
54
blocks ,
55
+ mockModifiedNlpBlock ,
54
56
mockNlpBlock ,
55
- nlpBlocks ,
57
+ mockNlpPatternsSetOne ,
58
+ mockNlpPatternsSetThree ,
59
+ mockNlpPatternsSetTwo ,
56
60
} from '@/utils/test/mocks/block' ;
57
61
import {
58
62
contextBlankInstance ,
@@ -81,25 +85,6 @@ import { CategoryRepository } from './../repositories/category.repository';
81
85
import { BlockService } from './block.service' ;
82
86
import { CategoryService } from './category.service' ;
83
87
84
- // Create a mock for the NlpEntityService
85
- const mockNlpEntityService = {
86
- findOne : jest . fn ( ) . mockImplementation ( ( query ) => {
87
- if ( query . name === 'intent' ) {
88
- return Promise . resolve ( {
89
- lookups : [ 'trait' ] ,
90
- id : '67e3e41eff551ca5be70559c' ,
91
- } ) ;
92
- }
93
- if ( query . name === 'firstname' ) {
94
- return Promise . resolve ( {
95
- lookups : [ 'trait' ] ,
96
- id : '67e3e41eff551ca5be70559d' ,
97
- } ) ;
98
- }
99
- return Promise . resolve ( null ) ; // Default response if the entity isn't found
100
- } ) ,
101
- } ;
102
-
103
88
describe ( 'BlockService' , ( ) => {
104
89
let blockRepository : BlockRepository ;
105
90
let categoryRepository : CategoryRepository ;
@@ -110,8 +95,6 @@ describe('BlockService', () => {
110
95
let contentService : ContentService ;
111
96
let contentTypeService : ContentTypeService ;
112
97
let nlpEntityService : NlpEntityService ;
113
- let settingService : SettingService ;
114
- let settings : Settings ;
115
98
116
99
beforeAll ( async ( ) => {
117
100
const { getMocks } = await buildTestingMocks ( {
@@ -129,8 +112,8 @@ describe('BlockService', () => {
129
112
LabelModel ,
130
113
LanguageModel ,
131
114
NlpEntityModel ,
132
- NlpValueModel ,
133
115
NlpSampleEntityModel ,
116
+ NlpValueModel ,
134
117
] ) ,
135
118
] ,
136
119
providers : [
@@ -140,19 +123,19 @@ describe('BlockService', () => {
140
123
ContentRepository ,
141
124
AttachmentRepository ,
142
125
LanguageRepository ,
143
- NlpEntityRepository ,
144
- NlpSampleEntityRepository ,
145
- NlpValueRepository ,
146
126
BlockService ,
147
127
CategoryService ,
148
128
ContentTypeService ,
149
129
ContentService ,
150
130
AttachmentService ,
151
131
LanguageService ,
152
- NlpValueService ,
132
+ NlpEntityRepository ,
133
+ NlpValueRepository ,
134
+ NlpSampleEntityRepository ,
135
+ NlpEntityService ,
153
136
{
154
- provide : NlpEntityService , // Mocking NlpEntityService
155
- useValue : mockNlpEntityService ,
137
+ provide : NlpValueService ,
138
+ useValue : { } ,
156
139
} ,
157
140
{
158
141
provide : PluginService ,
@@ -186,14 +169,22 @@ describe('BlockService', () => {
186
169
} ,
187
170
} ,
188
171
] ,
189
- } ) . compile ( ) ;
190
- blockService = module . get < BlockService > ( BlockService ) ;
191
- contentService = module . get < ContentService > ( ContentService ) ;
192
- settingService = module . get < SettingService > ( SettingService ) ;
193
- contentTypeService = module . get < ContentTypeService > ( ContentTypeService ) ;
194
- categoryRepository = module . get < CategoryRepository > ( CategoryRepository ) ;
195
- blockRepository = module . get < BlockRepository > ( BlockRepository ) ;
196
- nlpEntityService = module . get < NlpEntityService > ( NlpEntityService ) ;
172
+ } ) ;
173
+ [
174
+ blockService ,
175
+ contentService ,
176
+ contentTypeService ,
177
+ categoryRepository ,
178
+ blockRepository ,
179
+ nlpEntityService ,
180
+ ] = await getMocks ( [
181
+ BlockService ,
182
+ ContentService ,
183
+ ContentTypeService ,
184
+ CategoryRepository ,
185
+ BlockRepository ,
186
+ NlpEntityService ,
187
+ ] ) ;
197
188
category = ( await categoryRepository . findOne ( { label : 'default' } ) ) ! ;
198
189
hasPreviousBlocks = ( await blockRepository . findOne ( {
199
190
name : 'hasPreviousBlocks' ,
@@ -554,59 +545,6 @@ describe('BlockService', () => {
554
545
} ) ;
555
546
} ) ;
556
547
557
- describe ( 'matchBestNLP' , ( ) => {
558
- it ( 'should return undefined if blocks is empty' , async ( ) => {
559
- const result = await blockService . matchBestNLP ( [ ] ) ;
560
- expect ( result ) . toBeUndefined ( ) ;
561
- } ) ;
562
-
563
- it ( 'should return the only block if there is one' , async ( ) => {
564
- const result = await blockService . matchBestNLP ( [ blockEmpty ] ) ;
565
- expect ( result ) . toBe ( blockEmpty ) ;
566
- } ) ;
567
-
568
- it ( 'should correctly select the best block based on NLP scores' , async ( ) => {
569
- const result = await blockService . matchBestNLP ( nlpBlocks ) ;
570
- expect ( result ) . toBe ( mockNlpBlock ) ;
571
-
572
- // Iterate over each block
573
- for ( const block of nlpBlocks ) {
574
- // Flatten the patterns array and filter valid NLP patterns
575
- block . patterns
576
- . flatMap ( ( pattern ) => ( Array . isArray ( pattern ) ? pattern : [ ] ) )
577
- . filter ( ( p ) => typeof p === 'object' && 'entity' in p && 'match' in p ) // Filter only valid patterns with entity and match
578
- . forEach ( ( p ) => {
579
- // Check if findOne was called with the correct entity
580
- expect ( nlpEntityService . findOne ) . toHaveBeenCalledWith (
581
- { name : p . entity } ,
582
- undefined ,
583
- { _id : 0 , lookups : 1 } ,
584
- ) ;
585
- } ) ;
586
- }
587
- } ) ;
588
-
589
- it ( 'should return the block with the highest combined score' , async ( ) => {
590
- const result = await blockService . matchBestNLP ( nlpBlocks ) ;
591
- expect ( result ) . toBe ( mockNlpBlock ) ;
592
- // Iterate over each block
593
- for ( const block of nlpBlocks ) {
594
- // Flatten the patterns array and filter valid NLP patterns
595
- block . patterns
596
- . flatMap ( ( pattern ) => ( Array . isArray ( pattern ) ? pattern : [ ] ) )
597
- . filter ( ( p ) => typeof p === 'object' && 'entity' in p && 'match' in p ) // Filter only valid patterns with entity and match
598
- . forEach ( ( p ) => {
599
- // Check if findOne was called with the correct entity
600
- expect ( nlpEntityService . findOne ) . toHaveBeenCalledWith (
601
- { name : p . entity } ,
602
- undefined ,
603
- { _id : 0 , lookups : 1 } ,
604
- ) ;
605
- } ) ;
606
- }
607
- } ) ;
608
- } ) ;
609
-
610
548
describe ( 'matchText' , ( ) => {
611
549
it ( 'should return false for matching an empty text' , ( ) => {
612
550
const result = blockService . matchText ( '' , blockGetStarted ) ;
0 commit comments