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 243 lines 7.51 KB
Raw
1 import { describe, expect, test } from 'bun:test';
2
3 import { sliceApiPageProps, sliceDocumentForPage } from '../../lib/openapi-slice';
4
5 // The bail-outs are as important as the per-page narrowing: a larger valid
6 // payload is safer than a smaller document with dangling references.
7
8 const doc = {
9 openapi: '3.1.0',
10 info: { title: 'API', version: '1' },
11 servers: [{ url: 'https://api.example.com' }],
12 paths: {
13 '/users': {
14 parameters: [{ name: 'trace', in: 'header' }],
15 get: {
16 summary: 'List users',
17 responses: {
18 '200': { content: { 'application/json': { schema: { $ref: '#/components/schemas/User' } } } },
19 },
20 },
21 post: {
22 summary: 'Create user',
23 requestBody: {
24 content: { 'application/json': { schema: { $ref: '#/components/schemas/CreateUser' } } },
25 },
26 },
27 },
28 '/orders': {
29 get: {
30 summary: 'List orders',
31 responses: {
32 '200': { content: { 'application/json': { schema: { $ref: '#/components/schemas/Order' } } } },
33 },
34 },
35 },
36 },
37 components: {
38 schemas: {
39 User: { type: 'object', properties: { address: { $ref: '#/components/schemas/Address' } } },
40 Address: { type: 'object', properties: { city: { type: 'string' } } },
41 CreateUser: { type: 'object', properties: { name: { type: 'string' } } },
42 Order: { type: 'object', properties: { total: { type: 'number' } } },
43 },
44 securitySchemes: { apiKey: { type: 'apiKey', name: 'x-api-key', in: 'header' } },
45 },
46 };
47
48 describe('sliceDocumentForPage', () => {
49 test('keeps only the requested operation', () => {
50 const out = sliceDocumentForPage(doc, [{ path: '/users', method: 'get' }]);
51
52 expect(Object.keys(out.paths)).toEqual(['/users']);
53 expect(Object.keys(out.paths['/users'])).toContain('get');
54 expect(Object.keys(out.paths['/users'])).not.toContain('post');
55 });
56
57 test('carries shared path-item keys along with the operation', () => {
58 const out = sliceDocumentForPage(doc, [{ path: '/users', method: 'get' }]);
59
60 expect(out.paths['/users'].parameters).toEqual([{ name: 'trace', in: 'header' }]);
61 });
62
63 test('keeps transitively reachable components and drops the rest', () => {
64 const out = sliceDocumentForPage(doc, [{ path: '/users', method: 'get' }]);
65
66 // User -> Address must survive; unrelated schemas must not.
67 expect(Object.keys(out.components.schemas).sort()).toEqual(['Address', 'User']);
68 });
69
70 test('always keeps securitySchemes, which are referenced by name', () => {
71 const out = sliceDocumentForPage(doc, [{ path: '/users', method: 'get' }]);
72
73 expect(out.components.securitySchemes).toEqual(doc.components.securitySchemes);
74 });
75
76 test('preserves top-level document metadata', () => {
77 const out = sliceDocumentForPage(doc, [{ path: '/users', method: 'get' }]);
78
79 expect(out.openapi).toBe('3.1.0');
80 expect(out.info).toEqual(doc.info);
81 expect(out.servers).toEqual(doc.servers);
82 });
83
84 test('supports a page rendering several operations', () => {
85 const out = sliceDocumentForPage(doc, [
86 { path: '/users', method: 'get' },
87 { path: '/orders', method: 'get' },
88 ]);
89
90 expect(Object.keys(out.paths).sort()).toEqual(['/orders', '/users']);
91 expect(Object.keys(out.components.schemas).sort()).toEqual(['Address', 'Order', 'User']);
92 });
93
94 test('does not mutate the source document', () => {
95 sliceDocumentForPage(doc, [{ path: '/users', method: 'get' }]);
96
97 expect(Object.keys(doc.paths).sort()).toEqual(['/orders', '/users']);
98 expect(Object.keys(doc.components.schemas).sort()).toEqual([
99 'Address',
100 'CreateUser',
101 'Order',
102 'User',
103 ]);
104 });
105
106 test('slices webhook pages', () => {
107 const withWebhook = {
108 ...doc,
109 webhooks: {
110 userCreated: {
111 post: {
112 requestBody: {
113 content: { 'application/json': { schema: { $ref: '#/components/schemas/User' } } },
114 },
115 },
116 },
117 ignored: { post: { summary: 'other' } },
118 },
119 };
120
121 const out = sliceDocumentForPage(withWebhook, [], [{ name: 'userCreated', method: 'post' }]);
122
123 expect(Object.keys(out.webhooks)).toEqual(['userCreated']);
124 expect(Object.keys(out.components.schemas).sort()).toEqual(['Address', 'User']);
125 });
126
127 test('keeps the whole document when nothing is requested', () => {
128 expect(sliceDocumentForPage(doc, [], [])).toBe(doc);
129 });
130
131 test('keeps the whole document for a reference outside components', () => {
132 const odd = {
133 ...doc,
134 paths: {
135 '/users': {
136 get: { responses: { '200': { $ref: '#/paths/~1orders/get' } } },
137 },
138 },
139 };
140
141 expect(sliceDocumentForPage(odd, [{ path: '/users', method: 'get' }])).toBe(odd);
142 });
143
144 test('keeps the whole document for a deep component reference', () => {
145 const deep = {
146 ...doc,
147 paths: {
148 '/users': {
149 get: {
150 responses: {
151 '200': {
152 $ref: '#/components/schemas/User/properties/address',
153 },
154 },
155 },
156 },
157 },
158 };
159
160 expect(sliceDocumentForPage(deep, [{ path: '/users', method: 'get' }])).toBe(deep);
161 });
162
163 test('supports escaped slashes in component names', () => {
164 const escaped = {
165 ...doc,
166 paths: {
167 '/users': {
168 get: {
169 responses: {
170 '200': { $ref: '#/components/schemas/a~1b' },
171 },
172 },
173 },
174 },
175 components: {
176 ...doc.components,
177 schemas: {
178 ...doc.components.schemas,
179 'a/b': { type: 'string' },
180 },
181 },
182 };
183
184 const out = sliceDocumentForPage(escaped, [{ path: '/users', method: 'get' }]);
185
186 expect(out).not.toBe(escaped);
187 expect(out.components.schemas).toEqual({
188 'a/b': { type: 'string' },
189 });
190 });
191
192 test('keeps the whole document for a dangling component reference', () => {
193 const dangling = {
194 ...doc,
195 paths: { '/users': { get: { responses: { '200': { $ref: '#/components/schemas/Missing' } } } } },
196 };
197
198 expect(sliceDocumentForPage(dangling, [{ path: '/users', method: 'get' }])).toBe(dangling);
199 });
200
201 test('keeps the whole document when the operation is absent', () => {
202 expect(sliceDocumentForPage(doc, [{ path: '/nope', method: 'get' }])).toBe(doc);
203 expect(sliceDocumentForPage(doc, [{ path: '/users', method: 'delete' }])).toBe(doc);
204 });
205
206 test('leaves external references alone', () => {
207 const external = {
208 ...doc,
209 paths: {
210 '/users': { get: { responses: { '200': { $ref: 'https://example.com/s.json#/User' } } } },
211 },
212 };
213
214 const out = sliceDocumentForPage(external, [{ path: '/users', method: 'get' }]);
215
216 expect(out.paths['/users'].get.responses['200']).toEqual({
217 $ref: 'https://example.com/s.json#/User',
218 });
219 });
220 });
221
222 describe('sliceApiPageProps', () => {
223 test('narrows payload.bundled while preserving other props', () => {
224 const props = {
225 payload: { bundled: doc, proxyUrl: '/api/proxy' },
226 operations: [{ path: '/users', method: 'get' }],
227 hasHead: true,
228 };
229
230 const out = sliceApiPageProps(props);
231
232 expect(Object.keys(out.payload.bundled.paths)).toEqual(['/users']);
233 expect(out.payload.proxyUrl).toBe('/api/proxy');
234 expect(out.hasHead).toBe(true);
235 expect(out.operations).toBe(props.operations);
236 });
237
238 test('passes through props without a bundled payload', () => {
239 const preloaded = { document: 'openapi.json', preloaded: { docs: {} } };
240
241 expect(sliceApiPageProps(preloaded)).toBe(preloaded);
242 });
243 });