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, { |
| 3 | APIUserAbortError, |
| 4 | NotFoundError, |
| 5 | PermissionDeniedError, |
| 6 | } from '@composio/client'; |
| 7 | import { SessionConfigs } from '../../src/models/SessionConfigs'; |
| 8 | import { telemetry } from '../../src/telemetry/Telemetry'; |
| 9 | import { ValidationError } from '../../src/errors/ValidationErrors'; |
| 10 | import { ComposioRequestCancelledError } from '../../src/errors/SDKErrors'; |
| 11 | import logger from '../../src/utils/logger'; |
| 12 | |
| 13 | vi.mock('../../src/telemetry/Telemetry', () => ({ |
| 14 | telemetry: { |
| 15 | instrument: vi.fn(), |
| 16 | }, |
| 17 | })); |
| 18 | |
| 19 | const createMockClient = () => ({ |
| 20 | baseURL: 'https://api.composio.dev', |
| 21 | apiKey: 'test-api-key', |
| 22 | sessionConfigs: { |
| 23 | list: vi.fn(), |
| 24 | retrieve: vi.fn(), |
| 25 | }, |
| 26 | }); |
| 27 | |
| 28 | const rawSummary = { |
| 29 | id: 'sc_1', |
| 30 | name: 'Daily digest', |
| 31 | archived: false, |
| 32 | created_at: '2026-01-01T00:00:00.000Z', |
| 33 | updated_at: '2026-01-02T00:00:00.000Z', |
| 34 | }; |
| 35 | const summary = { |
| 36 | id: 'sc_1', |
| 37 | name: 'Daily digest', |
| 38 | archived: false, |
| 39 | createdAt: '2026-01-01T00:00:00.000Z', |
| 40 | updatedAt: '2026-01-02T00:00:00.000Z', |
| 41 | }; |
| 42 | |
| 43 | describe('SessionConfigs', () => { |
| 44 | let sessionConfigs: SessionConfigs; |
| 45 | let mockClient: ReturnType<typeof createMockClient>; |
| 46 | |
| 47 | beforeEach(() => { |
| 48 | vi.clearAllMocks(); |
| 49 | mockClient = createMockClient(); |
| 50 | sessionConfigs = new SessionConfigs(mockClient as unknown as ComposioClient); |
| 51 | }); |
| 52 | |
| 53 | it('instruments the class with telemetry', () => { |
| 54 | expect(telemetry.instrument).toHaveBeenCalledWith(sessionConfigs, 'SessionConfigs'); |
| 55 | }); |
| 56 | |
| 57 | describe('list', () => { |
| 58 | it('sends an empty query when called without params', async () => { |
| 59 | mockClient.sessionConfigs.list.mockResolvedValue({ items: [], next_cursor: null }); |
| 60 | |
| 61 | const result = await sessionConfigs.list(); |
| 62 | |
| 63 | expect(mockClient.sessionConfigs.list).toHaveBeenCalledWith({}, undefined); |
| 64 | expect(result).toEqual({ items: [], nextCursor: null }); |
| 65 | }); |
| 66 | |
| 67 | it('forwards archived, limit and cursor unchanged', async () => { |
| 68 | mockClient.sessionConfigs.list.mockResolvedValue({ items: [], next_cursor: null }); |
| 69 | const signal = new AbortController().signal; |
| 70 | |
| 71 | await sessionConfigs.list({ archived: true, limit: 50, cursor: 'c1' }, { signal }); |
| 72 | |
| 73 | expect(mockClient.sessionConfigs.list).toHaveBeenCalledWith( |
| 74 | { archived: true, limit: 50, cursor: 'c1' }, |
| 75 | { signal } |
| 76 | ); |
| 77 | }); |
| 78 | |
| 79 | it('drops the removed search option and warns', async () => { |
| 80 | mockClient.sessionConfigs.list.mockResolvedValue({ items: [], next_cursor: null }); |
| 81 | const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => {}); |
| 82 | |
| 83 | await sessionConfigs.list({ search: 'Daily', limit: 50 }); |
| 84 | |
| 85 | expect(mockClient.sessionConfigs.list).toHaveBeenCalledWith({ limit: 50 }, undefined); |
| 86 | expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('search')); |
| 87 | warnSpy.mockRestore(); |
| 88 | }); |
| 89 | |
| 90 | it('forwards a limit above the backend cap without clamping', async () => { |
| 91 | mockClient.sessionConfigs.list.mockResolvedValue({ items: [], next_cursor: null }); |
| 92 | |
| 93 | await sessionConfigs.list({ limit: 500 }); |
| 94 | |
| 95 | expect(mockClient.sessionConfigs.list).toHaveBeenCalledWith({ limit: 500 }, undefined); |
| 96 | }); |
| 97 | |
| 98 | it('camelCases each item and the next cursor, keeping ISO strings unchanged', async () => { |
| 99 | mockClient.sessionConfigs.list.mockResolvedValue({ |
| 100 | items: [rawSummary, { ...rawSummary, id: 'sc_2', archived: true }], |
| 101 | next_cursor: 'c2', |
| 102 | }); |
| 103 | |
| 104 | const result = await sessionConfigs.list(); |
| 105 | |
| 106 | expect(result).toEqual({ |
| 107 | items: [summary, { ...summary, id: 'sc_2', archived: true }], |
| 108 | nextCursor: 'c2', |
| 109 | }); |
| 110 | }); |
| 111 | |
| 112 | it('returns a null next cursor on the last page', async () => { |
| 113 | mockClient.sessionConfigs.list.mockResolvedValue({ |
| 114 | items: [rawSummary], |
| 115 | next_cursor: null, |
| 116 | }); |
| 117 | |
| 118 | const result = await sessionConfigs.list({ cursor: 'c2' }); |
| 119 | |
| 120 | expect(result.nextCursor).toBeNull(); |
| 121 | }); |
| 122 | |
| 123 | it('throws a ValidationError for a non-numeric limit and makes no call', async () => { |
| 124 | await expect( |
| 125 | sessionConfigs.list({ limit: 'ten' as unknown as number }) |
| 126 | ).rejects.toBeInstanceOf(ValidationError); |
| 127 | expect(mockClient.sessionConfigs.list).not.toHaveBeenCalled(); |
| 128 | }); |
| 129 | |
| 130 | it('propagates a 403 from the client unchanged', async () => { |
| 131 | const error = new PermissionDeniedError(403, undefined, 'Forbidden', {}); |
| 132 | mockClient.sessionConfigs.list.mockRejectedValue(error); |
| 133 | |
| 134 | await expect(sessionConfigs.list()).rejects.toBe(error); |
| 135 | }); |
| 136 | |
| 137 | it('reports a cancelled request when the signal is aborted', async () => { |
| 138 | const controller = new AbortController(); |
| 139 | controller.abort(); |
| 140 | mockClient.sessionConfigs.list.mockRejectedValue(new APIUserAbortError()); |
| 141 | |
| 142 | await expect(sessionConfigs.list({}, { signal: controller.signal })).rejects.toBeInstanceOf( |
| 143 | ComposioRequestCancelledError |
| 144 | ); |
| 145 | }); |
| 146 | }); |
| 147 | |
| 148 | describe('get', () => { |
| 149 | const rawConfig = { |
| 150 | ...rawSummary, |
| 151 | description: null, |
| 152 | config: { |
| 153 | toolkits: { enable: ['github', 'google_calendar'] }, |
| 154 | tools: { |
| 155 | github: { enable: ['GITHUB_GET_REPO'] }, |
| 156 | google_calendar: { tags: { enable: ['readOnlyHint'], disable: ['destructiveHint'] } }, |
| 157 | }, |
| 158 | tags: { disable: ['destructiveHint'] }, |
| 159 | }, |
| 160 | }; |
| 161 | |
| 162 | it('retrieves a config by id and camelCases only the metadata fields', async () => { |
| 163 | mockClient.sessionConfigs.retrieve.mockResolvedValue(rawConfig); |
| 164 | |
| 165 | const result = await sessionConfigs.get('sc_1'); |
| 166 | |
| 167 | expect(mockClient.sessionConfigs.retrieve).toHaveBeenCalledWith('sc_1', undefined); |
| 168 | expect(result).toEqual({ |
| 169 | ...summary, |
| 170 | description: null, |
| 171 | config: rawConfig.config, |
| 172 | }); |
| 173 | }); |
| 174 | |
| 175 | it('keeps toolkit slug keys and tag names exactly as sent', async () => { |
| 176 | mockClient.sessionConfigs.retrieve.mockResolvedValue(rawConfig); |
| 177 | |
| 178 | const result = await sessionConfigs.get('sc_1'); |
| 179 | |
| 180 | expect(Object.keys(result.config.tools ?? {})).toEqual(['github', 'google_calendar']); |
| 181 | expect(result.config.tools?.google_calendar).toEqual({ |
| 182 | tags: { enable: ['readOnlyHint'], disable: ['destructiveHint'] }, |
| 183 | }); |
| 184 | expect(result.config.tags).toEqual({ disable: ['destructiveHint'] }); |
| 185 | }); |
| 186 | |
| 187 | it('does not invent tools or tags for a toolkit-only policy', async () => { |
| 188 | mockClient.sessionConfigs.retrieve.mockResolvedValue({ |
| 189 | ...rawConfig, |
| 190 | description: 'Read-only access', |
| 191 | config: { toolkits: { disable: ['gmail'] } }, |
| 192 | }); |
| 193 | |
| 194 | const result = await sessionConfigs.get('sc_1'); |
| 195 | |
| 196 | expect(result.description).toBe('Read-only access'); |
| 197 | expect(result.config).toEqual({ toolkits: { disable: ['gmail'] } }); |
| 198 | expect('tools' in result.config).toBe(false); |
| 199 | expect('tags' in result.config).toBe(false); |
| 200 | }); |
| 201 | |
| 202 | it('keeps the require_approval lists of a stored policy', async () => { |
| 203 | const config = { |
| 204 | toolkits: { require_approval: ['gmail'] }, |
| 205 | tools: { |
| 206 | github: { enable: ['GITHUB_GET_REPO'], require_approval: ['GITHUB_GET_REPO'] }, |
| 207 | slack: { require_approval: ['SLACK_SEND_MESSAGE'] }, |
| 208 | google_calendar: { tags: { require_approval: ['destructiveHint'] } }, |
| 209 | }, |
| 210 | tags: { enable: ['readOnlyHint'], require_approval: ['destructiveHint'] }, |
| 211 | }; |
| 212 | mockClient.sessionConfigs.retrieve.mockResolvedValue({ ...rawConfig, config }); |
| 213 | |
| 214 | const result = await sessionConfigs.get('sc_1'); |
| 215 | |
| 216 | expect(result.config).toEqual(config); |
| 217 | }); |
| 218 | |
| 219 | it.each(['', ' '])('rejects the id %j with a ValidationError and makes no call', async id => { |
| 220 | await expect(sessionConfigs.get(id)).rejects.toBeInstanceOf(ValidationError); |
| 221 | expect(mockClient.sessionConfigs.retrieve).not.toHaveBeenCalled(); |
| 222 | }); |
| 223 | |
| 224 | it('propagates a 404 from the client unchanged', async () => { |
| 225 | const error = new NotFoundError(404, undefined, 'Not found', {}); |
| 226 | mockClient.sessionConfigs.retrieve.mockRejectedValue(error); |
| 227 | |
| 228 | await expect(sessionConfigs.get('sc_missing')).rejects.toBe(error); |
| 229 | }); |
| 230 | |
| 231 | it('reports a cancelled request when the signal is aborted', async () => { |
| 232 | const controller = new AbortController(); |
| 233 | controller.abort(); |
| 234 | mockClient.sessionConfigs.retrieve.mockRejectedValue(new APIUserAbortError()); |
| 235 | |
| 236 | await expect( |
| 237 | sessionConfigs.get('sc_1', { signal: controller.signal }) |
| 238 | ).rejects.toBeInstanceOf(ComposioRequestCancelledError); |
| 239 | }); |
| 240 | }); |
| 241 | }); |