feat: fix unit test

This commit is contained in:
hexastack 2025-01-22 18:11:45 +01:00
parent 5d1793cce8
commit 3051e84251
4 changed files with 82 additions and 30 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* 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.
@ -110,7 +110,7 @@ export class BlockController extends BaseController<
throw new NotFoundException('Plugin Not Found');
}
return plugin.settings;
return plugin.getDefaultSettings();
} catch (e) {
this.logger.error('Unable to fetch plugin settings', e);
throw e;
@ -134,7 +134,7 @@ export class BlockController extends BaseController<
...p.template,
message: {
plugin: p.name,
args: p.settings.reduce(
args: p.getDefaultSettings().reduce(
(acc, setting) => {
acc[setting.label] = setting.value;
return acc;

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* 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.
@ -10,7 +10,9 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
import { Test, TestingModule } from '@nestjs/testing';
import { I18nService } from '@/i18n/services/i18n.service';
import { BasePlugin } from '@/plugins/base-plugin.service';
import { PluginService } from '@/plugins/plugins.service';
import { PluginBlockTemplate } from '@/plugins/types';
import { SettingType } from '@/setting/schemas/types';
import { SettingService } from '@/setting/services/setting.service';
@ -157,26 +159,56 @@ describe('TranslationService', () => {
attachedBlock: null,
};
const mockedPlugin: any = {
name: 'ollama-plugin',
type: 'block',
settings: [
{
label: 'model',
group: 'default',
type: SettingType.text,
value: 'llama3.2',
translatable: false,
},
{
label: 'context',
group: 'default',
type: SettingType.multiple_text,
value: ['Answer the user QUESTION using the DOCUMENTS text above.'],
translatable: true,
},
],
};
class MockPlugin extends BasePlugin {
template: PluginBlockTemplate = { name: 'Ollama Plugin' };
name: `${string}-plugin`;
type: any;
private settings: {
label: string;
group: string;
type: SettingType;
value: any;
translatable: boolean;
}[];
constructor() {
super('ollama-plugin', pluginService);
this.name = 'ollama-plugin';
this.type = 'block';
this.settings = [
{
label: 'model',
group: 'default',
type: SettingType.text,
value: 'llama3.2',
translatable: false,
},
{
label: 'context',
group: 'default',
type: SettingType.multiple_text,
value: ['Answer the user QUESTION using the DOCUMENTS text above.'],
translatable: true,
},
];
}
// Implementing the 'getPath' method (with a mock return value)
getPath() {
// Return a mock path
return '/mock/path';
}
getDefaultSettings() {
return this.settings;
}
}
// Create an instance of the mock plugin
const mockedPlugin = new MockPlugin();
jest
.spyOn(pluginService, 'getPlugin')

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* 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.
@ -59,7 +59,9 @@ export class TranslationService extends BaseService<Translation> {
// plugin
Object.entries(block.message.args).forEach(([l, arg]) => {
const setting = plugin?.settings.find(({ label }) => label === l);
const setting = plugin
?.getDefaultSettings()
.find(({ label }) => label === l);
if (setting?.translatable) {
if (Array.isArray(arg)) {
// array of text

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* 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.
@ -30,7 +30,7 @@ export abstract class BaseBlockPlugin<
> extends BasePlugin {
public readonly type: PluginType = PluginType.block;
public readonly settings: T;
private readonly settings: T;
constructor(name: PluginName, pluginService: PluginService<BasePlugin>) {
super(name, pluginService);
@ -38,6 +38,10 @@ export abstract class BaseBlockPlugin<
this.settings = require(path.join(this.getPath(), 'settings')).default;
}
getDefaultSettings(): T {
return this.settings;
}
abstract template: PluginBlockTemplate;
effects?: PluginEffects;
@ -50,8 +54,22 @@ export abstract class BaseBlockPlugin<
protected getArguments(block: Block) {
if ('args' in block.message) {
return block.message.args as SettingObject<T>;
return (
Object.entries(block.message.args)
// Filter out old settings
.filter(
([argKey]) =>
this.settings.findIndex(({ label }) => label === argKey) !== -1,
)
.reduce(
(acc, [k, v]) => ({
...acc,
[k]: v,
}),
{} as SettingObject<T>,
)
);
}
throw new Error(`Block "${block.name}" does not have any arguments.`);
throw new Error(`Block ${block.name} does not have any arguments.`);
}
}