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 type { |
| 2 | LogCreateToolExecutionResponse, |
| 3 | LogRetrieveToolExecutionResponse, |
| 4 | } from '@composio/client/resources/logs'; |
| 5 | import { describe, expect, it } from 'vitest'; |
| 6 | import { |
| 7 | formatTriggerLogInfo, |
| 8 | formatToolLogInfo, |
| 9 | formatToolLogsTable, |
| 10 | formatTriggerLogsTable, |
| 11 | } from 'src/commands/logs/format'; |
| 12 | import { |
| 13 | decodeTriggerLogRecord, |
| 14 | type TriggerLogRecord, |
| 15 | } from 'src/commands/logs/trigger-log-record'; |
| 16 | import { green, red } from 'src/ui/colors'; |
| 17 | |
| 18 | type ToolLog = LogCreateToolExecutionResponse['logs'][number]; |
| 19 | type ToolLogDetailed = LogRetrieveToolExecutionResponse; |
| 20 | |
| 21 | const makeTriggerLog = (createdAt: string | number): TriggerLogRecord => |
| 22 | decodeTriggerLogRecord({ |
| 23 | id: 'trigger_log_1', |
| 24 | clientId: 'client_1', |
| 25 | type: 'trigger', |
| 26 | createdAt, |
| 27 | status: 'success', |
| 28 | appName: 'gmail', |
| 29 | meta: { |
| 30 | triggerId: 'trigger_123', |
| 31 | triggerNanoId: 'ti_123', |
| 32 | triggerName: 'NEW_GMAIL_MESSAGE', |
| 33 | }, |
| 34 | entityId: 'entity_123', |
| 35 | connectionId: 'conn_123', |
| 36 | }); |
| 37 | |
| 38 | const makeToolLog = ( |
| 39 | createdAt: number | string | null, |
| 40 | overrides: { |
| 41 | status?: string; |
| 42 | connectedAccountId?: string | null; |
| 43 | metrics?: Record<string, unknown>; |
| 44 | } = {} |
| 45 | ): ToolLog => |
| 46 | ({ |
| 47 | id: 'tool_log_1', |
| 48 | type: 'tool.execution', |
| 49 | level: 'info', |
| 50 | parent: null, |
| 51 | timestamp: typeof createdAt === 'number' ? new Date(createdAt).toISOString() : createdAt, |
| 52 | status: overrides.status ?? 'success', |
| 53 | metadata: { |
| 54 | toolkit: { slug: 'gmail' }, |
| 55 | tool: { slug: 'GMAIL_SEND_EMAIL' }, |
| 56 | connected_account_id: |
| 57 | overrides.connectedAccountId === undefined ? 'conn_123' : overrides.connectedAccountId, |
| 58 | }, |
| 59 | metrics: overrides.metrics ?? { duration_ms: 20 }, |
| 60 | }) as ToolLog; |
| 61 | |
| 62 | const makeToolLogDetailed = ( |
| 63 | overrides: Partial<{ |
| 64 | startTime: number | null; |
| 65 | endTime: number | null; |
| 66 | metrics: Record<string, unknown>; |
| 67 | }> = {} |
| 68 | ): ToolLogDetailed => |
| 69 | ({ |
| 70 | id: 'tool_log_1', |
| 71 | status: 'success', |
| 72 | metadata: {}, |
| 73 | timings: { |
| 74 | start_time: overrides.startTime === undefined ? 0 : overrides.startTime, |
| 75 | end_time: overrides.endTime === undefined ? 0 : overrides.endTime, |
| 76 | }, |
| 77 | metrics: overrides.metrics ?? { duration_ms: 20 }, |
| 78 | data: {}, |
| 79 | }) as unknown as ToolLogDetailed; |
| 80 | |
| 81 | describe('formatTriggerLogsTable', () => { |
| 82 | it('[Given] createdAt is epoch number [Then] it formats without crashing', () => { |
| 83 | const output = formatTriggerLogsTable([makeTriggerLog(0)]); |
| 84 | expect(output).toContain('1970-01-01T00:00:00.000Z'); |
| 85 | expect(output).toContain('ti_123'); |
| 86 | expect(output).toContain('NEW_GMAIL_MESSAGE'); |
| 87 | }); |
| 88 | |
| 89 | it('[Given] createdAt is string [Then] it preserves previous behavior', () => { |
| 90 | const output = formatTriggerLogsTable([makeTriggerLog('2026-02-24T12:34:56.000Z')]); |
| 91 | expect(output).toContain('2026-02-24T12:34:56.000Z'); |
| 92 | }); |
| 93 | |
| 94 | it('[Given] rendering table [Then] it uses Trigger Id and Toolkit headers', () => { |
| 95 | const output = stripAnsi(formatTriggerLogsTable([makeTriggerLog(0)])); |
| 96 | const [headerLine] = output.split('\n'); |
| 97 | |
| 98 | expect(headerLine).toContain('Log Id'); |
| 99 | expect(headerLine).toContain('Trigger Id'); |
| 100 | expect(headerLine).toContain('Toolkit'); |
| 101 | expect(headerLine).toContain('Trigger'); |
| 102 | expect(headerLine).toContain('User Id'); |
| 103 | expect(headerLine).toContain('Connected Account Id'); |
| 104 | expect(headerLine).not.toContain('Status'); |
| 105 | expect(headerLine).not.toContain('App'); |
| 106 | }); |
| 107 | }); |
| 108 | |
| 109 | describe('formatToolLogsTable', () => { |
| 110 | it('[Given] createdAt is null [Then] it renders dash instead of epoch zero', () => { |
| 111 | const output = formatToolLogsTable([makeToolLog(null)]); |
| 112 | expect(output).toContain('tool_log_1'); |
| 113 | expect(output).not.toContain('1970-01-01T00:00:00.000Z'); |
| 114 | }); |
| 115 | |
| 116 | it('[Given] rendering table [Then] it shows Created At before Log Id', () => { |
| 117 | const output = stripAnsi(formatToolLogsTable([makeToolLog(0)])); |
| 118 | const [headerLine] = output.split('\n'); |
| 119 | |
| 120 | expect(headerLine.indexOf('Created At')).toBeLessThan(headerLine.indexOf('Log Id')); |
| 121 | }); |
| 122 | |
| 123 | it('[Given] rendering table [Then] it uses Toolkit and Tool headers', () => { |
| 124 | const output = stripAnsi(formatToolLogsTable([makeToolLog(0)])); |
| 125 | const [headerLine] = output.split('\n'); |
| 126 | |
| 127 | expect(headerLine).toContain('Toolkit'); |
| 128 | expect(headerLine).toContain('Tool'); |
| 129 | expect(headerLine).not.toContain('App'); |
| 130 | expect(headerLine).not.toContain('Action'); |
| 131 | }); |
| 132 | |
| 133 | it('[Given] success and failure statuses [Then] it colors them green and red', () => { |
| 134 | const output = formatToolLogsTable([ |
| 135 | makeToolLog(0, { status: 'success' }), |
| 136 | makeToolLog(0, { status: 'failed' }), |
| 137 | ]); |
| 138 | |
| 139 | expect(output).toContain(green('success '.padEnd(8))); |
| 140 | expect(output).toContain(red('failed '.padEnd(8))); |
| 141 | }); |
| 142 | |
| 143 | it.each([ |
| 144 | { metrics: { duration_ms: 20 }, expected: '20ms' }, |
| 145 | { metrics: {}, expected: '-' }, |
| 146 | ])('[Given] metrics $metrics [Then] Exec Time is $expected', ({ metrics, expected }) => { |
| 147 | const [header, row] = stripAnsi(formatToolLogsTable([makeToolLog(0, { metrics })])).split('\n'); |
| 148 | const column = header.indexOf('Exec Time'); |
| 149 | |
| 150 | expect(row.slice(column, column + 12).trim()).toBe(expected); |
| 151 | }); |
| 152 | |
| 153 | it('[Given] null connected account [Then] it renders dash', () => { |
| 154 | const output = stripAnsi(formatToolLogsTable([makeToolLog(0, { connectedAccountId: null })])); |
| 155 | const [, rowLine] = output.split('\n'); |
| 156 | |
| 157 | expect(rowLine).toMatch(/\s-\s*$/); |
| 158 | }); |
| 159 | }); |
| 160 | |
| 161 | const stripAnsi = (value: string): string => value.replace(/\u001B\[[0-9;]*m/g, ''); |
| 162 | |
| 163 | describe('formatToolLogInfo', () => { |
| 164 | it('[Given] nullable app/connection/steps [Then] it renders fallback values', () => { |
| 165 | const output = formatToolLogInfo(makeToolLogDetailed()); |
| 166 | const plain = output.replace(/\x1b\[[0-9;]*m/g, ''); |
| 167 | expect(plain).toContain('toolkit: -'); |
| 168 | expect(plain).toContain('Connection ID: -'); |
| 169 | expect(plain).toContain('Entity: -'); |
| 170 | expect(plain).toContain('Version: -'); |
| 171 | }); |
| 172 | |
| 173 | it('[Given] null start/end time [Then] it renders dashes for timestamps', () => { |
| 174 | const output = formatToolLogInfo(makeToolLogDetailed({ startTime: null, endTime: null })); |
| 175 | const plain = output.replace(/\x1b\[[0-9;]*m/g, ''); |
| 176 | expect(plain).toContain('Start Time: -'); |
| 177 | expect(plain).toContain('End Time: -'); |
| 178 | expect(plain).not.toContain('1970-01-01T00:00:00.000Z'); |
| 179 | }); |
| 180 | |
| 181 | it('[Given] no recorded duration [Then] it renders a dash', () => { |
| 182 | const plain = stripAnsi(formatToolLogInfo(makeToolLogDetailed({ metrics: {} }))); |
| 183 | expect(plain).toContain('Total Duration: -'); |
| 184 | }); |
| 185 | }); |
| 186 | |
| 187 | describe('formatTriggerLogInfo', () => { |
| 188 | it('[Given] trigger detailed log [Then] it renders key fields', () => { |
| 189 | const output = formatTriggerLogInfo( |
| 190 | decodeTriggerLogRecord({ |
| 191 | id: 'trigger_log_1', |
| 192 | createdAt: 0, |
| 193 | status: 'success', |
| 194 | appName: 'gmail', |
| 195 | meta: { |
| 196 | triggerId: 'trigger_123', |
| 197 | triggerNanoId: 'ti_123', |
| 198 | triggerName: 'NEW_GMAIL_MESSAGE', |
| 199 | }, |
| 200 | entityId: 'user_123', |
| 201 | connectionId: 'conn_123', |
| 202 | }) |
| 203 | ); |
| 204 | |
| 205 | expect(output).toContain('logId:'); |
| 206 | expect(output).toContain('trigger_log_1'); |
| 207 | expect(output).toContain('triggerId:'); |
| 208 | expect(output).toContain('trigger_123'); |
| 209 | expect(output).toContain('triggerNanoId:'); |
| 210 | expect(output).toContain('ti_123'); |
| 211 | expect(output).toContain('trigger:'); |
| 212 | expect(output).toContain('NEW_GMAIL_MESSAGE'); |
| 213 | expect(output).toContain('toolkit:'); |
| 214 | expect(output).toContain('gmail'); |
| 215 | }); |
| 216 | |
| 217 | it('[Given] missing fields [Then] it renders fallback dashes', () => { |
| 218 | const output = formatTriggerLogInfo(decodeTriggerLogRecord({})); |
| 219 | const plain = output.replace(/\x1b\[[0-9;]*m/g, ''); |
| 220 | expect(plain).toContain('logId: -'); |
| 221 | expect(plain).toContain('triggerId: -'); |
| 222 | expect(plain).toContain('triggerNanoId: -'); |
| 223 | expect(plain).toContain('trigger: -'); |
| 224 | expect(plain).toContain('Created At: -'); |
| 225 | }); |
| 226 | |
| 227 | it('[Given] retrieve response wrapped under log [Then] it unwraps and renders values', () => { |
| 228 | const output = formatTriggerLogInfo( |
| 229 | decodeTriggerLogRecord({ |
| 230 | log: { |
| 231 | id: 'log_QNzKGStH-ruD', |
| 232 | status: 'info', |
| 233 | appName: 'gmail', |
| 234 | createdAt: '2026-02-25T22:01:22.865Z', |
| 235 | entityId: 'pg-test-37ee710c-d5be-4775-91f2-a8e06b937d9b', |
| 236 | connectionId: 'ca_zkX9njO68E8A', |
| 237 | meta: { |
| 238 | triggerId: '77ac1dbf-6db0-4039-8dbe-e903b3f2057e', |
| 239 | triggerNanoId: 'ti_-nGUzD9N6JNf', |
| 240 | triggerName: 'GMAIL_NEW_GMAIL_MESSAGE', |
| 241 | }, |
| 242 | }, |
| 243 | }) |
| 244 | ); |
| 245 | |
| 246 | expect(output).toContain('log_QNzKGStH-ruD'); |
| 247 | expect(output).toContain('77ac1dbf-6db0-4039-8dbe-e903b3f2057e'); |
| 248 | expect(output).toContain('ti_-nGUzD9N6JNf'); |
| 249 | expect(output).toContain('GMAIL_NEW_GMAIL_MESSAGE'); |
| 250 | expect(output).toContain('gmail'); |
| 251 | }); |
| 252 | }); |