@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 104 lines 4.23 KB
Raw
1 /**
2 * Regression test for the Mastra provider against the **real**
3 * `@mastra/schema-compat` (no `vi.mock` for it). Without dereferencing,
4 * `applyCompatLayer` silently degrades a `$ref`-typed property to a permissive
5 * `anyOf` of all primitives — losing the type info from `$defs`. We assert
6 * the post-wrap schemas preserve the structure described by `$defs`.
7 *
8 * Vitest mock scoping is per-file by default, so the top-level `vi.mock`
9 * for `@mastra/schema-compat` in `mastra.test.ts` does not leak here.
10 */
11
12 import { describe, it, expect, vi, beforeEach } from 'vitest';
13 import type { Tool } from '@composio/core';
14 import { MastraProvider } from '../src';
15
16 // `@mastra/core`'s `createTool` (>= 1.43) wraps JSON Schemas in a
17 // `JsonSchemaWrapper` (a Standard Schema), so a wrapped tool's `inputSchema` /
18 // `outputSchema` is no longer the raw JSON Schema. Reach through `getSchema()`
19 // to inspect the resolved JSON Schema the compat layer produced.
20 const unwrapSchema = (schema: unknown): unknown =>
21 (schema as { getSchema?: () => unknown }).getSchema?.() ?? schema;
22
23 const containsRef = (value: unknown): boolean => {
24 if (value === null || typeof value !== 'object') return false;
25 if (Array.isArray(value)) return value.some(containsRef);
26 if ('$ref' in (value as Record<string, unknown>)) return true;
27 return Object.values(value as Record<string, unknown>).some(containsRef);
28 };
29
30 const findProperty = (schema: unknown, key: string): Record<string, unknown> | undefined => {
31 if (schema === null || typeof schema !== 'object') return undefined;
32 for (const value of Object.values(schema as Record<string, unknown>)) {
33 if (Array.isArray(value)) {
34 for (const item of value) {
35 const found = findProperty(item, key);
36 if (found) return found;
37 }
38 } else if (typeof value === 'object' && value !== null) {
39 const props = value as Record<string, unknown> as Record<string, unknown>;
40 if (key in props && typeof props[key] === 'object') {
41 return props[key] as Record<string, unknown>;
42 }
43 const found = findProperty(value, key);
44 if (found) return found;
45 }
46 }
47 return undefined;
48 };
49
50 const refTool: Tool = {
51 slug: 'PLEN_2244_TOOL',
52 name: 'PLEN-2244 Tool',
53 description: 'Tool whose schema carries internal $ref pointers',
54 toolkit: { slug: 'plen2244', name: 'PLEN 2244' },
55 version: '20260430_00',
56 availableVersions: ['20260430_00'],
57 tags: [],
58 inputParameters: {
59 type: 'object',
60 properties: { user: { $ref: '#/$defs/User' } as never },
61 required: ['user'],
62 $defs: {
63 User: { type: 'object', properties: { id: { type: 'string' } }, required: ['id'] },
64 },
65 } as unknown as Tool['inputParameters'],
66 outputParameters: {
67 type: 'object',
68 properties: { item: { $ref: '#/definitions/Item' } as never },
69 definitions: {
70 Item: { type: 'object', properties: { sku: { type: 'string' } }, required: ['sku'] },
71 },
72 } as unknown as Tool['outputParameters'],
73 };
74
75 describe('MastraProvider regression: $ref in JSON Schema', () => {
76 let wrapped: { inputSchema: unknown; outputSchema: unknown };
77
78 beforeEach(() => {
79 const provider = new MastraProvider();
80 const exec = vi.fn().mockResolvedValue({ data: {}, error: null, successful: true });
81 provider._setExecuteToolFn(exec);
82 wrapped = provider.wrapTool(refTool, exec) as typeof wrapped;
83 });
84
85 it('preserves type info from $defs (no degraded permissive anyOf)', () => {
86 const idProp = findProperty(unwrapSchema(wrapped.inputSchema), 'id');
87 expect(idProp).toBeDefined();
88 expect(idProp?.type).toBe('string');
89 });
90
91 it('preserves type info from Draft-7 `definitions` on the output schema', () => {
92 const skuProp = findProperty(unwrapSchema(wrapped.outputSchema), 'sku');
93 expect(skuProp).toBeDefined();
94 // The output schema is relaxed to be nullable (issue #3047), so the
95 // dereferenced `string` becomes `['string', 'null']` — the type info from
96 // `definitions` is still preserved (not degraded to a permissive anyOf).
97 expect(skuProp?.type).toEqual(['string', 'null']);
98 });
99
100 it('leaves no $ref in the produced schemas', () => {
101 expect(containsRef(unwrapSchema(wrapped.inputSchema))).toBe(false);
102 expect(containsRef(unwrapSchema(wrapped.outputSchema))).toBe(false);
103 });
104 });