-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathnlp-sample.controller.spec.ts
More file actions
547 lines (501 loc) · 17.6 KB
/
nlp-sample.controller.spec.ts
File metadata and controls
547 lines (501 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import { BadRequestException, NotFoundException } from '@nestjs/common';
import { NlpValueMatchPattern } from '@/chat/schemas/types/pattern';
import { Language } from '@/i18n/schemas/language.schema';
import { LanguageService } from '@/i18n/services/language.service';
import { getUpdateOneError } from '@/utils/test/errors/messages';
import { installAttachmentFixtures } from '@/utils/test/fixtures/attachment';
import { nlpSampleFixtures } from '@/utils/test/fixtures/nlpsample';
import { installNlpSampleEntityFixtures } from '@/utils/test/fixtures/nlpsampleentity';
import { getPageQuery } from '@/utils/test/pagination';
import {
closeInMongodConnection,
rootMongooseTestModule,
} from '@/utils/test/test';
import { TFixtures } from '@/utils/test/types';
import { buildTestingMocks } from '@/utils/test/utils';
import { NlpSampleDto } from '../dto/nlp-sample.dto';
import { NlpSample, NlpSampleFull } from '../schemas/nlp-sample.schema';
import { NlpSampleState } from '../schemas/types';
import { NlpEntityService } from '../services/nlp-entity.service';
import { NlpSampleEntityService } from '../services/nlp-sample-entity.service';
import { NlpSampleService } from '../services/nlp-sample.service';
import { NlpValueService } from '../services/nlp-value.service';
import { NlpSampleController } from './nlp-sample.controller';
describe('NlpSampleController', () => {
let nlpSampleController: NlpSampleController;
let nlpSampleEntityService: NlpSampleEntityService;
let nlpSampleService: NlpSampleService;
let nlpEntityService: NlpEntityService;
let nlpValueService: NlpValueService;
let languageService: LanguageService;
let byeJhonSampleId: string | null;
let languages: Language[];
beforeAll(async () => {
const { getMocks } = await buildTestingMocks({
autoInjectFrom: ['controllers'],
controllers: [NlpSampleController],
imports: [
rootMongooseTestModule(async () => {
await installNlpSampleEntityFixtures();
await installAttachmentFixtures();
}),
],
});
[
nlpSampleController,
nlpSampleEntityService,
nlpSampleService,
nlpEntityService,
nlpValueService,
languageService,
] = await getMocks([
NlpSampleController,
NlpSampleEntityService,
NlpSampleService,
NlpEntityService,
NlpValueService,
LanguageService,
]);
byeJhonSampleId =
(
await nlpSampleService.findOne({
text: 'Bye Jhon',
})
)?.id || null;
languages = await languageService.findAll();
});
afterAll(closeInMongodConnection);
afterEach(jest.clearAllMocks);
describe('findPage', () => {
it('should find nlp samples, and foreach sample populate the corresponding entities', async () => {
const pageQuery = getPageQuery<NlpSample>({ sort: ['text', 'desc'] });
const result = await nlpSampleController.findPage(
pageQuery,
['language', 'entities'],
{},
);
const nlpSamples = await nlpSampleService.findAll();
const nlpSampleEntities = await nlpSampleEntityService.findAll();
const nlpSampleFixturesWithEntities = nlpSamples.reduce(
(acc, currSample) => {
const sampleWithEntities = {
...currSample,
entities: nlpSampleEntities.filter(
(currSampleEntity) => currSampleEntity.sample === currSample.id,
),
language:
languages.find((lang) => lang.id === currSample.language) || null,
};
acc.push(sampleWithEntities);
return acc;
},
[] as TFixtures<NlpSampleFull>[],
);
expect(result).toEqualPayload(nlpSampleFixturesWithEntities);
});
it('should find nlp samples', async () => {
const pageQuery = getPageQuery<NlpSample>({ sort: ['text', 'desc'] });
const result = await nlpSampleController.findPage(
pageQuery,
['invalidCriteria'],
{},
);
expect(result).toEqualPayload(
nlpSampleFixtures.map((sample) => ({
...sample,
language: sample.language ? languages[sample.language].id : null,
})),
);
});
it('should find nlp samples with patterns', async () => {
const pageQuery = getPageQuery<NlpSample>({ sort: ['text', 'desc'] });
const patterns: NlpValueMatchPattern[] = [
{ entity: 'intent', match: 'value', value: 'greeting' },
];
const result = await nlpSampleController.findPage(
pageQuery,
['language', 'entities'],
{},
patterns,
);
// Should only return samples matching the pattern
const nlpSamples = await nlpSampleService.findByPatternsAndPopulate(
{ filters: {}, patterns },
pageQuery,
);
expect(result).toEqualPayload(nlpSamples);
});
it('should return empty array if no samples match the patterns', async () => {
const pageQuery = getPageQuery<NlpSample>({ sort: ['text', 'desc'] });
const patterns: NlpValueMatchPattern[] = [
{ entity: 'intent', match: 'value', value: 'nonexistent' },
];
jest.spyOn(nlpSampleService, 'findByPatternsAndPopulate');
const result = await nlpSampleController.findPage(
pageQuery,
['language', 'entities'],
{},
patterns,
);
expect(nlpSampleService.findByPatternsAndPopulate).toHaveBeenCalledTimes(
1,
);
expect(Array.isArray(result)).toBe(true);
expect(result).toHaveLength(0);
});
});
describe('count', () => {
it('should count the nlp samples', async () => {
jest.spyOn(nlpSampleService, 'count');
const result = await nlpSampleController.count({});
expect(nlpSampleService.count).toHaveBeenCalledTimes(1);
const count = nlpSampleFixtures.length;
expect(result).toEqual({ count });
});
});
describe('create', () => {
it('should create nlp sample', async () => {
const enLang = await languageService.findOne({ code: 'en' });
const nlSample: NlpSampleDto = {
text: 'text1',
trained: true,
type: NlpSampleState.test,
entities: [],
language: 'en',
};
const result = await nlpSampleController.create(nlSample);
expect(result).toEqualPayload({
...nlSample,
language: enLang,
});
});
});
describe('deleteOne', () => {
it('should delete a nlp sample', async () => {
const result = await nlpSampleController.deleteOne(byeJhonSampleId!);
expect(result.deletedCount).toEqual(1);
});
it('should throw exception when nlp sample id not found', async () => {
await expect(
nlpSampleController.deleteOne(byeJhonSampleId!),
).rejects.toThrow(NotFoundException);
});
});
describe('findOne', () => {
it('should find a nlp sample', async () => {
const yessSample = await nlpSampleService.findOne({
text: 'yess',
});
const result = await nlpSampleController.findOne(yessSample!.id, [
'invalidCreteria',
]);
expect(result).toEqualPayload({
...nlpSampleFixtures[0],
language: nlpSampleFixtures[0].language
? languages[nlpSampleFixtures?.[0]?.language]?.id
: null,
});
});
it('should find a nlp sample and populate its entities', async () => {
const yessSample = await nlpSampleService.findOne({
text: 'yess',
});
const yessSampleEntity = await nlpSampleEntityService.findOne({
sample: yessSample!.id,
});
const result = await nlpSampleController.findOne(yessSample!.id, [
'entities',
]);
const samplesWithEntities = {
...nlpSampleFixtures[0],
entities: [yessSampleEntity],
language: nlpSampleFixtures[0].language
? languages[nlpSampleFixtures[0].language]
: null,
};
expect(result).toEqualPayload(samplesWithEntities);
});
it('should throw NotFoundException when Id does not exist', async () => {
await expect(
nlpSampleController.findOne(byeJhonSampleId!, ['entities']),
).rejects.toThrow(NotFoundException);
});
});
describe('updateOne', () => {
it('should update a nlp sample', async () => {
const yessSample = await nlpSampleService.findOne({
text: 'yess',
});
const frLang = await languageService.findOne({
code: 'fr',
});
const result = await nlpSampleController.updateOne(yessSample!.id, {
text: 'updated',
trained: true,
type: NlpSampleState.test,
entities: [
{
entity: 'intent',
value: 'update',
},
],
language: 'fr',
});
const updatedSample = {
text: 'updated',
trained: false,
type: NlpSampleState.test,
entities: [
{
entity: expect.stringMatching(/^[a-z0-9]+$/),
sample: expect.stringMatching(/^[a-z0-9]+$/),
value: expect.stringMatching(/^[a-z0-9]+$/),
},
],
language: frLang,
};
expect(result.text).toEqual(updatedSample.text);
expect(result.type).toEqual(updatedSample.type);
expect(result.trained).toEqual(updatedSample.trained);
expect(result.entities).toMatchObject(updatedSample.entities);
expect(result.language).toEqualPayload(updatedSample.language!);
});
it('should throw exception when nlp sample id not found', async () => {
await expect(
nlpSampleController.updateOne(byeJhonSampleId!, {
text: 'updated',
trained: true,
type: NlpSampleState.test,
language: 'fr',
}),
).rejects.toThrow(getUpdateOneError(NlpSample.name, byeJhonSampleId!));
});
});
describe('importFile', () => {
it('should throw exception when something is wrong with the upload', async () => {
const file = {
buffer: Buffer.from('', 'utf-8'),
size: 0,
mimetype: 'text/csv',
} as Express.Multer.File;
await expect(nlpSampleController.importFile(file)).rejects.toThrow(
'Bad Request Exception',
);
});
it('should return a failure if an error occurs when parsing csv file ', async () => {
const mockCsvDataWithErrors: string = `intent,entities,lang,question
greeting,person,en`;
const buffer = Buffer.from(mockCsvDataWithErrors, 'utf-8');
const file = {
buffer,
size: buffer.length,
mimetype: 'text/csv',
} as Express.Multer.File;
await expect(nlpSampleController.importFile(file)).rejects.toThrow();
});
it('should import data from a CSV file', async () => {
const mockCsvData: string = [
`text,intent,language`,
`How much does a BMW cost?,price,en`,
].join('\n');
const buffer = Buffer.from(mockCsvData, 'utf-8');
const file = {
buffer,
size: buffer.length,
mimetype: 'text/csv',
} as Express.Multer.File;
const result = await nlpSampleController.importFile(file);
const intentEntityResult = await nlpEntityService.findOne({
name: 'intent',
});
const priceValueResult = await nlpValueService.findOne({
value: 'price',
});
const textSampleResult = await nlpSampleService.findOne({
text: 'How much does a BMW cost?',
});
const language = await languageService.findOne({
code: 'en',
});
const intentEntity = {
name: 'intent',
lookups: ['trait'],
doc: '',
builtin: false,
weight: 1,
};
const priceValueEntity = await nlpEntityService.findOne({
name: 'intent',
});
const priceValue = {
value: 'price',
expressions: [],
builtin: false,
entity: priceValueEntity!.id,
doc: '',
};
const textSample = {
text: 'How much does a BMW cost?',
trained: false,
type: 'train',
language: language!.id,
};
expect(intentEntityResult).toEqualPayload(intentEntity);
expect(priceValueResult).toEqualPayload(priceValue);
expect(textSampleResult).toEqualPayload(textSample);
expect(result).toEqualPayload([textSample]);
});
});
describe('deleteMany', () => {
it('should delete multiple nlp samples', async () => {
const samplesToDelete = [
(
await nlpSampleService.findOne({
text: 'How much does a BMW cost?',
})
)?.id,
(
await nlpSampleService.findOne({
text: 'text1',
})
)?.id,
] as string[];
const result = await nlpSampleController.deleteMany(samplesToDelete);
expect(result.deletedCount).toEqual(samplesToDelete.length);
const remainingSamples = await nlpSampleService.find({
_id: { $in: samplesToDelete },
});
expect(remainingSamples.length).toBe(0);
});
it('should throw BadRequestException when no IDs are provided', async () => {
await expect(nlpSampleController.deleteMany([])).rejects.toThrow(
BadRequestException,
);
});
it('should throw NotFoundException when provided IDs do not exist', async () => {
const nonExistentIds = [
'614c1b2f58f4f04c876d6b8d',
'614c1b2f58f4f04c876d6b8e',
];
await expect(
nlpSampleController.deleteMany(nonExistentIds),
).rejects.toThrow(NotFoundException);
});
});
describe('filterCount', () => {
it('should count the nlp samples without patterns', async () => {
const filters = { text: 'Hello' };
jest.spyOn(nlpSampleService, 'countByPatterns');
const result = await nlpSampleController.filterCount(filters, []);
expect(nlpSampleService.countByPatterns).toHaveBeenCalledTimes(1);
expect(result).toEqual({ count: 1 });
});
it('should count the nlp samples with patterns', async () => {
const filters = { text: 'Hello' };
const patterns: NlpValueMatchPattern[] = [
{ entity: 'intent', match: 'value', value: 'greeting' },
];
jest.spyOn(nlpSampleService, 'countByPatterns');
const result = await nlpSampleController.filterCount(filters, patterns);
expect(nlpSampleService.countByPatterns).toHaveBeenCalledTimes(1);
expect(result).toEqual({ count: 1 });
});
it('should return zero count when no samples match the filters and patterns', async () => {
const filters = { text: 'Nonexistent' };
const patterns: NlpValueMatchPattern[] = [
{ entity: 'intent', match: 'value', value: 'nonexistent' },
];
const result = await nlpSampleController.filterCount(filters, patterns);
expect(result).toEqual({ count: 0 });
});
});
describe('message', () => {
it('should filter out intent values that are not user-defined', async () => {
// Create a user-defined intent value
const intentEntity = await nlpEntityService.findOne({ name: 'intent' });
await nlpValueService.create({
entity: intentEntity!.id,
value: 'greeting',
expressions: [],
});
// Mock the NLU helper to return both user-defined and inferred intents
const mockPredict = jest.fn().mockResolvedValue({
entities: [
{
entity: 'intent',
value: 'greeting', // User-defined
confidence: 0.95,
},
{
entity: 'intent',
value: 'greetings_goodevening', // Not user-defined (inferred by Ludwig)
confidence: 0.99,
},
{
entity: 'language',
value: 'en',
confidence: 0.98,
},
],
});
jest
.spyOn(nlpSampleController['helperService'], 'getDefaultHelper')
.mockResolvedValue({
predict: mockPredict,
} as any);
const result = await nlpSampleController.message('Good evening');
// Should only include the user-defined intent value
expect(result.entities).toHaveLength(2); // greeting intent + language
expect(result.entities).toEqual(
expect.arrayContaining([
expect.objectContaining({
entity: 'intent',
value: 'greeting',
}),
expect.objectContaining({
entity: 'language',
value: 'en',
}),
]),
);
// Should NOT include the inferred intent
expect(result.entities).not.toEqual(
expect.arrayContaining([
expect.objectContaining({
value: 'greetings_goodevening',
}),
]),
);
});
it('should include all entities when they are user-defined', async () => {
const mockPredict = jest.fn().mockResolvedValue({
entities: [
{
entity: 'intent',
value: 'greeting',
confidence: 0.95,
},
{
entity: 'language',
value: 'en',
confidence: 0.98,
},
],
});
jest
.spyOn(nlpSampleController['helperService'], 'getDefaultHelper')
.mockResolvedValue({
predict: mockPredict,
} as any);
const result = await nlpSampleController.message('Hello');
expect(result.entities).toHaveLength(2);
});
});
});