@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, 03:37 UTC

next
ts 169 lines 5.41 KB
Raw
1 import { afterEach, describe, expect, spyOn, test } from 'bun:test';
2 import { mkdtemp, readFile, rm, writeFile } from 'fs/promises';
3 import { tmpdir } from 'os';
4 import { join } from 'path';
5 import { prepareApiSpec, writeWebhookSnapshot } from '../../scripts/fetch-openapi.mjs';
6
7 const fixtureDirectories: string[] = [];
8
9 function createWebhookSpec() {
10 return {
11 openapi: '3.1.0',
12 tags: [{ name: 'Webhook Events' }],
13 webhooks: {
14 'composio.test.event': {
15 post: {
16 operationId: 'composio_test_event',
17 tags: ['Webhook Events'],
18 requestBody: {
19 content: {
20 'application/json': {
21 schema: {
22 type: 'object',
23 properties: { id: { type: 'string' } },
24 },
25 },
26 },
27 },
28 },
29 },
30 },
31 };
32 }
33
34 function createApiSpec() {
35 return {
36 openapi: '3.0.0',
37 info: { title: 'Fixture', version: '1.0.0' },
38 tags: [{ name: 'Public' }, { name: 'x-internal' }],
39 paths: {
40 '/visible': {
41 parameters: [{ name: 'request-id', in: 'header' }],
42 get: {
43 tags: ['Public', 'Secondary'],
44 operationId: 'getV3_1Visible',
45 security: [{ CookieAuth: [] }, { ApiKeyAuth: [] }],
46 responses: {
47 200: {
48 content: {
49 'application/json': {
50 schema: { nullable: true },
51 },
52 },
53 },
54 },
55 },
56 },
57 '/internal': {
58 post: {
59 tags: ['x-internal'],
60 operationId: 'postV3_1Internal',
61 },
62 },
63 },
64 components: {
65 securitySchemes: {
66 CookieAuth: { type: 'apiKey' },
67 ApiKeyAuth: { type: 'apiKey' },
68 },
69 },
70 };
71 }
72
73 afterEach(async () => {
74 await Promise.all(fixtureDirectories.splice(0).map(path => rm(path, { recursive: true })));
75 });
76
77 describe('webhook OpenAPI snapshot validation', () => {
78 test('writes a valid webhook document', async () => {
79 const fixtureDir = await mkdtemp(join(tmpdir(), 'composio-webhook-openapi-'));
80 fixtureDirectories.push(fixtureDir);
81 const outputPath = join(fixtureDir, 'openapi-webhooks.json');
82
83 expect(writeWebhookSnapshot(createWebhookSpec(), outputPath)).toBe(true);
84 expect(JSON.parse(await readFile(outputPath, 'utf-8'))).toEqual(createWebhookSpec());
85 });
86
87 test('keeps the last-good snapshot when a webhook entry is incomplete', async () => {
88 const fixtureDir = await mkdtemp(join(tmpdir(), 'composio-webhook-openapi-'));
89 fixtureDirectories.push(fixtureDir);
90 const outputPath = join(fixtureDir, 'openapi-webhooks.json');
91 const lastGoodSnapshot = JSON.stringify(createWebhookSpec(), null, 2);
92 await writeFile(outputPath, lastGoodSnapshot);
93
94 const warning = spyOn(console, 'warn').mockImplementation(() => {});
95 const written = writeWebhookSnapshot(
96 {
97 openapi: '3.1.0',
98 tags: [{ name: 'Webhook Events' }],
99 webhooks: { 'composio.test.event': {} },
100 },
101 outputPath,
102 'https://example.com/openapi-webhooks.json'
103 );
104
105 expect(written).toBe(false);
106 expect(await readFile(outputPath, 'utf-8')).toBe(lastGoodSnapshot);
107 expect(warning).toHaveBeenCalledWith(
108 expect.stringContaining('webhooks.composio.test.event.post')
109 );
110 warning.mockRestore();
111 });
112 });
113
114 describe('REST OpenAPI processing', () => {
115 test('parses, filters, and normalizes without mutating the response', () => {
116 const payload = createApiSpec();
117 const { spec, removedCount } = prepareApiSpec(payload, '3.1');
118 const operation = spec.paths['/visible'].get;
119
120 expect(removedCount).toBe(1);
121 expect(Object.keys(spec.paths)).toEqual(['/visible']);
122 expect(spec.paths['/visible'].parameters).toEqual(payload.paths['/visible'].parameters);
123 expect(operation.tags).toEqual(['Public']);
124 expect(operation.operationId).toBe('getVisible');
125 expect(operation['x-api-version']).toBe('3.1');
126 expect(operation.security).toEqual([{ ApiKeyAuth: [] }]);
127 expect(operation.responses[200].content['application/json'].schema.type).toBe('object');
128 expect(spec.tags).toEqual([{ name: 'Public' }]);
129 expect(spec.components.securitySchemes.CookieAuth).toBeUndefined();
130 expect(spec.servers).toEqual([
131 {
132 url: 'https://backend.composio.dev',
133 description: 'PRODUCTION API',
134 },
135 ]);
136 expect(payload.paths['/visible'].get.operationId).toBe('getV3_1Visible');
137 });
138
139 test('declares operation tags missing from the top-level tags array', () => {
140 const payload = createApiSpec();
141 (payload.paths as Record<string, unknown>)['/project/usage/summary'] = {
142 post: {
143 tags: ['Projects'],
144 operationId: 'postProjectUsageSummary',
145 },
146 };
147
148 const { spec } = prepareApiSpec(payload, '3.1');
149
150 expect(spec.tags).toEqual([{ name: 'Public' }, { name: 'Projects' }]);
151 });
152
153 test('keeps the first non-ignored tag from mixed-tag operations', () => {
154 const payload = createApiSpec();
155 payload.tags.push({ name: 'Admin' });
156 (payload.paths as Record<string, unknown>)['/projects'] = {
157 get: {
158 tags: ['Admin', 'Projects'],
159 operationId: 'getProjects',
160 },
161 };
162
163 const { spec } = prepareApiSpec(payload, '3.1');
164
165 expect(spec.paths['/projects'].get.tags).toEqual(['Projects']);
166 expect(spec.tags).toContainEqual({ name: 'Projects' });
167 expect(spec.tags).not.toContainEqual({ name: 'Admin' });
168 });
169 });