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, mock, test } from 'bun:test'; |
| 2 | import { createToolkitResolver } from '../../lib/toolkit-resolution'; |
| 3 | import type { Toolkit } from '../../types/toolkit'; |
| 4 | |
| 5 | const snapshotToolkit: Toolkit = { |
| 6 | slug: 'github', |
| 7 | name: 'GitHub', |
| 8 | logo: 'https://logos.composio.dev/api/github', |
| 9 | description: 'GitHub snapshot description', |
| 10 | category: 'developer tools', |
| 11 | authSchemes: ['OAUTH2'], |
| 12 | composioManagedAuthSchemes: ['OAUTH2'], |
| 13 | toolCount: 10, |
| 14 | triggerCount: 2, |
| 15 | version: '20260817_00', |
| 16 | tools: [], |
| 17 | triggers: [], |
| 18 | }; |
| 19 | |
| 20 | const liveToolkit: Toolkit = { |
| 21 | ...snapshotToolkit, |
| 22 | slug: 'live-only', |
| 23 | name: 'Live Only', |
| 24 | description: 'Production fallback description', |
| 25 | version: null, |
| 26 | }; |
| 27 | |
| 28 | function createTestResolver() { |
| 29 | const getToolkitBySlug = mock(async (_slug: string): Promise<Toolkit | null> => null); |
| 30 | const fetchToolkitFromProduction = mock( |
| 31 | async (_slug: string): Promise<Toolkit | null> => null |
| 32 | ); |
| 33 | const resolveToolkit = createToolkitResolver({ |
| 34 | getToolkitBySlug, |
| 35 | fetchToolkitFromProduction, |
| 36 | }); |
| 37 | |
| 38 | return { resolveToolkit, getToolkitBySlug, fetchToolkitFromProduction }; |
| 39 | } |
| 40 | |
| 41 | describe('resolveToolkit', () => { |
| 42 | test('returns a snapshot hit without calling production', async () => { |
| 43 | const { resolveToolkit, getToolkitBySlug, fetchToolkitFromProduction } = createTestResolver(); |
| 44 | getToolkitBySlug.mockResolvedValue(snapshotToolkit); |
| 45 | |
| 46 | expect(await resolveToolkit('github')).toEqual(snapshotToolkit); |
| 47 | expect(getToolkitBySlug).toHaveBeenCalledWith('github'); |
| 48 | expect(fetchToolkitFromProduction).not.toHaveBeenCalled(); |
| 49 | }); |
| 50 | |
| 51 | test('normalizes the slug before reading the snapshot', async () => { |
| 52 | const { resolveToolkit, getToolkitBySlug, fetchToolkitFromProduction } = createTestResolver(); |
| 53 | getToolkitBySlug.mockResolvedValue(snapshotToolkit); |
| 54 | |
| 55 | expect(await resolveToolkit('GitHub')).toEqual(snapshotToolkit); |
| 56 | expect(getToolkitBySlug).toHaveBeenCalledWith('github'); |
| 57 | expect(fetchToolkitFromProduction).not.toHaveBeenCalled(); |
| 58 | }); |
| 59 | |
| 60 | test('returns a production toolkit after a snapshot miss', async () => { |
| 61 | const { resolveToolkit, getToolkitBySlug, fetchToolkitFromProduction } = createTestResolver(); |
| 62 | getToolkitBySlug.mockResolvedValue(null); |
| 63 | fetchToolkitFromProduction.mockResolvedValue(liveToolkit); |
| 64 | |
| 65 | expect(await resolveToolkit('live-only')).toEqual(liveToolkit); |
| 66 | expect(fetchToolkitFromProduction).toHaveBeenCalledWith('live-only'); |
| 67 | }); |
| 68 | |
| 69 | test('returns null when both snapshot and production miss', async () => { |
| 70 | const { resolveToolkit, getToolkitBySlug, fetchToolkitFromProduction } = createTestResolver(); |
| 71 | getToolkitBySlug.mockResolvedValue(null); |
| 72 | fetchToolkitFromProduction.mockResolvedValue(null); |
| 73 | |
| 74 | expect(await resolveToolkit('__definitely-not-a-toolkit__')).toBeNull(); |
| 75 | }); |
| 76 | |
| 77 | test('rejects test_app without reading the snapshot or production', async () => { |
| 78 | const { resolveToolkit, getToolkitBySlug, fetchToolkitFromProduction } = createTestResolver(); |
| 79 | expect(await resolveToolkit('TEST_APP')).toBeNull(); |
| 80 | expect(getToolkitBySlug).not.toHaveBeenCalled(); |
| 81 | expect(fetchToolkitFromProduction).not.toHaveBeenCalled(); |
| 82 | }); |
| 83 | }); |