mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
feat: add more test cases and refine edge case handling
This commit is contained in:
parent
3fc5762ee8
commit
f61e1ebba8
@ -117,16 +117,18 @@ describe('nlpEntityService', () => {
|
||||
expect(result).toEqualPayload(entitiesWithValues);
|
||||
});
|
||||
});
|
||||
|
||||
describe('NlpEntityService - updateWeight', () => {
|
||||
it('should update the weight of an NLP entity', async () => {
|
||||
const createdEntity = await nlpEntityRepository.create({
|
||||
let createdEntity: NlpEntity;
|
||||
beforeEach(async () => {
|
||||
createdEntity = await nlpEntityRepository.create({
|
||||
name: 'testentity',
|
||||
builtin: true,
|
||||
weight: 1,
|
||||
builtin: false,
|
||||
weight: 3,
|
||||
});
|
||||
});
|
||||
|
||||
const newWeight = 3;
|
||||
it('should update the weight of an NLP entity', async () => {
|
||||
const newWeight = 8;
|
||||
|
||||
const updatedEntity = await nlpEntityService.updateWeight(
|
||||
createdEntity.id,
|
||||
@ -135,6 +137,56 @@ describe('nlpEntityService', () => {
|
||||
|
||||
expect(updatedEntity.weight).toBe(newWeight);
|
||||
});
|
||||
|
||||
it('should handle updating weight of non-existent entity', async () => {
|
||||
const nonExistentId = '507f1f77bcf86cd799439011'; // Example MongoDB ObjectId
|
||||
|
||||
try {
|
||||
await nlpEntityService.updateWeight(nonExistentId, 5);
|
||||
fail('Expected error was not thrown');
|
||||
} catch (error) {
|
||||
expect(error).toBeDefined();
|
||||
}
|
||||
});
|
||||
|
||||
it('should use default weight of 1 when creating entity without weight', async () => {
|
||||
const createdEntity = await nlpEntityRepository.create({
|
||||
name: 'entityWithoutWeight',
|
||||
builtin: true,
|
||||
// weight not specified
|
||||
});
|
||||
|
||||
expect(createdEntity.weight).toBe(1);
|
||||
});
|
||||
|
||||
it('should throw an error if weight is less than 1', async () => {
|
||||
const invalidWeight = 0;
|
||||
|
||||
await expect(
|
||||
nlpEntityService.updateWeight(createdEntity.id, invalidWeight),
|
||||
).rejects.toThrow('Weight must be a positive integer');
|
||||
});
|
||||
|
||||
it('should throw an error if weight is a decimal', async () => {
|
||||
const invalidWeight = 2.5;
|
||||
|
||||
await expect(
|
||||
nlpEntityService.updateWeight(createdEntity.id, invalidWeight),
|
||||
).rejects.toThrow('Weight must be a positive integer');
|
||||
});
|
||||
|
||||
it('should throw an error if weight is negative', async () => {
|
||||
const invalidWeight = -3;
|
||||
|
||||
await expect(
|
||||
nlpEntityService.updateWeight(createdEntity.id, invalidWeight),
|
||||
).rejects.toThrow('Weight must be a positive integer');
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
// Clean the collection after each test
|
||||
await nlpEntityRepository.deleteOne(createdEntity.id);
|
||||
});
|
||||
});
|
||||
|
||||
describe('storeNewEntities', () => {
|
||||
|
@ -48,12 +48,19 @@ export class NlpEntityService extends BaseService<
|
||||
/**
|
||||
* Updates the `weight` field of a specific NLP entity by its ID.
|
||||
*
|
||||
* This method is part of the NLP-based blocks prioritization strategy.
|
||||
+ * The weight influences the scoring of blocks when multiple blocks match a user's input.
|
||||
* @param id - The unique identifier of the entity to update.
|
||||
* @param updatedWeight - The new weight value to be assigned to the entity.
|
||||
* @returns A promise that resolves to the updated entity.
|
||||
* @param updatedWeight - The new weight to assign. Must be a positive integer.
|
||||
* @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) {
|
||||
if (!Number.isInteger(updatedWeight) || updatedWeight < 1) {
|
||||
throw new Error('Weight must be a positive integer');
|
||||
}
|
||||
|
||||
return this.repository.updateOne(
|
||||
id,
|
||||
{ weight: updatedWeight },
|
||||
|
Loading…
Reference in New Issue
Block a user