@devries-cameron20 / composio Mirror · read-only
Fork 0

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 21, 04:45 UTC

next
ts 297 lines 8.98 KB
Raw
1 import { describe, it, expect, vi } from 'vitest';
2 import { mockClient } from '../utils/mocks/client.mock';
3 import { toolMocks } from '../utils/mocks/data.mock';
4 import { Tool } from '../../src/types/tool.types';
5 import {
6 createTestContext,
7 setupTest,
8 mockToolExecution,
9 createSchemaModifier,
10 createExecutionModifiers,
11 } from '../utils/toolExecuteUtils';
12
13 describe('Tools Modifiers', () => {
14 const context = createTestContext();
15 setupTest(context);
16
17 describe('Schema Modifiers', () => {
18 it('should modify the tool schema with the provided modifier', async () => {
19 const userId = 'test-user';
20 const slug = 'COMPOSIO_TOOL';
21 const schemaModifier = createSchemaModifier({
22 name: 'Modified Name',
23 description: 'Modified description',
24 });
25
26 // mock client to send back the tool
27 mockClient.tools.retrieve.mockResolvedValueOnce(toolMocks.rawTool);
28
29 const result = await context.tools.getRawComposioToolBySlug(slug, {
30 modifySchema: schemaModifier,
31 });
32
33 expect(schemaModifier).toHaveBeenCalledTimes(1);
34 expect(schemaModifier).toHaveBeenCalledWith(
35 expect.objectContaining({
36 toolSlug: slug,
37 toolkitSlug: expect.any(String),
38 schema: expect.objectContaining({ slug }),
39 })
40 );
41 expect(result.name).toEqual('Modified Name');
42 expect(result.description).toEqual('Modified description');
43 });
44
45 it('should apply schema modifiers to all tools when getting multiple tools', async () => {
46 const userId = 'test-user';
47 const schemaModifier = createSchemaModifier(({ schema }) => ({
48 ...schema,
49 name: `Modified ${schema.name}`,
50 }));
51
52 mockClient.tools.list.mockResolvedValueOnce({
53 items: [
54 { ...toolMocks.rawTool, slug: 'TOOL1' },
55 { ...toolMocks.rawTool, slug: 'TOOL2' },
56 ],
57 totalPages: 1,
58 });
59
60 const result = await context.tools.getRawComposioTools(
61 { tools: ['TOOL1', 'TOOL2'] },
62 { modifySchema: schemaModifier }
63 );
64
65 expect(schemaModifier).toHaveBeenCalledTimes(2);
66 expect(result[0].name).toContain('Modified');
67 expect(result[1].name).toContain('Modified');
68 });
69 });
70
71 describe('Execution Modifiers', () => {
72 it('should apply beforeExecute modifier before executing a tool', async () => {
73 const slug = 'COMPOSIO_TOOL';
74 const body = {
75 userId: 'test-user',
76 arguments: { limit: 5 },
77 dangerouslySkipVersionCheck: true,
78 };
79
80 const { beforeExecute } = createExecutionModifiers({
81 beforeModifications: {
82 arguments: { limit: 10 },
83 },
84 });
85
86 await mockToolExecution(context.tools);
87 await context.tools.execute(slug, body, { beforeExecute });
88
89 expect(beforeExecute).toHaveBeenCalledTimes(1);
90 expect(beforeExecute).toHaveBeenCalledWith(
91 expect.objectContaining({
92 toolSlug: slug,
93 toolkitSlug: expect.any(String),
94 params: body,
95 })
96 );
97 expect(mockClient.tools.execute).toHaveBeenCalledWith(
98 slug,
99 expect.objectContaining({
100 arguments: { limit: 10 },
101 }),
102 { maxRetries: 0 }
103 );
104 });
105
106 it('should apply afterExecute modifier after executing a tool', async () => {
107 const slug = 'COMPOSIO_TOOL';
108 const body = { userId: 'test-user', arguments: {}, dangerouslySkipVersionCheck: true };
109
110 const { afterExecute } = createExecutionModifiers({
111 afterModifications: {
112 data: { enhanced: true },
113 },
114 });
115
116 await mockToolExecution(context.tools);
117 const result = await context.tools.execute(slug, body, { afterExecute });
118
119 expect(afterExecute).toHaveBeenCalledTimes(1);
120 expect(afterExecute).toHaveBeenCalledWith(
121 expect.objectContaining({
122 toolSlug: slug,
123 toolkitSlug: expect.any(String),
124 result: expect.objectContaining({
125 data: expect.any(Object),
126 successful: expect.any(Boolean),
127 }),
128 })
129 );
130 expect(result.data).toHaveProperty('enhanced', true);
131 });
132 });
133
134 describe('Combined Modifiers', () => {
135 it('should apply both schema and execution modifiers together', async () => {
136 const userId = 'test-user';
137 const slug = 'GITHUB_GET_REPOS';
138
139 // Mock analytics and logging functions
140 const trackToolUsage = vi.fn();
141 const logToolError = vi.fn();
142
143 const schemaModifier = createSchemaModifier(({ schema, toolSlug }) => ({
144 ...schema,
145 description: `Enhanced ${toolSlug} for better context`,
146 tags: [...(schema.tags || []), 'enhanced', 'modified'],
147 inputParameters: {
148 type: 'object' as const,
149 properties: {
150 ...schema.inputParameters?.properties,
151 tracking: {
152 type: 'object' as const,
153 properties: {
154 enabled: {
155 type: 'boolean' as const,
156 default: true,
157 },
158 },
159 },
160 },
161 },
162 }));
163
164 const executionModifiers = {
165 beforeExecute: vi.fn(({ params, toolSlug }) => {
166 // Add analytics tracking
167 trackToolUsage(toolSlug);
168 return {
169 ...params,
170 arguments: {
171 ...(params.arguments || {}),
172 tracking: {
173 enabled: true,
174 timestamp: expect.any(String),
175 },
176 },
177 };
178 }),
179 afterExecute: vi.fn(({ result, toolSlug }) => {
180 // Log results and handle errors
181 if (!result.successful) {
182 logToolError(toolSlug, result.error);
183 }
184 return {
185 ...result,
186 data: {
187 ...(result.data || {}),
188 processed: true,
189 timestamp: expect.any(String),
190 },
191 };
192 }),
193 };
194
195 const getRawComposioToolBySlugSpy = vi.spyOn(context.tools, 'getRawComposioToolBySlug');
196 const mockTool = {
197 ...(toolMocks.transformedTool as unknown as Tool),
198 description: 'Enhanced GITHUB_GET_REPOS for better context',
199 tags: ['enhanced', 'modified'],
200 inputParameters: {
201 type: 'object' as const,
202 properties: {
203 tracking: {
204 type: 'object' as const,
205 properties: {
206 enabled: {
207 type: 'boolean' as const,
208 default: true,
209 },
210 },
211 },
212 },
213 },
214 };
215 getRawComposioToolBySlugSpy.mockResolvedValueOnce(mockTool);
216
217 const createExecuteToolFnSpy = vi.spyOn(context.tools as unknown, 'createExecuteToolFn');
218 const executeWithModifiers = async (params: unknown) => {
219 // Apply beforeExecute modifier
220 const modifiedParams = await executionModifiers.beforeExecute({
221 toolSlug: slug,
222 toolkitSlug: 'default',
223 params,
224 });
225
226 // Execute the tool
227 const response = await mockClient.tools.execute(slug, modifiedParams);
228
229 // Apply afterExecute modifier
230 return executionModifiers.afterExecute({
231 toolSlug: slug,
232 toolkitSlug: 'default',
233 result: response,
234 });
235 };
236 createExecuteToolFnSpy.mockReturnValueOnce(executeWithModifiers);
237
238 const tool = await context.tools.get(userId, slug, {
239 modifySchema: schemaModifier,
240 ...executionModifiers,
241 });
242
243 // Verify schema modification
244 expect(getRawComposioToolBySlugSpy).toHaveBeenCalledWith(slug, undefined, undefined);
245 expect(schemaModifier).toHaveBeenCalledOnce();
246
247 // Verify execution function creation
248 expect(createExecuteToolFnSpy).toHaveBeenCalledWith(
249 userId,
250 expect.objectContaining(executionModifiers),
251 expect.any(Array)
252 );
253
254 // Mock a tool execution to verify the full flow
255 await mockToolExecution(context.tools);
256 mockClient.tools.execute.mockResolvedValueOnce({
257 data: {
258 results: true,
259 },
260 error: null,
261 successful: true,
262 log_id: '123',
263 session_info: {},
264 });
265
266 // Execute the tool using the wrapped execute function
267 const result = await executeWithModifiers({ query: 'test' });
268
269 // Verify analytics tracking was called
270 expect(trackToolUsage).toHaveBeenCalledWith(slug);
271
272 // Verify the execution flow
273 expect(executionModifiers.beforeExecute).toHaveBeenCalledWith({
274 toolSlug: slug,
275 toolkitSlug: 'default',
276 params: { query: 'test' },
277 });
278
279 expect(executionModifiers.afterExecute).toHaveBeenCalledWith({
280 toolSlug: slug,
281 toolkitSlug: 'default',
282 result: {
283 data: {
284 results: true,
285 },
286 error: null,
287 successful: true,
288 log_id: '123',
289 session_info: {},
290 },
291 });
292
293 // Verify no error logging occurred
294 expect(logToolError).not.toHaveBeenCalled();
295 });
296 });
297 });