@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 78 lines 4.83 KB
Raw
1 import { describe, expect, test, mock } from 'bun:test';
2 import { renderToStaticMarkup } from 'react-dom/server';
3 import { readFileSync } from 'node:fs';
4 import { filterToolkits, withInstantEligibility } from '../../lib/instant-toolkit-filter';
5 import { fetchInstantToolkitSlugs } from '../../lib/instant-toolkit-api';
6 import { TOOLKIT_PAGE_SIZE } from '../../lib/composio-api';
7 import { InstantBolt } from '../../components/toolkits/instant-bolt';
8 import { InstantBadge } from '../../components/toolkits/instant-badge';
9
10 const toolkits = [
11 { slug: 'alpha', name: 'Alpha search', instant: { supported: true } },
12 { slug: 'beta', name: 'Beta search', instant: { supported: false } },
13 { slug: 'gamma', name: 'Gamma' },
14 ].map(toolkit => ({ ...toolkit, logo: null, category: null, toolCount: 1, triggerCount: 0 }));
15
16 describe('Instant catalog filter', () => {
17 test('strict eligibility intersects name/slug search; clearing restores all', () => {
18 expect(filterToolkits(toolkits, '', true).map(t => t.slug)).toEqual(['alpha']);
19 expect(filterToolkits(toolkits, ' SEARCH ', true).map(t => t.slug)).toEqual(['alpha']);
20 expect(filterToolkits(toolkits, 'beta', true)).toEqual([]);
21 expect(filterToolkits(toolkits, 'GAMMA', false).map(t => t.slug)).toEqual(['gamma']);
22 expect(filterToolkits(toolkits, '', false)).toEqual(toolkits);
23 });
24
25 test('live eligibility updates only existing public snapshot rows, replacing stale support', () => {
26 const merged = withInstantEligibility(toolkits, ['GAMMA', 'not-in-snapshot']);
27 expect(filterToolkits(merged, '', true).map(t => t.slug)).toEqual(['gamma']);
28 expect(merged).toHaveLength(toolkits.length);
29 });
30
31 test('the card is a toggle, guide is separate, popular rows hide during filtering', () => {
32 const source = readFileSync(new URL('../../components/toolkits/toolkits-landing.tsx', import.meta.url), 'utf8');
33 expect(source).toContain('aria-pressed={instantOnly}');
34 expect(source).toContain('href="/docs/instant-tools"');
35 expect(source).toContain('setSearch(\'\'); setInstantOnly(false)');
36 expect(source).toContain('!instantOnly && !deferredSearch');
37 expect(source).toContain('Instant toolkit availability could not be loaded.');
38 });
39
40 test('uses the exact dashboard Nucleo bolt geometry, with neutral outline badges', () => {
41 const outline = renderToStaticMarkup(<InstantBolt />);
42 const filled = renderToStaticMarkup(<InstantBolt filled />);
43 expect(outline).toContain('viewBox="0 0 18 18"');
44 expect(outline).toContain('M14.7505 7.25H9.49905');
45 expect(outline).toContain('stroke-width="1.5"');
46 expect(filled).toContain('M15.6449 7.0522C15.474');
47 expect(filled).not.toContain('stroke-width');
48 expect(renderToStaticMarkup(<InstantBadge />)).toContain('M14.7505 7.25H9.49905');
49 });
50 });
51
52 describe('live Instant eligibility', () => {
53 const item = (slug: string, supported = true) => ({ slug, type: 'native', enabled: true, instant: { supported } });
54 const fakeFetch = (fn: (input: Parameters<typeof fetch>[0], init?: RequestInit) => Promise<Response>) => Object.assign(mock(fn), { preconnect: fetch.preconnect });
55
56 test('follows cursors, deduplicates and only accepts explicit support', async () => {
57 const fetcher = fakeFetch(async input => String(input).includes('cursor=next')
58 ? Response.json({ items: [item('ALPHA'), item('beta'), item('no', false)], next_cursor: null })
59 : Response.json({ items: [item('alpha'), { slug: 'null', instant: null }, { slug: 'missing' }], next_cursor: 'next' }));
60 expect(await fetchInstantToolkitSlugs('test-key', fetcher)).toEqual(['alpha', 'beta']);
61 expect(fetcher).toHaveBeenCalledTimes(2);
62 expect(new URL(String(fetcher.mock.calls[0][0])).pathname).toBe('/api/v3.1/toolkits');
63 expect(fetcher.mock.calls.map(([input]) => new URL(String(input)).searchParams.get('limit'))).toEqual([String(TOOLKIT_PAGE_SIZE), String(TOOLKIT_PAGE_SIZE)]);
64 expect(new Headers(fetcher.mock.calls[0][1]?.headers).get('x-api-key')).toBe('test-key');
65 });
66
67 test('partial responses, missing eligibility, malformed data and repeated cursors fail rather than imply no results', async () => {
68 for (const payload of [{ items: [{ slug: 'alpha' }] }, { items: [{ slug: 'alpha', instant: { supported: 'true' } }] }, { items: [item('alpha')], next_cursor: 'loop' }]) {
69 await expect(fetchInstantToolkitSlugs('test-key', fakeFetch(async () => Response.json(payload)))).rejects.toThrow();
70 }
71 let calls = 0;
72 await expect(fetchInstantToolkitSlugs('test-key', fakeFetch(async () => ++calls === 1 ? Response.json({ items: [item('alpha')], next_cursor: 'next' }) : new Response('', { status: 503 })))).rejects.toThrow();
73 });
74
75 test('explicit all-false metadata is a genuine empty result', async () => {
76 expect(await fetchInstantToolkitSlugs('test-key', fakeFetch(async () => Response.json({ items: [item('alpha', false)] })))).toEqual([]);
77 });
78 });