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, 04:45 UTC
| 1 | import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | import ComposioClient from '@composio/client'; |
| 3 | import { Logs } from '../../src/models/Logs'; |
| 4 | import { telemetry } from '../../src/telemetry/Telemetry'; |
| 5 | import { ValidationError } from '../../src/errors/ValidationErrors'; |
| 6 | |
| 7 | vi.mock('../../src/telemetry/Telemetry', () => ({ |
| 8 | telemetry: { |
| 9 | instrument: vi.fn(), |
| 10 | }, |
| 11 | })); |
| 12 | |
| 13 | const createMockClient = () => ({ |
| 14 | baseURL: 'https://api.composio.dev', |
| 15 | apiKey: 'test-api-key', |
| 16 | logs: { |
| 17 | createToolExecution: vi.fn(), |
| 18 | retrieveToolExecution: vi.fn(), |
| 19 | }, |
| 20 | }); |
| 21 | |
| 22 | const rawLog = { |
| 23 | id: 'log_1', |
| 24 | timestamp: '2026-01-01T00:00:00Z', |
| 25 | type: 'tool.execution', |
| 26 | status: 'success', |
| 27 | level: 'info', |
| 28 | message: 'ok', |
| 29 | metadata: { tool_slug: 'GITHUB_GET_REPO', instant_charge: '0.012' }, |
| 30 | metrics: { duration_ms: 12 }, |
| 31 | parent: { log_id: 'log_0', tool_slug: 'COMPOSIO_MULTI_EXECUTE_TOOL' }, |
| 32 | }; |
| 33 | const transformedLog = { |
| 34 | id: 'log_1', |
| 35 | timestamp: '2026-01-01T00:00:00Z', |
| 36 | type: 'tool.execution', |
| 37 | status: 'success', |
| 38 | level: 'info', |
| 39 | message: 'ok', |
| 40 | metadata: { tool_slug: 'GITHUB_GET_REPO', instant_charge: '0.012' }, |
| 41 | metrics: { duration_ms: 12 }, |
| 42 | parent: { logId: 'log_0', toolSlug: 'COMPOSIO_MULTI_EXECUTE_TOOL' }, |
| 43 | }; |
| 44 | |
| 45 | describe('Logs', () => { |
| 46 | let logs: Logs; |
| 47 | let mockClient: ReturnType<typeof createMockClient>; |
| 48 | |
| 49 | beforeEach(() => { |
| 50 | vi.clearAllMocks(); |
| 51 | mockClient = createMockClient(); |
| 52 | logs = new Logs(mockClient as unknown as ComposioClient); |
| 53 | }); |
| 54 | |
| 55 | it('instruments the class with telemetry', () => { |
| 56 | expect(telemetry.instrument).toHaveBeenCalledWith(logs, 'Logs'); |
| 57 | }); |
| 58 | |
| 59 | describe('search', () => { |
| 60 | it('sends an empty body when called without params', async () => { |
| 61 | mockClient.logs.createToolExecution.mockResolvedValue({ logs: [], next_cursor: null }); |
| 62 | |
| 63 | const result = await logs.search(); |
| 64 | |
| 65 | expect(mockClient.logs.createToolExecution).toHaveBeenCalledWith( |
| 66 | { limit: undefined, cursor: undefined, filters: undefined, time_range: undefined }, |
| 67 | undefined |
| 68 | ); |
| 69 | expect(result).toEqual({ logs: [], nextCursor: null }); |
| 70 | }); |
| 71 | |
| 72 | it('maps camelCase params to the wire and transforms the response', async () => { |
| 73 | mockClient.logs.createToolExecution.mockResolvedValue({ |
| 74 | logs: [rawLog, { ...rawLog, id: 'log_2', parent: null, message: undefined }], |
| 75 | next_cursor: 'cursor_2', |
| 76 | }); |
| 77 | const signal = new AbortController().signal; |
| 78 | |
| 79 | const result = await logs.search( |
| 80 | { |
| 81 | limit: 2, |
| 82 | cursor: 'cursor_1', |
| 83 | filters: [{ field: 'toolkit_slug', operator: '==', value: 'github' }], |
| 84 | timeRange: { from: 1, to: 2 }, |
| 85 | }, |
| 86 | { signal } |
| 87 | ); |
| 88 | |
| 89 | expect(mockClient.logs.createToolExecution).toHaveBeenCalledWith( |
| 90 | { |
| 91 | limit: 2, |
| 92 | cursor: 'cursor_1', |
| 93 | filters: [{ field: 'toolkit_slug', operator: '==', value: 'github' }], |
| 94 | time_range: { from: 1, to: 2 }, |
| 95 | }, |
| 96 | { signal } |
| 97 | ); |
| 98 | expect(result).toEqual({ |
| 99 | logs: [ |
| 100 | transformedLog, |
| 101 | { ...transformedLog, id: 'log_2', parent: null, message: undefined }, |
| 102 | ], |
| 103 | nextCursor: 'cursor_2', |
| 104 | }); |
| 105 | expect(result.logs[0]?.metadata.instant_charge).toBe('0.012'); |
| 106 | }); |
| 107 | |
| 108 | it('throws a ValidationError for an unknown filter field', async () => { |
| 109 | await expect( |
| 110 | logs.search({ |
| 111 | filters: [{ field: 'nope' as 'tool_slug', operator: '==', value: 'x' }], |
| 112 | }) |
| 113 | ).rejects.toThrow(ValidationError); |
| 114 | expect(mockClient.logs.createToolExecution).not.toHaveBeenCalled(); |
| 115 | }); |
| 116 | |
| 117 | it('rejects a non-string Instant charge in log metadata', async () => { |
| 118 | mockClient.logs.createToolExecution.mockResolvedValue({ |
| 119 | logs: [{ ...rawLog, metadata: { instant_charge: 0.012 } }], |
| 120 | next_cursor: null, |
| 121 | }); |
| 122 | |
| 123 | await expect(logs.search()).rejects.toThrow(); |
| 124 | }); |
| 125 | }); |
| 126 | |
| 127 | describe('get', () => { |
| 128 | it('retrieves a log by id and transforms the detail fields', async () => { |
| 129 | mockClient.logs.retrieveToolExecution.mockResolvedValue({ |
| 130 | ...rawLog, |
| 131 | timings: { start_time: 10, end_time: 22 }, |
| 132 | context: { session_id: 'trs_1', trace_id: 'trace_1', request_id: 'req_1' }, |
| 133 | source: { host: 'node', framework: 'vercel', language: 'typescript' }, |
| 134 | data: { input: { owner: 'composio' } }, |
| 135 | }); |
| 136 | |
| 137 | const result = await logs.get('log_1'); |
| 138 | |
| 139 | expect(mockClient.logs.retrieveToolExecution).toHaveBeenCalledWith('log_1', undefined); |
| 140 | expect(result).toEqual({ |
| 141 | ...transformedLog, |
| 142 | timings: { startTime: 10, endTime: 22 }, |
| 143 | context: { sessionId: 'trs_1', traceId: 'trace_1', requestId: 'req_1' }, |
| 144 | source: { host: 'node', framework: 'vercel', language: 'typescript' }, |
| 145 | data: { input: { owner: 'composio' } }, |
| 146 | }); |
| 147 | }); |
| 148 | |
| 149 | it('leaves timings undefined when the API omits them', async () => { |
| 150 | mockClient.logs.retrieveToolExecution.mockResolvedValue({ |
| 151 | ...rawLog, |
| 152 | context: {}, |
| 153 | source: {}, |
| 154 | data: {}, |
| 155 | }); |
| 156 | |
| 157 | const result = await logs.get('log_1'); |
| 158 | |
| 159 | expect(result.timings).toBeUndefined(); |
| 160 | expect(result.context).toEqual({}); |
| 161 | }); |
| 162 | |
| 163 | it('leaves the Instant charge absent for an uncharged call', async () => { |
| 164 | mockClient.logs.retrieveToolExecution.mockResolvedValue({ |
| 165 | ...rawLog, |
| 166 | metadata: { tool_slug: 'GITHUB_GET_REPO' }, |
| 167 | context: {}, |
| 168 | source: {}, |
| 169 | data: {}, |
| 170 | }); |
| 171 | |
| 172 | const result = await logs.get('log_1'); |
| 173 | |
| 174 | expect(result.metadata.instant_charge).toBeUndefined(); |
| 175 | }); |
| 176 | }); |
| 177 | }); |