1
/**
2
- * Integration test for the Mastra provider against the **real**
3
- * `@mastra/schema-compat` package (no `vi.mock` for it). This is the regression
4
- * test for [PLEN-2244]: a Composio tool whose `inputParameters` carry a `$ref`
5
- * (legal under both Draft 7 and 2020-12).
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
*
7
- * Without dereferencing, `applyCompatLayer` silently *degrades* the `$ref`-typed
8
- * property to a permissive `anyOf` of all primitives — losing the type
9
- * information from `$defs`. This test asserts the post-wrap `inputSchema`
10
- * preserves the structure described by `$defs`, which is only possible if
11
- * `$ref` is resolved before the conversion.
12
- *
13
- * Vitest mock scoping is per-file by default, so the top-level `vi.mock` for
14
- * `@mastra/schema-compat` in `mastra.test.ts` does **not** leak into this file.
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
17
-import { describe, it, expect, vi } from 'vitest';
12
+import { describe, it, expect, vi, beforeEach } from 'vitest';
13
import type { Tool } from '@composio/core';
14
import { MastraProvider } from '../src';
15
22
23
const findProperty = (schema: unknown, key: string): Record<string, unknown> | undefined => {
24
if (schema === null || typeof schema !== 'object') return undefined;
30
- const node = schema as Record<string, unknown>;
31
- if (node.properties && typeof node.properties === 'object') {
32
- const props = node.properties as Record<string, unknown>;
33
- if (key in props) return props[key] as Record<string, unknown>;
34
- for (const value of Object.values(props)) {
35
- const found = findProperty(value, key);
36
- if (found) return found;
37
- }
38
- }
39
- for (const value of Object.values(node)) {
25
+ for (const value of Object.values(schema as Record<string, unknown>)) {
26
if (Array.isArray(value)) {
27
for (const item of value) {
28
const found = findProperty(item, key);
29
if (found) return found;
30
}
31
} else if (typeof value === 'object' && value !== null) {
32
+ const props = value as Record<string, unknown> as Record<string, unknown>;
33
+ if (key in props && typeof props[key] === 'object') {
34
+ return props[key] as Record<string, unknown>;
35
+ }
36
const found = findProperty(value, key);
37
if (found) return found;
38
}
53
properties: { user: { $ref: '#/$defs/User' } as never },
54
required: ['user'],
55
$defs: {
66
- User: {
67
- type: 'object',
68
- properties: { id: { type: 'string' } },
69
- required: ['id'],
70
- },
56
+ User: { type: 'object', properties: { id: { type: 'string' } }, required: ['id'] },
57
},
58
} as unknown as Tool['inputParameters'],
59
outputParameters: {
60
type: 'object',
61
properties: { item: { $ref: '#/definitions/Item' } as never },
62
definitions: {
77
- Item: {
78
- type: 'object',
79
- properties: { sku: { type: 'string' } },
80
- required: ['sku'],
81
- },
63
+ Item: { type: 'object', properties: { sku: { type: 'string' } }, required: ['sku'] },
64
},
65
} as unknown as Tool['outputParameters'],
66
};
67
86
-describe('MastraProvider regression: PLEN-2244 ($ref in JSON Schema)', () => {
87
- it('does not throw when wrapping a tool whose schema uses $ref / $defs', () => {
88
- const provider = new MastraProvider();
89
- const executeToolFn = vi.fn().mockResolvedValue({ data: {}, error: null, successful: true });
90
- provider._setExecuteToolFn(executeToolFn);
91
-
92
- expect(() => provider.wrapTool(refTool, executeToolFn)).not.toThrow();
93
- });
68
+describe('MastraProvider regression: $ref in JSON Schema', () => {
69
+ let wrapped: { inputSchema: unknown; outputSchema: unknown };
70
95
- it('preserves type information from $defs (does not degrade $ref to permissive anyOf)', () => {
71
+ beforeEach(() => {
72
const provider = new MastraProvider();
97
- const executeToolFn = vi.fn().mockResolvedValue({ data: {}, error: null, successful: true });
98
- provider._setExecuteToolFn(executeToolFn);
99
-
100
- const wrapped = provider.wrapTool(refTool, executeToolFn) as { inputSchema: unknown };
73
+ const exec = vi.fn().mockResolvedValue({ data: {}, error: null, successful: true });
74
+ provider._setExecuteToolFn(exec);
75
+ wrapped = provider.wrapTool(refTool, exec) as typeof wrapped;
76
+ });
77
102
- // Without the dereference step, `$ref` is silently dropped during
103
- // JSON-Schema → Zod → JSON-Schema round-trip and the `id` field disappears
104
- // entirely (the property gets degraded to a permissive `anyOf`).
78
+ it('preserves type info from $defs (no degraded permissive anyOf)', () => {
79
const idProp = findProperty(wrapped.inputSchema, 'id');
80
expect(idProp).toBeDefined();
81
expect(idProp?.type).toBe('string');
82
});
83
110
- it('preserves type information from `definitions` on outputParameters (Draft 7)', () => {
111
- const provider = new MastraProvider();
112
- const executeToolFn = vi.fn().mockResolvedValue({ data: {}, error: null, successful: true });
113
- provider._setExecuteToolFn(executeToolFn);
114
-
115
- const wrapped = provider.wrapTool(refTool, executeToolFn) as { outputSchema: unknown };
116
-
84
+ it('preserves type info from Draft-7 `definitions` on the output schema', () => {
85
const skuProp = findProperty(wrapped.outputSchema, 'sku');
86
expect(skuProp).toBeDefined();
87
expect(skuProp?.type).toBe('string');
88
});
89
122
- it('does not leave any $ref in the produced schemas', () => {
123
- const provider = new MastraProvider();
124
- const executeToolFn = vi.fn().mockResolvedValue({ data: {}, error: null, successful: true });
125
- provider._setExecuteToolFn(executeToolFn);
126
-
127
- const wrapped = provider.wrapTool(refTool, executeToolFn) as {
128
- inputSchema: unknown;
129
- outputSchema: unknown;
130
- };
131
-
90
+ it('leaves no $ref in the produced schemas', () => {
91
expect(containsRef(wrapped.inputSchema)).toBe(false);
92
expect(containsRef(wrapped.outputSchema)).toBe(false);
93
});