1
+/**
2
+ * Regression test: `OpenAIAgentsProvider.wrapTool`'s non-strict fallback
3
+ * rebuilds the root schema from `properties`/`required`, which used to
4
+ * discard the `$defs` block while `$ref` pointers inside `properties`
5
+ * stayed dangling. `wrapTool` now dereferences `$ref`/`$defs` before that
6
+ * rebuild.
7
+ *
8
+ * The strict branch is intentionally different: OpenAI's structured outputs
9
+ * support `$defs`/`$ref` natively, including recursion, so `toStrictJsonSchema`
10
+ * keeps local refs rather than inlining them. This file also guards that the
11
+ * strict branch stays untouched by the fix applied to the fallback.
12
+ */
13
+
14
+import { describe, it, expect, vi, beforeEach } from 'vitest';
15
+import type { Tool, ExecuteToolFn } from '@composio/core';
16
+import { OpenAIAgentsProvider } from '../src';
17
+
18
+interface MockedOpenAIAgentTool {
19
+ name: string;
20
+ description: string;
21
+ parameters: Record<string, unknown>;
22
+ strict: boolean;
23
+ execute: Function;
24
+ _isMockedOpenAIAgentTool: boolean;
25
+}
26
+
27
+// Mock the @openai/agents module (per-file scoping keeps this independent of
28
+// openai-agents.test.ts).
29
+vi.mock('@openai/agents', () => {
30
+ return {
31
+ tool: vi.fn().mockImplementation(toolConfig => {
32
+ return {
33
+ ...toolConfig,
34
+ _isMockedOpenAIAgentTool: true,
35
+ } as MockedOpenAIAgentTool;
36
+ }),
37
+ };
38
+});
39
+
40
+const containsInternalRef = (value: unknown): boolean => {
41
+ if (value === null || typeof value !== 'object') return false;
42
+ if (Array.isArray(value)) return value.some(containsInternalRef);
43
+ const node = value as Record<string, unknown>;
44
+ if (typeof node.$ref === 'string' && node.$ref.startsWith('#')) return true;
45
+ return Object.values(node).some(containsInternalRef);
46
+};
47
+
48
+const refTool: Tool = {
49
+ slug: 'TEST_REF_TOOL',
50
+ name: 'Ref Tool',
51
+ description: 'Tool whose schema carries internal $ref pointers',
52
+ toolkit: { slug: 'reftoolkit', name: 'Ref Toolkit' },
53
+ version: '20260828_00',
54
+ availableVersions: ['20260828_00'],
55
+ tags: [],
56
+ inputParameters: {
57
+ type: 'object',
58
+ properties: {
59
+ message: { $ref: '#/$defs/Message' },
60
+ },
61
+ required: ['message'],
62
+ $defs: {
63
+ Message: {
64
+ type: 'object',
65
+ properties: {
66
+ subject: { type: 'string' },
67
+ body: { type: 'string' },
68
+ },
69
+ required: ['subject', 'body'],
70
+ additionalProperties: false,
71
+ },
72
+ },
73
+ } as unknown as Tool['inputParameters'],
74
+} as unknown as Tool;
75
+
76
+const danglingRefTool: Tool = {
77
+ ...refTool,
78
+ slug: 'TEST_DANGLING_REF_TOOL',
79
+ inputParameters: {
80
+ type: 'object',
81
+ properties: {
82
+ message: { $ref: '#/$defs/Message' },
83
+ },
84
+ required: ['message'],
85
+ } as unknown as Tool['inputParameters'],
86
+};
87
+
88
+const recursiveRefTool: Tool = {
89
+ ...refTool,
90
+ slug: 'TEST_RECURSIVE_REF_TOOL',
91
+ inputParameters: {
92
+ type: 'object',
93
+ properties: {
94
+ node: { $ref: '#/$defs/Node' },
95
+ },
96
+ required: ['node'],
97
+ $defs: {
98
+ Node: {
99
+ type: 'object',
100
+ properties: {
101
+ value: { type: 'string' },
102
+ child: { $ref: '#/$defs/Node' },
103
+ },
104
+ required: ['value'],
105
+ },
106
+ },
107
+ } as unknown as Tool['inputParameters'],
108
+};
109
+
110
+describe('OpenAIAgentsProvider regression: $ref in JSON Schema', () => {
111
+ let provider: OpenAIAgentsProvider;
112
+ let executeToolFn: ExecuteToolFn;
113
+
114
+ beforeEach(() => {
115
+ provider = new OpenAIAgentsProvider();
116
+ executeToolFn = vi.fn().mockResolvedValue({
117
+ data: {},
118
+ error: null,
119
+ successful: true,
120
+ }) as unknown as ExecuteToolFn;
121
+ vi.clearAllMocks();
122
+ });
123
+
124
+ describe('non-strict fallback (bucket B fix)', () => {
125
+ it('inlines internal $ref/$defs instead of stranding a dangling pointer', () => {
126
+ const wrapped = provider.wrapTool(refTool, executeToolFn) as unknown as MockedOpenAIAgentTool;
127
+
128
+ expect(containsInternalRef(wrapped.parameters)).toBe(false);
129
+ expect(wrapped.parameters.required).toContain('message');
130
+ });
131
+
132
+ it('does not throw when $ref points into an undeclared $defs block', () => {
133
+ expect(() => provider.wrapTool(danglingRefTool, executeToolFn)).not.toThrow();
134
+ });
135
+
136
+ it('does not throw on a self-referential $defs schema', () => {
137
+ expect(() => provider.wrapTool(recursiveRefTool, executeToolFn)).not.toThrow();
138
+ });
139
+ });
140
+
141
+ describe('strict branch (must stay untouched)', () => {
142
+ it('keeps $defs and the $ref pointer intact under strict mode', () => {
143
+ const strictProvider = new OpenAIAgentsProvider({ strict: true });
144
+ const wrapped = strictProvider.wrapTool(
145
+ refTool,
146
+ executeToolFn
147
+ ) as unknown as MockedOpenAIAgentTool;
148
+
149
+ expect(wrapped.strict).toBe(true);
150
+ expect(containsInternalRef(wrapped.parameters)).toBe(true);
151
+ expect(wrapped.parameters.$defs).toBeDefined();
152
+ });
153
+ });
154
+});