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 assert from 'node:assert/strict'; |
| 2 | import { describe, expect, it } from '@effect/vitest'; |
| 3 | import { Effect, Option } from 'effect'; |
| 4 | import { |
| 5 | ConnectedAccountResolutionError, |
| 6 | formatConnectedAccountChoices, |
| 7 | groupCachedConnectedAccountsByToolkit, |
| 8 | listConnectedAccountsForToolkit, |
| 9 | resolveConnectedAccountForToolkit, |
| 10 | resolveConnectedAccountSelection, |
| 11 | resolveDefaultConnectedAccountsByToolkit, |
| 12 | } from 'src/services/connected-account-selection'; |
| 13 | import type { ConnectedAccountItem } from 'src/models/connected-accounts'; |
| 14 | import { stubComposioClient } from 'test/__utils__/services/stub-composio-client'; |
| 15 | import { TerminalUITest } from 'test/__utils__/services/terminal-ui-test'; |
| 16 | |
| 17 | const makeAccount = (overrides: Partial<ConnectedAccountItem>): ConnectedAccountItem => ({ |
| 18 | id: 'con_default', |
| 19 | alias: null, |
| 20 | word_id: null, |
| 21 | status: 'ACTIVE', |
| 22 | status_reason: null, |
| 23 | is_disabled: false, |
| 24 | user_id: 'default', |
| 25 | toolkit: { slug: 'gmail' }, |
| 26 | auth_config: { |
| 27 | id: 'ac_gmail', |
| 28 | auth_scheme: 'OAUTH2', |
| 29 | is_composio_managed: true, |
| 30 | is_disabled: false, |
| 31 | }, |
| 32 | created_at: '2026-01-01T00:00:00.000Z', |
| 33 | updated_at: '2026-01-01T00:00:00.000Z', |
| 34 | test_request_endpoint: '', |
| 35 | ...overrides, |
| 36 | }); |
| 37 | |
| 38 | describe('connected-account-selection', () => { |
| 39 | it('prefers alias=default when no selector is provided', () => { |
| 40 | const selected = resolveConnectedAccountSelection([ |
| 41 | makeAccount({ |
| 42 | id: 'con_newer', |
| 43 | updated_at: '2026-01-03T00:00:00.000Z', |
| 44 | }), |
| 45 | makeAccount({ |
| 46 | id: 'con_default_alias', |
| 47 | alias: 'default', |
| 48 | updated_at: '2026-01-02T00:00:00.000Z', |
| 49 | }), |
| 50 | ]); |
| 51 | |
| 52 | expect(selected?.id).toBe('con_default_alias'); |
| 53 | }); |
| 54 | |
| 55 | it('matches explicit selectors by alias, word_id, and id', () => { |
| 56 | const items = [ |
| 57 | makeAccount({ id: 'con_alpha', alias: 'work', word_id: 'castle' }), |
| 58 | makeAccount({ id: 'con_beta', alias: 'default', word_id: 'forest' }), |
| 59 | ]; |
| 60 | |
| 61 | expect(resolveConnectedAccountSelection(items, 'work')?.id).toBe('con_alpha'); |
| 62 | expect(resolveConnectedAccountSelection(items, 'castle')?.id).toBe('con_alpha'); |
| 63 | expect(resolveConnectedAccountSelection(items, 'con_beta')?.id).toBe('con_beta'); |
| 64 | }); |
| 65 | |
| 66 | it('builds default account mappings and grouped cache payloads', () => { |
| 67 | const items = [ |
| 68 | makeAccount({ id: 'con_default_gmail', alias: 'default' }), |
| 69 | makeAccount({ |
| 70 | id: 'con_alt_gmail', |
| 71 | alias: 'secondary', |
| 72 | word_id: 'castle', |
| 73 | updated_at: '2026-01-02T00:00:00.000Z', |
| 74 | }), |
| 75 | makeAccount({ |
| 76 | id: 'con_default_slack', |
| 77 | alias: 'default', |
| 78 | toolkit: { slug: 'slack' }, |
| 79 | }), |
| 80 | ]; |
| 81 | |
| 82 | expect(resolveDefaultConnectedAccountsByToolkit(items)).toEqual({ |
| 83 | gmail: 'con_default_gmail', |
| 84 | slack: 'con_default_slack', |
| 85 | }); |
| 86 | |
| 87 | expect(groupCachedConnectedAccountsByToolkit(items)).toMatchObject({ |
| 88 | gmail: [ |
| 89 | expect.objectContaining({ id: 'con_alt_gmail', alias: 'secondary', wordId: 'castle' }), |
| 90 | expect.objectContaining({ id: 'con_default_gmail', alias: 'default', wordId: null }), |
| 91 | ], |
| 92 | slack: [expect.objectContaining({ id: 'con_default_slack', alias: 'default', wordId: null })], |
| 93 | }); |
| 94 | }); |
| 95 | |
| 96 | it('formats available account choices for CLI error messages', () => { |
| 97 | expect( |
| 98 | formatConnectedAccountChoices([ |
| 99 | makeAccount({ id: 'con_default', alias: 'default', word_id: 'castle' }), |
| 100 | ]) |
| 101 | ).toEqual(['default / castle (con_default)']); |
| 102 | }); |
| 103 | }); |
| 104 | |
| 105 | // One `GET /connected_accounts` page as the server sends it. |
| 106 | const page = ( |
| 107 | items: ReadonlyArray<ConnectedAccountItem>, |
| 108 | counts: { readonly total_items: number; readonly next_cursor: string | null } = { |
| 109 | total_items: items.length, |
| 110 | next_cursor: null, |
| 111 | } |
| 112 | ) => |
| 113 | Response.json({ |
| 114 | items, |
| 115 | total_items: counts.total_items, |
| 116 | total_pages: 1, |
| 117 | current_page: 1, |
| 118 | next_cursor: counts.next_cursor, |
| 119 | }); |
| 120 | |
| 121 | // A transport that answers each request with the next page, in order. |
| 122 | const pagesInOrder = (...pages: ReadonlyArray<Response>) => { |
| 123 | const queue = [...pages]; |
| 124 | return () => { |
| 125 | const next = queue.shift(); |
| 126 | assert.ok(next, 'more requests than pages'); |
| 127 | return next; |
| 128 | }; |
| 129 | }; |
| 130 | |
| 131 | const toolkitSlugsQuery = (path: string) => |
| 132 | new URL(path, 'https://stub.invalid').searchParams.get('toolkit_slugs'); |
| 133 | |
| 134 | describe('listConnectedAccountsForToolkit', () => { |
| 135 | it.effect('stops at an empty terminal cursor without duplicating accounts', () => |
| 136 | Effect.gen(function* () { |
| 137 | const { client, requests } = stubComposioClient( |
| 138 | pagesInOrder( |
| 139 | page([makeAccount({ id: 'con_gmail' })], { total_items: 1, next_cursor: '' }), |
| 140 | page([makeAccount({ id: 'con_gmail' })]) |
| 141 | ) |
| 142 | ); |
| 143 | |
| 144 | const accounts = yield* listConnectedAccountsForToolkit({ |
| 145 | client, |
| 146 | userId: 'default', |
| 147 | toolkitSlug: 'gmail', |
| 148 | }); |
| 149 | |
| 150 | expect(accounts.map(item => item.id)).toEqual(['con_gmail']); |
| 151 | expect(requests).toHaveLength(1); |
| 152 | }) |
| 153 | ); |
| 154 | |
| 155 | it.effect('matches toolkit slugs the way the grouping helpers do, from the shared list', () => |
| 156 | Effect.gen(function* () { |
| 157 | const { client, requests } = stubComposioClient( |
| 158 | pagesInOrder( |
| 159 | page([ |
| 160 | makeAccount({ id: 'con_github', toolkit: { slug: 'GitHub ' } }), |
| 161 | makeAccount({ id: 'con_gmail', toolkit: { slug: 'gmail' } }), |
| 162 | ]) |
| 163 | ) |
| 164 | ); |
| 165 | |
| 166 | const accounts = yield* listConnectedAccountsForToolkit({ |
| 167 | client, |
| 168 | userId: 'default', |
| 169 | toolkitSlug: ' github', |
| 170 | }); |
| 171 | |
| 172 | expect(accounts.map(item => item.id)).toEqual(['con_github']); |
| 173 | expect(requests).toHaveLength(1); |
| 174 | expect(toolkitSlugsQuery(requests[0]!.path)).toBeNull(); |
| 175 | }) |
| 176 | ); |
| 177 | |
| 178 | it.effect('derives from a short page even when total_items counts more', () => |
| 179 | Effect.gen(function* () { |
| 180 | // A server that reports the all-status count must not push every call |
| 181 | // onto the filtered request. |
| 182 | const { client, requests } = stubComposioClient( |
| 183 | pagesInOrder( |
| 184 | page([makeAccount({ id: 'con_gmail', toolkit: { slug: 'gmail' } })], { |
| 185 | total_items: 7, |
| 186 | next_cursor: null, |
| 187 | }) |
| 188 | ) |
| 189 | ); |
| 190 | |
| 191 | const accounts = yield* listConnectedAccountsForToolkit({ |
| 192 | client, |
| 193 | userId: 'default', |
| 194 | toolkitSlug: 'gmail', |
| 195 | }); |
| 196 | |
| 197 | expect(accounts.map(item => item.id)).toEqual(['con_gmail']); |
| 198 | expect(requests).toHaveLength(1); |
| 199 | }) |
| 200 | ); |
| 201 | |
| 202 | it.effect('derives from every page of the shared list, following next_cursor', () => |
| 203 | Effect.gen(function* () { |
| 204 | // The server serves at most 50 accounts per page, so the toolkit's |
| 205 | // account can sit past the first page. |
| 206 | const firstPage = Array.from({ length: 50 }, (_, index) => |
| 207 | makeAccount({ id: `con_slack_${index}`, toolkit: { slug: 'slack' } }) |
| 208 | ); |
| 209 | const { client, requests } = stubComposioClient( |
| 210 | pagesInOrder( |
| 211 | page(firstPage, { total_items: 51, next_cursor: 'page-2' }), |
| 212 | page([makeAccount({ id: 'con_gmail', toolkit: { slug: 'gmail' } })]) |
| 213 | ) |
| 214 | ); |
| 215 | |
| 216 | const accounts = yield* listConnectedAccountsForToolkit({ |
| 217 | client, |
| 218 | userId: 'default', |
| 219 | toolkitSlug: 'gmail', |
| 220 | }); |
| 221 | |
| 222 | expect(accounts.map(item => item.id)).toEqual(['con_gmail']); |
| 223 | expect(requests).toHaveLength(2); |
| 224 | const query = (path: string) => new URL(path, 'https://stub.invalid').searchParams; |
| 225 | expect(query(requests[0]!.path).get('limit')).toBe('50'); |
| 226 | expect(query(requests[1]!.path).get('cursor')).toBe('page-2'); |
| 227 | expect(requests.map(request => toolkitSlugsQuery(request.path))).toEqual([null, null]); |
| 228 | }) |
| 229 | ); |
| 230 | }); |
| 231 | |
| 232 | describe('resolveConnectedAccountForToolkit', () => { |
| 233 | it.effect('reports a rejected account listing in the wording it has always used', () => |
| 234 | Effect.gen(function* () { |
| 235 | const body = { |
| 236 | error: { message: 'Invalid user id', code: 301, slug: 'Invalid_UserId', status: 400 }, |
| 237 | }; |
| 238 | const { client } = stubComposioClient(() => Response.json(body, { status: 400 })); |
| 239 | |
| 240 | const failure = yield* resolveConnectedAccountForToolkit({ |
| 241 | client, |
| 242 | toolkitSlug: 'gmail', |
| 243 | userId: 'default', |
| 244 | selector: Option.none(), |
| 245 | }).pipe(Effect.flip, Effect.provide(TerminalUITest)); |
| 246 | |
| 247 | expect(failure).toBeInstanceOf(ConnectedAccountResolutionError); |
| 248 | expect(failure.message).toBe( |
| 249 | `Failed to load connected accounts for toolkit "gmail": Error: 400 ${JSON.stringify(body)}` |
| 250 | ); |
| 251 | }) |
| 252 | ); |
| 253 | |
| 254 | // #4745: with several usable accounts and none aliased `default`, picking the |
| 255 | // newest silently could act on the wrong mailbox; the caller must choose. |
| 256 | it.effect( |
| 257 | 'fails instead of silently picking one, when several accounts are connected and none is alias=default', |
| 258 | () => |
| 259 | Effect.gen(function* () { |
| 260 | const { client } = stubComposioClient(() => |
| 261 | page([ |
| 262 | makeAccount({ id: 'con_work', alias: 'work', toolkit: { slug: 'gmail' } }), |
| 263 | makeAccount({ |
| 264 | id: 'con_personal', |
| 265 | alias: 'personal', |
| 266 | toolkit: { slug: 'gmail' }, |
| 267 | updated_at: '2026-01-02T00:00:00.000Z', |
| 268 | }), |
| 269 | ]) |
| 270 | ); |
| 271 | |
| 272 | const error = yield* resolveConnectedAccountForToolkit({ |
| 273 | client, |
| 274 | toolkitSlug: 'gmail', |
| 275 | userId: 'default', |
| 276 | selector: Option.none(), |
| 277 | }).pipe(Effect.flip, Effect.provide(TerminalUITest)); |
| 278 | |
| 279 | expect(error).toBeInstanceOf(ConnectedAccountResolutionError); |
| 280 | expect(error.message).toContain('Multiple connected accounts exist'); |
| 281 | expect(error.message).toContain('work'); |
| 282 | expect(error.message).toContain('personal'); |
| 283 | expect(error.message).toContain('Pass --account <alias or id> to choose one.'); |
| 284 | }) |
| 285 | ); |
| 286 | |
| 287 | it.effect( |
| 288 | 'still picks the alias=default account automatically when one exists, even with several accounts connected', |
| 289 | () => |
| 290 | Effect.gen(function* () { |
| 291 | const { client } = stubComposioClient(() => |
| 292 | page([ |
| 293 | makeAccount({ id: 'con_work', alias: 'work', toolkit: { slug: 'gmail' } }), |
| 294 | makeAccount({ id: 'con_main', alias: 'default', toolkit: { slug: 'gmail' } }), |
| 295 | ]) |
| 296 | ); |
| 297 | |
| 298 | const selected = yield* resolveConnectedAccountForToolkit({ |
| 299 | client, |
| 300 | toolkitSlug: 'gmail', |
| 301 | userId: 'default', |
| 302 | selector: Option.none(), |
| 303 | }).pipe(Effect.provide(TerminalUITest)); |
| 304 | |
| 305 | expect(selected).toBe('con_main'); |
| 306 | }) |
| 307 | ); |
| 308 | |
| 309 | it.effect('still picks the only account automatically when exactly one is connected', () => |
| 310 | Effect.gen(function* () { |
| 311 | const { client } = stubComposioClient(() => |
| 312 | page([makeAccount({ id: 'con_only', toolkit: { slug: 'gmail' } })]) |
| 313 | ); |
| 314 | |
| 315 | const selected = yield* resolveConnectedAccountForToolkit({ |
| 316 | client, |
| 317 | toolkitSlug: 'gmail', |
| 318 | userId: 'default', |
| 319 | selector: Option.none(), |
| 320 | }).pipe(Effect.provide(TerminalUITest)); |
| 321 | |
| 322 | expect(selected).toBe('con_only'); |
| 323 | }) |
| 324 | ); |
| 325 | |
| 326 | it.effect( |
| 327 | 'an explicit --account selector still matches as before, even with several accounts', |
| 328 | () => |
| 329 | Effect.gen(function* () { |
| 330 | const { client } = stubComposioClient(() => |
| 331 | page([ |
| 332 | makeAccount({ id: 'con_work', alias: 'work', toolkit: { slug: 'gmail' } }), |
| 333 | makeAccount({ id: 'con_personal', alias: 'personal', toolkit: { slug: 'gmail' } }), |
| 334 | ]) |
| 335 | ); |
| 336 | |
| 337 | const selected = yield* resolveConnectedAccountForToolkit({ |
| 338 | client, |
| 339 | toolkitSlug: 'gmail', |
| 340 | userId: 'default', |
| 341 | selector: Option.some('personal'), |
| 342 | }).pipe(Effect.provide(TerminalUITest)); |
| 343 | |
| 344 | expect(selected).toBe('con_personal'); |
| 345 | }) |
| 346 | ); |
| 347 | |
| 348 | it.effect( |
| 349 | 'an empty or whitespace-only --account value is treated as no selector, not a bypass', |
| 350 | () => |
| 351 | Effect.gen(function* () { |
| 352 | const { client } = stubComposioClient(() => |
| 353 | page([ |
| 354 | makeAccount({ id: 'con_work', alias: 'work', toolkit: { slug: 'gmail' } }), |
| 355 | makeAccount({ |
| 356 | id: 'con_personal', |
| 357 | alias: 'personal', |
| 358 | toolkit: { slug: 'gmail' }, |
| 359 | updated_at: '2026-01-02T00:00:00.000Z', |
| 360 | }), |
| 361 | ]) |
| 362 | ); |
| 363 | |
| 364 | const error = yield* resolveConnectedAccountForToolkit({ |
| 365 | client, |
| 366 | toolkitSlug: 'gmail', |
| 367 | userId: 'default', |
| 368 | selector: Option.some(' '), |
| 369 | }).pipe(Effect.flip, Effect.provide(TerminalUITest)); |
| 370 | |
| 371 | expect(error).toBeInstanceOf(ConnectedAccountResolutionError); |
| 372 | expect(error.message).toContain('Multiple connected accounts exist'); |
| 373 | }) |
| 374 | ); |
| 375 | }); |