diff --git a/api/src/chat/services/block.service.spec.ts b/api/src/chat/services/block.service.spec.ts index bfa1f2f8..aa7abbc2 100644 --- a/api/src/chat/services/block.service.spec.ts +++ b/api/src/chat/services/block.service.spec.ts @@ -85,12 +85,6 @@ import { CategoryRepository } from './../repositories/category.repository'; import { BlockService } from './block.service'; import { CategoryService } from './category.service'; -// Create a mock for the NlpEntityService -// const mockNlpEntityService: Partial> = -// { -// getNlpMap: jest.fn().mockResolvedValue(mockNlpCacheMap), -// }; - describe('BlockService', () => { let blockRepository: BlockRepository; let categoryRepository: CategoryRepository; diff --git a/api/src/chat/services/block.service.ts b/api/src/chat/services/block.service.ts index af42e79e..bc6a223e 100644 --- a/api/src/chat/services/block.service.ts +++ b/api/src/chat/services/block.service.ts @@ -200,16 +200,15 @@ export class BlockService extends BaseService< // This ensures that only blocks with valid matches are kept, and blocks with no matches are excluded, // all while iterating through the list only once. - const matchesWithPatterns = filteredBlocks.reduce< - NlpPatternMatchResult[] - >((acc, b) => { - const matchedPattern = this.matchNLP(nlp, b); + const matchesWithPatterns: NlpPatternMatchResult[] = + filteredBlocks.reduce((acc, b) => { + const matchedPattern = this.matchNLP(nlp, b); - if (matchedPattern && matchedPattern.length > 0) { - acc.push({ block: b, matchedPattern }); - } - return acc; - }, []); + if (matchedPattern && matchedPattern.length > 0) { + acc.push({ block: b, matchedPattern }); + } + return acc; + }, []); // @TODO Make nluPenaltyFactor configurable in UI settings const nluPenaltyFactor = 0.95; diff --git a/api/src/nlp/controllers/nlp-entity.controller.spec.ts b/api/src/nlp/controllers/nlp-entity.controller.spec.ts index 0a659648..8eadf357 100644 --- a/api/src/nlp/controllers/nlp-entity.controller.spec.ts +++ b/api/src/nlp/controllers/nlp-entity.controller.spec.ts @@ -313,9 +313,11 @@ describe('NlpEntityController', () => { builtin: false, weight: 4, }; - const originalEntity = await nlpEntityService.findOne(buitInEntityId!); + const originalEntity: NlpEntity | null = await nlpEntityService.findOne( + buitInEntityId!, + ); - const result = await nlpEntityController.updateOne( + const result: NlpEntity = await nlpEntityController.updateOne( buitInEntityId!, updatedNlpEntity, ); diff --git a/api/src/nlp/services/nlp-entity.service.ts b/api/src/nlp/services/nlp-entity.service.ts index 1307bacd..39932ef7 100644 --- a/api/src/nlp/services/nlp-entity.service.ts +++ b/api/src/nlp/services/nlp-entity.service.ts @@ -62,7 +62,7 @@ export class NlpEntityService extends BaseService< * @throws Error if the weight is not a positive integer. * @returns A promise that resolves to the updated entity. */ - async updateWeight(id: string, updatedWeight: number) { + async updateWeight(id: string, updatedWeight: number): Promise { if (!Number.isInteger(updatedWeight) || updatedWeight < 1) { throw new Error('Weight must be a positive integer'); } @@ -166,6 +166,9 @@ export class NlpEntityService extends BaseService< async getNlpMap(entityNames: string[]): Promise { const lookups = await this.findAndPopulate({ name: { $in: entityNames } }); const map: NlpCacheMap = new Map(); + if (!lookups.length) { + return map; // Return empty map if no entities found + } for (const lookup of lookups) { map.set(lookup.name, { id: lookup.id, diff --git a/api/src/utils/test/mocks/block.ts b/api/src/utils/test/mocks/block.ts index 2f941d1c..314d24d5 100644 --- a/api/src/utils/test/mocks/block.ts +++ b/api/src/utils/test/mocks/block.ts @@ -284,7 +284,7 @@ export const mockNlpPatternsSetThree: NlpPattern[] = [ }, ]; -export const mockNlpBlock = { +export const mockNlpBlock: BlockFull = { ...baseBlockInstance, name: 'Mock Nlp', patterns: [ @@ -311,7 +311,7 @@ export const mockNlpBlock = { message: ['Good to see you again '], } as unknown as BlockFull; -export const mockModifiedNlpBlock = { +export const mockModifiedNlpBlock: BlockFull = { ...baseBlockInstance, name: 'Modified Mock Nlp', patterns: [ diff --git a/frontend/src/components/nlp/components/NlpEntityForm.tsx b/frontend/src/components/nlp/components/NlpEntityForm.tsx index fd0e4a79..2e7cfc36 100644 --- a/frontend/src/components/nlp/components/NlpEntityForm.tsx +++ b/frontend/src/components/nlp/components/NlpEntityForm.tsx @@ -145,7 +145,7 @@ export const NlpEntityVarForm: FC> = ({ message: t("message.weight_positive_integer_error"), }, validate: (value) => - Number.isInteger(value) && value! > 0 + value && Number.isInteger(value) && value! > 0 ? true : t("message.weight_positive_integer_error"), })}