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, 03:37 UTC
| 1 | import { describe, expect, test } from 'bun:test'; |
| 2 | import { Effect, Schema } from 'effect'; |
| 3 | import { GitHub, request } from '../scripts/sdk-release/github'; |
| 4 | import { json, runRelease, type Fetch } from './sdk-release-harness'; |
| 5 | |
| 6 | const Ref = Schema.Struct({ ref: Schema.String }); |
| 7 | |
| 8 | /** Answers each request with the next response and counts the requests. */ |
| 9 | const scripted = (...responses: Array<() => Response | Promise<Response>>) => { |
| 10 | const seen: string[] = []; |
| 11 | const fetcher: Fetch = async (input, init) => { |
| 12 | seen.push(`${init?.method ?? 'GET'} ${new URL(String(input)).pathname}`); |
| 13 | const next = responses[Math.min(seen.length, responses.length) - 1]; |
| 14 | return next(); |
| 15 | }; |
| 16 | return { fetcher, seen }; |
| 17 | }; |
| 18 | |
| 19 | const githubError = (status: number, message: string, headers: Record<string, string> = {}) => |
| 20 | new Response(JSON.stringify({ message, documentation_url: 'https://docs.github.com' }), { |
| 21 | status, |
| 22 | headers: { 'x-github-request-id': 'C766:174D:1', ...headers }, |
| 23 | }); |
| 24 | |
| 25 | const call = ( |
| 26 | fetcher: Fetch, |
| 27 | method: 'GET' | 'POST' = 'GET', |
| 28 | policy: Parameters<typeof GitHub.layer>[1] = { baseDelayMs: 1 } |
| 29 | ) => |
| 30 | runRelease( |
| 31 | request( |
| 32 | method, |
| 33 | '/repos/ComposioHQ/composio/git/refs', |
| 34 | Ref, |
| 35 | method === 'GET' ? undefined : {} |
| 36 | ).pipe(Effect.provide(GitHub.layer('token', policy))), |
| 37 | fetcher |
| 38 | ); |
| 39 | |
| 40 | describe('GitHub client', () => { |
| 41 | test("a 422 reports GitHub's message and request ID and is not retried", async () => { |
| 42 | const github = scripted(() => githubError(422, 'Reference already exists')); |
| 43 | const failure = call(github.fetcher, 'POST'); |
| 44 | await expect(failure).rejects.toThrow('Reference already exists'); |
| 45 | await expect(failure).rejects.toThrow('C766:174D:1'); |
| 46 | expect(github.seen).toHaveLength(1); |
| 47 | }); |
| 48 | |
| 49 | test('a GET that returns 502 succeeds on the retry', async () => { |
| 50 | const github = scripted( |
| 51 | () => githubError(502, 'Server Error'), |
| 52 | () => json({ ref: 'refs/heads/next' }) |
| 53 | ); |
| 54 | expect(await call(github.fetcher)).toEqual({ ref: 'refs/heads/next' }); |
| 55 | expect(github.seen).toHaveLength(2); |
| 56 | }); |
| 57 | |
| 58 | test('a POST that returns 502 is not retried, since GitHub may have applied it', async () => { |
| 59 | const github = scripted(() => githubError(502, 'Server Error')); |
| 60 | const failure = call(github.fetcher, 'POST'); |
| 61 | await expect(failure).rejects.toThrow('(502): Server Error'); |
| 62 | await expect(failure).rejects.toThrow('C766:174D:1'); |
| 63 | expect(github.seen).toHaveLength(1); |
| 64 | }); |
| 65 | |
| 66 | test('a rate-limited write waits for Retry-After and retries', async () => { |
| 67 | const github = scripted( |
| 68 | () => |
| 69 | githubError(403, 'You have exceeded a secondary rate limit.', { |
| 70 | 'x-ratelimit-remaining': '0', |
| 71 | 'retry-after': '1', |
| 72 | }), |
| 73 | () => json({ ref: 'refs/tags/sdk-release/8' }) |
| 74 | ); |
| 75 | const started = performance.now(); |
| 76 | expect(await call(github.fetcher, 'POST')).toEqual({ ref: 'refs/tags/sdk-release/8' }); |
| 77 | expect(performance.now() - started).toBeGreaterThanOrEqual(950); |
| 78 | expect(github.seen).toHaveLength(2); |
| 79 | }); |
| 80 | |
| 81 | test("a 403 that is not a rate limit fails at once with GitHub's message", async () => { |
| 82 | const github = scripted(() => githubError(403, 'Resource not accessible by integration')); |
| 83 | await expect(call(github.fetcher)).rejects.toThrow( |
| 84 | '(403): Resource not accessible by integration' |
| 85 | ); |
| 86 | expect(github.seen).toHaveLength(1); |
| 87 | }); |
| 88 | |
| 89 | test('a rate limit asking for a longer wait than the policy allows gives up at once', async () => { |
| 90 | const github = scripted(() => githubError(429, 'Too Many Requests', { 'retry-after': '3600' })); |
| 91 | await expect(call(github.fetcher)).rejects.toThrow('gave up after 1 attempt'); |
| 92 | expect(github.seen).toHaveLength(1); |
| 93 | }); |
| 94 | |
| 95 | test('repeated 503s stop after the attempt budget', async () => { |
| 96 | const github = scripted(() => githubError(503, 'Service Unavailable')); |
| 97 | await expect(call(github.fetcher)).rejects.toThrow('after 5 attempts'); |
| 98 | expect(github.seen).toHaveLength(5); |
| 99 | }); |
| 100 | |
| 101 | test('a request that times out is retried', async () => { |
| 102 | const github = scripted( |
| 103 | () => new Promise<Response>(() => {}), |
| 104 | () => json({ ref: 'refs/heads/next' }) |
| 105 | ); |
| 106 | expect(await call(github.fetcher, 'GET', { baseDelayMs: 1, timeout: '50 millis' })).toEqual({ |
| 107 | ref: 'refs/heads/next', |
| 108 | }); |
| 109 | expect(github.seen).toHaveLength(2); |
| 110 | }); |
| 111 | |
| 112 | test('a response whose body stalls after its headers times out and is retried', async () => { |
| 113 | const stalledBody = () => |
| 114 | new Response( |
| 115 | new ReadableStream({ |
| 116 | start: controller => controller.enqueue(new TextEncoder().encode('{"ref":')), |
| 117 | }), |
| 118 | { status: 200 } |
| 119 | ); |
| 120 | const github = scripted(stalledBody, () => json({ ref: 'refs/heads/next' })); |
| 121 | expect(await call(github.fetcher, 'GET', { baseDelayMs: 1, timeout: '50 millis' })).toEqual({ |
| 122 | ref: 'refs/heads/next', |
| 123 | }); |
| 124 | expect(github.seen).toHaveLength(2); |
| 125 | }); |
| 126 | |
| 127 | test('a response the schema cannot read is not retried', async () => { |
| 128 | const github = scripted(() => json({ object: {} })); |
| 129 | await expect(call(github.fetcher)).rejects.toThrow('cannot read'); |
| 130 | expect(github.seen).toHaveLength(1); |
| 131 | }); |
| 132 | }); |