Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 294 lines 9.04 KB
Raw
1 import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest';
2 import { VercelProvider } from '../src';
3 import { Tool } from '@composio/core';
4 import { tool } from 'ai';
5
6 // Define an interface for our mocked Vercel tool
7 interface MockedVercelTool {
8 description: string;
9 inputSchema: unknown;
10 execute: Function;
11 _isMockedVercelTool: boolean;
12 }
13
14 /** The AI SDK schema the provider builds; `validate` runs the tool's Zod schema. */
15 type ValidatingSchema = { validate: (value: unknown) => Promise<{ success: boolean }> };
16
17 // Mock the ai module
18 vi.mock('ai', async importOriginal => {
19 const actual = await importOriginal<typeof import('ai')>();
20 return {
21 tool: vi.fn().mockImplementation(toolConfig => {
22 return {
23 ...toolConfig,
24 _isMockedVercelTool: true,
25 } as MockedVercelTool;
26 }),
27 jsonSchema: actual.jsonSchema,
28 };
29 });
30
31 describe('VercelProvider', () => {
32 let provider: VercelProvider;
33 let mockTool: Tool;
34 let mockExecuteToolFn: Mock;
35
36 beforeEach(() => {
37 provider = new VercelProvider();
38
39 // Mock the global execute tool function
40 mockExecuteToolFn = vi.fn().mockResolvedValue({
41 data: { result: 'success' },
42 error: null,
43 successful: true,
44 });
45 provider._setExecuteToolFn(mockExecuteToolFn);
46
47 // Create a mock Composio tool
48 mockTool = {
49 slug: 'test-tool',
50 name: 'Test Tool',
51 description: 'A tool for testing',
52 version: '20250909_00',
53 availableVersions: ['20250909_00', '20250901_00'],
54 inputParameters: {
55 type: 'object',
56 properties: {
57 input: {
58 type: 'string',
59 description: 'Test input',
60 },
61 },
62 required: ['input'],
63 },
64 tags: [],
65 };
66
67 // Reset mocks before each test
68 vi.clearAllMocks();
69 });
70
71 describe('name property', () => {
72 it('should have the correct name', () => {
73 expect(provider.name).toBe('vercel');
74 });
75 });
76
77 describe('_isAgentic property', () => {
78 it('should be agentic', () => {
79 expect(provider._isAgentic).toBe(true);
80 });
81 });
82
83 describe('wrapTool', () => {
84 it('should wrap a tool in Vercel tool format', () => {
85 const wrapped = provider.wrapTool(mockTool, mockExecuteToolFn) as unknown as MockedVercelTool;
86
87 expect(tool).toHaveBeenCalledWith({
88 description: mockTool.description,
89 inputSchema: expect.any(Object),
90 execute: expect.any(Function),
91 });
92
93 expect(wrapped._isMockedVercelTool).toBe(true);
94 });
95
96 it('should handle tools without input parameters', () => {
97 const toolWithoutParams: Tool = {
98 ...mockTool,
99 inputParameters: undefined,
100 };
101
102 const wrapped = provider.wrapTool(
103 toolWithoutParams,
104 mockExecuteToolFn
105 ) as unknown as MockedVercelTool;
106
107 expect(tool).toHaveBeenCalledWith({
108 description: toolWithoutParams.description,
109 inputSchema: expect.any(Object),
110 execute: expect.any(Function),
111 });
112 expect(wrapped._isMockedVercelTool).toBe(true);
113 });
114
115 it('should create a function that executes the tool with the right parameters', async () => {
116 provider.wrapTool(mockTool, mockExecuteToolFn) as unknown as MockedVercelTool;
117
118 // Extract the execute function from the call to tool()
119 const executeFunction = (
120 tool as unknown as Mock<
121 (config: { execute: (input: unknown) => Promise<unknown> }) => unknown
122 >
123 ).mock.calls[0][0].execute;
124
125 // Test the execute function with an object parameter
126 const params = { input: 'test-value' };
127 await executeFunction(params);
128
129 expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, params);
130
131 // Reset and test with a JSON string parameter
132 vi.clearAllMocks();
133 const stringParams = JSON.stringify(params);
134 await executeFunction(stringParams);
135
136 expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, params);
137 });
138
139 it('keeps optional and nested parameters available under strict mode', async () => {
140 const strictProvider = new VercelProvider({ strict: true });
141 const wrapped = strictProvider.wrapTool(
142 {
143 ...mockTool,
144 inputParameters: {
145 type: 'object',
146 properties: {
147 cfg: {
148 type: 'object',
149 properties: { url: { type: 'string' }, note: { type: 'string' } },
150 required: ['url'],
151 },
152 id: { type: ['string', 'null'] },
153 label: { type: 'string' },
154 },
155 required: ['cfg', 'id'],
156 },
157 },
158 mockExecuteToolFn
159 ) as unknown as MockedVercelTool;
160
161 const schema = wrapped.inputSchema as ValidatingSchema;
162
163 // Optional parameters are still accepted, and may be null.
164 expect(
165 (
166 await schema.validate({
167 cfg: { url: 'https://example.com', note: 'x' },
168 id: null,
169 label: null,
170 })
171 ).success
172 ).toBe(true);
173 // Every property is required under strict mode, so omitting one fails.
174 expect(
175 (await schema.validate({ cfg: { url: 'https://example.com' }, id: 'a' })).success
176 ).toBe(false);
177 // Objects are closed, so unknown keys fail.
178 expect(
179 (
180 await schema.validate({
181 cfg: { url: 'https://example.com', note: null, extra: 1 },
182 id: 'a',
183 label: null,
184 })
185 ).success
186 ).toBe(false);
187 });
188
189 it('omits null arguments before executing under strict mode', async () => {
190 const strictProvider = new VercelProvider({ strict: true });
191 const wrapped = strictProvider.wrapTool(
192 {
193 ...mockTool,
194 inputParameters: {
195 type: 'object',
196 properties: {
197 cfg: {
198 type: 'object',
199 properties: { url: { type: 'string' }, note: { type: 'string' } },
200 required: ['url'],
201 },
202 label: { type: 'string' },
203 },
204 required: ['cfg'],
205 },
206 },
207 mockExecuteToolFn
208 ) as unknown as MockedVercelTool;
209
210 await wrapped.execute(
211 { cfg: { url: 'https://example.com', note: null }, label: null },
212 { toolCallId: 'call_1', messages: [] }
213 );
214
215 expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, {
216 cfg: { url: 'https://example.com' },
217 });
218 });
219
220 it('keeps the original schema for tools strict mode cannot express', async () => {
221 const strictProvider = new VercelProvider({ strict: true });
222 const wrapped = strictProvider.wrapTool(
223 {
224 ...mockTool,
225 inputParameters: {
226 type: 'object',
227 properties: {
228 headers: { type: 'object', additionalProperties: { type: 'string' } },
229 name: { type: 'string' },
230 },
231 required: ['headers'],
232 },
233 },
234 mockExecuteToolFn
235 ) as unknown as MockedVercelTool;
236
237 const schema = wrapped.inputSchema as ValidatingSchema;
238
239 // `name` stays optional and `headers` still accepts arbitrary keys.
240 expect((await schema.validate({ headers: { 'x-a': '1' } })).success).toBe(true);
241 });
242 });
243
244 describe('wrapTools', () => {
245 it('should wrap multiple tools', () => {
246 const anotherTool: Tool = {
247 ...mockTool,
248 slug: 'another-tool',
249 name: 'Another Tool',
250 };
251 const tools = [mockTool, anotherTool];
252
253 const wrapped = provider.wrapTools(tools, mockExecuteToolFn);
254
255 // Verify the result has the expected properties
256 expect(Object.keys(wrapped)).toHaveLength(2);
257 expect(wrapped['test-tool']).toBeDefined();
258 expect(wrapped['another-tool']).toBeDefined();
259
260 // Verify tool was called with the right parameters for each tool
261 expect(tool).toHaveBeenCalledTimes(2);
262 expect(tool).toHaveBeenCalledWith({
263 description: mockTool.description,
264 inputSchema: expect.any(Object),
265 execute: expect.any(Function),
266 });
267 expect(tool).toHaveBeenCalledWith({
268 description: anotherTool.description,
269 inputSchema: expect.any(Object),
270 execute: expect.any(Function),
271 });
272 });
273
274 it('should return an empty object for empty tools array', () => {
275 const wrapped = provider.wrapTools([], mockExecuteToolFn);
276 expect(wrapped).toEqual({});
277 expect(tool).not.toHaveBeenCalled();
278 });
279 });
280
281 describe('integration with Vercel AI SDK', () => {
282 it('should produce tools compatible with Vercel AI SDK', () => {
283 const wrapped = provider.wrapTool(mockTool, mockExecuteToolFn) as unknown as MockedVercelTool;
284
285 // Verify the wrapped tool has the expected structure
286 expect(wrapped).toHaveProperty('description');
287 expect(wrapped).toHaveProperty('inputSchema'); // Changed from 'parameters' to 'inputSchema'
288 expect(wrapped).toHaveProperty('execute');
289
290 // The tool should be compatible with Vercel AI SDK's expected structure
291 expect(typeof wrapped.execute).toBe('function');
292 });
293 });
294 });