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 132 lines 4.1 KB
Raw
1 /**
2 * Regression test: `VercelProvider.wrapTool` must dereference internal
3 * `$ref`/`$defs` before handing the schema to `jsonSchemaToZodSchema`, on
4 * both the default and strict paths. Without dereferencing, a `$ref`-typed
5 * property degrades to `z.any()` and silently accepts any value.
6 */
7
8 import { describe, it, expect, vi, beforeEach } from 'vitest';
9 import type { Tool } from '@composio/core';
10 import { VercelProvider } from '../src';
11
12 interface MockedVercelTool {
13 description: string;
14 inputSchema: unknown;
15 execute: Function;
16 _isMockedVercelTool: boolean;
17 }
18
19 /** The AI SDK schema the provider builds; `validate` runs the tool's Zod schema. */
20 type ValidatingSchema = { validate: (value: unknown) => Promise<{ success: boolean }> };
21
22 // Mock the ai module (per-file scoping keeps this independent of vercel.test.ts).
23 vi.mock('ai', async importOriginal => {
24 const actual = await importOriginal<typeof import('ai')>();
25 return {
26 tool: vi.fn().mockImplementation(toolConfig => {
27 return {
28 ...toolConfig,
29 _isMockedVercelTool: true,
30 } as MockedVercelTool;
31 }),
32 jsonSchema: actual.jsonSchema,
33 };
34 });
35
36 const refTool: Tool = {
37 slug: 'TEST_REF_TOOL',
38 name: 'Ref Tool',
39 description: 'Tool whose schema carries internal $ref pointers',
40 toolkit: { slug: 'reftoolkit', name: 'Ref Toolkit' },
41 version: '20260828_00',
42 availableVersions: ['20260828_00'],
43 tags: [],
44 inputParameters: {
45 type: 'object',
46 properties: {
47 message: { $ref: '#/$defs/Message' },
48 },
49 required: ['message'],
50 $defs: {
51 Message: {
52 type: 'object',
53 properties: {
54 subject: { type: 'string' },
55 body: { type: 'string' },
56 },
57 required: ['subject', 'body'],
58 additionalProperties: false,
59 },
60 },
61 } as unknown as Tool['inputParameters'],
62 } as unknown as Tool;
63
64 const danglingRefTool: Tool = {
65 ...refTool,
66 slug: 'TEST_DANGLING_REF_TOOL',
67 inputParameters: {
68 type: 'object',
69 properties: {
70 message: { $ref: '#/$defs/Message' },
71 },
72 required: ['message'],
73 } as unknown as Tool['inputParameters'],
74 };
75
76 const recursiveRefTool: Tool = {
77 ...refTool,
78 slug: 'TEST_RECURSIVE_REF_TOOL',
79 inputParameters: {
80 type: 'object',
81 properties: {
82 node: { $ref: '#/$defs/Node' },
83 },
84 required: ['node'],
85 $defs: {
86 Node: {
87 type: 'object',
88 properties: {
89 value: { type: 'string' },
90 child: { $ref: '#/$defs/Node' },
91 },
92 required: ['value'],
93 },
94 },
95 } as unknown as Tool['inputParameters'],
96 };
97
98 describe('VercelProvider regression: $ref in JSON Schema', () => {
99 let provider: VercelProvider;
100 let executeToolFn: (toolSlug: string, params: Record<string, unknown>) => Promise<unknown>;
101
102 beforeEach(() => {
103 provider = new VercelProvider();
104 executeToolFn = vi.fn().mockResolvedValue({ data: {}, error: null, successful: true });
105 vi.clearAllMocks();
106 });
107
108 it('validates the $defs-described shape (default path)', async () => {
109 const wrapped = provider.wrapTool(refTool, executeToolFn) as unknown as MockedVercelTool;
110 const schema = wrapped.inputSchema as ValidatingSchema;
111
112 expect((await schema.validate({ message: { subject: 's', body: 'b' } })).success).toBe(true);
113 expect((await schema.validate({ message: 42 })).success).toBe(false);
114 });
115
116 it('validates the $defs-described shape (strict path)', async () => {
117 const strictProvider = new VercelProvider({ strict: true });
118 const wrapped = strictProvider.wrapTool(refTool, executeToolFn) as unknown as MockedVercelTool;
119 const schema = wrapped.inputSchema as ValidatingSchema;
120
121 expect((await schema.validate({ message: { subject: 's', body: 'b' } })).success).toBe(true);
122 expect((await schema.validate({ message: 42 })).success).toBe(false);
123 });
124
125 it('does not throw when $ref points into an undeclared $defs block', () => {
126 expect(() => provider.wrapTool(danglingRefTool, executeToolFn)).not.toThrow();
127 });
128
129 it('does not throw on a self-referential $defs schema', () => {
130 expect(() => provider.wrapTool(recursiveRefTool, executeToolFn)).not.toThrow();
131 });
132 });