Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { describe, expect, test } from 'bun:test'; |
| 2 | import { readFileSync, readdirSync } from 'node:fs'; |
| 3 | import { join, relative } from 'node:path'; |
| 4 | import sitemap from '@/app/sitemap'; |
| 5 | import { GET as getLlmsIndex } from '@/app/llms-index.txt/route'; |
| 6 | import { getAlgoliaSearchDocuments, getDocsSearchIndexes } from '@/lib/search-index'; |
| 7 | import * as searchIndexModule from '@/lib/search-index'; |
| 8 | import { getLocalKnowledgeDiscoveryPaths } from '@/lib/knowledge/discovery'; |
| 9 | import { getPublishedKbGuides, readKbManifest } from '@/lib/kb/repository'; |
| 10 | import type { KnowledgeSourceType } from '@/lib/knowledge/types'; |
| 11 | import type { KbManifest } from '@/lib/kb/types'; |
| 12 | |
| 13 | const REQUIRED_PUBLIC_SOURCES: KnowledgeSourceType[] = [ |
| 14 | 'docs', |
| 15 | 'kb', |
| 16 | 'oauth-guide', |
| 17 | 'toolkit', |
| 18 | 'example', |
| 19 | 'reference', |
| 20 | 'changelog', |
| 21 | ]; |
| 22 | |
| 23 | function readTree(root: string): string { |
| 24 | return readdirSync(root, { withFileTypes: true }) |
| 25 | .flatMap((entry) => { |
| 26 | const file = join(root, entry.name); |
| 27 | if (entry.isDirectory()) return [readTree(file)]; |
| 28 | return entry.isFile() ? [readFileSync(file, 'utf8')] : []; |
| 29 | }) |
| 30 | .join('\n'); |
| 31 | } |
| 32 | |
| 33 | function listFiles(root: string): string[] { |
| 34 | return readdirSync(root, { recursive: true, withFileTypes: true }) |
| 35 | .filter(entry => entry.isFile()) |
| 36 | .map(entry => relative(root, join(entry.parentPath, entry.name)).replace(/\\/g, '/')) |
| 37 | .sort(); |
| 38 | } |
| 39 | |
| 40 | describe('unified public knowledge corpus', () => { |
| 41 | test('publishes vendor security review as docs and retires its KB URL', async () => { |
| 42 | const canonical = '/docs/security/vendor-security-review'; |
| 43 | const retired = '/kb/guide/platform-compliance-data-handling'; |
| 44 | const records = await getAlgoliaSearchDocuments(); |
| 45 | expect(records.some(record => record.canonical_url === canonical && record.source_type === 'docs')).toBe(true); |
| 46 | expect(records.some(record => record.canonical_url === retired)).toBe(false); |
| 47 | expect(await getLocalKnowledgeDiscoveryPaths()).not.toContain(retired); |
| 48 | const sitemapUrls = (await sitemap()).map(entry => entry.url); |
| 49 | expect(sitemapUrls).toContain(`https://docs.composio.dev${canonical}`); |
| 50 | expect(sitemapUrls).not.toContain(`https://docs.composio.dev${retired}`); |
| 51 | const llmsIndex = await (await getLlmsIndex()).text(); |
| 52 | expect(llmsIndex).toContain(canonical); |
| 53 | expect(llmsIndex).not.toContain(retired); |
| 54 | }); |
| 55 | |
| 56 | test('does not interpret fenced code comments as document headings', () => { |
| 57 | const recordsFromMarkdownPage = ( |
| 58 | searchIndexModule as { |
| 59 | recordsFromMarkdownPage?: (input: { |
| 60 | url: string; |
| 61 | type: string; |
| 62 | title: string; |
| 63 | markdown: string; |
| 64 | }) => Array<{ section?: string; content: string }>; |
| 65 | } |
| 66 | ).recordsFromMarkdownPage; |
| 67 | |
| 68 | expect(typeof recordsFromMarkdownPage).toBe('function'); |
| 69 | if (!recordsFromMarkdownPage) return; |
| 70 | |
| 71 | const records = recordsFromMarkdownPage({ |
| 72 | url: '/docs/test-fenced-comments', |
| 73 | type: 'docs', |
| 74 | title: 'Fenced comments', |
| 75 | markdown: [ |
| 76 | '# Fenced comments', |
| 77 | '', |
| 78 | '## Setup', |
| 79 | '', |
| 80 | 'Before code.', |
| 81 | '', |
| 82 | '```python', |
| 83 | '# not a heading', |
| 84 | 'value = 1', |
| 85 | '```', |
| 86 | '', |
| 87 | '## Next', |
| 88 | '', |
| 89 | 'After code.', |
| 90 | ].join('\n'), |
| 91 | }); |
| 92 | |
| 93 | expect(records.map(record => record.section)).toEqual([ |
| 94 | 'Fenced comments', |
| 95 | 'Setup', |
| 96 | 'Next', |
| 97 | ]); |
| 98 | expect(records.find(record => record.section === 'Setup')?.content) |
| 99 | .toContain('# not a heading'); |
| 100 | }); |
| 101 | |
| 102 | test('excludes hidden API operations from local and Algolia search corpora', async () => { |
| 103 | const hiddenOperationUrls = [ |
| 104 | '/reference/api-reference/invite-codes/postOrgClankerCreateClaim', |
| 105 | '/reference/api-reference/authentication/getAuthSessionInfo', |
| 106 | ]; |
| 107 | const [records, indexes] = await Promise.all([ |
| 108 | getAlgoliaSearchDocuments(), |
| 109 | getDocsSearchIndexes(), |
| 110 | ]); |
| 111 | |
| 112 | for (const url of hiddenOperationUrls) { |
| 113 | expect(records.some((record) => record.url === url)).toBe(false); |
| 114 | expect(indexes.some((index) => index.url === url)).toBe(false); |
| 115 | } |
| 116 | }); |
| 117 | |
| 118 | test('contains every public source with normalized canonical metadata', async () => { |
| 119 | const records = await getAlgoliaSearchDocuments(); |
| 120 | for (const sourceType of REQUIRED_PUBLIC_SOURCES) { |
| 121 | expect(records.some((record) => record.source_type === sourceType)).toBe(true); |
| 122 | } |
| 123 | expect(records.some((record) => record.source_type === 'legacy')).toBe(true); |
| 124 | |
| 125 | for (const record of records) { |
| 126 | expect(record.canonical_url.length).toBeGreaterThan(0); |
| 127 | expect(Array.isArray(record.product_areas)).toBe(true); |
| 128 | expect(Array.isArray(record.toolkit_slugs)).toBe(true); |
| 129 | expect(Array.isArray(record.intents)).toBe(true); |
| 130 | } |
| 131 | }); |
| 132 | |
| 133 | test('keeps the local global-search fallback on the same OAuth corpus', async () => { |
| 134 | const indexes = await getDocsSearchIndexes(); |
| 135 | const records = await getAlgoliaSearchDocuments(); |
| 136 | const oauthUrls = new Set( |
| 137 | records.filter(record => record.source_type === 'oauth-guide').map(record => record.canonical_url), |
| 138 | ); |
| 139 | expect(oauthUrls.size).toBeGreaterThan(0); |
| 140 | for (const url of oauthUrls) { |
| 141 | expect(url.startsWith('/toolkits/')).toBe(true); |
| 142 | expect(indexes.filter(index => index.url === url)).toHaveLength(1); |
| 143 | } |
| 144 | }); |
| 145 | |
| 146 | test('discovers local hub and browse routes without duplicating external OAuth pages', async () => { |
| 147 | const paths = await getLocalKnowledgeDiscoveryPaths(); |
| 148 | const guide = getPublishedKbGuides()[0]; |
| 149 | expect(guide).toBeDefined(); |
| 150 | expect(paths).toContain('/kb'); |
| 151 | expect(paths).toContain('/kb/search'); |
| 152 | expect(paths).toContain('/kb/topic/authentication-and-connected-accounts'); |
| 153 | expect(paths).toContain('/kb/toolkits'); |
| 154 | expect(paths.some(path => path.startsWith('/kb/toolkit/'))).toBe(true); |
| 155 | expect(paths).toContain(`/kb/guide/${guide!.slug}`); |
| 156 | expect(paths.some((path) => path.startsWith('https://composio.dev/auth/'))).toBe(false); |
| 157 | |
| 158 | const sitemapUrls = (await sitemap()).map((entry) => entry.url); |
| 159 | for (const path of paths.filter(path => path !== '/kb/search')) { |
| 160 | expect(sitemapUrls).toContain(`https://docs.composio.dev${path}`); |
| 161 | } |
| 162 | expect(sitemapUrls).not.toContain('https://docs.composio.dev/kb/search'); |
| 163 | expect(sitemapUrls.some((url) => url.startsWith('https://composio.dev/auth/'))).toBe(false); |
| 164 | }); |
| 165 | |
| 166 | test('publishes flat guide and browse routes in the LLM index', async () => { |
| 167 | const response = await getLlmsIndex(); |
| 168 | const body = await response.text(); |
| 169 | const guide = getPublishedKbGuides()[0]; |
| 170 | expect(guide).toBeDefined(); |
| 171 | expect(body).toContain('https://docs.composio.dev/kb'); |
| 172 | expect(body).toContain('https://docs.composio.dev/kb/toolkits'); |
| 173 | expect(body).toMatch(/https:\/\/docs\.composio\.dev\/kb\/toolkit\/[^\s]+/); |
| 174 | expect(body).toContain(`https://docs.composio.dev/kb/guide/${guide!.slug}.md`); |
| 175 | expect(body).not.toContain('https://composio.dev/auth/'); |
| 176 | }); |
| 177 | |
| 178 | test('keeps generated and searchable KB content free of internal-only markers', async () => { |
| 179 | const records = await getAlgoliaSearchDocuments(); |
| 180 | const searchableKb = records |
| 181 | .filter((record) => record.source_type === 'kb') |
| 182 | .map((record) => [ |
| 183 | record.title, |
| 184 | record.description, |
| 185 | record.canonical_url, |
| 186 | record.content, |
| 187 | ...(record.keywords ?? []), |
| 188 | ...(record.headings ?? []), |
| 189 | ].filter(Boolean).join('\n')) |
| 190 | .join('\n'); |
| 191 | const generatedKb = readTree(join(process.cwd(), 'content/kb')); |
| 192 | const forbidden = [ |
| 193 | /\bT-\d{2,}\b/, |
| 194 | /app\.plain\.com/i, |
| 195 | /slack\.com\/archives\//i, |
| 196 | /linear\.app\/composio/i, |
| 197 | /X-Amz-(?:Signature|Credential)/i, |
| 198 | /(?:\/Users\/|\/home\/|[A-Za-z]:\\Users\\)/, |
| 199 | /\bcandidate-only\b/i, |
| 200 | /^#{2,6}\s+(?:Internal|Support checks|Debug checklist|Related Plain refs)\b/im, |
| 201 | /(?:draft )?response (?:shape|template)/i, |
| 202 | ]; |
| 203 | |
| 204 | for (const pattern of forbidden) { |
| 205 | expect(searchableKb).not.toMatch(pattern); |
| 206 | expect(generatedKb).not.toMatch(pattern); |
| 207 | } |
| 208 | }); |
| 209 | |
| 210 | test('accounts for every snapshot file and excludes retired guides from public discovery', async () => { |
| 211 | const manifest = readKbManifest(); |
| 212 | const snapshotFiles = listFiles(join(process.cwd(), 'kb/source')); |
| 213 | const guideSourcePaths = [...new Set( |
| 214 | manifest.guides.flatMap(guide => guide.sources.map(source => source.sourcePath)), |
| 215 | )].sort(); |
| 216 | |
| 217 | const records = await getAlgoliaSearchDocuments(); |
| 218 | const sitemapUrls = (await sitemap()).map(entry => entry.url); |
| 219 | const llmsIndex = await (await getLlmsIndex()).text(); |
| 220 | for (const guide of manifest.guides.filter(guide => guide.state === 'retired')) { |
| 221 | const path = `/kb/guide/${guide.slug}`; |
| 222 | expect(getPublishedKbGuides().some(published => published.slug === guide.slug)).toBe(false); |
| 223 | expect(records.some(record => record.canonical_url.includes(path))).toBe(false); |
| 224 | expect(sitemapUrls.some(url => url.includes(path))).toBe(false); |
| 225 | expect(llmsIndex).not.toContain(path); |
| 226 | } |
| 227 | expect(guideSourcePaths).toEqual(snapshotFiles); |
| 228 | }); |
| 229 | }); |