fix: update unit tests

This commit is contained in:
yassinedorbozgithub 2025-06-12 11:27:26 +01:00
parent 53d4ae3ab8
commit 44efa5b31c
2 changed files with 189 additions and 5 deletions

View File

@ -6,8 +6,19 @@
* 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 { CACHE_MANAGER } from '@nestjs/cache-manager';
import { MongooseModule } from '@nestjs/mongoose';
import LlmNluHelper from '@/extensions/helpers/llm-nlu/index.helper';
import { HelperService } from '@/helper/helper.service';
import { LanguageRepository } from '@/i18n/repositories/language.repository';
import { LanguageModel } from '@/i18n/schemas/language.schema';
import { LanguageService } from '@/i18n/services/language.service';
import { LoggerService } from '@/logger/logger.service';
import { SettingRepository } from '@/setting/repositories/setting.repository';
import { SettingModel } from '@/setting/schemas/setting.schema';
import { SettingSeeder } from '@/setting/seeds/setting.seed';
import { SettingService } from '@/setting/services/setting.service';
import { nlpEntityFixtures } from '@/utils/test/fixtures/nlpentity';
import { installNlpValueFixtures } from '@/utils/test/fixtures/nlpvalue';
import { getPageQuery } from '@/utils/test/pagination';
@ -19,40 +30,113 @@ import { buildTestingMocks } from '@/utils/test/utils';
import { NlpEntity, NlpEntityModel } from '../schemas/nlp-entity.schema';
import { NlpSampleEntityModel } from '../schemas/nlp-sample-entity.schema';
import { NlpSampleModel } from '../schemas/nlp-sample.schema';
import { NlpValueModel } from '../schemas/nlp-value.schema';
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 { NlpService } from '../services/nlp.service';
import { NlpEntityRepository } from './nlp-entity.repository';
import { NlpSampleEntityRepository } from './nlp-sample-entity.repository';
import { NlpSampleRepository } from './nlp-sample.repository';
import { NlpValueRepository } from './nlp-value.repository';
describe('NlpEntityRepository', () => {
let nlpEntityRepository: NlpEntityRepository;
let nlpValueRepository: NlpValueRepository;
let firstNameNlpEntity: NlpEntity | null;
let nlpService: NlpService;
let helperService: HelperService;
let languageService: LanguageService;
let settingService: SettingService;
let nlpEntityService: NlpEntityService;
beforeAll(async () => {
const { getMocks } = await buildTestingMocks({
const { getMocks, resolveMocks } = await buildTestingMocks({
imports: [
rootMongooseTestModule(installNlpValueFixtures),
MongooseModule.forFeature([
NlpEntityModel,
NlpValueModel,
NlpSampleEntityModel,
NlpSampleModel,
LanguageModel,
SettingModel,
]),
],
providers: [
HelperService,
NlpEntityRepository,
NlpValueRepository,
NlpSampleEntityRepository,
NlpService,
NlpSampleService,
NlpEntityService,
NlpValueService,
{
provide: CACHE_MANAGER,
useValue: {
del: jest.fn(),
get: jest.fn(),
set: jest.fn(),
},
},
NlpSampleEntityService,
NlpSampleRepository,
LanguageService,
{
provide: SettingService,
useValue: {
getConfig: jest.fn(() => ({
chatbot: { lang: { default: 'fr' } },
})),
getSettings: jest.fn(() => ({
chatbot_settings: {
default_nlu_helper: 'llm-nlu-helper',
},
})),
},
},
LanguageRepository,
SettingRepository,
SettingSeeder,
],
});
[nlpEntityRepository, nlpValueRepository] = await getMocks([
[
nlpEntityRepository,
nlpValueRepository,
nlpService,
helperService,
settingService,
languageService,
nlpEntityService,
] = await getMocks([
NlpEntityRepository,
NlpValueRepository,
NlpService,
HelperService,
SettingService,
LanguageService,
NlpEntityService,
]);
firstNameNlpEntity = await nlpEntityRepository.findOne({
name: 'firstname',
});
const [loggerService] = await resolveMocks([LoggerService]);
helperService.register(
new LlmNluHelper(
settingService,
helperService,
loggerService,
languageService,
nlpEntityService,
),
);
});
afterAll(closeInMongodConnection);
@ -66,6 +150,11 @@ describe('NlpEntityRepository', () => {
});
const result = await nlpEntityRepository.deleteOne(intentNlpEntity!.id);
await nlpService.handleEntityDelete(
{},
{ _id: intentNlpEntity!.id, builtin: { $ne: true } },
);
expect(result.deletedCount).toEqual(1);
const intentNlpValues = await nlpValueRepository.find({

View File

@ -6,8 +6,19 @@
* 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 { CACHE_MANAGER } from '@nestjs/cache-manager';
import { MongooseModule } from '@nestjs/mongoose';
import LlmNluHelper from '@/extensions/helpers/llm-nlu/index.helper';
import { HelperService } from '@/helper/helper.service';
import { LanguageRepository } from '@/i18n/repositories/language.repository';
import { LanguageModel } from '@/i18n/schemas/language.schema';
import { LanguageService } from '@/i18n/services/language.service';
import { LoggerService } from '@/logger/logger.service';
import { SettingRepository } from '@/setting/repositories/setting.repository';
import { SettingModel } from '@/setting/schemas/setting.schema';
import { SettingSeeder } from '@/setting/seeds/setting.seed';
import { SettingService } from '@/setting/services/setting.service';
import { nlpEntityFixtures } from '@/utils/test/fixtures/nlpentity';
import { installNlpSampleEntityFixtures } from '@/utils/test/fixtures/nlpsampleentity';
import { nlpValueFixtures } from '@/utils/test/fixtures/nlpvalue';
@ -21,38 +32,116 @@ import { buildTestingMocks } from '@/utils/test/utils';
import { NlpEntityModel } from '../schemas/nlp-entity.schema';
import { NlpSampleEntityModel } from '../schemas/nlp-sample-entity.schema';
import { NlpSampleModel } from '../schemas/nlp-sample.schema';
import {
NlpValue,
NlpValueFull,
NlpValueModel,
} from '../schemas/nlp-value.schema';
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 { NlpService } from '../services/nlp.service';
import { NlpEntityRepository } from './nlp-entity.repository';
import { NlpSampleEntityRepository } from './nlp-sample-entity.repository';
import { NlpSampleRepository } from './nlp-sample.repository';
import { NlpValueRepository } from './nlp-value.repository';
describe('NlpValueRepository', () => {
let nlpValueRepository: NlpValueRepository;
let nlpSampleEntityRepository: NlpSampleEntityRepository;
let nlpValues: NlpValue[];
let nlpService: NlpService;
let helperService: HelperService;
let languageService: LanguageService;
let settingService: SettingService;
let nlpEntityService: NlpEntityService;
beforeAll(async () => {
const { getMocks } = await buildTestingMocks({
const { getMocks, resolveMocks } = await buildTestingMocks({
imports: [
rootMongooseTestModule(installNlpSampleEntityFixtures),
MongooseModule.forFeature([
NlpValueModel,
NlpSampleEntityModel,
NlpEntityModel,
LanguageModel,
SettingModel,
NlpSampleModel,
]),
],
providers: [NlpValueRepository, NlpSampleEntityRepository],
providers: [
LanguageService,
LanguageRepository,
LoggerService,
{
provide: CACHE_MANAGER,
useValue: {
del: jest.fn(),
get: jest.fn(),
set: jest.fn(),
},
},
NlpService,
NlpSampleService,
NlpEntityService,
NlpValueService,
NlpValueRepository,
NlpEntityRepository,
NlpSampleEntityService,
NlpSampleRepository,
NlpSampleEntityRepository,
HelperService,
{
provide: SettingService,
useValue: {
getConfig: jest.fn(() => ({
chatbot: { lang: { default: 'fr' } },
})),
getSettings: jest.fn(() => ({
chatbot_settings: {
default_nlu_helper: 'llm-nlu-helper',
},
})),
},
},
SettingRepository,
SettingSeeder,
],
});
[nlpValueRepository, nlpSampleEntityRepository] = await getMocks([
[
nlpValueRepository,
nlpSampleEntityRepository,
nlpService,
helperService,
settingService,
languageService,
nlpEntityService,
] = await getMocks([
NlpValueRepository,
NlpSampleEntityRepository,
NlpService,
HelperService,
SettingService,
LanguageService,
NlpEntityService,
]);
nlpValues = await nlpValueRepository.findAll();
const [loggerService] = await resolveMocks([LoggerService]);
helperService.register(
new LlmNluHelper(
settingService,
helperService,
loggerService,
languageService,
nlpEntityService,
),
);
});
afterAll(closeInMongodConnection);
@ -108,6 +197,12 @@ describe('NlpValueRepository', () => {
describe('The deleteCascadeOne function', () => {
it('should delete a nlp Value', async () => {
const result = await nlpValueRepository.deleteOne(nlpValues[1].id);
await nlpService.handleValueDelete(
{},
{ _id: nlpValues[1].id, builtin: { $ne: true } },
);
expect(result.deletedCount).toEqual(1);
const sampleEntities = await nlpSampleEntityRepository.find({
value: nlpValues[1].id,