@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 523 lines 21.6 KB
Raw
1 import { afterEach, describe, expect, test } from 'bun:test';
2 import {
3 existsSync,
4 mkdirSync,
5 mkdtempSync,
6 readFileSync,
7 readdirSync,
8 rmSync,
9 statSync,
10 utimesSync,
11 writeFileSync,
12 } from 'node:fs';
13 import { tmpdir } from 'node:os';
14 import { join, relative } from 'node:path';
15 import GithubSlugger from 'github-slugger';
16 import { generateKbContent, markdownForMdx } from '@/lib/kb/generate';
17 import { buildKbCatalog } from '@/lib/kb/catalog';
18 import { createKbArticleReader, getKbCatalog, readKbManifest } from '@/lib/kb/repository';
19 import type { KbManifest } from '@/lib/kb/types';
20 import { evaluate } from '@mdx-js/mdx';
21 import { createElement } from 'react';
22 import * as jsxRuntime from 'react/jsx-runtime';
23 import { renderToStaticMarkup } from 'react-dom/server';
24 import { Tabs, Tab } from 'fumadocs-ui/components/tabs';
25 import { Callout } from '@/components/callout';
26 import { checkCodeExamples, parseCodeExamples, walkCodeExamples } from '@/lib/code-examples';
27 import { componentsToMarkdown } from '@/lib/example-document';
28
29 const temporaryDirectories: string[] = [];
30
31 afterEach(() => {
32 for (const directory of temporaryDirectories.splice(0)) {
33 rmSync(directory, { recursive: true, force: true });
34 }
35 });
36
37 function listFiles(directory: string): string[] {
38 return readdirSync(directory, { recursive: true, withFileTypes: true })
39 .filter(entry => entry.isFile())
40 .map(entry => relative(directory, join(entry.parentPath, entry.name)))
41 .sort();
42 }
43
44 function exactRedirects(): Map<string, string> {
45 const config = readFileSync(join(process.cwd(), 'next.config.mjs'), 'utf8');
46 return new Map(
47 [...config.matchAll(/source:\s*(['"])([^'"]+)\1,\s*destination:\s*(['"])([^'"]+)\3,/g)]
48 .map(match => [match[2]!, match[4]!]),
49 );
50 }
51
52 function resolveDocsMarkdown(
53 pathname: string,
54 docsRoot: string,
55 redirects: Map<string, string>,
56 ): string | null {
57 const visited = new Set<string>();
58 let current = pathname;
59
60 while (!visited.has(current)) {
61 visited.add(current);
62 const relativePath = current.replace(/^\/docs\/?/, '');
63 const candidates = relativePath
64 ? [join(docsRoot, `${relativePath}.mdx`), join(docsRoot, relativePath, 'index.mdx')]
65 : [join(docsRoot, 'index.mdx')];
66 const target = candidates.find(candidate => existsSync(candidate));
67 if (target) return target;
68
69 const destination = redirects.get(current);
70 if (!destination?.startsWith('/docs')) return null;
71 current = destination;
72 }
73
74 return null;
75 }
76
77 function renderedHeadingFragments(markdown: string): string[] {
78 const slugger = new GithubSlugger();
79 const fragments: string[] = [];
80 let fence: { marker: string; length: number } | null = null;
81
82 for (const line of markdown.split('\n')) {
83 const fenceMatch = line.match(/^\s*(`{3,}|~{3,})/);
84 if (fenceMatch) {
85 const marker = fenceMatch[1]!;
86 if (!fence) fence = { marker: marker[0]!, length: marker.length };
87 else if (marker[0] === fence.marker && marker.length >= fence.length) fence = null;
88 continue;
89 }
90 if (fence) continue;
91
92 const heading = line.match(/^#{1,6}\s+(.+)$/)?.[1];
93 if (heading) fragments.push(slugger.slug(heading));
94 }
95
96 return fragments;
97 }
98
99 describe('public KB content generation', () => {
100 test('makes authoritative Markdown safe for MDX without changing rendered prose', () => {
101 expect(markdownForMdx([
102 'Use {"field": "value"}, `<placeholder>`, and <https://example.com/path>.',
103 '',
104 '```ts',
105 'await undeclaredClient.run();',
106 '```',
107 ].join('\n'))).toBe([
108 'Use &#123;"field": "value"&#125;, `<placeholder>`, and [https://example.com/path](https://example.com/path).',
109 '',
110 '```ts',
111 'await undeclaredClient.run();',
112 '```',
113 ].join('\n'));
114 });
115
116 test('renders language headings as real selectable tabs and preserves agent-facing language labels', async () => {
117 const source = [
118 '## Operation',
119 '### Python',
120 '```python',
121 'print("python option")',
122 '```',
123 '### TypeScript',
124 '```typescript',
125 'console.log("typescript option");',
126 '```',
127 '### API',
128 '```bash',
129 'curl https://backend.composio.dev/api/v3.1/toolkits',
130 '```',
131 ].join('\n');
132 const mdx = markdownForMdx(source);
133 expect(checkCodeExamples([{ path: 'generated.mdx', source: mdx }], { requireApi: true })).toEqual([]);
134 const { default: Content } = await evaluate(mdx, jsxRuntime);
135 const html = renderToStaticMarkup(createElement(Content, { components: { Tabs, Tab, Callout } }));
136 expect(html).toContain('role="tablist"');
137 for (const label of ['Python', 'TypeScript', 'API']) {
138 expect(html).toContain(`>${label}</button>`);
139 expect(componentsToMarkdown(mdx)).toContain(`**${label}:**`);
140 }
141 expect(mdx).toContain('```typescript');
142 expect(mdx).not.toContain('```text');
143 });
144
145 test('end-languages moves shared prose after the generated Tabs', () => {
146 const mdx = markdownForMdx([
147 '### Python',
148 '```python',
149 'print("python option")',
150 '```',
151 '### TypeScript',
152 '```typescript',
153 'console.log("typescript option");',
154 '```',
155 '### API',
156 '```bash',
157 'curl https://backend.composio.dev/api/v3.1/toolkits',
158 '```',
159 '',
160 '<!-- end-languages -->',
161 '',
162 'Shared follow-up.',
163 ].join('\n'));
164 expect(mdx).toMatch(/<\/Tabs>\s+Shared follow-up\.$/);
165 expect(mdx).not.toContain('end-languages');
166 });
167
168 test('turns Markdown SDK-gap notes into Callouts without leaking validation metadata into rendered prose', () => {
169 const source = [
170 '#### Python',
171 '> [!NOTE]',
172 '> The SDK does not expose this admin endpoint.',
173 '#### TypeScript',
174 '> [!NOTE]',
175 '> The SDK does not expose this admin endpoint.',
176 '#### API',
177 '<!-- code-example: unchecked-api rule="api-legacy-version" operation="GET /api/healthz" reason="Self-hosted admin liveness is unversioned." -->',
178 '```bash',
179 'curl "$COMPOSIO_BASE_URL/api/healthz"',
180 '```',
181 ].join('\n');
182 const mdx = markdownForMdx(source);
183 expect(mdx.match(/<Callout type="info">/g)).toHaveLength(2);
184 expect(mdx).not.toContain('code-example:');
185 expect(checkCodeExamples([{ path: 'generated.mdx', source: mdx }], { requireApi: true })).toEqual([]);
186 });
187
188 test('preserves typed source code and options in every migrated curated KB article', () => {
189 const cases = [
190 ['platform-session-tool-policies.md', 4],
191 ['mcp-tool-router-sessions.md', 1],
192 ['platform-custom-connection-data-fields.md', 1],
193 ['platform-health-endpoints.md', 4],
194 ['toolkits-microsoft-teams.md', 2],
195 ] as const;
196 for (const [article, expectedGroups] of cases) {
197 const source = readFileSync(join(process.cwd(), 'kb/articles', article), 'utf8');
198 const mdx = markdownForMdx(source);
199 const parsed = parseCodeExamples(mdx, article.replace('.md', '.mdx'));
200 let groups = 0;
201 walkCodeExamples(parsed.units, (unit) => {
202 if (unit.type === 'tabs' && unit.groupId === 'language') {
203 groups++;
204 expect(unit.items).toEqual(['Python', 'TypeScript', 'API']);
205 }
206 });
207 expect(groups, article).toBe(expectedGroups);
208 expect(checkCodeExamples([{ path: `${article}x`, source: mdx }], { requireApi: true })).toEqual([]);
209 }
210 });
211
212 test('demotes bare identifier URLs to code spans so they never publish as dead links', () => {
213 expect(markdownForMdx([
214 'Ahrefs API calls should use the API host https://api.ahrefs.com/v3. If actions are hitting https://ahrefs.com/v3 and returning 404 HTML, treat it as a configuration problem.',
215 'Include the Meet scopes https://www.googleapis.com/auth/meetings.space.created and https://www.googleapis.com/auth/meetings.space.settings in the auth config.',
216 'Read https://developers.google.com/identity/protocols/oauth2 and [the policy](https://developers.google.com/identity/protocols/oauth2/policies) for details.',
217 'Explicit syntax keeps its form: <https://developers.google.com/identity/protocols/oauth2> stays a link, [scope](https://www.googleapis.com/auth/gmail.send) and <https://www.googleapis.com/auth/gmail.send> cite identifiers, so only the first remains a link.',
218 'Already code: `https://www.googleapis.com/auth/drive` and deep API paths such as https://backend.composio.dev/api/v3/tools/X remain links.',
219 '',
220 '```text',
221 'const scope = "https://www.googleapis.com/auth/drive";',
222 '```',
223 ].join('\n'))).toBe([
224 'Ahrefs API calls should use the API host `https://api.ahrefs.com/v3`. If actions are hitting `https://ahrefs.com/v3` and returning 404 HTML, treat it as a configuration problem.',
225 'Include the Meet scopes `https://www.googleapis.com/auth/meetings.space.created` and `https://www.googleapis.com/auth/meetings.space.settings` in the auth config.',
226 'Read https://developers.google.com/identity/protocols/oauth2 and [the policy](https://developers.google.com/identity/protocols/oauth2/policies) for details.',
227 'Explicit syntax keeps its form: [https://developers.google.com/identity/protocols/oauth2](https://developers.google.com/identity/protocols/oauth2) stays a link, [scope](https://www.googleapis.com/auth/gmail.send) and `https://www.googleapis.com/auth/gmail.send` cite identifiers, so only the first remains a link.',
228 'Already code: `https://www.googleapis.com/auth/drive` and deep API paths such as https://backend.composio.dev/api/v3/tools/X remain links.',
229 '',
230 '```text',
231 'const scope = "https://www.googleapis.com/auth/drive";',
232 '```',
233 ].join('\n'));
234 });
235
236 test('defines multi-source provenance in the KB frontmatter schema', () => {
237 const sourceConfig = readFileSync(join(process.cwd(), 'source.config.ts'), 'utf8');
238
239 expect(sourceConfig).toMatch(/sources:\s*z\s*\.\s*array\(/);
240 expect(sourceConfig).toContain('sourcePath: z.string(),');
241 expect(sourceConfig).toContain('sourceHeading: z.string().nullable(),');
242 expect(sourceConfig).not.toContain('sourcePath: z.string().optional()');
243 expect(sourceConfig).not.toContain('sourceHeading: z.string().optional()');
244 });
245
246 test('keeps direct docs fragment links pointed at rendered Markdown headings', () => {
247 const articlesRoot = join(process.cwd(), 'kb/articles');
248 const docsRoot = join(process.cwd(), 'content/docs');
249 const redirects = exactRedirects();
250 const links: Array<{ article: string; href: string }> = [];
251
252 for (const article of readdirSync(articlesRoot).filter(name => name.endsWith('.md'))) {
253 const markdown = readFileSync(join(articlesRoot, article), 'utf8');
254 for (const match of markdown.matchAll(/https:\/\/docs\.composio\.dev(\/docs\/[^\s)#]+)#([^\s)]+)/g)) {
255 links.push({ article, href: `${match[1]}#${match[2]}` });
256 }
257 }
258
259 for (const { article, href } of links) {
260 const url = new URL(href, 'https://docs.composio.dev');
261 const target = resolveDocsMarkdown(url.pathname, docsRoot, redirects);
262 if (!target) throw new Error(`${article} links to unresolved docs path ${url.pathname}`);
263
264 const fragments = renderedHeadingFragments(readFileSync(target, 'utf8'));
265 expect(fragments, `${article} links to missing fragment ${href}`).toContain(url.hash.slice(1));
266 }
267 });
268
269 test('does not accept a docs fragment that appears only inside fenced code', () => {
270 expect(renderedHeadingFragments([
271 '## Real heading',
272 '```python',
273 '# Not a heading',
274 '```',
275 ].join('\n'))).toEqual(['real-heading']);
276 });
277
278 test('generates native Fumadocs pages for published guides only', () => {
279 const outputDir = mkdtempSync(join(tmpdir(), 'composio-kb-'));
280 temporaryDirectories.push(outputDir);
281
282 const summary = generateKbContent({ outputDir });
283 const files = listFiles(outputDir);
284
285 // Counts track the manifest rather than a fixed seed size: every published
286 // guide gets one page, plus index.mdx, meta.json, and guide/meta.json.
287 const guides = getKbCatalog().manifest.guides;
288 const publishedCount = guides.filter(guide => guide.state === 'published').length;
289 const heldCount = guides.filter(guide => guide.state === 'needs-review').length;
290
291 expect(summary).toEqual({
292 published: publishedCount,
293 held: heldCount,
294 files: files.length,
295 });
296 expect(files).toHaveLength(publishedCount + 3);
297 expect(files).toContain('index.mdx');
298 expect(files).toContain('meta.json');
299 expect(files).toContain('guide/meta.json');
300 expect(files.some(file => file.startsWith('toolkits/'))).toBe(false);
301 expect(files.some(file => file.startsWith('sdk-and-api/'))).toBe(false);
302
303 const manifest = readKbManifest();
304 const published = manifest.guides.filter(guide => guide.state === 'published');
305 const newlyAuthored = published.filter(guide => guide.articlePath !== undefined);
306 const held = manifest.guides.filter(guide => guide.state === 'needs-review');
307 expect(published).toHaveLength(publishedCount);
308 // Every published guide renders from an authored article, never from the
309 // source snapshot. That keeps kb/source a verbatim copy of upstream, so it
310 // stays comparable for drift detection instead of drifting under editing.
311 expect(newlyAuthored).toHaveLength(published.length);
312 expect(new Set(newlyAuthored.map(guide => guide.articlePath)).size).toBe(published.length);
313 expect(new Set(newlyAuthored.map(guide => `/kb/guide/${guide.slug}`)).size).toBe(
314 published.length
315 );
316 // The published snapshot only contains reviewed public
317 // leaves. Generating it must not invent editorial holds downstream.
318 expect(held).toHaveLength(0);
319
320 expect(JSON.parse(readFileSync(join(outputDir, 'meta.json'), 'utf8'))).toEqual({
321 title: 'Knowledge Base',
322 root: true,
323 pages: ['index', 'guide'],
324 });
325 // Nav order is the manifest's published order, so it stays correct as
326 // batches are appended rather than needing a re-listing on every publish.
327 // Page order mirrors published manifest order rather than a frozen list, so
328 // adding a guide does not require restating the whole corpus here.
329 expect(JSON.parse(readFileSync(join(outputDir, 'guide/meta.json'), 'utf8'))).toEqual({
330 title: 'Guides',
331 pages: published.map(guide => guide.slug),
332 });
333
334 // Assert the transformation for every guide without making any factual KB
335 // claim part of the test contract.
336 for (const definition of published) {
337 expect(files).toContain(`guide/${definition.slug}.mdx`);
338 const generated = readFileSync(join(outputDir, 'guide', `${definition.slug}.mdx`), 'utf8');
339 const body = generated.split('\n---\n').at(-1)?.trim() ?? '';
340 expect(body.length).toBeGreaterThan(0);
341 expect(body).not.toMatch(/\]\(\.\.?\/[^)]*public\.md/);
342 expect(generated).toContain(`sources: ${JSON.stringify(definition.sources)}`);
343 expect(generated).toContain(`lastVerifiedAt: "${definition.lastVerifiedAt}"`);
344 expect(generated).toContain(`reviewAfter: "${definition.reviewAfter}"`);
345 expect(generated).not.toContain('articlePath:');
346 }
347 });
348
349 test('leaves unchanged generated files untouched', () => {
350 const outputDir = mkdtempSync(join(tmpdir(), 'composio-kb-'));
351 temporaryDirectories.push(outputDir);
352 const unchangedPath = join(outputDir, 'index.mdx');
353 const preservedTime = new Date('2000-01-01T00:00:00.000Z');
354
355 generateKbContent({ outputDir });
356 utimesSync(unchangedPath, preservedTime, preservedTime);
357
358 generateKbContent({ outputDir });
359
360 expect(statSync(unchangedPath).mtimeMs).toBe(preservedTime.getTime());
361 });
362
363 test('repairs changed output and removes stale files without touching unchanged files', () => {
364 const outputDir = mkdtempSync(join(tmpdir(), 'composio-kb-'));
365 temporaryDirectories.push(outputDir);
366 const unchangedPath = join(outputDir, 'index.mdx');
367 const changedPath = join(outputDir, 'guide/meta.json');
368 const stalePath = join(outputDir, 'guide/stale-guide.mdx');
369 const preservedTime = new Date('2000-01-01T00:00:00.000Z');
370
371 generateKbContent({ outputDir });
372 const expectedChangedContent = readFileSync(changedPath, 'utf8');
373 utimesSync(unchangedPath, preservedTime, preservedTime);
374 writeFileSync(changedPath, 'stale content', 'utf8');
375 writeFileSync(stalePath, 'stale guide', 'utf8');
376
377 generateKbContent({ outputDir });
378
379 expect(readFileSync(changedPath, 'utf8')).toBe(expectedChangedContent);
380 expect(existsSync(stalePath)).toBe(false);
381 expect(statSync(unchangedPath).mtimeMs).toBe(preservedTime.getTime());
382 });
383
384 test('renders an editorial body read from a temporary articles root without exposing its path', () => {
385 const outputDir = mkdtempSync(join(tmpdir(), 'composio-kb-'));
386 temporaryDirectories.push(outputDir);
387 const root = mkdtempSync(join(tmpdir(), 'composio-kb-articles-'));
388 temporaryDirectories.push(root);
389 const articlesRoot = join(root, 'articles');
390 mkdirSync(articlesRoot);
391 writeFileSync(
392 join(articlesRoot, 'editorial-guide.md'),
393 'This is the authored editorial body.',
394 'utf8'
395 );
396 const manifest: KbManifest = {
397 schemaVersion: 2,
398 topics: [
399 { slug: 'platform', title: 'Platform', description: 'Platform guidance.', featuredRank: 1 },
400 ],
401 guides: [
402 {
403 slug: 'editorial-guide',
404 title: 'Editorial guide',
405 description: 'A guide with an authored body.',
406 articlePath: 'editorial-guide.md',
407 sources: [
408 { sourcePath: 'kb/platform/example/public.md', sourceHeading: 'Stable answer' },
409 ],
410 topics: ['platform'],
411 tags: [],
412 aliases: [],
413 relatedGuides: [],
414 externalResources: [],
415 updatedAt: '2026-07-20',
416 lastVerifiedAt: '2026-07-21',
417 reviewAfter: '2027-01-17',
418 freshness: 'evergreen',
419 state: 'published',
420 featured: false,
421 },
422 ],
423 };
424 const source = `---\ntype: reference\ntitle: Example\ndescription: Public example.\ncategory: platform/example\nvisibility: public\ntimestamp: 2026-07-20T00:00:00Z\ntags:\n - example\n---\n# Example\n\n## Stable answer\n\nPublic source provenance.\n`;
425 const catalog = buildKbCatalog(
426 manifest,
427 () => source,
428 new Date('2026-07-21'),
429 createKbArticleReader(articlesRoot)
430 );
431 generateKbContent({ outputDir, catalog });
432
433 const generated = readFileSync(join(outputDir, 'guide/editorial-guide.mdx'), 'utf8');
434 expect(generated).toContain('This is the authored editorial body.');
435 expect(generated).toContain(
436 'sources: [{"sourcePath":"kb/platform/example/public.md","sourceHeading":"Stable answer"}]'
437 );
438 expect(generated).not.toContain('articlePath');
439 });
440
441 test('rewrites source-repository cross-links to canonical KB guide URLs', () => {
442 const outputDir = mkdtempSync(join(tmpdir(), 'composio-kb-'));
443 temporaryDirectories.push(outputDir);
444 const manifest: KbManifest = {
445 schemaVersion: 2,
446 topics: [],
447 guides: [
448 {
449 slug: 'toolkits-gmail',
450 title: 'Gmail',
451 description: 'Gmail guidance.',
452 sources: [{ sourcePath: 'toolkits/gmail/public.md', sourceHeading: null }],
453 topics: [],
454 tags: [],
455 aliases: [],
456 relatedGuides: [],
457 externalResources: [],
458 updatedAt: '2026-07-20',
459 lastVerifiedAt: '2026-07-21',
460 reviewAfter: '2027-01-17',
461 freshness: 'evergreen',
462 state: 'published',
463 featured: false,
464 },
465 {
466 slug: 'toolkits-googlesuper',
467 title: 'Google Super',
468 description: 'Google Super guidance.',
469 sources: [
470 { sourcePath: 'toolkits/googlesuper/public.md', sourceHeading: 'Setup' },
471 { sourcePath: 'toolkits/googlesuper/public.md', sourceHeading: 'Operations' },
472 ],
473 topics: [],
474 tags: [],
475 aliases: [],
476 relatedGuides: [],
477 externalResources: [],
478 updatedAt: '2026-07-20',
479 lastVerifiedAt: '2026-07-21',
480 reviewAfter: '2027-01-17',
481 freshness: 'evergreen',
482 state: 'published',
483 featured: false,
484 },
485 ],
486 };
487 const sourceByPath = new Map([
488 [
489 'toolkits/gmail/public.md',
490 '---\ntype: guide\ntitle: Gmail\ndescription: Gmail guidance.\ncategory: toolkits/gmail\nvisibility: public\ntimestamp: 2026-07-20T00:00:00Z\ntags:\n - gmail\n---\n# Gmail\n\nSee [Google Super](../googlesuper/public.md#unified-auth) and [Google Super setup](../googlesuper/public.md#setup).',
491 ],
492 [
493 'toolkits/googlesuper/public.md',
494 '---\ntype: guide\ntitle: Google Super\ndescription: Google Super guidance.\ncategory: toolkits/googlesuper\nvisibility: public\ntimestamp: 2026-07-20T00:00:00Z\ntags:\n - google\n---\n# Google Super\n\n## Setup\n\nUnified authentication.\n\n## Operations\n\nRun Google tools.',
495 ],
496 ]);
497 const catalog = buildKbCatalog(
498 manifest,
499 sourcePath => sourceByPath.get(sourcePath)!,
500 new Date('2026-07-21'),
501 );
502
503 generateKbContent({ outputDir, catalog });
504
505 const generated = readFileSync(join(outputDir, 'guide/toolkits-gmail.mdx'), 'utf8');
506 expect(generated).toContain(
507 '[Google Super](/kb/guide/toolkits-googlesuper)',
508 );
509 expect(generated).toContain(
510 '[Google Super setup](/kb/guide/toolkits-googlesuper#setup)',
511 );
512 expect(generated).not.toContain('../googlesuper/public.md');
513 });
514
515 test('detects generated content drift in check mode', () => {
516 const outputDir = mkdtempSync(join(tmpdir(), 'composio-kb-'));
517 temporaryDirectories.push(outputDir);
518
519 expect(() => generateKbContent({ outputDir, check: true })).toThrow(
520 'Generated KB content is out of date'
521 );
522 });
523 });