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, expect, layer } from '@effect/vitest'; |
| 2 | import { vi, beforeEach, afterEach } from 'vitest'; |
| 3 | import * as FileSystem from 'effect/FileSystem'; |
| 4 | import { ConfigProvider, DateTime, Effect, Option } from 'effect'; |
| 5 | import path from 'node:path'; |
| 6 | import { setupCacheDir } from 'src/effects/setup-cache-dir'; |
| 7 | import * as composioAccount from 'src/services/composio-account'; |
| 8 | import { |
| 9 | getFreshConsumerConnectedToolkitsFromCache, |
| 10 | getFreshConsumerToolRouterAuthConfigsFromCache, |
| 11 | getFreshConsumerToolRouterConnectedAccountsFromCache, |
| 12 | refreshConsumerConnectedToolkitsCache, |
| 13 | writeConsumerConnectedToolkitsCache, |
| 14 | } from 'src/services/consumer-short-term-cache'; |
| 15 | import { TestLive } from 'test/__utils__'; |
| 16 | |
| 17 | const makeTestConfigProvider = (entries: Array<[string, string]>) => |
| 18 | ConfigProvider.fromEnv({ |
| 19 | env: Object.fromEntries([ |
| 20 | ['COMPOSIO_USER_API_KEY', 'test_api_key'], |
| 21 | ['COMPOSIO_BASE_URL', 'https://backend.composio.dev'], |
| 22 | ...entries, |
| 23 | ]), |
| 24 | }); |
| 25 | |
| 26 | const defaultTestConfigProvider = makeTestConfigProvider([]); |
| 27 | const cacheEnabledTestConfigProvider = makeTestConfigProvider([ |
| 28 | ['COMPOSIO_DISABLE_CONNECTED_ACCOUNT_CACHE', 'false'], |
| 29 | ]); |
| 30 | |
| 31 | // Instant the wall clock is pinned to; fixture expiresAt values below are |
| 32 | // written relative to it so freshness checks in the SUT stay deterministic. |
| 33 | const PINNED_NOW = '2026-01-01T00:00:00.000Z'; |
| 34 | const ONE_MINUTE_FROM_PINNED_NOW = '2026-01-01T00:01:00.000Z'; |
| 35 | |
| 36 | describe('consumer short-term cache', () => { |
| 37 | beforeEach(() => { |
| 38 | // Fake ONLY Date so real timers and promise scheduling keep working. |
| 39 | vi.useFakeTimers({ toFake: ['Date'] }); |
| 40 | vi.setSystemTime(new Date(PINNED_NOW)); |
| 41 | }); |
| 42 | |
| 43 | afterEach(() => { |
| 44 | vi.useRealTimers(); |
| 45 | vi.restoreAllMocks(); |
| 46 | }); |
| 47 | |
| 48 | layer(TestLive({ baseConfigProvider: defaultTestConfigProvider }))( |
| 49 | '[Given] the default cache config [Then] connected-account cache stays disabled', |
| 50 | it => { |
| 51 | it.effect('returns none even after a write is attempted', () => |
| 52 | Effect.gen(function* () { |
| 53 | yield* writeConsumerConnectedToolkitsCache({ |
| 54 | orgId: 'org_test', |
| 55 | consumerUserId: 'consumer-user-test', |
| 56 | toolkits: ['github'], |
| 57 | }); |
| 58 | |
| 59 | const cached = yield* getFreshConsumerConnectedToolkitsFromCache({ |
| 60 | orgId: 'org_test', |
| 61 | consumerUserId: 'consumer-user-test', |
| 62 | }); |
| 63 | |
| 64 | expect(cached).toEqual(Option.none()); |
| 65 | }) |
| 66 | ); |
| 67 | } |
| 68 | ); |
| 69 | |
| 70 | layer( |
| 71 | TestLive({ |
| 72 | baseConfigProvider: cacheEnabledTestConfigProvider, |
| 73 | toolkitsData: { |
| 74 | toolkits: [ |
| 75 | { |
| 76 | name: 'GitHub', |
| 77 | slug: 'github', |
| 78 | auth_schemes: ['OAUTH2'], |
| 79 | composio_managed_auth_schemes: ['OAUTH2'], |
| 80 | is_local_toolkit: false, |
| 81 | no_auth: false, |
| 82 | meta: { |
| 83 | description: 'GitHub toolkit', |
| 84 | categories: [], |
| 85 | created_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'), |
| 86 | updated_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'), |
| 87 | available_versions: [], |
| 88 | tools_count: 0, |
| 89 | triggers_count: 0, |
| 90 | }, |
| 91 | }, |
| 92 | { |
| 93 | name: 'Hacker News', |
| 94 | slug: 'hackernews', |
| 95 | auth_schemes: [], |
| 96 | composio_managed_auth_schemes: [], |
| 97 | is_local_toolkit: false, |
| 98 | no_auth: true, |
| 99 | meta: { |
| 100 | description: 'No-auth toolkit', |
| 101 | categories: [], |
| 102 | created_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'), |
| 103 | updated_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'), |
| 104 | available_versions: [], |
| 105 | tools_count: 0, |
| 106 | triggers_count: 0, |
| 107 | }, |
| 108 | }, |
| 109 | ], |
| 110 | }, |
| 111 | }) |
| 112 | )('[Given] no-auth toolkits [Then] refresh caches them as connected', it => { |
| 113 | it.effect('stores connected and no-auth toolkit slugs together', () => |
| 114 | Effect.gen(function* () { |
| 115 | vi.spyOn(composioAccount, 'getConsumerConnectedToolkits').mockReturnValue( |
| 116 | Effect.succeed({ toolkits: ['github'] }) |
| 117 | ); |
| 118 | |
| 119 | yield* refreshConsumerConnectedToolkitsCache({ |
| 120 | orgId: 'org_test', |
| 121 | consumerUserId: 'consumer-user-test', |
| 122 | }); |
| 123 | |
| 124 | const cached = yield* getFreshConsumerConnectedToolkitsFromCache({ |
| 125 | orgId: 'org_test', |
| 126 | consumerUserId: 'consumer-user-test', |
| 127 | }); |
| 128 | |
| 129 | expect(cached).toEqual(Option.some(['github', 'hackernews'])); |
| 130 | }) |
| 131 | ); |
| 132 | }); |
| 133 | |
| 134 | layer( |
| 135 | TestLive({ |
| 136 | baseConfigProvider: cacheEnabledTestConfigProvider, |
| 137 | toolkitsData: { |
| 138 | toolkits: [ |
| 139 | { |
| 140 | name: 'GitHub', |
| 141 | slug: 'github', |
| 142 | auth_schemes: ['OAUTH2'], |
| 143 | composio_managed_auth_schemes: ['OAUTH2'], |
| 144 | is_local_toolkit: false, |
| 145 | no_auth: false, |
| 146 | meta: { |
| 147 | description: 'GitHub toolkit', |
| 148 | categories: [], |
| 149 | created_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'), |
| 150 | updated_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'), |
| 151 | available_versions: [], |
| 152 | tools_count: 0, |
| 153 | triggers_count: 0, |
| 154 | }, |
| 155 | }, |
| 156 | { |
| 157 | name: 'Hacker News', |
| 158 | slug: 'hackernews', |
| 159 | auth_schemes: [], |
| 160 | composio_managed_auth_schemes: [], |
| 161 | is_local_toolkit: false, |
| 162 | no_auth: true, |
| 163 | meta: { |
| 164 | description: 'No-auth toolkit', |
| 165 | categories: [], |
| 166 | created_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'), |
| 167 | updated_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'), |
| 168 | available_versions: [], |
| 169 | tools_count: 0, |
| 170 | triggers_count: 0, |
| 171 | }, |
| 172 | }, |
| 173 | ], |
| 174 | }, |
| 175 | }) |
| 176 | )('[Given] a search cache write [Then] no-auth toolkits are preserved', it => { |
| 177 | it.effect('stores active and no-auth toolkit slugs together', () => |
| 178 | Effect.gen(function* () { |
| 179 | yield* writeConsumerConnectedToolkitsCache({ |
| 180 | orgId: 'org_test', |
| 181 | consumerUserId: 'consumer-user-test', |
| 182 | toolkits: ['github'], |
| 183 | }); |
| 184 | |
| 185 | const cached = yield* getFreshConsumerConnectedToolkitsFromCache({ |
| 186 | orgId: 'org_test', |
| 187 | consumerUserId: 'consumer-user-test', |
| 188 | }); |
| 189 | |
| 190 | expect(cached).toEqual(Option.some(['github', 'hackernews'])); |
| 191 | }) |
| 192 | ); |
| 193 | }); |
| 194 | |
| 195 | layer(TestLive({ baseConfigProvider: cacheEnabledTestConfigProvider }))( |
| 196 | '[Given] a malformed persisted cache [Then] cache reads fail closed', |
| 197 | it => { |
| 198 | it.effect('ignores cache entries that do not match the persisted schema', () => |
| 199 | Effect.gen(function* () { |
| 200 | const fs = yield* FileSystem.FileSystem; |
| 201 | const cacheDir = yield* setupCacheDir; |
| 202 | yield* fs.writeFileString( |
| 203 | path.join(cacheDir, 'consumer-short-term-cache.json'), |
| 204 | JSON.stringify({ |
| 205 | 'org_test:consumer-user-test': { |
| 206 | toolkits: 'github', |
| 207 | expiresAt: ONE_MINUTE_FROM_PINNED_NOW, |
| 208 | }, |
| 209 | }) |
| 210 | ); |
| 211 | |
| 212 | const cached = yield* getFreshConsumerConnectedToolkitsFromCache({ |
| 213 | orgId: 'org_test', |
| 214 | consumerUserId: 'consumer-user-test', |
| 215 | }); |
| 216 | |
| 217 | expect(cached).toEqual(Option.none()); |
| 218 | }) |
| 219 | ); |
| 220 | } |
| 221 | ); |
| 222 | |
| 223 | layer(TestLive({ baseConfigProvider: cacheEnabledTestConfigProvider }))( |
| 224 | '[Given] one corrupt entry among valid ones [Then] only the corrupt entry is dropped', |
| 225 | it => { |
| 226 | it.effect('keeps valid cache entries when a sibling entry is corrupt', () => |
| 227 | Effect.gen(function* () { |
| 228 | const fs = yield* FileSystem.FileSystem; |
| 229 | const cacheDir = yield* setupCacheDir; |
| 230 | yield* fs.writeFileString( |
| 231 | path.join(cacheDir, 'consumer-short-term-cache.json'), |
| 232 | JSON.stringify({ |
| 233 | 'org_bad:consumer-user-bad': { toolkits: 'not-an-array', expiresAt: 42 }, |
| 234 | 'org_test:consumer-user-test': { |
| 235 | toolkits: ['github'], |
| 236 | expiresAt: ONE_MINUTE_FROM_PINNED_NOW, |
| 237 | }, |
| 238 | }) |
| 239 | ); |
| 240 | |
| 241 | const cached = yield* getFreshConsumerConnectedToolkitsFromCache({ |
| 242 | orgId: 'org_test', |
| 243 | consumerUserId: 'consumer-user-test', |
| 244 | }); |
| 245 | |
| 246 | const toolkits = Option.getOrElse(cached, (): ReadonlyArray<string> => []); |
| 247 | expect(toolkits).toContain('github'); |
| 248 | }) |
| 249 | ); |
| 250 | } |
| 251 | ); |
| 252 | |
| 253 | layer(TestLive({ baseConfigProvider: cacheEnabledTestConfigProvider }))( |
| 254 | '[Given] a full auth-config cache hit [Then] cache reads are toolkit-complete', |
| 255 | it => { |
| 256 | it.effect('returns cached auth configs when every requested toolkit is covered', () => |
| 257 | Effect.gen(function* () { |
| 258 | yield* writeConsumerConnectedToolkitsCache({ |
| 259 | orgId: 'org_test', |
| 260 | consumerUserId: 'consumer-user-test', |
| 261 | toolkits: ['posthog', 'hubspot'], |
| 262 | toolRouterAuthConfigs: { |
| 263 | authConfigs: { |
| 264 | posthog: 'ac_posthog', |
| 265 | hubspot: 'ac_hubspot', |
| 266 | }, |
| 267 | }, |
| 268 | }); |
| 269 | |
| 270 | const cached = yield* getFreshConsumerToolRouterAuthConfigsFromCache({ |
| 271 | orgId: 'org_test', |
| 272 | consumerUserId: 'consumer-user-test', |
| 273 | toolkits: ['posthog', 'hubspot'], |
| 274 | }); |
| 275 | |
| 276 | expect(cached).toEqual( |
| 277 | Option.some({ |
| 278 | authConfigs: { |
| 279 | posthog: 'ac_posthog', |
| 280 | hubspot: 'ac_hubspot', |
| 281 | }, |
| 282 | }) |
| 283 | ); |
| 284 | }) |
| 285 | ); |
| 286 | } |
| 287 | ); |
| 288 | |
| 289 | layer(TestLive({ baseConfigProvider: cacheEnabledTestConfigProvider }))( |
| 290 | '[Given] a partial auth-config cache hit [Then] cache read fails closed', |
| 291 | it => { |
| 292 | it.effect('returns none unless every requested toolkit has a cached auth config', () => |
| 293 | Effect.gen(function* () { |
| 294 | yield* writeConsumerConnectedToolkitsCache({ |
| 295 | orgId: 'org_test', |
| 296 | consumerUserId: 'consumer-user-test', |
| 297 | toolkits: ['posthog', 'hubspot'], |
| 298 | toolRouterAuthConfigs: { |
| 299 | authConfigs: { |
| 300 | posthog: 'ac_posthog', |
| 301 | }, |
| 302 | }, |
| 303 | }); |
| 304 | |
| 305 | const cached = yield* getFreshConsumerToolRouterAuthConfigsFromCache({ |
| 306 | orgId: 'org_test', |
| 307 | consumerUserId: 'consumer-user-test', |
| 308 | toolkits: ['posthog', 'hubspot'], |
| 309 | }); |
| 310 | |
| 311 | expect(cached).toEqual(Option.none()); |
| 312 | }) |
| 313 | ); |
| 314 | } |
| 315 | ); |
| 316 | |
| 317 | layer(TestLive({ baseConfigProvider: cacheEnabledTestConfigProvider }))( |
| 318 | '[Given] cached connected account metadata [Then] default mappings and summaries are readable', |
| 319 | it => { |
| 320 | it.effect('returns cached connected account selectors by toolkit', () => |
| 321 | Effect.gen(function* () { |
| 322 | yield* writeConsumerConnectedToolkitsCache({ |
| 323 | orgId: 'org_test', |
| 324 | consumerUserId: 'consumer-user-test', |
| 325 | toolkits: ['gmail'], |
| 326 | toolRouterConnectedAccounts: { |
| 327 | connectedAccounts: { |
| 328 | gmail: 'con_default', |
| 329 | }, |
| 330 | availableConnectedAccounts: { |
| 331 | gmail: [ |
| 332 | { |
| 333 | id: 'con_default', |
| 334 | alias: 'default', |
| 335 | wordId: 'castle', |
| 336 | updatedAt: '2026-01-02T00:00:00.000Z', |
| 337 | createdAt: '2026-01-01T00:00:00.000Z', |
| 338 | }, |
| 339 | ], |
| 340 | }, |
| 341 | }, |
| 342 | }); |
| 343 | |
| 344 | const cached = yield* getFreshConsumerToolRouterConnectedAccountsFromCache({ |
| 345 | orgId: 'org_test', |
| 346 | consumerUserId: 'consumer-user-test', |
| 347 | toolkits: ['gmail'], |
| 348 | }); |
| 349 | |
| 350 | expect(cached).toEqual( |
| 351 | Option.some({ |
| 352 | connectedAccounts: { |
| 353 | gmail: 'con_default', |
| 354 | }, |
| 355 | availableConnectedAccounts: { |
| 356 | gmail: [ |
| 357 | { |
| 358 | id: 'con_default', |
| 359 | alias: 'default', |
| 360 | wordId: 'castle', |
| 361 | updatedAt: '2026-01-02T00:00:00.000Z', |
| 362 | createdAt: '2026-01-01T00:00:00.000Z', |
| 363 | }, |
| 364 | ], |
| 365 | }, |
| 366 | }) |
| 367 | ); |
| 368 | }) |
| 369 | ); |
| 370 | } |
| 371 | ); |
| 372 | }); |