@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
tsx 395 lines 15.9 KB
Raw
1 import { describe, expect, test } from 'bun:test';
2 import { readFileSync } from 'node:fs';
3 import { join } from 'node:path';
4 import { renderToStaticMarkup } from 'react-dom/server';
5 import {
6 getFeaturedKnowledgeLinks,
7 getKnowledgeByProductArea,
8 getKnowledgeByToolkit,
9 getKnowledgeToolkitSummaries,
10 } from '@/lib/knowledge/catalog';
11 import { PRODUCT_AREAS } from '@/lib/knowledge/taxonomy';
12 import { getPublishedKbGuides } from '@/lib/kb/repository';
13 import { getAlgoliaSearchDocuments } from '@/lib/search-index';
14 import { BrowseResults } from '@/components/kb/browse-results';
15 import * as browseResultsModule from '@/components/kb/browse-results';
16 import { ToolkitGrid } from '@/components/kb/toolkit-grid';
17 import { KnowledgeHub } from '@/components/kb/knowledge-hub';
18 import { KbArticleShell } from '@/components/kb/kb-article-shell';
19 import type { KnowledgeLink } from '@/lib/knowledge/catalog';
20 import KnowledgeToolkitsPage from '@/app/(home)/kb/toolkits/page';
21 import { getToolkitKnowledgeHref } from '@/lib/knowledge/toolkit-routing';
22
23 function source(path: string): string {
24 return readFileSync(join(import.meta.dir, '../..', path), 'utf8');
25 }
26
27 describe('knowledge browse pages', () => {
28 test('defines a valid page for every stable product area', async () => {
29 const stableAreas = PRODUCT_AREAS.filter((area) => area.defaultBrowse);
30 expect(stableAreas).toHaveLength(5);
31 for (const area of stableAreas) {
32 expect((await getKnowledgeByProductArea(area.slug)).length).toBeGreaterThan(0);
33 }
34
35 const topicRoute = source('app/(home)/kb/topic/[slug]/page.tsx');
36 expect(topicRoute).toContain('isProductAreaSlug');
37 expect(topicRoute).toContain('notFound()');
38 });
39
40 // Asserted against the route source rather than by invoking the page. Importing
41 // an App Router page pulls in `next/navigation`, which is an opaque CJS
42 // re-export (`module.exports = require('./dist/client/components/navigation')`);
43 // bun has to execute Next's client internals to discover its named exports, and
44 // that fails on CI Linux with "Export named 'notFound' not found". A test that
45 // cannot run in CI guards nothing, so this pins the same contract — the short
46 // slug redirects permanently to the canonical topic — without the fragile import.
47 test('permanently redirects legacy product-area URLs before rejecting unknown topics', () => {
48 const topicRoute = source('app/(home)/kb/topic/[slug]/page.tsx');
49 expect(topicRoute).toContain('getProductAreaRedirect');
50 expect(topicRoute.indexOf('getProductAreaRedirect(slug)'))
51 .toBeLessThan(topicRoute.indexOf('isProductAreaSlug(slug)'));
52 expect(topicRoute.indexOf('permanentRedirect(redirect)'))
53 .toBeLessThan(topicRoute.indexOf('notFound()'));
54 expect(PRODUCT_AREAS.map((area) => area.slug)).toContain(
55 'authentication-and-connected-accounts'
56 );
57 expect(PRODUCT_AREAS.map((area) => area.slug)).not.toContain('authentication');
58 });
59
60 test('migrated auth guides stay on the current docs origin without duplicate toolkit cards', async () => {
61 const links = await getKnowledgeByToolkit('github');
62 const toolkitLinks = links.filter(link => link.href.endsWith('/toolkits/github'));
63 expect(toolkitLinks).toHaveLength(1);
64 expect(toolkitLinks[0].href).toBe('/toolkits/github');
65 const html = renderToStaticMarkup(<BrowseResults links={toolkitLinks} variant="topic" />);
66 expect(html).toContain('href="/toolkits/github"');
67 expect(html).not.toContain('target="_blank"');
68 });
69
70 test('combines OAuth and verified support answers on toolkit pages', async () => {
71 const toolkitGuides = getPublishedKbGuides().flatMap(guide =>
72 guide.sources.flatMap(source => {
73 const toolkit = source.sourcePath.match(/^toolkits\/([^/]+)\/public\.md$/)?.[1];
74 return toolkit ? [{ toolkit, guide }] : [];
75 }),
76 );
77 let combined: Awaited<ReturnType<typeof getKnowledgeByToolkit>> | null = null;
78 for (const { toolkit } of toolkitGuides) {
79 const links = await getKnowledgeByToolkit(toolkit);
80 if (links.some(item => item.sourceType === 'oauth-guide')) {
81 combined = links;
82 break;
83 }
84 }
85 expect(combined).not.toBeNull();
86 expect(combined!.some(item => item.sourceType === 'kb')).toBe(true);
87 expect(combined!.some(item => item.sourceType === 'oauth-guide')).toBe(true);
88
89 const toolkitRoute = source('app/(home)/kb/toolkit/[slug]/page.tsx');
90 expect(toolkitRoute).toContain('notFound()');
91 });
92
93 test('aggregates every indexed toolkit guide under its canonical toolkit slug', async () => {
94 const kbRecords = (await getAlgoliaSearchDocuments())
95 .filter(record => record.source_type === 'kb');
96 expect(kbRecords.some(record => record.toolkit_slugs.length > 0)).toBe(true);
97 for (const record of kbRecords) {
98 for (const toolkit of record.toolkit_slugs) {
99 expect((await getKnowledgeByToolkit(toolkit)).map(item => item.href)).toContain(
100 record.canonical_url,
101 );
102 }
103 }
104 });
105
106 test('indexes every published guide even when it is not assigned to a support topic', async () => {
107 const indexedGuideUrls = new Set(
108 (await getAlgoliaSearchDocuments())
109 .filter((record) => record.source_type === 'kb')
110 .map((record) => record.canonical_url),
111 );
112 const publishedGuideUrls = getPublishedKbGuides()
113 .map((guide) => `/kb/guide/${guide.slug}`);
114
115 expect(publishedGuideUrls.filter((url) => !indexedGuideUrls.has(url))).toEqual([]);
116 });
117
118 test('keeps featured links canonical and present in the corpus', async () => {
119 const links = await getFeaturedKnowledgeLinks();
120 expect(links.length).toBeGreaterThan(0);
121 expect(new Set(links.map(item => item.href)).size).toBe(links.length);
122 expect(links.every(item => item.href.startsWith('/kb/guide/') || item.href.startsWith('/toolkits/')))
123 .toBe(true);
124 });
125
126 test('renders headings only for source groups with matches', () => {
127 const html = renderToStaticMarkup(<BrowseResults links={[{
128 title: 'Authentication',
129 description: 'Understand connected accounts.',
130 href: '/docs/authentication',
131 sourceType: 'docs',
132 sourceLabel: 'Docs',
133 productAreas: ['authentication-and-connected-accounts'],
134 toolkitSlugs: [],
135 lastVerifiedAt: null,
136 }]} />);
137
138 expect(html).toContain('Docs');
139 expect(html).not.toContain('OAuth guides');
140 expect(html).not.toContain('Changelog');
141 expect(html).not.toContain('target="_blank"');
142 });
143
144 test('opens only external browse results in a new tab', () => {
145 const html = renderToStaticMarkup(<BrowseResults links={[{
146 title: 'External answer',
147 description: 'A resource hosted outside the docs site.',
148 href: 'https://example.com/answer',
149 sourceType: 'docs',
150 sourceLabel: 'Docs',
151 productAreas: [],
152 toolkitSlugs: [],
153 lastVerifiedAt: null,
154 }]} />);
155
156 expect(html).toContain('target="_blank"');
157 expect(html).toContain('rel="noopener noreferrer"');
158 expect(html).toContain('opens in a new tab');
159 });
160
161 test('keeps large browse groups searchable and progressively disclosed', () => {
162 const links: KnowledgeLink[] = Array.from({ length: 20 }, (_, index) => ({
163 title: `Answer ${index + 1}`,
164 description: `Troubleshooting guidance ${index + 1}.`,
165 href: `/kb/guide/answer-${index + 1}`,
166 sourceType: 'kb',
167 sourceLabel: 'Knowledge Base',
168 productAreas: ['authentication-and-connected-accounts'],
169 toolkitSlugs: [],
170 lastVerifiedAt: null,
171 }));
172 const html = renderToStaticMarkup(<BrowseResults links={links} />);
173
174 expect(html).toContain('name="knowledge-browse-search"');
175 expect(html).toContain('Answer 12');
176 expect(html).not.toContain('Answer 13');
177 expect(html).toContain('Show 8 more');
178 });
179
180 test('renders a lightweight support-answer heading without duplicate controls', () => {
181 const knowledgeBaseLinks: KnowledgeLink[] = Array.from({ length: 20 }, (_, index) => ({
182 title: `Answer ${index + 1}`,
183 description: `Troubleshooting guidance ${index + 1}.`,
184 href: `/kb/guide/answer-${index + 1}`,
185 sourceType: 'kb',
186 sourceLabel: 'Knowledge Base',
187 productAreas: ['authentication-and-connected-accounts'],
188 toolkitSlugs: [],
189 lastVerifiedAt: null,
190 }));
191 const oauthLink: KnowledgeLink = {
192 title: 'Configure GitHub OAuth',
193 description: 'Create credentials for GitHub.',
194 href: 'https://composio.dev/auth/github',
195 sourceType: 'oauth-guide',
196 sourceLabel: 'OAuth',
197 productAreas: ['authentication-and-connected-accounts'],
198 toolkitSlugs: ['github'],
199 lastVerifiedAt: null,
200 };
201 const html = renderToStaticMarkup(
202 <BrowseResults links={[...knowledgeBaseLinks, oauthLink]} variant="topic" />,
203 );
204
205 expect(html).not.toContain('name="knowledge-browse-search"');
206 expect(html).not.toContain('Knowledge Base answers');
207 expect(html).toContain('Support answers');
208 expect(html).toContain('>20</span>');
209 expect(html).toContain('Answer 1');
210 expect(html).not.toContain('Answer 13');
211 expect(html).toContain('Show 8 more');
212 expect(html).toContain('OAuth guides');
213 expect((html.match(/target="_blank"/g) ?? []).length).toBe(1);
214 expect((html.match(/rel="noopener noreferrer"/g) ?? []).length).toBe(1);
215 expect((html.match(/opens in a new tab/g) ?? []).length).toBe(1);
216 });
217
218 test('renders toolkit knowledge as one labeled card collection', () => {
219 const links: KnowledgeLink[] = [
220 {
221 title: 'HubSpot',
222 description: 'Public support knowledge for HubSpot.',
223 href: '/kb/guide/toolkits-hubspot',
224 sourceType: 'kb',
225 sourceLabel: 'Knowledge Base',
226 productAreas: ['authentication-and-connected-accounts'],
227 toolkitSlugs: ['hubspot'],
228 lastVerifiedAt: null,
229 },
230 {
231 title: 'How to create OAuth2 credentials for HubSpot',
232 description: 'Create a HubSpot OAuth2 application.',
233 href: 'https://composio.dev/auth/hubspot',
234 sourceType: 'oauth-guide',
235 sourceLabel: 'OAuth guide',
236 productAreas: ['authentication-and-connected-accounts'],
237 toolkitSlugs: ['hubspot'],
238 lastVerifiedAt: null,
239 },
240 {
241 title: 'HubSpot toolkit',
242 description: 'HubSpot actions and triggers.',
243 href: '/toolkits/hubspot',
244 sourceType: 'toolkit',
245 sourceLabel: 'Toolkit',
246 productAreas: [],
247 toolkitSlugs: ['hubspot'],
248 lastVerifiedAt: null,
249 },
250 ];
251 const html = renderToStaticMarkup(
252 <BrowseResults links={links} variant="toolkit" toolkitName="HubSpot" />,
253 );
254
255 expect(html).toContain('aria-label="Toolkit knowledge sources"');
256 expect(html).toContain('Support answer');
257 expect(html).toContain('OAuth guide');
258 expect(html).toContain('Toolkit');
259 expect(html).toContain('HubSpot support &amp; troubleshooting');
260 expect(html).toContain('HubSpot tools reference');
261 expect(html).toContain('How to create OAuth2 credentials for HubSpot');
262 expect(html).toContain('Setup and troubleshooting guidance for HubSpot in Composio.');
263 expect(html).not.toContain('Public support knowledge for HubSpot.');
264 expect(html).toContain('Create a HubSpot OAuth2 application.');
265 expect(html).not.toContain('>HubSpot toolkit</h3>');
266 expect(html).not.toContain('<h2');
267 expect((html.match(/target="_blank"/g) ?? []).length).toBe(1);
268 expect((html.match(/rel="noopener noreferrer"/g) ?? []).length).toBe(1);
269 });
270
271 test('starts guide navigation with support topics', () => {
272 const html = renderToStaticMarkup(
273 <KbArticleShell><main>Guide content</main></KbArticleShell>,
274 );
275
276 expect(html).not.toContain('Knowledge Base home');
277 expect(html).toContain('Support topics');
278 expect(html).toContain('Browse all toolkits');
279 });
280
281 test('filters browse links by their title and description', () => {
282 const filterKnowledgeLinks = (
283 browseResultsModule as {
284 filterKnowledgeLinks?: (links: KnowledgeLink[], query: string) => KnowledgeLink[];
285 }
286 ).filterKnowledgeLinks;
287 const links: KnowledgeLink[] = [
288 {
289 title: 'Reconnect GitHub',
290 description: 'Restore a revoked OAuth connection.',
291 href: '/kb/guide/github',
292 sourceType: 'kb',
293 sourceLabel: 'Knowledge Base',
294 productAreas: ['authentication-and-connected-accounts'],
295 toolkitSlugs: ['github'],
296 lastVerifiedAt: null,
297 },
298 {
299 title: 'Retry an action',
300 description: 'Handle transient execution failures.',
301 href: '/kb/guide/retries',
302 sourceType: 'kb',
303 sourceLabel: 'Knowledge Base',
304 productAreas: ['tools-actions-and-execution'],
305 toolkitSlugs: [],
306 lastVerifiedAt: null,
307 },
308 ];
309
310 expect(typeof filterKnowledgeLinks).toBe('function');
311 expect(filterKnowledgeLinks?.(links, 'oauth github')).toEqual([links[0]]);
312 });
313
314 test('builds a searchable toolkit grid from existing toolkit data', async () => {
315 const summaries = await getKnowledgeToolkitSummaries();
316 const html = renderToStaticMarkup(<ToolkitGrid toolkits={summaries.slice(0, 65)} />);
317
318 expect(summaries.length).toBeGreaterThan(50);
319 expect(new Set(summaries.map(toolkit => toolkit.slug)).size).toBe(summaries.length);
320 expect(html).toContain('Search toolkits');
321 expect(html).toContain('name="toolkit-search"');
322 expect(html).toContain('/kb/toolkit/');
323 expect(html).toContain(getToolkitKnowledgeHref(summaries[59]!));
324 expect(html).not.toContain(getToolkitKnowledgeHref(summaries[60]!));
325 expect(html).toContain('Show 5 more');
326 });
327
328 test('labels toolkit card counts as resources', () => {
329 const html = renderToStaticMarkup(<ToolkitGrid toolkits={[
330 {
331 slug: 'single',
332 name: 'Single',
333 logo: 'https://example.com/logo.png',
334 category: null,
335 knowledgeCount: 1,
336 },
337 {
338 slug: 'multiple',
339 name: 'Multiple',
340 logo: null,
341 category: null,
342 knowledgeCount: 2,
343 },
344 ]} />);
345
346 expect(html).toContain('1 resource');
347 expect(html).toContain('2 resources');
348 expect(html).not.toContain('public page');
349 expect(html).toContain('width="28"');
350 expect(html).toContain('height="28"');
351 expect(html).toContain('href="/toolkits/single"');
352 expect(html).toContain('href="/kb/toolkit/multiple"');
353 });
354
355 test('keeps agent-readable KB browse copy aligned with the public UI', () => {
356 const route = source('app/llms.mdx/[[...slug]]/route.ts');
357
358 expect(route).toContain('Search support knowledge');
359 expect(route).toContain('canonical public support answers and toolkit-specific fixes');
360 expect(route).toContain('knowledgeCount} resource');
361 expect(route).not.toContain('knowledgeCount} public page');
362 expect(route).not.toContain('across every indexed Composio source');
363 });
364
365 test('renders the toolkit directory without a redundant browse eyebrow', async () => {
366 const html = renderToStaticMarkup(await KnowledgeToolkitsPage());
367
368 expect(html).toContain('Toolkit knowledge');
369 expect(html).not.toContain('Browse by provider');
370 });
371
372 test('preserves the canonical toolkit commonality order for browsing', async () => {
373 const summaries = await getKnowledgeToolkitSummaries();
374
375 expect(summaries.slice(0, 6).map((toolkit) => toolkit.slug)).toEqual([
376 'gmail',
377 'composio',
378 'github',
379 'googlecalendar',
380 'notion',
381 'googlesheets',
382 ]);
383 });
384
385 test('keeps toolkit catalog pages out of support topics while retaining toolkit browse', async () => {
386 const toolkitRecords = (await getAlgoliaSearchDocuments())
387 .filter((record) => record.source_type === 'toolkit');
388 const html = renderToStaticMarkup(await KnowledgeHub());
389
390 expect(toolkitRecords.length).toBeGreaterThan(0);
391 expect(toolkitRecords.every((record) => record.product_areas.length === 0)).toBe(true);
392 expect(html).toContain('href="/kb/toolkits"');
393 expect(html).not.toContain('href="/kb/topic/composio-for-you"');
394 });
395 });