@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, 06:00 UTC

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