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 103 lines 2.97 KB
Raw
1 /**
2 * Regression test: `GoogleProvider.wrapTool` rebuilds the root schema from
3 * `properties`/`required`, which used to discard the `$defs` block while
4 * `$ref` pointers inside `properties` stayed dangling. `wrapTool` now
5 * dereferences `$ref`/`$defs` before the rebuild.
6 */
7
8 import { describe, it, expect, beforeEach } from 'vitest';
9 import type { Tool } from '@composio/core';
10 import { GoogleProvider } from '../src';
11
12 const containsInternalRef = (value: unknown): boolean => {
13 if (value === null || typeof value !== 'object') return false;
14 if (Array.isArray(value)) return value.some(containsInternalRef);
15 const node = value as Record<string, unknown>;
16 if (typeof node.$ref === 'string' && node.$ref.startsWith('#')) return true;
17 return Object.values(node).some(containsInternalRef);
18 };
19
20 const refTool: Tool = {
21 slug: 'TEST_REF_TOOL',
22 name: 'Ref Tool',
23 description: 'Tool whose schema carries internal $ref pointers',
24 toolkit: { slug: 'reftoolkit', name: 'Ref Toolkit' },
25 version: '20260828_00',
26 availableVersions: ['20260828_00'],
27 tags: [],
28 inputParameters: {
29 type: 'object',
30 properties: {
31 message: { $ref: '#/$defs/Message' },
32 },
33 required: ['message'],
34 $defs: {
35 Message: {
36 type: 'object',
37 properties: {
38 subject: { type: 'string' },
39 body: { type: 'string' },
40 },
41 required: ['subject', 'body'],
42 additionalProperties: false,
43 },
44 },
45 } as unknown as Tool['inputParameters'],
46 } as unknown as Tool;
47
48 const danglingRefTool: Tool = {
49 ...refTool,
50 slug: 'TEST_DANGLING_REF_TOOL',
51 inputParameters: {
52 type: 'object',
53 properties: {
54 message: { $ref: '#/$defs/Message' },
55 },
56 required: ['message'],
57 } as unknown as Tool['inputParameters'],
58 };
59
60 const recursiveRefTool: Tool = {
61 ...refTool,
62 slug: 'TEST_RECURSIVE_REF_TOOL',
63 inputParameters: {
64 type: 'object',
65 properties: {
66 node: { $ref: '#/$defs/Node' },
67 },
68 required: ['node'],
69 $defs: {
70 Node: {
71 type: 'object',
72 properties: {
73 value: { type: 'string' },
74 child: { $ref: '#/$defs/Node' },
75 },
76 required: ['value'],
77 },
78 },
79 } as unknown as Tool['inputParameters'],
80 };
81
82 describe('GoogleProvider regression: $ref in JSON Schema', () => {
83 let provider: GoogleProvider;
84
85 beforeEach(() => {
86 provider = new GoogleProvider();
87 });
88
89 it('inlines internal $ref/$defs instead of stranding a dangling pointer', () => {
90 const wrapped = provider.wrapTool(refTool);
91
92 expect(containsInternalRef(wrapped.parametersJsonSchema)).toBe(false);
93 expect((wrapped.parametersJsonSchema as { required: string[] }).required).toContain('message');
94 });
95
96 it('does not throw when $ref points into an undeclared $defs block', () => {
97 expect(() => provider.wrapTool(danglingRefTool)).not.toThrow();
98 });
99
100 it('does not throw on a self-referential $defs schema', () => {
101 expect(() => provider.wrapTool(recursiveRefTool)).not.toThrow();
102 });
103 });