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 562 lines 17.8 KB
Raw
1 import { describe, it, expect } from 'vitest';
2 import {
3 transformToolRouterTagsParams,
4 transformToolRouterMultiAccountParams,
5 transformToolRouterToolsParams,
6 transformToolRouterUpdateParams,
7 } from '../../src/lib/toolRouterParams';
8 import {
9 ToolRouterConfigTags,
10 ToolRouterConfigToolsSchema,
11 } from '../../src/types/toolRouter.types';
12
13 describe('transformToolRouterMultiAccountParams', () => {
14 it('should return undefined when params is undefined', () => {
15 expect(transformToolRouterMultiAccountParams(undefined)).toBeUndefined();
16 });
17
18 it('should return undefined when all fields are undefined (empty object)', () => {
19 expect(transformToolRouterMultiAccountParams({})).toBeUndefined();
20 });
21
22 it('should transform enable only', () => {
23 const result = transformToolRouterMultiAccountParams({ enable: true });
24 expect(result).toEqual({
25 enable: true,
26 max_accounts_per_toolkit: undefined,
27 require_explicit_selection: true,
28 });
29 });
30
31 it('should transform all fields', () => {
32 const result = transformToolRouterMultiAccountParams({
33 enable: true,
34 maxAccountsPerToolkit: 3,
35 requireExplicitSelection: true,
36 });
37 expect(result).toEqual({
38 enable: true,
39 max_accounts_per_toolkit: 3,
40 require_explicit_selection: true,
41 });
42 });
43
44 it('should transform with enable false', () => {
45 const result = transformToolRouterMultiAccountParams({ enable: false });
46 expect(result).toEqual({
47 enable: false,
48 max_accounts_per_toolkit: undefined,
49 require_explicit_selection: undefined,
50 });
51 });
52
53 it('should transform maxAccountsPerToolkit only', () => {
54 const result = transformToolRouterMultiAccountParams({ maxAccountsPerToolkit: 7 });
55 expect(result).toEqual({
56 enable: undefined,
57 max_accounts_per_toolkit: 7,
58 require_explicit_selection: undefined,
59 });
60 });
61 });
62
63 describe('transformToolRouterTagsParams', () => {
64 describe('with undefined input', () => {
65 it('should return undefined when params is undefined', () => {
66 const result = transformToolRouterTagsParams(undefined);
67 expect(result).toBeUndefined();
68 });
69 });
70
71 describe('with array input', () => {
72 it('should transform array of tags to enable format', () => {
73 const params: ToolRouterConfigTags = ['readOnlyHint', 'idempotentHint'];
74 const result = transformToolRouterTagsParams(params);
75
76 expect(result).toEqual({
77 enable: ['readOnlyHint', 'idempotentHint'],
78 });
79 });
80
81 it('should handle single tag in array', () => {
82 const params: ToolRouterConfigTags = ['readOnlyHint'];
83 const result = transformToolRouterTagsParams(params);
84
85 expect(result).toEqual({
86 enable: ['readOnlyHint'],
87 });
88 });
89
90 it('should handle all tag types', () => {
91 const params: ToolRouterConfigTags = [
92 'readOnlyHint',
93 'destructiveHint',
94 'idempotentHint',
95 'openWorldHint',
96 ];
97 const result = transformToolRouterTagsParams(params);
98
99 expect(result).toEqual({
100 enable: ['readOnlyHint', 'destructiveHint', 'idempotentHint', 'openWorldHint'],
101 });
102 });
103
104 it('should handle empty array', () => {
105 const params: ToolRouterConfigTags = [];
106 const result = transformToolRouterTagsParams(params);
107
108 expect(result).toEqual({
109 enable: [],
110 });
111 });
112 });
113
114 describe('with object input', () => {
115 it('should transform object with enable property', () => {
116 const params: ToolRouterConfigTags = {
117 enable: ['readOnlyHint', 'idempotentHint'],
118 };
119 const result = transformToolRouterTagsParams(params);
120
121 expect(result).toEqual({
122 enable: ['readOnlyHint', 'idempotentHint'],
123 });
124 });
125
126 it('should transform object with disable property', () => {
127 const params: ToolRouterConfigTags = {
128 disable: ['destructiveHint'],
129 };
130 const result = transformToolRouterTagsParams(params);
131
132 expect(result).toEqual({
133 enable: undefined,
134 disable: ['destructiveHint'],
135 });
136 });
137
138 it('should transform object with both enable and disable properties', () => {
139 const params: ToolRouterConfigTags = {
140 enable: ['readOnlyHint', 'idempotentHint'],
141 disable: ['destructiveHint'],
142 };
143 const result = transformToolRouterTagsParams(params);
144
145 expect(result).toEqual({
146 enable: ['readOnlyHint', 'idempotentHint'],
147 disable: ['destructiveHint'],
148 });
149 });
150
151 it('should handle object with only enable property', () => {
152 const params: ToolRouterConfigTags = {
153 enable: ['readOnlyHint'],
154 };
155 const result = transformToolRouterTagsParams(params);
156
157 expect(result).toEqual({
158 enable: ['readOnlyHint'],
159 });
160 });
161
162 it('should handle object with only disable property', () => {
163 const params: ToolRouterConfigTags = {
164 disable: ['destructiveHint', 'openWorldHint'],
165 };
166 const result = transformToolRouterTagsParams(params);
167
168 expect(result).toEqual({
169 enable: undefined,
170 disable: ['destructiveHint', 'openWorldHint'],
171 });
172 });
173
174 it('should handle empty arrays in object', () => {
175 const params: ToolRouterConfigTags = {
176 enable: [],
177 disable: [],
178 };
179 const result = transformToolRouterTagsParams(params);
180
181 expect(result).toEqual({
182 enable: [],
183 disable: [],
184 });
185 });
186 });
187 });
188
189 describe('transformToolRouterUpdateParams', () => {
190 it('maps the experimental Instant usage policy without changing other session settings', () => {
191 expect(
192 transformToolRouterUpdateParams({
193 instant: {
194 toolkits: { disable: ['exa'] },
195 returnInstantCharge: true,
196 returnPricesInSearch: true,
197 },
198 })
199 ).toEqual({
200 instant: {
201 toolkits: { disable: ['exa'] },
202 return_instant_charge: true,
203 return_prices_in_search: true,
204 },
205 });
206 expect(transformToolRouterUpdateParams({ instant: false })).toEqual({
207 instant: false,
208 });
209 });
210
211 it('maps proxyExecute as given, expands a boolean, and null clears it', () => {
212 expect(transformToolRouterUpdateParams({ proxyExecute: { enable: true } })).toEqual({
213 proxy_execute: { enable: true },
214 });
215 expect(transformToolRouterUpdateParams({ proxyExecute: { enable: false } })).toEqual({
216 proxy_execute: { enable: false },
217 });
218 expect(transformToolRouterUpdateParams({ proxyExecute: true })).toEqual({
219 proxy_execute: { enable: true },
220 });
221 expect(transformToolRouterUpdateParams({ proxyExecute: false })).toEqual({
222 proxy_execute: { enable: false },
223 });
224 expect(transformToolRouterUpdateParams({ proxyExecute: null })).toEqual({
225 proxy_execute: null,
226 });
227 });
228
229 it('maps hooks to snake_case, and null removes every hook', () => {
230 expect(
231 transformToolRouterUpdateParams({
232 hooks: [
233 {
234 type: 'before_execute',
235 id: 'no_external_email',
236 name: 'No external email',
237 appliesTo: { toolkits: ['gmail'] },
238 handler: {
239 type: 'condition_check',
240 condition: 'Sending email to anyone outside example.org',
241 action: 'block',
242 },
243 },
244 {
245 type: 'after_execute',
246 id: 'block_injection',
247 appliesTo: 'all',
248 handler: { type: 'prompt_injection_check', action: 'block' },
249 },
250 ],
251 })
252 ).toEqual({
253 hooks: [
254 {
255 type: 'before_execute',
256 id: 'no_external_email',
257 name: 'No external email',
258 applies_to: { toolkits: ['gmail'] },
259 handler: {
260 type: 'condition_check',
261 condition: 'Sending email to anyone outside example.org',
262 action: 'block',
263 },
264 },
265 {
266 type: 'after_execute',
267 id: 'block_injection',
268 applies_to: 'all',
269 handler: { type: 'prompt_injection_check', action: 'block' },
270 },
271 ],
272 });
273 expect(transformToolRouterUpdateParams({ hooks: null })).toEqual({ hooks: null });
274 });
275
276 it('keeps an empty toolkit allowlist in the request', () => {
277 expect(transformToolRouterUpdateParams({ toolkits: [] })).toEqual({
278 toolkits: { enable: [] },
279 });
280 expect(transformToolRouterUpdateParams({ toolkits: { enable: [] } })).toEqual({
281 toolkits: { enable: [] },
282 });
283 });
284
285 it('sends null for every clearable block', () => {
286 expect(
287 transformToolRouterUpdateParams({
288 toolkits: null,
289 tools: null,
290 tags: null,
291 authConfigs: null,
292 connectedAccounts: null,
293 preload: null,
294 search: null,
295 execute: null,
296 experimental: null,
297 })
298 ).toEqual({
299 toolkits: null,
300 tools: null,
301 tags: null,
302 auth_configs: null,
303 connected_accounts: null,
304 preload: null,
305 search: null,
306 execute: null,
307 experimental: null,
308 });
309 });
310
311 it('passes a null callback URL through manage_connections', () => {
312 expect(transformToolRouterUpdateParams({ manageConnections: { callbackUrl: null } })).toEqual({
313 manage_connections: { callback_url: null },
314 });
315 });
316
317 it('maps enableConnectionRemoval through manage_connections, including null', () => {
318 expect(
319 transformToolRouterUpdateParams({ manageConnections: { enableConnectionRemoval: false } })
320 ).toEqual({ manage_connections: { enable_connection_removal: false } });
321 expect(
322 transformToolRouterUpdateParams({ manageConnections: { enableConnectionRemoval: null } })
323 ).toEqual({ manage_connections: { enable_connection_removal: null } });
324 });
325
326 it('passes a null maximum through multi_account', () => {
327 expect(
328 transformToolRouterUpdateParams({ multiAccount: { maxAccountsPerToolkit: null } })
329 ).toEqual({ multi_account: { max_accounts_per_toolkit: null } });
330 });
331
332 it('maps search, execute and experimental leaves to their wire names', () => {
333 expect(
334 transformToolRouterUpdateParams({
335 search: { enable: false },
336 execute: { enableMultiExecute: false },
337 experimental: {
338 linkUrlOverwrite: null,
339 fastMode: true,
340 submitFeedback: { enable: true },
341 permissions: {
342 default: 'allow_all',
343 overrides: { 'GMAIL_SEND:__none__': 'always_deny' },
344 },
345 sessionConfigId: 'cfg_1',
346 },
347 })
348 ).toEqual({
349 search: { enable: false },
350 execute: { enable_multi_execute: false },
351 experimental: {
352 link_url_overwrite: null,
353 fast_mode: true,
354 submit_feedback: { enable: true },
355 permissions: { default: 'allow_all', overrides: { 'GMAIL_SEND:__none__': 'always_deny' } },
356 session_config_id: 'cfg_1',
357 },
358 });
359 });
360
361 it('never serializes the expectedConfigVersion option itself', () => {
362 expect(
363 transformToolRouterUpdateParams({ toolkits: ['github'], expectedConfigVersion: 4 })
364 ).toEqual({
365 toolkits: { enable: ['github'] },
366 });
367 });
368
369 it('should pass through toolkits correctly', () => {
370 const result = transformToolRouterUpdateParams({
371 toolkits: ['github', 'gmail'],
372 });
373 expect(result).toEqual({
374 toolkits: { enable: ['github', 'gmail'] },
375 });
376 });
377
378 it('should pass through tools correctly', () => {
379 const result = transformToolRouterUpdateParams({
380 tools: {
381 github: ['GITHUB_CREATE_REPO'],
382 },
383 });
384 expect(result).toEqual({
385 tools: {
386 github: { enable: ['GITHUB_CREATE_REPO'] },
387 },
388 });
389 });
390
391 it('should pass through tags correctly', () => {
392 const result = transformToolRouterUpdateParams({
393 tags: ['readOnlyHint'],
394 });
395 expect(result).toEqual({
396 tags: { enable: ['readOnlyHint'] },
397 });
398 });
399
400 it('should handle null for manageConnections (clearing)', () => {
401 const result = transformToolRouterUpdateParams({
402 manageConnections: null,
403 });
404 expect(result).toEqual({
405 manage_connections: null,
406 });
407 });
408
409 it('should handle null for sandbox (clearing)', () => {
410 const result = transformToolRouterUpdateParams({
411 sandbox: null,
412 });
413 expect(result).toEqual({
414 workbench: null,
415 });
416 });
417
418 it('should handle null for workbench (clearing)', () => {
419 const result = transformToolRouterUpdateParams({
420 workbench: null,
421 });
422 expect(result).toEqual({
423 workbench: null,
424 });
425 });
426
427 it('should handle null for multiAccount (clearing)', () => {
428 const result = transformToolRouterUpdateParams({
429 multiAccount: null,
430 });
431 expect(result).toEqual({
432 multi_account: null,
433 });
434 });
435
436 it('should only include fields that were explicitly set', () => {
437 const result = transformToolRouterUpdateParams({
438 toolkits: ['github'],
439 });
440 // Should only have toolkits, not tools, tags, manage_connections, etc.
441 expect(result).toEqual({
442 toolkits: { enable: ['github'] },
443 });
444 expect(result).not.toHaveProperty('tools');
445 expect(result).not.toHaveProperty('tags');
446 expect(result).not.toHaveProperty('manage_connections');
447 expect(result).not.toHaveProperty('workbench');
448 expect(result).not.toHaveProperty('multi_account');
449 expect(result).not.toHaveProperty('preload');
450 expect(result).not.toHaveProperty('auth_configs');
451 expect(result).not.toHaveProperty('connected_accounts');
452 });
453
454 it('should return empty object when no fields are set', () => {
455 const result = transformToolRouterUpdateParams({});
456 expect(result).toEqual({});
457 });
458
459 it('should NOT apply default enable:true for manageConnections when only callbackUrl is provided', () => {
460 const result = transformToolRouterUpdateParams({
461 manageConnections: { callbackUrl: 'https://example.com/callback' },
462 });
463 // Should not have enable: true injected
464 expect(result.manage_connections).toBeDefined();
465 expect(result.manage_connections).not.toBeNull();
466 const mc = result.manage_connections as Record<string, unknown>;
467 expect(mc).not.toHaveProperty('enable');
468 expect(mc).toHaveProperty('callback_url', 'https://example.com/callback');
469 });
470
471 it('should NOT apply default enable:true for sandbox when only enableProxyExecution is provided', () => {
472 const result = transformToolRouterUpdateParams({
473 sandbox: { enableProxyExecution: true },
474 });
475 const wb = result.workbench as Record<string, unknown>;
476 expect(wb).not.toHaveProperty('enable');
477 expect(wb).toHaveProperty('enable_proxy_execution', true);
478 });
479
480 it('should NOT apply default enable:true for workbench when only enableProxyExecution is provided', () => {
481 const result = transformToolRouterUpdateParams({
482 workbench: { enableProxyExecution: true },
483 });
484 const wb = result.workbench as Record<string, unknown>;
485 expect(wb).not.toHaveProperty('enable');
486 expect(wb).toHaveProperty('enable_proxy_execution', true);
487 });
488
489 it('should include enable for manageConnections when explicitly set', () => {
490 const result = transformToolRouterUpdateParams({
491 manageConnections: { enable: false, callbackUrl: 'https://example.com' },
492 });
493 const mc = result.manage_connections as Record<string, unknown>;
494 expect(mc).toHaveProperty('enable', false);
495 expect(mc).toHaveProperty('callback_url', 'https://example.com');
496 });
497 });
498
499 describe('approval lists', () => {
500 it('maps requireApproval next to each toolkit, tool and tag filter', () => {
501 expect(
502 transformToolRouterUpdateParams({
503 toolkits: { disable: ['slack'], requireApproval: ['github'] },
504 tools: {
505 gmail: { enable: ['GMAIL_SEND_EMAIL'], requireApproval: ['GMAIL_SEND_EMAIL'] },
506 github: { disable: ['GITHUB_DELETE_A_REPOSITORY'], requireApproval: ['GITHUB_MERGE'] },
507 notion: { tags: ['readOnlyHint'], requireApproval: ['NOTION_CREATE_PAGE'] },
508 },
509 tags: { disable: ['openWorldHint'], requireApproval: ['destructiveHint'] },
510 })
511 ).toEqual({
512 toolkits: { disable: ['slack'], require_approval: ['github'] },
513 tools: {
514 gmail: { enable: ['GMAIL_SEND_EMAIL'], require_approval: ['GMAIL_SEND_EMAIL'] },
515 github: { disable: ['GITHUB_DELETE_A_REPOSITORY'], require_approval: ['GITHUB_MERGE'] },
516 notion: { tags: { enable: ['readOnlyHint'] }, require_approval: ['NOTION_CREATE_PAGE'] },
517 },
518 tags: { disable: ['openWorldHint'], require_approval: ['destructiveHint'] },
519 });
520 });
521
522 it('maps a toolkit or tool entry that only has an approval list', () => {
523 expect(
524 transformToolRouterUpdateParams({
525 toolkits: { requireApproval: ['gmail'] },
526 tools: { gmail: { requireApproval: ['GMAIL_SEND_EMAIL'] } },
527 })
528 ).toEqual({
529 toolkits: { require_approval: ['gmail'] },
530 tools: { gmail: { require_approval: ['GMAIL_SEND_EMAIL'] } },
531 });
532 });
533
534 it('leaves require_approval out when no list is given', () => {
535 expect(transformToolRouterToolsParams({ gmail: { enable: ['GMAIL_SEND_EMAIL'] } })).toEqual({
536 gmail: { enable: ['GMAIL_SEND_EMAIL'] },
537 });
538 expect(transformToolRouterTagsParams({ enable: ['readOnlyHint'] })).toEqual({
539 enable: ['readOnlyHint'],
540 disable: undefined,
541 });
542 });
543
544 it('allows requireApproval next to one of enable, disable and tags, but not two of them', () => {
545 expect(
546 ToolRouterConfigToolsSchema.safeParse({ enable: ['A'], requireApproval: ['A'] }).success
547 ).toBe(true);
548 expect(
549 ToolRouterConfigToolsSchema.safeParse({
550 enable: ['A'],
551 disable: ['B'],
552 requireApproval: ['A'],
553 }).success
554 ).toBe(false);
555 });
556
557 it('maps noElicitationSupportFallback to its wire name', () => {
558 expect(
559 transformToolRouterUpdateParams({ experimental: { noElicitationSupportFallback: 'deny' } })
560 ).toEqual({ experimental: { no_elicitation_support_fallback: 'deny' } });
561 });
562 });