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
| 1 | import { describe, expect, test } from 'bun:test'; |
| 2 | import { getPublishedKbGuides } from '@/lib/kb/repository'; |
| 3 | import { getKnowledgeToolkitSummaries } from '@/lib/knowledge/catalog'; |
| 4 | import { getToolkitKnowledgeRedirect } from '@/lib/knowledge/toolkit-routing'; |
| 5 | import { fetchNoRedirect, fetchPage } from './helpers'; |
| 6 | |
| 7 | function markdownAlternates(html: string): string[] { |
| 8 | return (html.match(/<link\b[^>]*>/g) ?? []) |
| 9 | .filter(tag => /\brel="alternate"/.test(tag) && /\btype="text\/markdown"/.test(tag)) |
| 10 | .map(tag => tag.match(/\bhref="([^"]+)"/)![1]); |
| 11 | } |
| 12 | |
| 13 | async function expectDiscoverableMarkdown(pathname: string) { |
| 14 | const html = await fetchPage(pathname, { |
| 15 | headers: { Accept: 'text/html', 'User-Agent': 'facebookexternalhit/1.1' }, |
| 16 | timeout: 30_000, |
| 17 | }); |
| 18 | expect(html.status).toBe(200); |
| 19 | expect(html.headers.get('content-type')).toContain('text/html'); |
| 20 | const links = markdownAlternates(await html.text()); |
| 21 | expect(links).toHaveLength(1); |
| 22 | const alternate = new URL(links[0], html.url); |
| 23 | expect(alternate.pathname).toBe(`${pathname}.md`); |
| 24 | expect(alternate.search).toBe(''); |
| 25 | |
| 26 | // Follow the public alternate on the test server, not the metadataBase host. |
| 27 | const markdown = await fetchPage(alternate.pathname, { timeout: 30_000 }); |
| 28 | expect(markdown.status).toBe(200); |
| 29 | expect(markdown.headers.get('content-type')).toContain('text/markdown'); |
| 30 | } |
| 31 | |
| 32 | describe('HTML Markdown alternate discovery', () => { |
| 33 | for (const pathname of [ |
| 34 | '/docs', |
| 35 | '/docs/quickstart', |
| 36 | '/examples', |
| 37 | '/examples/custom-tools', |
| 38 | '/reference', |
| 39 | '/reference/v3', |
| 40 | '/reference/api-reference/files/postFilesUploadRequest', |
| 41 | '/reference/v3/api-reference/files/postFilesUploadRequest', |
| 42 | '/toolkits', |
| 43 | '/toolkits/github', |
| 44 | '/toolkits/managed-auth', |
| 45 | '/toolkits/meta-tools', |
| 46 | '/toolkits/meta-tools/search_tools', |
| 47 | '/kb', |
| 48 | '/kb/search', |
| 49 | '/kb/toolkits', |
| 50 | '/kb/topic/authentication-and-connected-accounts', |
| 51 | ]) { |
| 52 | test(`${pathname} advertises a working public Markdown alternate`, async () => { |
| 53 | await expectDiscoverableMarkdown(pathname); |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | test('published KB guides advertise their own Markdown URL', async () => { |
| 58 | const guide = getPublishedKbGuides()[0]; |
| 59 | expect(guide).toBeDefined(); |
| 60 | await expectDiscoverableMarkdown(`/kb/guide/${guide.slug}`); |
| 61 | }); |
| 62 | |
| 63 | test('nonredirecting toolkit knowledge pages advertise their own Markdown URL', async () => { |
| 64 | const toolkit = (await getKnowledgeToolkitSummaries()).find(candidate => !getToolkitKnowledgeRedirect(candidate)); |
| 65 | expect(toolkit).toBeDefined(); |
| 66 | await expectDiscoverableMarkdown(`/kb/toolkit/${toolkit!.slug}`); |
| 67 | }); |
| 68 | |
| 69 | test('historical changelog HTML paths retain their canonical redirects', async () => { |
| 70 | for (const pathname of ['/docs/changelog', '/docs/changelog/1900/01/01']) { |
| 71 | const response = await fetchNoRedirect(pathname, { headers: { Accept: 'text/html' } }); |
| 72 | expect(response.status).toBe(308); |
| 73 | expect(new URL(response.headers.get('location')!, response.url).pathname).toBe('/reference/changelog'); |
| 74 | } |
| 75 | }); |
| 76 | |
| 77 | test('HTML-only reference changelog does not advertise unsupported Markdown', async () => { |
| 78 | const response = await fetchPage('/reference/changelog'); |
| 79 | expect(response.status).toBe(200); |
| 80 | expect(markdownAlternates(await response.text())).toEqual([]); |
| 81 | }); |
| 82 | }); |
| 83 | |
| 84 | describe('Negotiated HTTP cache variance', () => { |
| 85 | for (const accept of ['text/html', 'text/markdown', 'text/plain']) { |
| 86 | test(`${accept} responses retain Vary: Accept`, async () => { |
| 87 | const response = await fetchPage('/docs/quickstart', { headers: { Accept: accept } }); |
| 88 | expect(response.status).toBe(200); |
| 89 | expect(response.headers.get('content-type')).toContain(accept === 'text/html' ? 'text/html' : 'text/markdown'); |
| 90 | const vary = (response.headers.get('vary') ?? '').toLowerCase().split(',').map(value => value.trim()); |
| 91 | expect(vary).toContain('accept'); |
| 92 | if (accept === 'text/html') { |
| 93 | expect(vary).toContain('rsc'); |
| 94 | expect(vary).toContain('next-router-state-tree'); |
| 95 | } |
| 96 | }); |
| 97 | } |
| 98 | |
| 99 | test('HTML Fern redirects vary on Accept without changing their destination', async () => { |
| 100 | const response = await fetchNoRedirect('/reference/api-reference/files/post-files-upload-request', { |
| 101 | headers: { Accept: 'text/html' }, |
| 102 | }); |
| 103 | expect(response.status).toBe(301); |
| 104 | expect(new URL(response.headers.get('location')!, response.url).pathname).toBe('/reference/api-reference/files/postFilesUploadRequest'); |
| 105 | expect((response.headers.get('vary') ?? '').toLowerCase().split(',').map(value => value.trim())).toContain('accept'); |
| 106 | }); |
| 107 | }); |