Skip to content

Commit

Permalink
✅ test: add test for plugin service
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Dec 8, 2023
1 parent dbb92a7 commit c1d40df
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 7 deletions.
157 changes: 151 additions & 6 deletions src/services/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { LobeChatPluginMeta } from '@lobehub/chat-plugin-sdk';
import { act } from '@testing-library/react';
import { notification } from 'antd';
import { Mock, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { LobeChatPluginManifest } from '@lobehub/chat-plugin-sdk';
import { Mock, beforeEach, describe, expect, it, vi } from 'vitest';

import { getPluginIndexJSON } from '@/const/url';
import { PluginModel } from '@/database/models/plugin';
import { DB_Plugin } from '@/database/schemas/plugin';
import { globalHelpers } from '@/store/global/helpers';
import { useToolStore } from '@/store/tool';
import { LobeTool } from '@/types/tool';
import { LobeToolCustomPlugin } from '@/types/tool/plugin';

import { pluginService } from '../plugin';
import { InstallPluginParams, pluginService } from '../plugin';

// Mocking modules and functions
vi.mock('@/const/url', () => ({
Expand All @@ -18,6 +19,15 @@ vi.mock('@/store/global/helpers', () => ({
getCurrentLanguage: vi.fn(),
},
}));
vi.mock('@/database/models/plugin', () => ({
PluginModel: {
getList: vi.fn(),
create: vi.fn(),
delete: vi.fn(),
update: vi.fn(),
clear: vi.fn(),
},
}));

beforeEach(() => {
vi.resetAllMocks();
Expand Down Expand Up @@ -185,4 +195,139 @@ describe('PluginService', () => {
}
});
});

describe('installPlugin', () => {
it('should install a plugin', async () => {
// Arrange
const fakePlugin = {
identifier: 'test-plugin',
manifest: { name: 'TestPlugin', version: '1.0.0' } as unknown as LobeChatPluginManifest,
type: 'plugin',
} as InstallPluginParams;
vi.mocked(PluginModel.create).mockResolvedValue(fakePlugin);

// Act
const installedPlugin = await pluginService.installPlugin(fakePlugin);

// Assert
expect(PluginModel.create).toHaveBeenCalledWith(fakePlugin);
expect(installedPlugin).toEqual(fakePlugin);
});
});

describe('getInstalledPlugins', () => {
it('should return a list of installed plugins', async () => {
// Arrange
const fakePlugins = [{ identifier: 'test-plugin', type: 'plugin' }] as LobeTool[];
vi.mocked(PluginModel.getList).mockResolvedValue(fakePlugins as DB_Plugin[]);

// Act
const installedPlugins = await pluginService.getInstalledPlugins();

// Assert
expect(PluginModel.getList).toHaveBeenCalled();
expect(installedPlugins).toEqual(fakePlugins);
});
});

describe('uninstallPlugin', () => {
it('should uninstall a plugin', async () => {
// Arrange
const identifier = 'test-plugin';
vi.mocked(PluginModel.delete).mockResolvedValue();

// Act
const result = await pluginService.uninstallPlugin(identifier);

// Assert
expect(PluginModel.delete).toHaveBeenCalledWith(identifier);
expect(result).toBe(undefined);
});
});

describe('createCustomPlugin', () => {
it('should create a custom plugin', async () => {
// Arrange
const customPlugin = {
identifier: 'custom-plugin',
manifest: {},
type: 'customPlugin',
} as LobeToolCustomPlugin;
vi.mocked(PluginModel.create).mockResolvedValue(customPlugin);

// Act
const result = await pluginService.createCustomPlugin(customPlugin);

// Assert
expect(PluginModel.create).toHaveBeenCalledWith({
...customPlugin,
type: 'customPlugin',
});
expect(result).toEqual(customPlugin);
});
});

describe('updatePlugin', () => {
it('should update a plugin', async () => {
// Arrange
const id = 'plugin-id';
const value = { name: 'NewPluginName' };
vi.mocked(PluginModel.update).mockResolvedValue(1);

// Act
const result = await pluginService.updatePlugin(id, value);

// Assert
expect(PluginModel.update).toHaveBeenCalledWith(id, value);
expect(result).toEqual(1);
});
});

describe('updatePluginManifest', () => {
it('should update a plugin manifest', async () => {
// Arrange
const id = 'plugin-id';
const manifest = { name: 'NewPluginManifest' };
vi.mocked(PluginModel.update).mockResolvedValue(1);

// Act
const result = await pluginService.updatePluginManifest(id, manifest);

// Assert
expect(PluginModel.update).toHaveBeenCalledWith(id, { manifest });
expect(result).toEqual(1);
});
});

describe('removeAllPlugins', () => {
it('should remove all plugins', async () => {
// Arrange
vi.mocked(PluginModel.clear).mockResolvedValue(undefined);

// Act
const result = await pluginService.removeAllPlugins();

// Assert
expect(PluginModel.clear).toHaveBeenCalled();
expect(result).toBe(undefined);
});
});

describe('updatePluginSettings', () => {
it('should update plugin settings', async () => {
// Arrange
const id = 'plugin-id';
const settings = { color: 'blue' };
vi.mocked(PluginModel.update).mockResolvedValue(1);

// Act
const result = await pluginService.updatePluginSettings(id, settings);

// Assert
expect(PluginModel.update).toHaveBeenCalledWith(id, { settings });
expect(result).toEqual(1);
});
});

// Add any additional tests to cover edge cases or error handling as needed.
});
2 changes: 1 addition & 1 deletion src/services/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { globalHelpers } from '@/store/global/helpers';
import { LobeTool } from '@/types/tool';
import { LobeToolCustomPlugin } from '@/types/tool/plugin';

interface InstallPluginParams {
export interface InstallPluginParams {
identifier: string;
manifest: LobeChatPluginManifest;
type: 'plugin' | 'customPlugin';
Expand Down

0 comments on commit c1d40df

Please sign in to comment.