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 { getAllToolkitsSync } from '@/lib/toolkit-data'; |
| 3 | import { |
| 4 | getAuthGuideRegistry, |
| 5 | getAuthGuideSearchRecords, |
| 6 | validateAuthGuideUrls, |
| 7 | } from '@/lib/knowledge/auth-guides'; |
| 8 | import { |
| 9 | buildCompleteSearchReplacement, |
| 10 | replaceSearchDocuments, |
| 11 | } from '@/lib/knowledge/search-replacement'; |
| 12 | |
| 13 | function responseAt(url: string, status = 200): Response { |
| 14 | const response = new Response(null, { status }); |
| 15 | Object.defineProperty(response, 'url', { value: url }); |
| 16 | return response; |
| 17 | } |
| 18 | |
| 19 | describe('OAuth guide indexing', () => { |
| 20 | test('validates the current public inventory without pinning its contents', () => { |
| 21 | const entries = getAuthGuideRegistry(); |
| 22 | |
| 23 | expect(entries.length).toBeGreaterThan(0); |
| 24 | const toolkitSlugs = new Set(getAllToolkitsSync().map(toolkit => toolkit.slug)); |
| 25 | expect(entries.every(entry => toolkitSlugs.has(entry.toolkitSlug))).toBe(true); |
| 26 | expect(new Set(entries.map((entry) => entry.slug)).size).toBe(entries.length); |
| 27 | expect(new Set(entries.map((entry) => entry.canonicalUrl)).size).toBe(entries.length); |
| 28 | expect(entries.every((entry) => entry.canonicalUrl === `/toolkits/${entry.toolkitSlug}`)) |
| 29 | .toBe(true); |
| 30 | expect(entries.every((entry) => entry.title.trim() && entry.description.trim())) |
| 31 | .toBe(true); |
| 32 | }); |
| 33 | |
| 34 | test('resolves the legacy Google Apps alias to a real toolkit', () => { |
| 35 | expect(getAuthGuideRegistry().find(entry => entry.slug === 'googleapps')) |
| 36 | .toMatchObject({ toolkitSlug: 'googlesuper', canonicalUrl: '/toolkits/googlesuper' }); |
| 37 | }); |
| 38 | |
| 39 | test('creates one normalized OAuth search record per guide', () => { |
| 40 | const entries = getAuthGuideRegistry(); |
| 41 | const records = getAuthGuideSearchRecords(); |
| 42 | |
| 43 | expect(records).toHaveLength(entries.length); |
| 44 | expect(records.every((record) => record.source_type === 'oauth-guide')).toBe(true); |
| 45 | expect(records.every((record) => record.product_areas.includes('authentication-and-connected-accounts'))) |
| 46 | .toBe(true); |
| 47 | expect(records.every((record) => record.toolkit_slugs.length === 1)).toBe(true); |
| 48 | expect(records.map(record => record.canonical_url)) |
| 49 | .toEqual(entries.map(entry => entry.canonicalUrl)); |
| 50 | expect(records.every((record, index) => |
| 51 | record.toolkit_slugs[0] === entries[index]?.toolkitSlug && record.page_rank === 1_700)) |
| 52 | .toBe(true); |
| 53 | }); |
| 54 | |
| 55 | test('reports the URL when any external guide is unavailable', async () => { |
| 56 | const entries = getAuthGuideRegistry().slice(0, 2); |
| 57 | const fetchImpl = async (input: string | URL | Request) => { |
| 58 | const url = String(input); |
| 59 | return responseAt(url, url.endsWith('/gong') ? 503 : 200); |
| 60 | }; |
| 61 | |
| 62 | await expect(validateAuthGuideUrls(entries, fetchImpl as typeof fetch)) |
| 63 | .rejects.toThrow('https://docs.composio.dev/toolkits/gong'); |
| 64 | }); |
| 65 | |
| 66 | test('builds the full replacement only after every registered URL validates', async () => { |
| 67 | const entries = getAuthGuideRegistry(); |
| 68 | const visited: string[] = []; |
| 69 | const fetchImpl = async (input: string | URL | Request) => { |
| 70 | const url = String(input); |
| 71 | visited.push(url); |
| 72 | return responseAt(url); |
| 73 | }; |
| 74 | |
| 75 | const records = await buildCompleteSearchReplacement({ fetchImpl: fetchImpl as typeof fetch }); |
| 76 | |
| 77 | expect(visited).toEqual(entries.map(entry => `https://docs.composio.dev${entry.canonicalUrl}`)); |
| 78 | expect(records.filter((record) => record.source_type === 'oauth-guide')) |
| 79 | .toHaveLength(entries.length); |
| 80 | }); |
| 81 | |
| 82 | test('never calls Algolia replacement after external validation fails', async () => { |
| 83 | const [entry] = getAuthGuideRegistry(); |
| 84 | let replacementCalls = 0; |
| 85 | const client = { |
| 86 | async replaceAllObjects() { |
| 87 | replacementCalls += 1; |
| 88 | }, |
| 89 | }; |
| 90 | const fetchImpl = async (input: string | URL | Request) => { |
| 91 | const url = String(input); |
| 92 | return responseAt(url, url === `https://docs.composio.dev${entry?.canonicalUrl}` ? 503 : 200); |
| 93 | }; |
| 94 | |
| 95 | await expect(replaceSearchDocuments(client, 'docs_composio', { |
| 96 | fetchImpl: fetchImpl as typeof fetch, |
| 97 | })).rejects.toThrow(entry?.canonicalUrl); |
| 98 | expect(replacementCalls).toBe(0); |
| 99 | }); |
| 100 | |
| 101 | test('rejects a registered guide that redirects to a generic auth page', async () => { |
| 102 | const [entry] = getAuthGuideRegistry(); |
| 103 | const fetchImpl = async () => responseAt('https://docs.composio.dev/toolkits'); |
| 104 | |
| 105 | await expect(validateAuthGuideUrls([entry], fetchImpl as typeof fetch)) |
| 106 | .rejects.toThrow(entry.canonicalUrl); |
| 107 | }); |
| 108 | }); |