@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 350 lines 10.2 KB
Raw
1 import { describe, it, expect, vi, beforeEach } from 'vitest';
2 import { GoogleProvider } from '../src';
3 import { Tool } from '@composio/core';
4
5 describe('GoogleProvider', () => {
6 let provider: GoogleProvider;
7 let mockTool: Tool;
8 let mockExecuteToolFn: unknown;
9
10 beforeEach(() => {
11 provider = new GoogleProvider();
12
13 mockExecuteToolFn = vi.fn().mockResolvedValue({
14 data: { result: 'success' },
15 error: null,
16 successful: true,
17 });
18 provider._setExecuteToolFn(mockExecuteToolFn);
19
20 mockTool = {
21 slug: 'test-tool',
22 name: 'Test Tool',
23 description: 'A tool for testing',
24 inputParameters: {
25 type: 'object',
26 properties: {
27 input: {
28 type: 'string',
29 description: 'Test input',
30 },
31 },
32 required: ['input'],
33 },
34 tags: [],
35 };
36
37 vi.clearAllMocks();
38 });
39
40 describe('name property', () => {
41 it('should have the correct name', () => {
42 expect(provider.name).toBe('google');
43 });
44 });
45
46 describe('_isAgentic property', () => {
47 it('should be non-agentic', () => {
48 expect(provider._isAgentic).toBe(false);
49 });
50 });
51
52 describe('wrapTool', () => {
53 it('should wrap a tool in Google GenAI function declaration format', () => {
54 const wrapped = provider.wrapTool(mockTool);
55
56 expect(wrapped).toEqual({
57 name: mockTool.slug,
58 description: mockTool.description,
59 parametersJsonSchema: {
60 type: 'object',
61 description: mockTool.description,
62 properties: mockTool.inputParameters?.properties || {},
63 required: mockTool.inputParameters?.required || [],
64 },
65 });
66 });
67
68 it('should handle tools without input parameters', () => {
69 const toolWithoutParams: Tool = {
70 ...mockTool,
71 inputParameters: undefined,
72 };
73
74 const wrapped = provider.wrapTool(toolWithoutParams);
75
76 expect(wrapped).toEqual({
77 name: toolWithoutParams.slug,
78 description: toolWithoutParams.description,
79 parametersJsonSchema: {
80 type: 'object',
81 description: toolWithoutParams.description,
82 properties: {},
83 required: [],
84 },
85 });
86 });
87
88 it('deduplicates required entries for directly wrapped tools', () => {
89 const wrapped = provider.wrapTool({
90 ...mockTool,
91 inputParameters: {
92 ...mockTool.inputParameters!,
93 required: ['input', 'input'],
94 },
95 });
96
97 expect((wrapped.parametersJsonSchema as { required: string[] }).required).toEqual(['input']);
98 });
99
100 it('normalizes nested object schemas without treating property maps as schemas', () => {
101 const wrapped = provider.wrapTool({
102 ...mockTool,
103 inputParameters: {
104 type: 'object',
105 properties: {
106 properties: {
107 properties: { name: { type: 'string' } },
108 },
109 },
110 },
111 });
112
113 const params = wrapped.parametersJsonSchema as {
114 properties: Record<string, unknown>;
115 };
116 expect(params.properties).toEqual({
117 properties: {
118 type: 'object',
119 properties: { name: { type: 'string' } },
120 },
121 });
122 expect(params.properties).not.toHaveProperty('type');
123 });
124
125 it('sends JSON Schema keywords through parametersJsonSchema, not parameters', () => {
126 // Gemini answers 400 ("Unknown name \"examples\"") when these keywords
127 // are sent in the OpenAPI-subset `parameters` field.
128 const properties = {
129 query: { type: 'string', examples: ['is:unread'] },
130 kind: { type: 'string', const: 'message' },
131 page_token: { anyOf: [{ type: 'string' }, { type: 'null' }], default: null },
132 format: { type: 'integer', enum: [0, 1] },
133 };
134 const wrapped = provider.wrapTool({
135 ...mockTool,
136 inputParameters: { type: 'object', properties, required: ['query'] },
137 });
138
139 expect(wrapped.parametersJsonSchema).toEqual({
140 type: 'object',
141 description: mockTool.description,
142 properties,
143 required: ['query'],
144 });
145 });
146 });
147
148 describe('wrapTools', () => {
149 it('should wrap multiple tools', () => {
150 const anotherTool: Tool = {
151 ...mockTool,
152 slug: 'another-tool',
153 name: 'Another Tool',
154 };
155 const tools = [mockTool, anotherTool];
156
157 const wrapped = provider.wrapTools(tools);
158
159 expect(wrapped).toHaveLength(2);
160
161 expect(wrapped[0]).toEqual({
162 name: mockTool.slug,
163 description: mockTool.description,
164 parametersJsonSchema: {
165 type: 'object',
166 description: mockTool.description,
167 properties: mockTool.inputParameters?.properties || {},
168 required: mockTool.inputParameters?.required || [],
169 },
170 });
171
172 expect(wrapped[1]).toEqual({
173 name: anotherTool.slug,
174 description: anotherTool.description,
175 parametersJsonSchema: {
176 type: 'object',
177 description: anotherTool.description,
178 properties: anotherTool.inputParameters?.properties || {},
179 required: anotherTool.inputParameters?.required || [],
180 },
181 });
182 });
183
184 it('should return an empty array for empty tools array', () => {
185 const wrapped = provider.wrapTools([]);
186 expect(wrapped).toEqual([]);
187 });
188 });
189
190 describe('executeToolCall', () => {
191 it('should execute a tool call and return the result as string', async () => {
192 const userId = 'test-user';
193 const toolCall = {
194 name: 'test-tool',
195 args: { input: 'test-value' },
196 };
197
198 const result = await provider.executeToolCall(userId, toolCall);
199
200 expect(mockExecuteToolFn).toHaveBeenCalledWith(
201 'test-tool',
202 {
203 arguments: { input: 'test-value' },
204 userId: 'test-user',
205 connectedAccountId: undefined,
206 customAuthParams: undefined,
207 },
208 undefined
209 );
210 expect(result).toBe(
211 JSON.stringify({
212 data: { result: 'success' },
213 error: null,
214 successful: true,
215 })
216 );
217 });
218
219 it('should normalize a stringified-JSON args into an object before executing (issue #2406)', async () => {
220 const args = { input: 'test-value' };
221 const toolCall = { name: 'test-tool', args: JSON.stringify(args) as unknown as object };
222
223 await provider.executeToolCall('test-user', toolCall);
224
225 expect(mockExecuteToolFn).toHaveBeenCalledWith(
226 'test-tool',
227 expect.objectContaining({ arguments: args }),
228 undefined
229 );
230 });
231
232 it('should throw a typed error for malformed-JSON args (issue #2406)', async () => {
233 const toolCall = { name: 'test-tool', args: 'not json' as unknown as object };
234
235 await expect(provider.executeToolCall('test-user', toolCall)).rejects.toThrow(
236 /not valid JSON/
237 );
238 });
239
240 it('should pass options to executeTool', async () => {
241 const userId = 'test-user';
242 const toolCall = {
243 name: 'test-tool',
244 args: { input: 'test-value' },
245 };
246
247 const options = {
248 connectedAccountId: 'conn-123',
249 customAuthParams: {
250 parameters: [{ name: 'token', value: 'abc123', in: 'header' as const }],
251 },
252 };
253
254 const modifiers = {
255 beforeExecute: vi.fn(({ params }) => params),
256 afterExecute: vi.fn(({ result }) => result),
257 };
258
259 await provider.executeToolCall(userId, toolCall, options, modifiers);
260
261 expect(mockExecuteToolFn).toHaveBeenCalledWith(
262 'test-tool',
263 {
264 arguments: { input: 'test-value' },
265 userId: 'test-user',
266 connectedAccountId: 'conn-123',
267 customAuthParams: options.customAuthParams,
268 },
269 modifiers
270 );
271 });
272 });
273
274 describe('executeToolCall with a Tool Router session', () => {
275 it('keeps user ID calls working on @composio/core releases without session helpers', async () => {
276 // The peer range admits cores that predate executeToolForTarget.
277 Object.defineProperty(provider, 'executeToolForTarget', { value: undefined });
278
279 await provider.executeToolCall('test-user', { name: 'test-tool', args: {} });
280
281 expect(mockExecuteToolFn).toHaveBeenCalledWith(
282 'test-tool',
283 expect.objectContaining({ userId: 'test-user' }),
284 undefined
285 );
286 });
287
288 it('executes through the session instead of the direct tools API', async () => {
289 const session = {
290 execute: vi.fn().mockResolvedValue({
291 data: { result: 'session-success' },
292 error: null,
293 logId: 'log-session',
294 resultType: 'completed',
295 }),
296 };
297
298 const result = await provider.executeToolCall(session, {
299 name: 'COMPOSIO_SEARCH_TOOLS',
300 args: { query: 'send an email' },
301 });
302
303 expect(session.execute).toHaveBeenCalledWith('COMPOSIO_SEARCH_TOOLS', {
304 query: 'send an email',
305 });
306 expect(mockExecuteToolFn).not.toHaveBeenCalled();
307 expect(JSON.parse(result)).toEqual({
308 data: { result: 'session-success' },
309 error: null,
310 logId: 'log-session',
311 resultType: 'completed',
312 successful: true,
313 });
314 });
315
316 it('reports a failed session execution in the result', async () => {
317 const session = {
318 execute: vi.fn().mockResolvedValue({
319 data: {},
320 error: 'Tool failed',
321 logId: 'log-fail',
322 resultType: 'failed',
323 }),
324 };
325
326 const result = await provider.executeToolCall(session, {
327 name: 'COMPOSIO_SEARCH_TOOLS',
328 args: {},
329 });
330
331 expect(JSON.parse(result)).toMatchObject({ error: 'Tool failed', successful: false });
332 });
333
334 it('rejects direct execution options with a session', async () => {
335 const session = { execute: vi.fn() };
336
337 await expect(
338 provider.executeToolCall(
339 // @ts-expect-error direct execution options are not accepted with a session
340 session,
341 { name: 'COMPOSIO_SEARCH_TOOLS', args: {} },
342 { connectedAccountId: 'conn-123' }
343 )
344 ).rejects.toThrow(
345 'Direct execution options and modifiers cannot be used with a Tool Router session'
346 );
347 expect(session.execute).not.toHaveBeenCalled();
348 });
349 });
350 });