Skip to content

Commit d129a4d

Browse files
Add unit tests for the installed plugins API handler to verify tool retrieval and ID formatting.
1 parent 6c759a7 commit d129a4d

File tree

2 files changed

+147
-1
lines changed

2 files changed

+147
-1
lines changed

projects/app/src/pages/api/core/plugin/admin/marketplace/installed.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,16 @@ async function handler(
2424
const { type } = req.query;
2525

2626
const tools = await APIGetSystemToolList();
27+
console.log(tools);
2728

2829
return {
29-
ids: tools.map((tool) => tool.id.replace(`${AppToolSourceEnum.systemTool}-`, ''))
30+
list: tools.map((tool) => ({
31+
id: tool.id.replace(`${AppToolSourceEnum.systemTool}-`, ''),
32+
version: tool.version
33+
}))
3034
};
3135
}
3236

3337
export default NextAPI(handler);
38+
39+
export { handler };
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest';
2+
import type { ApiRequestProps, ApiResponseType } from '@fastgpt/service/type/next';
3+
import { AppToolSourceEnum } from '@fastgpt/global/core/app/tool/constants';
4+
5+
// --- MOCKS ---
6+
7+
vi.mock('@fastgpt/service/core/app/tool/api', async (importOriginal) => {
8+
const actual = await importOriginal<typeof import('@fastgpt/service/core/app/tool/api')>();
9+
return {
10+
...actual,
11+
APIGetSystemToolList: vi.fn()
12+
};
13+
});
14+
15+
vi.mock('@fastgpt/service/support/permission/user/auth', () => ({
16+
authSystemAdmin: vi.fn().mockResolvedValue(undefined)
17+
}));
18+
19+
import { handler } from '@/pages/api/core/plugin/admin/marketplace/installed';
20+
import { APIGetSystemToolList } from '@fastgpt/service/core/app/tool/api';
21+
22+
describe('handler (installed)', () => {
23+
beforeEach(() => {
24+
vi.clearAllMocks();
25+
});
26+
27+
it('should return an empty list if no tools are installed', async () => {
28+
vi.mocked(APIGetSystemToolList).mockResolvedValueOnce([]);
29+
30+
const req = {
31+
query: {}
32+
} as unknown as ApiRequestProps<{}, { type?: string }>;
33+
const res = {} as ApiResponseType<any>;
34+
35+
const result = await handler(req, res);
36+
37+
expect(result).toEqual({ list: [] });
38+
expect(APIGetSystemToolList).toHaveBeenCalledTimes(1);
39+
});
40+
41+
it('should return the list of installed tools with id and version', async () => {
42+
vi.mocked(APIGetSystemToolList).mockResolvedValueOnce([
43+
{
44+
id: `${AppToolSourceEnum.systemTool}-toolA`,
45+
version: '1.2.3',
46+
name: 'Tool A',
47+
desc: 'desc',
48+
avatar: '',
49+
moduleName: '',
50+
moduleLogo: '',
51+
showStatus: true,
52+
status: true,
53+
isSystem: true
54+
},
55+
{
56+
id: `${AppToolSourceEnum.systemTool}-toolB`,
57+
version: '4.5.6',
58+
name: 'Tool B',
59+
desc: 'desc',
60+
avatar: '',
61+
moduleName: '',
62+
moduleLogo: '',
63+
showStatus: true,
64+
status: true,
65+
isSystem: true
66+
}
67+
]);
68+
69+
const req = {
70+
query: {}
71+
} as unknown as ApiRequestProps<{}, { type?: string }>;
72+
const res = {} as ApiResponseType<any>;
73+
74+
const result = await handler(req, res);
75+
76+
expect(result).toEqual({
77+
list: [
78+
{ id: 'toolA', version: '1.2.3' },
79+
{ id: 'toolB', version: '4.5.6' }
80+
]
81+
});
82+
expect(APIGetSystemToolList).toHaveBeenCalledTimes(1);
83+
});
84+
85+
it('should remove only the systemTool- prefix from id', async () => {
86+
vi.mocked(APIGetSystemToolList).mockResolvedValueOnce([
87+
{
88+
id: `${AppToolSourceEnum.systemTool}-my-tool-123`,
89+
version: '0.0.1',
90+
name: 'My Tool',
91+
desc: 'desc',
92+
avatar: '',
93+
moduleName: '',
94+
moduleLogo: '',
95+
showStatus: true,
96+
status: true,
97+
isSystem: true
98+
}
99+
]);
100+
101+
const req = {
102+
query: { type: 'whatever' }
103+
} as unknown as ApiRequestProps<{}, { type?: string }>;
104+
const res = {} as ApiResponseType<any>;
105+
106+
const result = await handler(req, res);
107+
108+
expect(result).toEqual({
109+
list: [{ id: 'my-tool-123', version: '0.0.1' }]
110+
});
111+
});
112+
113+
it('should handle tools with ids not starting with the systemTool- prefix', async () => {
114+
vi.mocked(APIGetSystemToolList).mockResolvedValueOnce([
115+
{
116+
id: 'randomprefix-toolX',
117+
version: '9.9.9',
118+
name: 'Tool X',
119+
desc: 'desc',
120+
avatar: '',
121+
moduleName: '',
122+
moduleLogo: '',
123+
showStatus: true,
124+
status: true,
125+
isSystem: true
126+
}
127+
]);
128+
129+
const req = {
130+
query: {}
131+
} as unknown as ApiRequestProps<{}, { type?: string }>;
132+
const res = {} as ApiResponseType<any>;
133+
134+
const result = await handler(req, res);
135+
136+
expect(result).toEqual({
137+
list: [{ id: 'randomprefix-toolX', version: '9.9.9' }]
138+
});
139+
});
140+
});

0 commit comments

Comments
 (0)