Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 23, 03:00 UTC
Add tests for vercel toolset
haxzie committed
May 19, 2025 at 17:49 UTC
4af649bc29945f2782cdee36fc335dc18d55adab
1 file changed
+210
-4
packages/toolsets/vercel/test/vercel.test.ts
+210
-4
@@ -1,7 +1,213 @@
1
-import { describe, it, expect } from 'vitest';
1
+import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+import { VercelToolset } from '../src';
3
+import { Tool } from '@composio/core';
4
+import { jsonSchema, tool } from 'ai';
5
3
-describe('Vercel', () => {
4
- it('should be a test', () => {
5
- expect(true).toBe(true);
6
+// Define an interface for our mocked Vercel tool
7
+interface MockedVercelTool {
8
+ description: string;
9
+ parameters: any;
10
+ execute: Function;
11
+ _isMockedVercelTool: boolean;
12
+}
13
+
14
+// Mock the ai module
15
+vi.mock('ai', () => {
16
+ return {
17
+ tool: vi.fn().mockImplementation(toolConfig => {
18
+ return {
19
+ ...toolConfig,
20
+ _isMockedVercelTool: true,
21
+ } as MockedVercelTool;
22
+ }),
23
+ jsonSchema: vi.fn().mockImplementation(schema => schema),
24
+ };
25
+});
26
+
27
+describe('VercelToolset', () => {
28
+ let toolset: VercelToolset;
29
+ let mockTool: Tool;
30
+ let mockExecuteToolFn: any;
31
+
32
+ beforeEach(() => {
33
+ toolset = new VercelToolset();
34
+
35
+ // Mock the global execute tool function
36
+ mockExecuteToolFn = vi.fn().mockResolvedValue({
37
+ data: { result: 'success' },
38
+ error: null,
39
+ successful: true,
40
+ });
41
+ toolset._setExecuteToolFn(mockExecuteToolFn);
42
+
43
+ // Create a mock Composio tool
44
+ mockTool = {
45
+ slug: 'test-tool',
46
+ name: 'Test Tool',
47
+ description: 'A tool for testing',
48
+ inputParameters: {
49
+ type: 'object',
50
+ properties: {
51
+ input: {
52
+ type: 'string',
53
+ description: 'Test input',
54
+ },
55
+ },
56
+ required: ['input'],
57
+ },
58
+ tags: [],
59
+ };
60
+
61
+ // Reset mocks before each test
62
+ vi.clearAllMocks();
63
+ });
64
+
65
+ describe('name property', () => {
66
+ it('should have the correct name', () => {
67
+ expect(toolset.name).toBe('vercel');
68
+ });
69
+ });
70
+
71
+ describe('_isAgentic property', () => {
72
+ it('should be agentic', () => {
73
+ expect(toolset._isAgentic).toBe(true);
74
+ });
75
+ });
76
+
77
+ describe('wrapTool', () => {
78
+ it('should wrap a tool in Vercel tool format', () => {
79
+ const wrapped = toolset.wrapTool(mockTool, mockExecuteToolFn) as unknown as MockedVercelTool;
80
+
81
+ expect(tool).toHaveBeenCalledWith({
82
+ description: mockTool.description,
83
+ parameters: mockTool.inputParameters,
84
+ execute: expect.any(Function),
85
+ });
86
+
87
+ expect(jsonSchema).toHaveBeenCalledWith(mockTool.inputParameters);
88
+ expect(wrapped._isMockedVercelTool).toBe(true);
89
+ });
90
+
91
+ it('should handle tools without input parameters', () => {
92
+ const toolWithoutParams: Tool = {
93
+ ...mockTool,
94
+ inputParameters: undefined,
95
+ };
96
+
97
+ const wrapped = toolset.wrapTool(
98
+ toolWithoutParams,
99
+ mockExecuteToolFn
100
+ ) as unknown as MockedVercelTool;
101
+
102
+ expect(jsonSchema).toHaveBeenCalledWith({});
103
+ expect(wrapped._isMockedVercelTool).toBe(true);
104
+ });
105
+
106
+ it('should create a function that executes the tool with the right parameters', async () => {
107
+ toolset.wrapTool(mockTool, mockExecuteToolFn) as unknown as MockedVercelTool;
108
+
109
+ // Extract the execute function from the call to tool()
110
+ const executeFunction = (tool as any).mock.calls[0][0].execute;
111
+
112
+ // Test the execute function with an object parameter
113
+ const params = { input: 'test-value' };
114
+ await executeFunction(params);
115
+
116
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, params);
117
+
118
+ // Reset and test with a JSON string parameter
119
+ vi.clearAllMocks();
120
+ const stringParams = JSON.stringify(params);
121
+ await executeFunction(stringParams);
122
+
123
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, params);
124
+ });
125
+ });
126
+
127
+ describe('wrapTools', () => {
128
+ it('should wrap multiple tools', () => {
129
+ const anotherTool: Tool = {
130
+ ...mockTool,
131
+ slug: 'another-tool',
132
+ name: 'Another Tool',
133
+ };
134
+ const tools = [mockTool, anotherTool];
135
+
136
+ const wrapped = toolset.wrapTools(tools, mockExecuteToolFn);
137
+
138
+ // Verify the result has the expected properties
139
+ expect(Object.keys(wrapped)).toHaveLength(2);
140
+ expect(wrapped['test-tool']).toBeDefined();
141
+ expect(wrapped['another-tool']).toBeDefined();
142
+
143
+ // Verify tool was called with the right parameters for each tool
144
+ expect(tool).toHaveBeenCalledTimes(2);
145
+ expect(tool).toHaveBeenCalledWith({
146
+ description: mockTool.description,
147
+ parameters: mockTool.inputParameters,
148
+ execute: expect.any(Function),
149
+ });
150
+ expect(tool).toHaveBeenCalledWith({
151
+ description: anotherTool.description,
152
+ parameters: anotherTool.inputParameters,
153
+ execute: expect.any(Function),
154
+ });
155
+ });
156
+
157
+ it('should return an empty object for empty tools array', () => {
158
+ const wrapped = toolset.wrapTools([], mockExecuteToolFn);
159
+ expect(wrapped).toEqual({});
160
+ expect(tool).not.toHaveBeenCalled();
161
+ });
162
+ });
163
+
164
+ describe('executeTool', () => {
165
+ it('should execute a tool using the global execute function', async () => {
166
+ const toolSlug = 'test-tool';
167
+ const toolParams = {
168
+ userId: 'test-user',
169
+ arguments: { input: 'test-value' },
170
+ };
171
+
172
+ const result = await toolset.executeTool(toolSlug, toolParams);
173
+
174
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(toolSlug, toolParams, undefined);
175
+ expect(result).toEqual({
176
+ data: { result: 'success' },
177
+ error: null,
178
+ successful: true,
179
+ });
180
+ });
181
+
182
+ it('should pass modifiers to the global execute function', async () => {
183
+ const toolSlug = 'test-tool';
184
+ const toolParams = {
185
+ userId: 'test-user',
186
+ arguments: { input: 'test-value' },
187
+ };
188
+
189
+ const modifiers = {
190
+ beforeToolExecute: vi.fn(params => params),
191
+ afterToolExecute: vi.fn(response => response),
192
+ };
193
+
194
+ await toolset.executeTool(toolSlug, toolParams, modifiers);
195
+
196
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(toolSlug, toolParams, modifiers);
197
+ });
198
+ });
199
+
200
+ describe('integration with Vercel AI SDK', () => {
201
+ it('should produce tools compatible with Vercel AI SDK', () => {
202
+ const wrapped = toolset.wrapTool(mockTool, mockExecuteToolFn) as unknown as MockedVercelTool;
203
+
204
+ // Verify the wrapped tool has the expected structure
205
+ expect(wrapped).toHaveProperty('description');
206
+ expect(wrapped).toHaveProperty('parameters');
207
+ expect(wrapped).toHaveProperty('execute');
208
+
209
+ // The tool should be compatible with Vercel AI SDK's expected structure
210
+ expect(typeof wrapped.execute).toBe('function');
211
+ });
212
});
213
});