|
| 1 | +import { beforeAll, beforeEach, describe, expect, mock, test } from 'bun:test' |
| 2 | + |
| 3 | +import type { Logger } from '@codebuff/common/types/contracts/logger' |
| 4 | +import type { getComposioToolsForUser as GetComposioToolsForUser } from '../composio' |
| 5 | + |
| 6 | +let getComposioToolsForUser: typeof GetComposioToolsForUser |
| 7 | + |
| 8 | +let createSession: ReturnType<typeof mock> |
| 9 | +let useSession: ReturnType<typeof mock> |
| 10 | +let getRawToolRouterSessionTools: ReturnType<typeof mock> |
| 11 | + |
| 12 | +beforeAll(async () => { |
| 13 | + mock.module('server-only', () => ({})) |
| 14 | + mock.module('@codebuff/internal/env', () => ({ |
| 15 | + env: { COMPOSIO_API_KEY: 'test-composio-api-key' }, |
| 16 | + })) |
| 17 | + mock.module('@composio/core', () => ({ |
| 18 | + Composio: class { |
| 19 | + tools = { |
| 20 | + getRawToolRouterSessionTools, |
| 21 | + } |
| 22 | + |
| 23 | + create = createSession |
| 24 | + use = useSession |
| 25 | + }, |
| 26 | + })) |
| 27 | + ;({ getComposioToolsForUser } = await import('../composio')) |
| 28 | +}) |
| 29 | + |
| 30 | +describe('getComposioToolsForUser', () => { |
| 31 | + let logger: Logger |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + logger = { |
| 35 | + error: mock(() => {}), |
| 36 | + warn: mock(() => {}), |
| 37 | + info: mock(() => {}), |
| 38 | + debug: mock(() => {}), |
| 39 | + } |
| 40 | + createSession = mock(async () => ({ sessionId: 'fresh-session' })) |
| 41 | + useSession = mock(async () => ({ sessionId: 'stored-session' })) |
| 42 | + getRawToolRouterSessionTools = mock(async () => [ |
| 43 | + { |
| 44 | + slug: 'COMPOSIO_SEARCH_TOOLS', |
| 45 | + inputParameters: { type: 'object', properties: {} }, |
| 46 | + description: 'Search tools', |
| 47 | + }, |
| 48 | + ]) |
| 49 | + }) |
| 50 | + |
| 51 | + function makeDb(storedSessionId: string | null) { |
| 52 | + const findFirst = mock(async () => |
| 53 | + storedSessionId |
| 54 | + ? { |
| 55 | + user_id: 'user-123', |
| 56 | + session_id: storedSessionId, |
| 57 | + created_at: new Date(), |
| 58 | + updated_at: new Date(), |
| 59 | + } |
| 60 | + : null, |
| 61 | + ) |
| 62 | + const onConflictDoUpdate = mock(async () => undefined) |
| 63 | + const values = mock(() => ({ onConflictDoUpdate })) |
| 64 | + const whereDelete = mock(async () => undefined) |
| 65 | + |
| 66 | + return { |
| 67 | + db: { |
| 68 | + query: { |
| 69 | + composioSession: { |
| 70 | + findFirst, |
| 71 | + }, |
| 72 | + }, |
| 73 | + insert: mock(() => ({ values })), |
| 74 | + delete: mock(() => ({ where: whereDelete })), |
| 75 | + } as any, |
| 76 | + findFirst, |
| 77 | + onConflictDoUpdate, |
| 78 | + values, |
| 79 | + whereDelete, |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + test('replaces a stored session when Composio can no longer rehydrate it', async () => { |
| 84 | + const notFound = Object.assign(new Error('Composio session not found'), { |
| 85 | + status: 404, |
| 86 | + }) |
| 87 | + useSession = mock(async () => { |
| 88 | + throw notFound |
| 89 | + }) |
| 90 | + const { db, whereDelete, values } = makeDb('stored-session') |
| 91 | + |
| 92 | + const result = await getComposioToolsForUser({ |
| 93 | + db, |
| 94 | + userId: 'user-123', |
| 95 | + logger, |
| 96 | + }) |
| 97 | + |
| 98 | + expect(result).toEqual({ |
| 99 | + sessionId: 'fresh-session', |
| 100 | + tools: [ |
| 101 | + { |
| 102 | + toolName: 'COMPOSIO_SEARCH_TOOLS', |
| 103 | + inputSchema: { type: 'object', properties: {} }, |
| 104 | + description: 'Search tools', |
| 105 | + }, |
| 106 | + ], |
| 107 | + }) |
| 108 | + expect(useSession).toHaveBeenCalledWith('stored-session') |
| 109 | + expect(whereDelete).toHaveBeenCalledTimes(1) |
| 110 | + expect(createSession).toHaveBeenCalledWith('user-123') |
| 111 | + expect(values).toHaveBeenCalledWith({ |
| 112 | + user_id: 'user-123', |
| 113 | + session_id: 'fresh-session', |
| 114 | + }) |
| 115 | + }) |
| 116 | + |
| 117 | + test('keeps the stored session row when rehydration fails transiently', async () => { |
| 118 | + const transientError = Object.assign(new Error('Composio unavailable'), { |
| 119 | + status: 502, |
| 120 | + }) |
| 121 | + useSession = mock(async () => { |
| 122 | + throw transientError |
| 123 | + }) |
| 124 | + const { db, whereDelete } = makeDb('stored-session') |
| 125 | + |
| 126 | + await expect( |
| 127 | + getComposioToolsForUser({ |
| 128 | + db, |
| 129 | + userId: 'user-123', |
| 130 | + logger, |
| 131 | + }), |
| 132 | + ).rejects.toThrow('Composio unavailable') |
| 133 | + |
| 134 | + expect(whereDelete).not.toHaveBeenCalled() |
| 135 | + expect(createSession).not.toHaveBeenCalled() |
| 136 | + }) |
| 137 | +}) |
0 commit comments