feat: add typing and enforce safety checks

This commit is contained in:
MohamedAliBouhaouala 2025-04-24 19:09:44 +01:00
parent 1941c54bc3
commit 9318e3fed3
6 changed files with 19 additions and 21 deletions

View File

@ -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<Record<keyof NlpEntityService, jest.Mock>> =
// {
// getNlpMap: jest.fn().mockResolvedValue(mockNlpCacheMap),
// };
describe('BlockService', () => {
let blockRepository: BlockRepository;
let categoryRepository: CategoryRepository;

View File

@ -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<NlpPatternMatchResult[]>((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;

View File

@ -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,
);

View File

@ -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<NlpEntity> {
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<NlpCacheMap> {
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,

View File

@ -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: [

View File

@ -145,7 +145,7 @@ export const NlpEntityVarForm: FC<ComponentFormProps<INlpEntity>> = ({
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"),
})}