Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | import ComposioClient from '@composio/client'; |
| 3 | import { Experimental } from '../../src/models/Experimental'; |
| 4 | import { Usage } from '../../src/models/Usage'; |
| 5 | import { telemetry } from '../../src/telemetry/Telemetry'; |
| 6 | import { ValidationError } from '../../src/errors/ValidationErrors'; |
| 7 | |
| 8 | vi.mock('../../src/telemetry/Telemetry', () => ({ |
| 9 | telemetry: { |
| 10 | instrument: vi.fn(), |
| 11 | }, |
| 12 | })); |
| 13 | |
| 14 | const createMockClient = () => ({ |
| 15 | baseURL: 'https://api.composio.dev', |
| 16 | apiKey: 'test-api-key', |
| 17 | connectedAccounts: { |
| 18 | patch: vi.fn(), |
| 19 | }, |
| 20 | project: { |
| 21 | usage: { |
| 22 | retrieveSummary: vi.fn(), |
| 23 | retrieve: vi.fn(), |
| 24 | }, |
| 25 | }, |
| 26 | }); |
| 27 | |
| 28 | describe('Experimental.usage', () => { |
| 29 | let experimental: Experimental; |
| 30 | let mockClient: ReturnType<typeof createMockClient>; |
| 31 | |
| 32 | beforeEach(() => { |
| 33 | vi.clearAllMocks(); |
| 34 | mockClient = createMockClient(); |
| 35 | experimental = new Experimental(mockClient as unknown as ComposioClient); |
| 36 | }); |
| 37 | |
| 38 | it('mounts usage on the experimental namespace and instruments it', () => { |
| 39 | expect(experimental.usage).toBeInstanceOf(Usage); |
| 40 | expect(telemetry.instrument).toHaveBeenCalledWith(experimental.usage, 'Usage'); |
| 41 | expect(telemetry.instrument).toHaveBeenCalledWith(experimental, 'Experimental'); |
| 42 | }); |
| 43 | |
| 44 | describe('summary', () => { |
| 45 | it('sends an empty body when called without params', async () => { |
| 46 | mockClient.project.usage.retrieveSummary.mockResolvedValue({ |
| 47 | entities: {}, |
| 48 | instant_charge: '0', |
| 49 | }); |
| 50 | |
| 51 | const result = await experimental.usage.summary(); |
| 52 | |
| 53 | expect(mockClient.project.usage.retrieveSummary).toHaveBeenCalledWith( |
| 54 | { from: undefined, to: undefined, entity_types: undefined, filters: undefined }, |
| 55 | undefined |
| 56 | ); |
| 57 | expect(result).toEqual({ entities: {}, instantCharge: '0' }); |
| 58 | }); |
| 59 | |
| 60 | it('maps params to the wire and transforms the response', async () => { |
| 61 | mockClient.project.usage.retrieveSummary.mockResolvedValue({ |
| 62 | entities: { |
| 63 | tool_calls: { unit: 'call', total_quantity: '42', event_count: 42 }, |
| 64 | sessions: { unit: 'session', total_quantity: '3', event_count: 3 }, |
| 65 | }, |
| 66 | instant_charge: '0.025', |
| 67 | }); |
| 68 | const signal = new AbortController().signal; |
| 69 | |
| 70 | const result = await experimental.usage.summary( |
| 71 | { |
| 72 | from: 1, |
| 73 | to: 2, |
| 74 | entityTypes: ['tool_calls', 'sessions'], |
| 75 | filters: { userId: ['user_1'], sessionId: null }, |
| 76 | }, |
| 77 | { signal } |
| 78 | ); |
| 79 | |
| 80 | expect(mockClient.project.usage.retrieveSummary).toHaveBeenCalledWith( |
| 81 | { |
| 82 | from: 1, |
| 83 | to: 2, |
| 84 | entity_types: ['tool_calls', 'sessions'], |
| 85 | filters: { user_id: ['user_1'], session_id: null }, |
| 86 | }, |
| 87 | { signal } |
| 88 | ); |
| 89 | expect(result).toEqual({ |
| 90 | entities: { |
| 91 | tool_calls: { unit: 'call', totalQuantity: '42', eventCount: 42 }, |
| 92 | sessions: { unit: 'session', totalQuantity: '3', eventCount: 3 }, |
| 93 | }, |
| 94 | instantCharge: '0.025', |
| 95 | }); |
| 96 | }); |
| 97 | |
| 98 | it('throws a ValidationError for invalid params', async () => { |
| 99 | await expect( |
| 100 | experimental.usage.summary({ from: 'yesterday' as unknown as number }) |
| 101 | ).rejects.toThrow(ValidationError); |
| 102 | expect(mockClient.project.usage.retrieveSummary).not.toHaveBeenCalled(); |
| 103 | }); |
| 104 | }); |
| 105 | |
| 106 | describe('breakdown', () => { |
| 107 | it('passes the entity type positionally and maps params', async () => { |
| 108 | mockClient.project.usage.retrieve.mockResolvedValue({ |
| 109 | entity_type: 'tool_calls', |
| 110 | unit: 'call', |
| 111 | total_quantity: '10', |
| 112 | event_count: 10, |
| 113 | groups: [{ key: 'GITHUB_GET_REPO', total_quantity: '10', event_count: 10 }], |
| 114 | }); |
| 115 | |
| 116 | const result = await experimental.usage.breakdown('tool_calls', { |
| 117 | groupBy: 'tool_slug', |
| 118 | orderBy: 'event_count', |
| 119 | orderDirection: 'asc', |
| 120 | limit: 5, |
| 121 | filters: { sessionId: ['trs_1'] }, |
| 122 | }); |
| 123 | |
| 124 | expect(mockClient.project.usage.retrieve).toHaveBeenCalledWith( |
| 125 | 'tool_calls', |
| 126 | { |
| 127 | from: undefined, |
| 128 | to: undefined, |
| 129 | group_by: 'tool_slug', |
| 130 | order_by: 'event_count', |
| 131 | order_direction: 'asc', |
| 132 | limit: 5, |
| 133 | filters: { user_id: undefined, session_id: ['trs_1'] }, |
| 134 | }, |
| 135 | undefined |
| 136 | ); |
| 137 | expect(result).toEqual({ |
| 138 | entityType: 'tool_calls', |
| 139 | unit: 'call', |
| 140 | totalQuantity: '10', |
| 141 | eventCount: 10, |
| 142 | groups: [{ key: 'GITHUB_GET_REPO', totalQuantity: '10', eventCount: 10 }], |
| 143 | }); |
| 144 | }); |
| 145 | |
| 146 | it('throws a ValidationError for an unknown orderBy', async () => { |
| 147 | await expect( |
| 148 | experimental.usage.breakdown('tool_calls', { orderBy: 'name' as 'key' }) |
| 149 | ).rejects.toThrow(ValidationError); |
| 150 | expect(mockClient.project.usage.retrieve).not.toHaveBeenCalled(); |
| 151 | }); |
| 152 | }); |
| 153 | }); |