Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, expect, it } from '@effect/vitest'; |
| 2 | import { Clock, Effect, Exit, Fiber } from 'effect'; |
| 3 | import { TestClock } from 'effect/testing'; |
| 4 | import { |
| 5 | AUTHORIZATION_POLL_INTERVAL_SECONDS, |
| 6 | AUTHORIZATION_TIMEOUT_MINUTES, |
| 7 | AUTHORIZATION_TIMEOUT_SECONDS, |
| 8 | authorizationPollSchedule, |
| 9 | authorizationPollScheduleFor, |
| 10 | } from 'src/effects/authorization-poll'; |
| 11 | |
| 12 | /** The test-clock times of every check of an authorization that never completes. */ |
| 13 | const checksOfAPendingAuthorization = (schedule: ReturnType<typeof authorizationPollScheduleFor>) => |
| 14 | Effect.gen(function* () { |
| 15 | const checkedAt: number[] = []; |
| 16 | const check = Effect.flatMap(Clock.currentTimeMillis, now => { |
| 17 | checkedAt.push(now); |
| 18 | return Effect.fail('still pending' as const); |
| 19 | }); |
| 20 | const fiber = yield* Effect.forkChild(Effect.exit(Effect.retry(check, schedule))); |
| 21 | // Moves the clock in 100ms steps for as long as the retry runs; it ends by itself. |
| 22 | const pump = yield* Effect.forkChild( |
| 23 | Effect.forever(Effect.andThen(Effect.yieldNow, () => TestClock.adjust('100 millis'))) |
| 24 | ); |
| 25 | const exit = yield* Fiber.join(fiber); |
| 26 | yield* Fiber.interrupt(pump); |
| 27 | return { exit, checkedAt }; |
| 28 | }); |
| 29 | |
| 30 | describe('authorizationPollSchedule', () => { |
| 31 | it.effect('ramps from 0.3s and never waits longer than the interval between checks', () => |
| 32 | Effect.gen(function* () { |
| 33 | const { checkedAt } = yield* checksOfAPendingAuthorization(authorizationPollSchedule); |
| 34 | const gaps = checkedAt.slice(1).map((time, index) => time - checkedAt[index]!); |
| 35 | |
| 36 | expect(gaps.slice(0, 6)).toEqual([300, 600, 1200, 2400, 4800, 5000]); |
| 37 | expect(Math.max(...gaps)).toBe(AUTHORIZATION_POLL_INTERVAL_SECONDS * 1000); |
| 38 | }) |
| 39 | ); |
| 40 | |
| 41 | it.effect('gives up once the timeout has passed, not hours later', () => |
| 42 | Effect.gen(function* () { |
| 43 | // The wait a caller asks for no particular bound keeps: a pending login's whole lifetime. |
| 44 | expect(AUTHORIZATION_TIMEOUT_SECONDS).toBe(AUTHORIZATION_TIMEOUT_MINUTES * 60); |
| 45 | |
| 46 | const { exit, checkedAt } = yield* checksOfAPendingAuthorization(authorizationPollSchedule); |
| 47 | const timeoutMs = AUTHORIZATION_TIMEOUT_SECONDS * 1000; |
| 48 | |
| 49 | expect(Exit.isFailure(exit)).toBe(true); |
| 50 | expect(checkedAt.at(-1)).toBeGreaterThan(timeoutMs - 5_000); |
| 51 | expect(checkedAt.at(-1)).toBeLessThanOrEqual(timeoutMs + 5_000); |
| 52 | }) |
| 53 | ); |
| 54 | |
| 55 | it.effect('stops at the shorter bound a caller asks for', () => |
| 56 | Effect.gen(function* () { |
| 57 | const { exit, checkedAt } = yield* checksOfAPendingAuthorization( |
| 58 | authorizationPollScheduleFor(30) |
| 59 | ); |
| 60 | const lastCheckMs = checkedAt.at(-1)!; |
| 61 | |
| 62 | expect(Exit.isFailure(exit)).toBe(true); |
| 63 | expect(lastCheckMs).toBeGreaterThan(25_000); |
| 64 | expect(lastCheckMs).toBeLessThanOrEqual(35_000); |
| 65 | }) |
| 66 | ); |
| 67 | }); |