forked from QVerisAI/qveris-agent-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.test.ts
More file actions
215 lines (186 loc) · 6.29 KB
/
Copy pathexecute.test.ts
File metadata and controls
215 lines (186 loc) · 6.29 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* Unit tests for the call (execute_tool) MCP tool
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { executeExecuteTool, executeToolSchema } from './execute.js';
import { QverisClient } from '../api/client.js';
import type { ExecuteResponse } from '../types.js';
describe('call (execute_tool)', () => {
describe('executeToolSchema', () => {
it('should have tool_id, search_id, params_to_tool as required', () => {
expect(executeToolSchema.required).toContain('tool_id');
expect(executeToolSchema.required).toContain('search_id');
expect(executeToolSchema.required).toContain('params_to_tool');
});
it('should define tool_id as string', () => {
expect(executeToolSchema.properties.tool_id.type).toBe('string');
});
it('should define search_id as string', () => {
expect(executeToolSchema.properties.search_id.type).toBe('string');
});
it('should define params_to_tool as object', () => {
expect(executeToolSchema.properties.params_to_tool.type).toBe('object');
});
it('should define max_response_size with default', () => {
expect(executeToolSchema.properties.max_response_size.type).toBe('number');
expect(executeToolSchema.properties.max_response_size.default).toBe(20480);
});
it('should define session_id as optional', () => {
expect(executeToolSchema.properties.session_id.type).toBe('string');
expect(executeToolSchema.required).not.toContain('session_id');
});
});
describe('executeExecuteTool', () => {
let mockClient: QverisClient;
let executeToolMock: ReturnType<typeof vi.fn>;
beforeEach(() => {
executeToolMock = vi.fn();
mockClient = {
executeTool: executeToolMock,
} as unknown as QverisClient;
});
afterEach(() => {
vi.restoreAllMocks();
});
it('should pass object params and call client.executeTool', async () => {
const mockResponse: ExecuteResponse = {
execution_id: 'exec-123',
tool_id: 'weather-tool',
parameters: { city: 'Tokyo', units: 'metric' },
success: true,
result: { data: { temperature: 25, humidity: 60 } },
created_at: '2025-01-15T10:00:00Z',
};
executeToolMock.mockResolvedValueOnce(mockResponse);
const result = await executeExecuteTool(
mockClient,
{
tool_id: 'weather-tool',
search_id: 'search-123',
params_to_tool: { city: 'Tokyo', units: 'metric' },
},
'default-session'
);
expect(executeToolMock).toHaveBeenCalledWith('weather-tool', {
search_id: 'search-123',
session_id: 'default-session',
parameters: { city: 'Tokyo', units: 'metric' },
max_response_size: undefined,
});
expect(result).toEqual(mockResponse);
});
it('should use provided session_id over default', async () => {
executeToolMock.mockResolvedValueOnce({
execution_id: 'exec-123',
tool_id: 'tool-1',
parameters: {},
success: true,
created_at: '2025-01-15T10:00:00Z',
});
await executeExecuteTool(
mockClient,
{
tool_id: 'tool-1',
search_id: 'search-123',
params_to_tool: {},
session_id: 'custom-session',
},
'default-session'
);
expect(executeToolMock).toHaveBeenCalledWith('tool-1', {
search_id: 'search-123',
session_id: 'custom-session',
parameters: {},
max_response_size: undefined,
});
});
it('should pass max_response_size when provided', async () => {
executeToolMock.mockResolvedValueOnce({
execution_id: 'exec-123',
tool_id: 'tool-1',
parameters: {},
success: true,
created_at: '2025-01-15T10:00:00Z',
});
await executeExecuteTool(
mockClient,
{
tool_id: 'tool-1',
search_id: 'search-123',
params_to_tool: {},
max_response_size: 102400,
},
'default-session'
);
expect(executeToolMock).toHaveBeenCalledWith('tool-1', {
search_id: 'search-123',
session_id: 'default-session',
parameters: {},
max_response_size: 102400,
});
});
it.each(['not valid json', null, ['city']])(
'should reject non-object params_to_tool: %s',
async (paramsToTool) => {
await expect(
executeExecuteTool(
mockClient,
{
tool_id: 'tool-1',
search_id: 'search-123',
params_to_tool: paramsToTool as unknown as Record<string, unknown>,
},
'default-session'
)
).rejects.toThrow('params_to_tool must be a JSON object');
}
);
it('should handle complex nested parameters', async () => {
const complexParams = {
filters: {
category: 'electronics',
priceRange: { min: 100, max: 500 },
},
sort: ['price', 'rating'],
limit: 10,
};
executeToolMock.mockResolvedValueOnce({
execution_id: 'exec-123',
tool_id: 'search-products',
parameters: complexParams,
success: true,
created_at: '2025-01-15T10:00:00Z',
});
await executeExecuteTool(
mockClient,
{
tool_id: 'search-products',
search_id: 'search-123',
params_to_tool: complexParams,
},
'default-session'
);
expect(executeToolMock).toHaveBeenCalledWith('search-products', {
search_id: 'search-123',
session_id: 'default-session',
parameters: complexParams,
max_response_size: undefined,
});
});
it('should propagate errors from client', async () => {
const error = { status: 429, message: 'Rate limit exceeded' };
executeToolMock.mockRejectedValueOnce(error);
await expect(
executeExecuteTool(
mockClient,
{
tool_id: 'tool-1',
search_id: 'search-123',
params_to_tool: {},
},
'session'
)
).rejects.toEqual(error);
});
});
});