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, it, vi } from '@effect/vitest'; |
| 2 | import { ConfigProvider, DateTime, Effect, Layer } from 'effect'; |
| 3 | import * as tempy from 'tempy'; |
| 4 | import { InternalServerError } from '@composio/client/effect'; |
| 5 | import { |
| 6 | ComposioToolkitsRepository, |
| 7 | type ToolkitProjectScope, |
| 8 | } from 'src/services/composio-clients'; |
| 9 | import * as FileSystem from 'effect/FileSystem'; |
| 10 | import * as BunFileSystem from '@effect/platform-bun/BunFileSystem'; |
| 11 | import { |
| 12 | CACHE_FILES, |
| 13 | ComposioToolkitsRepositoryCached, |
| 14 | } from 'src/services/composio-clients-cached'; |
| 15 | import { toolkitsToJSON, type Toolkits } from 'src/models/toolkits'; |
| 16 | import { makeToolkitFixture } from 'test/__utils__/models/toolkits'; |
| 17 | import { |
| 18 | countingToolkitsRepository, |
| 19 | makeToolkitsRepositoryStub, |
| 20 | type GetProjectToolkitsError, |
| 21 | type GetToolkitsError, |
| 22 | } from 'test/__utils__/services/toolkits-repository-stub'; |
| 23 | |
| 24 | const testToolkits: Toolkits = [makeToolkitFixture('github'), makeToolkitFixture('gmail')]; |
| 25 | |
| 26 | const serviceUnavailable = (message: string) => |
| 27 | new InternalServerError({ |
| 28 | message, |
| 29 | method: 'GET', |
| 30 | url: 'https://backend.composio.dev/api/v3.1/toolkits', |
| 31 | status: 503, |
| 32 | requestId: undefined, |
| 33 | details: undefined, |
| 34 | body: undefined, |
| 35 | }); |
| 36 | |
| 37 | /** |
| 38 | * Cached repository over a counting stub, with the cache directory pointed at a |
| 39 | * fresh temp dir so nothing leaks between tests or into the developer's |
| 40 | * `~/.composio`. `FORCE_USE_CACHE` stays unset unless a test passes it in, i.e. |
| 41 | * the file cache is write-only — the default path this suite is about. |
| 42 | */ |
| 43 | const withCountingRepository = <A>( |
| 44 | getToolkits: () => Effect.Effect<Toolkits, GetToolkitsError>, |
| 45 | program: (context: { |
| 46 | readonly calls: () => number; |
| 47 | readonly projectCalls: () => number; |
| 48 | readonly cacheDir: string; |
| 49 | }) => Effect.Effect< |
| 50 | A, |
| 51 | GetToolkitsError | GetProjectToolkitsError, |
| 52 | ComposioToolkitsRepository | FileSystem.FileSystem |
| 53 | >, |
| 54 | config: ReadonlyArray<readonly [string, string]> = [], |
| 55 | getProjectToolkits?: ( |
| 56 | scope?: ToolkitProjectScope |
| 57 | ) => Effect.Effect<Toolkits, GetProjectToolkitsError> |
| 58 | ) => |
| 59 | Effect.suspend(() => { |
| 60 | const cacheDir = tempy.temporaryDirectory(); |
| 61 | const repository = countingToolkitsRepository(getToolkits, getProjectToolkits); |
| 62 | |
| 63 | return program({ |
| 64 | calls: repository.calls, |
| 65 | projectCalls: repository.projectCalls, |
| 66 | cacheDir, |
| 67 | }).pipe( |
| 68 | Effect.provide( |
| 69 | Layer.merge( |
| 70 | Layer.provide(ComposioToolkitsRepositoryCached, repository.layer), |
| 71 | BunFileSystem.layer |
| 72 | ) |
| 73 | ), |
| 74 | Effect.provideService( |
| 75 | ConfigProvider.ConfigProvider, |
| 76 | ConfigProvider.fromEnvRecord(Object.fromEntries([['CACHE_DIR', cacheDir], ...config])) |
| 77 | ) |
| 78 | ); |
| 79 | }); |
| 80 | |
| 81 | describe('ComposioToolkitsRepositoryCached', () => { |
| 82 | it.effect('fetches the toolkit list once per layer, however many callers ask for it', () => |
| 83 | withCountingRepository( |
| 84 | () => Effect.succeed(testToolkits), |
| 85 | ({ calls }) => |
| 86 | Effect.gen(function* () { |
| 87 | const repository = yield* ComposioToolkitsRepository; |
| 88 | |
| 89 | const results = yield* Effect.all([ |
| 90 | repository.getToolkits(), |
| 91 | repository.getToolkits(), |
| 92 | repository.getToolkits(), |
| 93 | ]); |
| 94 | |
| 95 | expect(results.map(toolkits => toolkits.map(t => t.slug))).toEqual([ |
| 96 | ['github', 'gmail'], |
| 97 | ['github', 'gmail'], |
| 98 | ['github', 'gmail'], |
| 99 | ]); |
| 100 | expect(calls()).toBe(1); |
| 101 | }) |
| 102 | ) |
| 103 | ); |
| 104 | |
| 105 | // `it.live`: the stubbed fetch takes real time, so the callers genuinely overlap. |
| 106 | it.live('fetches once even when concurrent callers ask at the same time', () => |
| 107 | withCountingRepository( |
| 108 | () => Effect.succeed(testToolkits).pipe(Effect.delay('10 millis')), |
| 109 | ({ calls }) => |
| 110 | Effect.gen(function* () { |
| 111 | const repository = yield* ComposioToolkitsRepository; |
| 112 | |
| 113 | yield* Effect.all( |
| 114 | [repository.getToolkits(), repository.getToolkits(), repository.getToolkits()], |
| 115 | { concurrency: 'unbounded' } |
| 116 | ); |
| 117 | |
| 118 | expect(calls()).toBe(1); |
| 119 | }) |
| 120 | ) |
| 121 | ); |
| 122 | |
| 123 | it.effect('surfaces a failed fetch once, and does not retry it', () => |
| 124 | withCountingRepository( |
| 125 | () => Effect.fail(serviceUnavailable('network down')), |
| 126 | ({ calls }) => |
| 127 | Effect.gen(function* () { |
| 128 | const repository = yield* ComposioToolkitsRepository; |
| 129 | |
| 130 | const first = yield* Effect.result(repository.getToolkits()); |
| 131 | const second = yield* Effect.result(repository.getToolkits()); |
| 132 | |
| 133 | expect(second).toEqual(first); |
| 134 | // One attempt for both callers: the caching layer must not mistake |
| 135 | // the fetch's own failure for a cache failure worth retrying. |
| 136 | expect(calls()).toBe(1); |
| 137 | }) |
| 138 | ) |
| 139 | ); |
| 140 | |
| 141 | it.effect('serves the cached file when FORCE_USE_CACHE is on, without fetching', () => |
| 142 | withCountingRepository( |
| 143 | () => Effect.die('the cached file should have answered this'), |
| 144 | ({ calls, cacheDir }) => |
| 145 | Effect.gen(function* () { |
| 146 | const fs = yield* FileSystem.FileSystem; |
| 147 | yield* fs |
| 148 | .writeFileString( |
| 149 | `${cacheDir}/${CACHE_FILES.toolkits}`, |
| 150 | yield* toolkitsToJSON(testToolkits).pipe(Effect.orDie) |
| 151 | ) |
| 152 | .pipe(Effect.orDie); |
| 153 | |
| 154 | const repository = yield* ComposioToolkitsRepository; |
| 155 | const toolkits = yield* repository.getToolkits(); |
| 156 | |
| 157 | expect(toolkits.map(t => t.slug)).toEqual(['github', 'gmail']); |
| 158 | expect(calls()).toBe(0); |
| 159 | }), |
| 160 | [['FORCE_USE_CACHE', 'true']] |
| 161 | ) |
| 162 | ); |
| 163 | |
| 164 | it.effect('fetches project toolkits once, and never from the cached file', () => |
| 165 | withCountingRepository( |
| 166 | () => Effect.die('the cached file should have answered this'), |
| 167 | ({ calls, projectCalls, cacheDir }) => |
| 168 | Effect.gen(function* () { |
| 169 | const fs = yield* FileSystem.FileSystem; |
| 170 | const cacheFile = `${cacheDir}/${CACHE_FILES.toolkits}`; |
| 171 | const cached = yield* toolkitsToJSON(testToolkits).pipe(Effect.orDie); |
| 172 | yield* fs.writeFileString(cacheFile, cached).pipe(Effect.orDie); |
| 173 | |
| 174 | const repository = yield* ComposioToolkitsRepository; |
| 175 | const toolkits = yield* repository.getToolkits(); |
| 176 | const projectToolkits = yield* repository.getProjectToolkits(); |
| 177 | yield* repository.getProjectToolkits(); |
| 178 | |
| 179 | expect(toolkits.map(t => t.slug)).toEqual(['github', 'gmail']); |
| 180 | expect(projectToolkits.map(t => t.slug)).toEqual(['custom_grain']); |
| 181 | expect(calls()).toBe(0); |
| 182 | expect(projectCalls()).toBe(1); |
| 183 | // `toolkits.json` holds the Composio-managed catalog only. |
| 184 | expect(yield* fs.readFileString(cacheFile).pipe(Effect.orDie)).toBe(cached); |
| 185 | }), |
| 186 | [['FORCE_USE_CACHE', 'true']], |
| 187 | () => Effect.succeed([makeToolkitFixture('custom_grain')]) |
| 188 | ) |
| 189 | ); |
| 190 | |
| 191 | it.effect('surfaces a failed project fetch once, and does not retry it', () => |
| 192 | withCountingRepository( |
| 193 | () => Effect.die('the project lookup should not read the catalog'), |
| 194 | ({ projectCalls }) => |
| 195 | Effect.gen(function* () { |
| 196 | const repository = yield* ComposioToolkitsRepository; |
| 197 | const scope = { orgId: 'org_a', projectId: 'pr_a' }; |
| 198 | |
| 199 | const first = yield* Effect.result(repository.getProjectToolkits(scope)); |
| 200 | const second = yield* Effect.result(repository.getProjectToolkits(scope)); |
| 201 | |
| 202 | expect(second).toEqual(first); |
| 203 | expect(projectCalls()).toBe(1); |
| 204 | }), |
| 205 | [], |
| 206 | () => Effect.fail(serviceUnavailable('network down')) |
| 207 | ) |
| 208 | ); |
| 209 | |
| 210 | it.effect('fetches project toolkits once per project scope', () => |
| 211 | withCountingRepository( |
| 212 | () => Effect.die('the project lookup should not read the catalog'), |
| 213 | ({ projectCalls }) => |
| 214 | Effect.gen(function* () { |
| 215 | const repository = yield* ComposioToolkitsRepository; |
| 216 | const scopeA = { orgId: 'org_a', projectId: 'pr_a' }; |
| 217 | const scopeB = { orgId: 'org_a', projectId: 'pr_b' }; |
| 218 | |
| 219 | const results = yield* Effect.all([ |
| 220 | repository.getProjectToolkits(scopeA), |
| 221 | repository.getProjectToolkits(scopeB), |
| 222 | repository.getProjectToolkits({ ...scopeA }), |
| 223 | repository.getProjectToolkits(scopeB), |
| 224 | repository.getProjectToolkits(), |
| 225 | ]); |
| 226 | |
| 227 | expect(results.map(toolkits => toolkits.map(t => t.slug))).toEqual([ |
| 228 | ['custom_pr_a'], |
| 229 | ['custom_pr_b'], |
| 230 | ['custom_pr_a'], |
| 231 | ['custom_pr_b'], |
| 232 | ['custom_unscoped'], |
| 233 | ]); |
| 234 | // Unscoped is a scope of its own: the project context decides it. |
| 235 | expect(projectCalls()).toBe(3); |
| 236 | }), |
| 237 | [], |
| 238 | scope => Effect.succeed([makeToolkitFixture(`custom_${scope?.projectId ?? 'unscoped'}`)]) |
| 239 | ) |
| 240 | ); |
| 241 | |
| 242 | it.effect('shares one in-flight project fetch between concurrent first callers', () => |
| 243 | withCountingRepository( |
| 244 | () => Effect.die('the project lookup should not read the catalog'), |
| 245 | ({ projectCalls }) => |
| 246 | Effect.gen(function* () { |
| 247 | const repository = yield* ComposioToolkitsRepository; |
| 248 | const scope = { orgId: 'org_a', projectId: 'pr_a' }; |
| 249 | |
| 250 | const results = yield* Effect.all( |
| 251 | [ |
| 252 | repository.getProjectToolkits(scope), |
| 253 | repository.getProjectToolkits({ ...scope }), |
| 254 | repository.getProjectToolkits(scope), |
| 255 | ], |
| 256 | { concurrency: 'unbounded' } |
| 257 | ); |
| 258 | |
| 259 | expect(results.map(toolkits => toolkits.map(t => t.slug))).toEqual([ |
| 260 | ['custom_pr_a'], |
| 261 | ['custom_pr_a'], |
| 262 | ['custom_pr_a'], |
| 263 | ]); |
| 264 | expect(projectCalls()).toBe(1); |
| 265 | }), |
| 266 | [], |
| 267 | // The fetch yields before answering, so every caller is waiting on it. |
| 268 | scope => |
| 269 | Effect.yieldNow.pipe( |
| 270 | Effect.as([makeToolkitFixture(`custom_${scope?.projectId ?? 'unscoped'}`)]) |
| 271 | ) |
| 272 | ) |
| 273 | ); |
| 274 | |
| 275 | it.effect('passes an uncached method straight through to the underlying repository', () => |
| 276 | Effect.gen(function* () { |
| 277 | const searchTools = vi.fn(() => |
| 278 | Effect.succeed({ |
| 279 | items: [], |
| 280 | total_items: 0, |
| 281 | total_pages: 0, |
| 282 | current_page: 1, |
| 283 | next_cursor: null, |
| 284 | }) |
| 285 | ); |
| 286 | const underlying = makeToolkitsRepositoryStub({ searchTools }); |
| 287 | const params = { search: 'gmail', limit: 3 }; |
| 288 | |
| 289 | const repository = yield* ComposioToolkitsRepository.pipe( |
| 290 | Effect.provide( |
| 291 | Layer.provide( |
| 292 | ComposioToolkitsRepositoryCached, |
| 293 | Layer.succeed(ComposioToolkitsRepository, underlying) |
| 294 | ) |
| 295 | ) |
| 296 | ); |
| 297 | yield* repository.searchTools(params); |
| 298 | |
| 299 | expect(searchTools).toHaveBeenCalledOnce(); |
| 300 | expect(searchTools).toHaveBeenCalledWith(params); |
| 301 | }) |
| 302 | ); |
| 303 | |
| 304 | it.effect('exposes an underlying method it does not know about', () => |
| 305 | Effect.gen(function* () { |
| 306 | const addedLater = () => Effect.succeed('added later'); |
| 307 | const underlying = Object.assign(makeToolkitsRepositoryStub({}), { addedLater }); |
| 308 | |
| 309 | const repository = yield* ComposioToolkitsRepository.pipe( |
| 310 | Effect.provide( |
| 311 | Layer.provide( |
| 312 | ComposioToolkitsRepositoryCached, |
| 313 | Layer.succeed(ComposioToolkitsRepository, underlying) |
| 314 | ) |
| 315 | ) |
| 316 | ); |
| 317 | |
| 318 | expect((repository as typeof underlying).addedLater).toBe(addedLater); |
| 319 | }) |
| 320 | ); |
| 321 | }); |