-
-
Notifications
You must be signed in to change notification settings - Fork 11.3k
/
Copy pathaction.test.ts
196 lines (155 loc) · 5.88 KB
/
action.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { LobeChatPluginMeta } from '@lobehub/chat-plugin-sdk';
import { act, renderHook } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { pluginService } from '@/services/plugin';
import { LobeTool } from '@/types/tool';
import { merge } from '@/utils/merge';
import { useToolStore } from '../../store';
vi.mock('@/services/plugin', () => ({
pluginService: {
updatePluginSettings: vi.fn(),
removeAllPlugins: vi.fn(),
},
}));
beforeEach(() => {
// Reset all mocks before each test
vi.resetAllMocks();
});
describe('useToolStore:plugin', () => {
describe('checkPluginsIsInstalled', () => {
it('should not perform any operations if the plugin list is empty', async () => {
const installPluginsMock = vi.fn();
useToolStore.setState({
loadPluginStore: vi.fn(),
installPlugins: installPluginsMock,
});
const { result } = renderHook(() => useToolStore());
await act(async () => {
await result.current.checkPluginsIsInstalled([]);
});
expect(installPluginsMock).not.toHaveBeenCalled();
});
it('should load the plugin store and install plugins if necessary', async () => {
const plugins = ['plugin1', 'plugin2'];
const loadPluginStoreMock = vi.fn();
const installPluginsMock = vi.fn();
useToolStore.setState({
loadPluginStore: loadPluginStoreMock,
installPlugins: installPluginsMock,
});
const { result } = renderHook(() => useToolStore());
await act(async () => {
await result.current.checkPluginsIsInstalled(plugins);
});
expect(loadPluginStoreMock).toHaveBeenCalled();
expect(installPluginsMock).toHaveBeenCalledWith(plugins);
});
it('should not load the plugin store and install plugins', async () => {
const plugins = ['plugin1', 'plugin2'];
const loadPluginStoreMock = vi.fn();
const installPluginsMock = vi.fn();
useToolStore.setState({
loadPluginStore: loadPluginStoreMock,
installPlugins: installPluginsMock,
installedPlugins: [{ identifier: 'abc' }] as LobeTool[],
pluginStoreList: [{ identifier: 'abc' }] as LobeChatPluginMeta[],
});
const { result } = renderHook(() => useToolStore());
await act(async () => {
await result.current.checkPluginsIsInstalled(plugins);
});
expect(loadPluginStoreMock).not.toHaveBeenCalled();
expect(installPluginsMock).toHaveBeenCalledWith(plugins);
});
});
describe('updatePluginSettings', () => {
it('should update settings for a given plugin', async () => {
const pluginId = 'test-plugin';
const newSettings = { setting1: 'new-value' };
const { result } = renderHook(() => useToolStore());
await act(async () => {
await result.current.updatePluginSettings(pluginId, newSettings);
});
expect(pluginService.updatePluginSettings).toBeCalledWith(
pluginId,
newSettings,
expect.any(AbortSignal),
);
});
it('should merge settings for a plugin with existing settings', async () => {
const pluginId = 'test-plugin';
const existingSettings = { setting1: 'old-value', setting2: 'old-value' };
const newSettings = { setting1: 'new-value' };
const mergedSettings = merge(existingSettings, newSettings);
useToolStore.setState({
installedPlugins: [{ identifier: pluginId, settings: existingSettings }] as LobeTool[],
});
const { result } = renderHook(() => useToolStore());
await act(async () => {
await result.current.updatePluginSettings(pluginId, newSettings);
});
expect(pluginService.updatePluginSettings).toBeCalledWith(
pluginId,
mergedSettings,
expect.any(AbortSignal),
);
});
});
describe('removeAllPlugins', () => {
it('should reset all plugin settings', () => {
const { result } = renderHook(() => useToolStore());
act(() => {
result.current.removeAllPlugins();
});
expect(pluginService.removeAllPlugins).toBeCalled();
});
});
describe('validatePluginSettings', () => {
// 模拟插件数据
const testPluginId = 'test-plugin';
// 定义测试用的 schema 和模拟的验证结果
const testSchema = {
properties: { abc: { type: 'string' } },
required: ['abc'],
type: 'object',
};
const testPluginSettings = { abc: 'valid-string' };
const testPlugin = {
type: 'plugin',
identifier: testPluginId,
manifest: {
identifier: testPluginId,
settings: testSchema,
},
settings: testPluginSettings,
} as unknown as LobeTool;
it('should validate settings against the schema and return valid result', async () => {
const { result } = renderHook(() => useToolStore());
act(() => {
useToolStore.setState({
installedPlugins: [testPlugin],
});
});
const validationResult = await result.current.validatePluginSettings(testPluginId);
expect(validationResult).toEqual({ valid: true, errors: [] });
});
it('should return invalid result if settings do not match the schema', async () => {
const { result } = renderHook(() => useToolStore());
act(() => {
useToolStore.setState({
installedPlugins: [{ ...testPlugin, settings: {} }] as any,
});
});
const validationResult = await result.current.validatePluginSettings(testPluginId);
expect(validationResult).toMatchSnapshot();
});
it('should return undefined if manifest or settings are not found', async () => {
useToolStore.setState({
installedPlugins: [],
});
const { result } = renderHook(() => useToolStore());
const validationResult = await result.current.validatePluginSettings(testPluginId);
expect(validationResult).toBeUndefined();
});
});
});