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 } from '@effect/vitest'; |
| 2 | import * as BunFileSystem from '@effect/platform-bun/BunFileSystem'; |
| 3 | import { ConfigProvider, DateTime, Deferred, Effect, FileSystem, Layer, Schedule } from 'effect'; |
| 4 | import * as tempy from 'tempy'; |
| 5 | import { toolkitFromToolSlug } from 'src/effects/toolkit-from-tool-slug'; |
| 6 | import type { Toolkits } from 'src/models/toolkits'; |
| 7 | import { InternalServerError } from '@composio/client/effect'; |
| 8 | import { |
| 9 | ComposioToolkitsRepository, |
| 10 | type ToolkitProjectScope, |
| 11 | } from 'src/services/composio-clients'; |
| 12 | import { KNOWN_TOOLKIT_SLUGS_FILE } from 'src/services/known-toolkit-slugs'; |
| 13 | import { ToolkitSlugCatalog } from 'src/services/toolkit-slug-catalog'; |
| 14 | import { makeToolkitFixture } from 'test/__utils__/models/toolkits'; |
| 15 | import { |
| 16 | countingToolkitsRepository, |
| 17 | type GetProjectToolkitsError, |
| 18 | type GetToolkitsError, |
| 19 | } from 'test/__utils__/services/toolkits-repository-stub'; |
| 20 | |
| 21 | /** |
| 22 | * A toolkit that cannot be in the baked catalog, standing in for one released |
| 23 | * after the binary was built. Tests of the fetch path must use slugs like this |
| 24 | * — anything real resolves locally and never reaches the repository. |
| 25 | */ |
| 26 | const UNRELEASED_TOOLKIT = 'acme_analytics'; |
| 27 | |
| 28 | /** |
| 29 | * A project-scoped custom toolkit. Custom toolkits never ship in the baked |
| 30 | * catalog, and their slugs share the `custom` prefix with nothing baked. |
| 31 | */ |
| 32 | const CUSTOM_TOOLKIT = 'custom_grain'; |
| 33 | |
| 34 | const failingFetch = () => |
| 35 | Effect.fail( |
| 36 | new InternalServerError({ |
| 37 | message: 'catalog unavailable', |
| 38 | method: 'GET', |
| 39 | url: 'https://backend.composio.dev/api/v3.1/toolkits', |
| 40 | status: 503, |
| 41 | requestId: undefined, |
| 42 | details: undefined, |
| 43 | body: undefined, |
| 44 | }) |
| 45 | ); |
| 46 | |
| 47 | /** |
| 48 | * A learned-slugs file recorded `daysAgo` days ago. Anything under a week old |
| 49 | * keeps the resolver from starting a background refresh, which is what makes |
| 50 | * "no catalog fetch" assertions meaningful. |
| 51 | */ |
| 52 | const learnedFileContent = (slugs: ReadonlyArray<string>, daysAgo = 0) => |
| 53 | JSON.stringify({ |
| 54 | slugs, |
| 55 | refreshedAt: DateTime.formatIso(DateTime.subtract(DateTime.nowUnsafe(), { days: daysAgo })), |
| 56 | }); |
| 57 | |
| 58 | /** |
| 59 | * A project catalog that lists each named project's toolkits. Unscoped |
| 60 | * lookups, and projects it does not name, list nothing. |
| 61 | */ |
| 62 | const projectCatalog = |
| 63 | (toolkitsByProject: Readonly<Record<string, ReadonlyArray<string>>>) => |
| 64 | (scope?: ToolkitProjectScope) => |
| 65 | Effect.succeed( |
| 66 | Object.entries(toolkitsByProject) |
| 67 | .filter(([projectId]) => projectId === scope?.projectId) |
| 68 | .flatMap(([, slugs]) => slugs.map(slug => makeToolkitFixture(slug))) |
| 69 | ); |
| 70 | |
| 71 | interface ResolverOptions { |
| 72 | readonly getToolkits?: () => Effect.Effect<Toolkits, GetToolkitsError>; |
| 73 | readonly getProjectToolkits?: ( |
| 74 | scope?: ToolkitProjectScope |
| 75 | ) => Effect.Effect<Toolkits, GetProjectToolkitsError>; |
| 76 | readonly seedLearnedFile?: string; |
| 77 | } |
| 78 | |
| 79 | interface ResolverContext { |
| 80 | readonly calls: () => number; |
| 81 | readonly projectCalls: () => number; |
| 82 | readonly waitForLearnedFile: ( |
| 83 | predicate: (content: string) => boolean |
| 84 | ) => Effect.Effect<string, unknown, FileSystem.FileSystem>; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Runs `program` against a private cache directory, so the resolver reads and |
| 89 | * writes a learned-slugs file belonging to this test alone. `seedLearnedFile` |
| 90 | * is written verbatim — including deliberately corrupt content. |
| 91 | */ |
| 92 | const withResolver = <A>( |
| 93 | options: ResolverOptions, |
| 94 | program: ( |
| 95 | context: ResolverContext |
| 96 | ) => Effect.Effect< |
| 97 | A, |
| 98 | unknown, |
| 99 | FileSystem.FileSystem | ComposioToolkitsRepository | ToolkitSlugCatalog |
| 100 | > |
| 101 | ) => |
| 102 | Effect.suspend(() => { |
| 103 | const cacheDir = tempy.temporaryDirectory(); |
| 104 | return runInCacheDir(cacheDir, options, program); |
| 105 | }); |
| 106 | |
| 107 | const runInCacheDir = <A>( |
| 108 | cacheDir: string, |
| 109 | options: ResolverOptions, |
| 110 | program: ( |
| 111 | context: ResolverContext |
| 112 | ) => Effect.Effect< |
| 113 | A, |
| 114 | unknown, |
| 115 | FileSystem.FileSystem | ComposioToolkitsRepository | ToolkitSlugCatalog |
| 116 | > |
| 117 | ) => |
| 118 | Effect.gen(function* () { |
| 119 | const fs = yield* FileSystem.FileSystem; |
| 120 | const learnedFile = `${cacheDir}/${KNOWN_TOOLKIT_SLUGS_FILE}`; |
| 121 | |
| 122 | if (options.seedLearnedFile !== undefined) { |
| 123 | yield* fs.writeFileString(learnedFile, options.seedLearnedFile); |
| 124 | } |
| 125 | |
| 126 | const repository = countingToolkitsRepository( |
| 127 | options.getToolkits ?? (() => Effect.succeed([] as Toolkits)), |
| 128 | options.getProjectToolkits |
| 129 | ); |
| 130 | |
| 131 | const readLearnedFile = fs.readFileString(learnedFile); |
| 132 | |
| 133 | return yield* program({ |
| 134 | calls: repository.calls, |
| 135 | projectCalls: repository.projectCalls, |
| 136 | // The resolver records what it learns in the background, so a test that |
| 137 | // asserts on the file has to wait for it rather than read once. |
| 138 | waitForLearnedFile: predicate => |
| 139 | readLearnedFile.pipe( |
| 140 | Effect.filterOrFail(predicate, () => 'learned file has not caught up yet' as const), |
| 141 | Effect.retry(Schedule.max([Schedule.spaced('10 millis'), Schedule.recurs(200)])) |
| 142 | ), |
| 143 | }).pipe( |
| 144 | // Built inside this test's cache directory, and fresh per test: the |
| 145 | // catalog memoizes what it reads, so a shared one would leak the first |
| 146 | // test's learned slugs into every later one. |
| 147 | Effect.provide( |
| 148 | Layer.mergeAll( |
| 149 | repository.layer, |
| 150 | Layer.provide(ToolkitSlugCatalog.Default, repository.layer) |
| 151 | ) |
| 152 | ) |
| 153 | ); |
| 154 | }).pipe( |
| 155 | Effect.provide(BunFileSystem.layer), |
| 156 | Effect.provide( |
| 157 | ConfigProvider.layer(ConfigProvider.fromEnvRecord({ COMPOSIO_CACHE_DIR: cacheDir })) |
| 158 | ) |
| 159 | ); |
| 160 | |
| 161 | describe('toolkitFromToolSlug', () => { |
| 162 | it.effect('resolves a baked multi-word toolkit without touching the network', () => |
| 163 | withResolver({ seedLearnedFile: learnedFileContent([]) }, ({ calls }) => |
| 164 | Effect.gen(function* () { |
| 165 | expect(yield* toolkitFromToolSlug('GOOGLE_ANALYTICS_RUN_REPORT')).toBe('google_analytics'); |
| 166 | expect(yield* toolkitFromToolSlug('GMAIL_SEND_EMAIL')).toBe('gmail'); |
| 167 | expect(calls()).toBe(0); |
| 168 | }) |
| 169 | ) |
| 170 | ); |
| 171 | |
| 172 | it.effect('gives meta tools no toolkit, even when one shadows their slug', () => |
| 173 | withResolver({ seedLearnedFile: learnedFileContent([]) }, ({ calls, projectCalls }) => |
| 174 | Effect.gen(function* () { |
| 175 | // `composio_search` is a real, linkable toolkit whose slug is a prefix |
| 176 | // of this session meta tool. Attributing the meta tool to it would |
| 177 | // send users to link an app they do not need. |
| 178 | expect(yield* toolkitFromToolSlug('COMPOSIO_SEARCH_TOOLS')).toBeUndefined(); |
| 179 | expect(yield* toolkitFromToolSlug('COMPOSIO_MANAGE_CONNECTIONS')).toBeUndefined(); |
| 180 | expect(calls()).toBe(0); |
| 181 | expect(projectCalls()).toBe(0); |
| 182 | }) |
| 183 | ) |
| 184 | ); |
| 185 | |
| 186 | it.effect('treats a bare `composio` match as resolved, not as a miss', () => |
| 187 | withResolver({ seedLearnedFile: learnedFileContent([]) }, ({ calls }) => |
| 188 | Effect.gen(function* () { |
| 189 | expect(yield* toolkitFromToolSlug('COMPOSIO_ENABLE_TRIGGER')).toBeUndefined(); |
| 190 | // A miss here would cost every `COMPOSIO_*` tool a catalog fetch. |
| 191 | expect(calls()).toBe(0); |
| 192 | }) |
| 193 | ) |
| 194 | ); |
| 195 | |
| 196 | it.live('answers from the baked list on a first run, and seeds the file behind it', () => |
| 197 | withResolver( |
| 198 | { getToolkits: () => Effect.succeed([makeToolkitFixture(UNRELEASED_TOOLKIT)]) }, |
| 199 | ({ waitForLearnedFile }) => |
| 200 | Effect.gen(function* () { |
| 201 | expect(yield* toolkitFromToolSlug('GOOGLE_ANALYTICS_RUN_REPORT')).toBe( |
| 202 | 'google_analytics' |
| 203 | ); |
| 204 | |
| 205 | const learned = yield* waitForLearnedFile(content => |
| 206 | content.includes(UNRELEASED_TOOLKIT) |
| 207 | ); |
| 208 | expect(learned).toContain('google_analytics'); |
| 209 | }) |
| 210 | ) |
| 211 | ); |
| 212 | |
| 213 | it.live('resolves a learned toolkit without touching the network', () => |
| 214 | withResolver( |
| 215 | { |
| 216 | seedLearnedFile: learnedFileContent([UNRELEASED_TOOLKIT]), |
| 217 | }, |
| 218 | ({ calls }) => |
| 219 | Effect.gen(function* () { |
| 220 | expect(yield* toolkitFromToolSlug('ACME_ANALYTICS_RUN_REPORT')).toBe(UNRELEASED_TOOLKIT); |
| 221 | expect(calls()).toBe(0); |
| 222 | }) |
| 223 | ) |
| 224 | ); |
| 225 | |
| 226 | it.live('fetches the catalog for an unknown slug, and remembers what it finds', () => |
| 227 | withResolver( |
| 228 | { |
| 229 | getToolkits: () => Effect.succeed([makeToolkitFixture(UNRELEASED_TOOLKIT)]), |
| 230 | seedLearnedFile: learnedFileContent([]), |
| 231 | }, |
| 232 | ({ calls, waitForLearnedFile }) => |
| 233 | Effect.gen(function* () { |
| 234 | expect(yield* toolkitFromToolSlug('ACME_ANALYTICS_RUN_REPORT')).toBe(UNRELEASED_TOOLKIT); |
| 235 | expect(calls()).toBe(1); |
| 236 | |
| 237 | const learned = yield* waitForLearnedFile(content => |
| 238 | content.includes(UNRELEASED_TOOLKIT) |
| 239 | ); |
| 240 | // Baked slugs are recorded alongside it, so the file stands alone. |
| 241 | expect(learned).toContain('google_analytics'); |
| 242 | }) |
| 243 | ) |
| 244 | ); |
| 245 | |
| 246 | it.live('merges later recordings in a run instead of dropping them', () => |
| 247 | withResolver({ seedLearnedFile: learnedFileContent([]) }, ({ waitForLearnedFile }) => |
| 248 | Effect.gen(function* () { |
| 249 | const catalog = yield* ToolkitSlugCatalog; |
| 250 | // Misses in one run can see different catalogs — an unscoped startup |
| 251 | // lookup, then the command's project — so a later recording that |
| 252 | // brings new slugs must land alongside the first one. |
| 253 | yield* catalog.remember([UNRELEASED_TOOLKIT]); |
| 254 | yield* catalog.remember([UNRELEASED_TOOLKIT, CUSTOM_TOOLKIT]); |
| 255 | |
| 256 | const learned = yield* waitForLearnedFile(content => content.includes(CUSTOM_TOOLKIT)); |
| 257 | expect(learned).toContain(UNRELEASED_TOOLKIT); |
| 258 | }) |
| 259 | ) |
| 260 | ); |
| 261 | |
| 262 | it.live('falls back to the first-underscore guess when the catalog is unreachable', () => |
| 263 | withResolver( |
| 264 | { |
| 265 | getToolkits: failingFetch, |
| 266 | seedLearnedFile: learnedFileContent([]), |
| 267 | }, |
| 268 | ({ calls }) => |
| 269 | Effect.gen(function* () { |
| 270 | expect(yield* toolkitFromToolSlug('ACME_ANALYTICS_RUN_REPORT')).toBe('acme'); |
| 271 | expect(calls()).toBe(1); |
| 272 | }) |
| 273 | ) |
| 274 | ); |
| 275 | |
| 276 | it.live('tolerates a corrupted learned-slugs file', () => |
| 277 | withResolver({ seedLearnedFile: '{ not json' }, ({ calls }) => |
| 278 | Effect.gen(function* () { |
| 279 | expect(yield* toolkitFromToolSlug('GOOGLE_ANALYTICS_RUN_REPORT')).toBe('google_analytics'); |
| 280 | expect(calls()).toBeLessThanOrEqual(1); |
| 281 | }) |
| 282 | ) |
| 283 | ); |
| 284 | |
| 285 | it.live('refreshes learned slugs in the background once they go stale', () => |
| 286 | withResolver( |
| 287 | { |
| 288 | getToolkits: () => Effect.succeed([makeToolkitFixture(UNRELEASED_TOOLKIT)]), |
| 289 | seedLearnedFile: learnedFileContent(['stale_toolkit'], 8), |
| 290 | }, |
| 291 | ({ waitForLearnedFile }) => |
| 292 | Effect.gen(function* () { |
| 293 | // Answered from the baked list — the refresh is what the fetch is for. |
| 294 | expect(yield* toolkitFromToolSlug('GOOGLE_ANALYTICS_RUN_REPORT')).toBe( |
| 295 | 'google_analytics' |
| 296 | ); |
| 297 | |
| 298 | const learned = yield* waitForLearnedFile(content => |
| 299 | content.includes(UNRELEASED_TOOLKIT) |
| 300 | ); |
| 301 | expect(learned).toContain('stale_toolkit'); |
| 302 | }) |
| 303 | ) |
| 304 | ); |
| 305 | |
| 306 | it.live('reads local knowledge once, however many slugs a run resolves', () => |
| 307 | withResolver( |
| 308 | { |
| 309 | getToolkits: () => Effect.succeed([makeToolkitFixture(UNRELEASED_TOOLKIT)]), |
| 310 | seedLearnedFile: learnedFileContent(['stale_toolkit'], 8), |
| 311 | }, |
| 312 | ({ calls, waitForLearnedFile }) => |
| 313 | Effect.gen(function* () { |
| 314 | expect(yield* toolkitFromToolSlug('GOOGLE_ANALYTICS_RUN_REPORT')).toBe( |
| 315 | 'google_analytics' |
| 316 | ); |
| 317 | expect(yield* toolkitFromToolSlug('GMAIL_SEND_EMAIL')).toBe('gmail'); |
| 318 | expect(yield* toolkitFromToolSlug('GITHUB_CREATE_AN_ISSUE')).toBe('github'); |
| 319 | |
| 320 | yield* waitForLearnedFile(content => content.includes(UNRELEASED_TOOLKIT)); |
| 321 | // The staleness verdict cannot change under a running process, so the |
| 322 | // refresh it triggers belongs to the run, not to each resolution. |
| 323 | expect(calls()).toBe(1); |
| 324 | }) |
| 325 | ) |
| 326 | ); |
| 327 | |
| 328 | it.live('does not refresh while the learned slugs are fresh', () => |
| 329 | withResolver( |
| 330 | { |
| 331 | getToolkits: failingFetch, |
| 332 | seedLearnedFile: learnedFileContent([UNRELEASED_TOOLKIT], 6), |
| 333 | }, |
| 334 | ({ calls }) => |
| 335 | Effect.gen(function* () { |
| 336 | expect(yield* toolkitFromToolSlug('ACME_ANALYTICS_RUN_REPORT')).toBe(UNRELEASED_TOOLKIT); |
| 337 | yield* Effect.sleep('50 millis'); |
| 338 | expect(calls()).toBe(0); |
| 339 | }) |
| 340 | ) |
| 341 | ); |
| 342 | describe('project-managed custom toolkits', () => { |
| 343 | it.live('resolves a custom toolkit from the project catalog, and remembers it', () => |
| 344 | withResolver( |
| 345 | { |
| 346 | getProjectToolkits: () => Effect.succeed([makeToolkitFixture(CUSTOM_TOOLKIT)]), |
| 347 | seedLearnedFile: learnedFileContent([]), |
| 348 | }, |
| 349 | ({ calls, projectCalls, waitForLearnedFile }) => |
| 350 | Effect.gen(function* () { |
| 351 | expect(yield* toolkitFromToolSlug('CUSTOM_GRAIN_SEARCH_PERSONS')).toBe(CUSTOM_TOOLKIT); |
| 352 | expect(calls()).toBe(1); |
| 353 | expect(projectCalls()).toBe(1); |
| 354 | |
| 355 | yield* waitForLearnedFile(content => content.includes(CUSTOM_TOOLKIT)); |
| 356 | }) |
| 357 | ) |
| 358 | ); |
| 359 | |
| 360 | it.live('uses a remembered custom toolkit when the project catalog is unreachable', () => |
| 361 | withResolver( |
| 362 | { |
| 363 | getToolkits: failingFetch, |
| 364 | getProjectToolkits: failingFetch, |
| 365 | seedLearnedFile: learnedFileContent([CUSTOM_TOOLKIT]), |
| 366 | }, |
| 367 | ({ calls, projectCalls }) => |
| 368 | Effect.gen(function* () { |
| 369 | expect(yield* toolkitFromToolSlug('CUSTOM_GRAIN_SEARCH_PERSONS')).toBe(CUSTOM_TOOLKIT); |
| 370 | expect(calls()).toBe(1); |
| 371 | expect(projectCalls()).toBe(1); |
| 372 | }) |
| 373 | ) |
| 374 | ); |
| 375 | |
| 376 | it.live.each(['custom_grain', 'custom_grain_search_persons'])( |
| 377 | "uses the active project's prefix instead of learned %s", |
| 378 | learned => |
| 379 | withResolver( |
| 380 | { |
| 381 | seedLearnedFile: learnedFileContent([learned]), |
| 382 | getProjectToolkits: projectCatalog({ proj_b: ['custom_grain_search'] }), |
| 383 | }, |
| 384 | () => |
| 385 | Effect.gen(function* () { |
| 386 | expect( |
| 387 | yield* toolkitFromToolSlug('CUSTOM_GRAIN_SEARCH_PERSONS_LIST', { |
| 388 | orgId: 'org_1', |
| 389 | projectId: 'proj_b', |
| 390 | }) |
| 391 | ).toBe('custom_grain_search'); |
| 392 | expect( |
| 393 | yield* toolkitFromToolSlug('CUSTOM_GRAIN_SEARCH_PERSONS_LIST', { |
| 394 | orgId: 'org_1', |
| 395 | projectId: 'proj_empty', |
| 396 | }) |
| 397 | ).toBe('custom'); |
| 398 | }) |
| 399 | ) |
| 400 | ); |
| 401 | |
| 402 | it.live('keeps a scoped discovery when an older background fetch completes later', () => |
| 403 | Effect.gen(function* () { |
| 404 | const releaseRefresh = yield* Deferred.make<void>(); |
| 405 | yield* withResolver( |
| 406 | { |
| 407 | seedLearnedFile: learnedFileContent([], 8), |
| 408 | getToolkits: () => |
| 409 | Deferred.await(releaseRefresh).pipe( |
| 410 | Effect.as([makeToolkitFixture(UNRELEASED_TOOLKIT)]) |
| 411 | ), |
| 412 | }, |
| 413 | ({ waitForLearnedFile }) => |
| 414 | Effect.gen(function* () { |
| 415 | expect(yield* toolkitFromToolSlug('GMAIL_SEND_EMAIL')).toBe('gmail'); |
| 416 | const catalog = yield* ToolkitSlugCatalog; |
| 417 | yield* catalog.remember([CUSTOM_TOOLKIT]); |
| 418 | yield* waitForLearnedFile(content => content.includes(CUSTOM_TOOLKIT)); |
| 419 | yield* Deferred.succeed(releaseRefresh, undefined); |
| 420 | const learned = yield* waitForLearnedFile(content => |
| 421 | content.includes(UNRELEASED_TOOLKIT) |
| 422 | ); |
| 423 | expect(learned).toContain(CUSTOM_TOOLKIT); |
| 424 | }) |
| 425 | ); |
| 426 | }) |
| 427 | ); |
| 428 | |
| 429 | it.live('retries a stale native catalog after remembering a partial project result', () => |
| 430 | Effect.gen(function* () { |
| 431 | const cacheDir = tempy.temporaryDirectory(); |
| 432 | const seed = learnedFileContent([], 8); |
| 433 | yield* runInCacheDir( |
| 434 | cacheDir, |
| 435 | { |
| 436 | seedLearnedFile: seed, |
| 437 | getToolkits: failingFetch, |
| 438 | getProjectToolkits: () => Effect.succeed([makeToolkitFixture(CUSTOM_TOOLKIT)]), |
| 439 | }, |
| 440 | ({ waitForLearnedFile }) => |
| 441 | Effect.gen(function* () { |
| 442 | expect(yield* toolkitFromToolSlug('CUSTOM_GRAIN_SEARCH_PERSONS')).toBe( |
| 443 | CUSTOM_TOOLKIT |
| 444 | ); |
| 445 | const learned = yield* waitForLearnedFile(content => |
| 446 | content.includes(CUSTOM_TOOLKIT) |
| 447 | ); |
| 448 | expect(JSON.parse(learned).refreshedAt).toBe(JSON.parse(seed).refreshedAt); |
| 449 | }) |
| 450 | ); |
| 451 | yield* runInCacheDir( |
| 452 | cacheDir, |
| 453 | { |
| 454 | getToolkits: () => Effect.succeed([makeToolkitFixture(UNRELEASED_TOOLKIT)]), |
| 455 | }, |
| 456 | ({ waitForLearnedFile }) => |
| 457 | Effect.gen(function* () { |
| 458 | expect(yield* toolkitFromToolSlug('GMAIL_SEND_EMAIL')).toBe('gmail'); |
| 459 | const learned = yield* waitForLearnedFile(content => |
| 460 | content.includes(UNRELEASED_TOOLKIT) |
| 461 | ); |
| 462 | expect(learned).toContain(CUSTOM_TOOLKIT); |
| 463 | expect(JSON.parse(learned).refreshedAt).not.toBe(JSON.parse(seed).refreshedAt); |
| 464 | }) |
| 465 | ); |
| 466 | }) |
| 467 | ); |
| 468 | |
| 469 | it.live('preserves learned custom slugs when an unscoped refresh cannot list them', () => |
| 470 | withResolver( |
| 471 | { |
| 472 | seedLearnedFile: learnedFileContent([CUSTOM_TOOLKIT], 8), |
| 473 | getToolkits: () => Effect.succeed([makeToolkitFixture(UNRELEASED_TOOLKIT)]), |
| 474 | getProjectToolkits: failingFetch, |
| 475 | }, |
| 476 | ({ waitForLearnedFile }) => |
| 477 | Effect.gen(function* () { |
| 478 | expect(yield* toolkitFromToolSlug('GMAIL_SEND_EMAIL')).toBe('gmail'); |
| 479 | const learned = yield* waitForLearnedFile(content => |
| 480 | content.includes(UNRELEASED_TOOLKIT) |
| 481 | ); |
| 482 | expect(learned).toContain(CUSTOM_TOOLKIT); |
| 483 | }) |
| 484 | ) |
| 485 | ); |
| 486 | |
| 487 | it.live('keeps resolving baked `custom*` toolkits locally', () => |
| 488 | withResolver( |
| 489 | { seedLearnedFile: learnedFileContent([CUSTOM_TOOLKIT]) }, |
| 490 | ({ calls, projectCalls }) => |
| 491 | Effect.gen(function* () { |
| 492 | expect(yield* toolkitFromToolSlug('CUSTOMERIO_SEND_EVENT')).toBe('customerio'); |
| 493 | expect(calls()).toBe(0); |
| 494 | expect(projectCalls()).toBe(0); |
| 495 | }) |
| 496 | ) |
| 497 | ); |
| 498 | |
| 499 | it.live('prefers the longest custom toolkit prefix', () => |
| 500 | withResolver( |
| 501 | { |
| 502 | getProjectToolkits: () => |
| 503 | Effect.succeed([ |
| 504 | makeToolkitFixture(CUSTOM_TOOLKIT), |
| 505 | makeToolkitFixture(`${CUSTOM_TOOLKIT}_search`), |
| 506 | ]), |
| 507 | seedLearnedFile: learnedFileContent([]), |
| 508 | }, |
| 509 | () => |
| 510 | Effect.gen(function* () { |
| 511 | expect(yield* toolkitFromToolSlug('CUSTOM_GRAIN_SEARCH_PERSONS')).toBe( |
| 512 | `${CUSTOM_TOOLKIT}_search` |
| 513 | ); |
| 514 | }) |
| 515 | ) |
| 516 | ); |
| 517 | |
| 518 | it.live('still resolves native toolkits when the project catalog is unreachable', () => |
| 519 | withResolver( |
| 520 | { |
| 521 | getToolkits: () => Effect.succeed([makeToolkitFixture(UNRELEASED_TOOLKIT)]), |
| 522 | getProjectToolkits: failingFetch, |
| 523 | seedLearnedFile: learnedFileContent([]), |
| 524 | }, |
| 525 | () => |
| 526 | Effect.gen(function* () { |
| 527 | expect(yield* toolkitFromToolSlug('ACME_ANALYTICS_RUN_REPORT')).toBe( |
| 528 | UNRELEASED_TOOLKIT |
| 529 | ); |
| 530 | expect(yield* toolkitFromToolSlug('CUSTOM_GRAIN_SEARCH_PERSONS')).toBe('custom'); |
| 531 | }) |
| 532 | ) |
| 533 | ); |
| 534 | |
| 535 | it.live('resolves custom toolkits when only the native catalog is unreachable', () => |
| 536 | withResolver( |
| 537 | { |
| 538 | getToolkits: failingFetch, |
| 539 | getProjectToolkits: () => Effect.succeed([makeToolkitFixture(CUSTOM_TOOLKIT)]), |
| 540 | seedLearnedFile: learnedFileContent([]), |
| 541 | }, |
| 542 | () => |
| 543 | Effect.gen(function* () { |
| 544 | expect(yield* toolkitFromToolSlug('CUSTOM_GRAIN_SEARCH_PERSONS')).toBe(CUSTOM_TOOLKIT); |
| 545 | }) |
| 546 | ) |
| 547 | ); |
| 548 | |
| 549 | it.live('guesses, and records nothing, when both catalogs are unreachable', () => { |
| 550 | const seed = learnedFileContent([]); |
| 551 | return withResolver( |
| 552 | { |
| 553 | getToolkits: failingFetch, |
| 554 | getProjectToolkits: failingFetch, |
| 555 | seedLearnedFile: seed, |
| 556 | }, |
| 557 | ({ waitForLearnedFile }) => |
| 558 | Effect.gen(function* () { |
| 559 | expect(yield* toolkitFromToolSlug('CUSTOM_GRAIN_SEARCH_PERSONS')).toBe('custom'); |
| 560 | yield* Effect.sleep('50 millis'); |
| 561 | expect(yield* waitForLearnedFile(() => true)).toBe(seed); |
| 562 | }) |
| 563 | ); |
| 564 | }); |
| 565 | |
| 566 | it.live('learns a custom toolkit found by a scoped lookup after an unscoped one', () => |
| 567 | withResolver( |
| 568 | { |
| 569 | getProjectToolkits: projectCatalog({ proj_1: [CUSTOM_TOOLKIT] }), |
| 570 | seedLearnedFile: learnedFileContent([]), |
| 571 | }, |
| 572 | ({ waitForLearnedFile }) => |
| 573 | Effect.gen(function* () { |
| 574 | // Startup telemetry resolves before the command knows its project. |
| 575 | expect(yield* toolkitFromToolSlug('CUSTOM_GRAIN_SEARCH_PERSONS')).toBe('custom'); |
| 576 | expect( |
| 577 | yield* toolkitFromToolSlug('CUSTOM_GRAIN_SEARCH_PERSONS', { |
| 578 | orgId: 'org_1', |
| 579 | projectId: 'proj_1', |
| 580 | }) |
| 581 | ).toBe(CUSTOM_TOOLKIT); |
| 582 | |
| 583 | yield* waitForLearnedFile(content => content.includes(CUSTOM_TOOLKIT)); |
| 584 | }) |
| 585 | ) |
| 586 | ); |
| 587 | |
| 588 | it.live('includes project toolkits in the background refresh', () => |
| 589 | withResolver( |
| 590 | { |
| 591 | getProjectToolkits: () => Effect.succeed([makeToolkitFixture(CUSTOM_TOOLKIT)]), |
| 592 | seedLearnedFile: learnedFileContent(['stale_toolkit'], 8), |
| 593 | }, |
| 594 | ({ waitForLearnedFile }) => |
| 595 | Effect.gen(function* () { |
| 596 | expect(yield* toolkitFromToolSlug('GMAIL_SEND_EMAIL')).toBe('gmail'); |
| 597 | yield* waitForLearnedFile(content => content.includes(CUSTOM_TOOLKIT)); |
| 598 | }) |
| 599 | ) |
| 600 | ); |
| 601 | }); |
| 602 | }); |