mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
feat: fix unit test
This commit is contained in:
parent
5d1793cce8
commit
3051e84251
@ -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:
|
* 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.
|
* 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');
|
throw new NotFoundException('Plugin Not Found');
|
||||||
}
|
}
|
||||||
|
|
||||||
return plugin.settings;
|
return plugin.getDefaultSettings();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error('Unable to fetch plugin settings', e);
|
this.logger.error('Unable to fetch plugin settings', e);
|
||||||
throw e;
|
throw e;
|
||||||
@ -134,7 +134,7 @@ export class BlockController extends BaseController<
|
|||||||
...p.template,
|
...p.template,
|
||||||
message: {
|
message: {
|
||||||
plugin: p.name,
|
plugin: p.name,
|
||||||
args: p.settings.reduce(
|
args: p.getDefaultSettings().reduce(
|
||||||
(acc, setting) => {
|
(acc, setting) => {
|
||||||
acc[setting.label] = setting.value;
|
acc[setting.label] = setting.value;
|
||||||
return acc;
|
return acc;
|
||||||
|
|||||||
@ -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:
|
* 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.
|
* 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 { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
|
||||||
import { I18nService } from '@/i18n/services/i18n.service';
|
import { I18nService } from '@/i18n/services/i18n.service';
|
||||||
|
import { BasePlugin } from '@/plugins/base-plugin.service';
|
||||||
import { PluginService } from '@/plugins/plugins.service';
|
import { PluginService } from '@/plugins/plugins.service';
|
||||||
|
import { PluginBlockTemplate } from '@/plugins/types';
|
||||||
import { SettingType } from '@/setting/schemas/types';
|
import { SettingType } from '@/setting/schemas/types';
|
||||||
import { SettingService } from '@/setting/services/setting.service';
|
import { SettingService } from '@/setting/services/setting.service';
|
||||||
|
|
||||||
@ -157,26 +159,56 @@ describe('TranslationService', () => {
|
|||||||
attachedBlock: null,
|
attachedBlock: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
const mockedPlugin: any = {
|
class MockPlugin extends BasePlugin {
|
||||||
name: 'ollama-plugin',
|
template: PluginBlockTemplate = { name: 'Ollama Plugin' };
|
||||||
type: 'block',
|
|
||||||
settings: [
|
name: `${string}-plugin`;
|
||||||
{
|
|
||||||
label: 'model',
|
type: any;
|
||||||
group: 'default',
|
|
||||||
type: SettingType.text,
|
private settings: {
|
||||||
value: 'llama3.2',
|
label: string;
|
||||||
translatable: false,
|
group: string;
|
||||||
},
|
type: SettingType;
|
||||||
{
|
value: any;
|
||||||
label: 'context',
|
translatable: boolean;
|
||||||
group: 'default',
|
}[];
|
||||||
type: SettingType.multiple_text,
|
|
||||||
value: ['Answer the user QUESTION using the DOCUMENTS text above.'],
|
constructor() {
|
||||||
translatable: true,
|
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
|
jest
|
||||||
.spyOn(pluginService, 'getPlugin')
|
.spyOn(pluginService, 'getPlugin')
|
||||||
|
|||||||
@ -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:
|
* 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.
|
* 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
|
// plugin
|
||||||
Object.entries(block.message.args).forEach(([l, arg]) => {
|
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 (setting?.translatable) {
|
||||||
if (Array.isArray(arg)) {
|
if (Array.isArray(arg)) {
|
||||||
// array of text
|
// array of text
|
||||||
|
|||||||
@ -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:
|
* 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.
|
* 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 {
|
> extends BasePlugin {
|
||||||
public readonly type: PluginType = PluginType.block;
|
public readonly type: PluginType = PluginType.block;
|
||||||
|
|
||||||
public readonly settings: T;
|
private readonly settings: T;
|
||||||
|
|
||||||
constructor(name: PluginName, pluginService: PluginService<BasePlugin>) {
|
constructor(name: PluginName, pluginService: PluginService<BasePlugin>) {
|
||||||
super(name, pluginService);
|
super(name, pluginService);
|
||||||
@ -38,6 +38,10 @@ export abstract class BaseBlockPlugin<
|
|||||||
this.settings = require(path.join(this.getPath(), 'settings')).default;
|
this.settings = require(path.join(this.getPath(), 'settings')).default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getDefaultSettings(): T {
|
||||||
|
return this.settings;
|
||||||
|
}
|
||||||
|
|
||||||
abstract template: PluginBlockTemplate;
|
abstract template: PluginBlockTemplate;
|
||||||
|
|
||||||
effects?: PluginEffects;
|
effects?: PluginEffects;
|
||||||
@ -50,8 +54,22 @@ export abstract class BaseBlockPlugin<
|
|||||||
|
|
||||||
protected getArguments(block: Block) {
|
protected getArguments(block: Block) {
|
||||||
if ('args' in block.message) {
|
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.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user