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 { Duration, Effect, Fiber, FileSystem, Layer, Path } from 'effect'; |
| 2 | import { FetchHttpClient } from 'effect/http'; |
| 3 | import { TestClock } from 'effect/testing'; |
| 4 | import { GitHub } from '../scripts/sdk-release/github'; |
| 5 | import { ReleaseServices, readFile } from '../scripts/sdk-release/runtime'; |
| 6 | |
| 7 | export type Fetch = (input: string | URL | Request, init?: RequestInit) => Promise<Response>; |
| 8 | |
| 9 | // Retries back off for a millisecond, so a test of a failing request does not wait seconds. |
| 10 | const TestServices = Layer.provideMerge(GitHub.layer('token', { baseDelayMs: 1 }), ReleaseServices); |
| 11 | type TestServices = Layer.Success<typeof TestServices>; |
| 12 | |
| 13 | /** Runs a release effect with Bun platform services, a test GitHub token, and an optional fake fetch. */ |
| 14 | export const runRelease = <A, E>( |
| 15 | effect: Effect.Effect<A, E, TestServices>, |
| 16 | fetch?: Fetch |
| 17 | ): Promise<A> => { |
| 18 | const withFetch = fetch |
| 19 | ? effect.pipe(Effect.provideService(FetchHttpClient.Fetch, fetch as typeof globalThis.fetch)) |
| 20 | : effect; |
| 21 | return Effect.runPromise(withFetch.pipe(Effect.provide(TestServices))); |
| 22 | }; |
| 23 | |
| 24 | /** One real macrotask, so the fake fetch's promises settle between test-clock steps. */ |
| 25 | const settle = Effect.promise(() => new Promise<void>(resolve => setTimeout(resolve, 0))); |
| 26 | |
| 27 | /** |
| 28 | * Runs a release effect on a test clock that moves `step` at a time until the effect ends, so a |
| 29 | * registry wait of many minutes takes milliseconds. `fetch` reads the test clock through `now`, for |
| 30 | * a registry that starts serving something at a given minute. |
| 31 | */ |
| 32 | export const runOnTestClock = <A, E>( |
| 33 | effect: Effect.Effect<A, E, TestServices>, |
| 34 | fetch: (now: () => number) => Fetch, |
| 35 | options: { readonly step?: Duration.Input; readonly limit?: Duration.Input } = {} |
| 36 | ): Promise<A> => |
| 37 | runRelease( |
| 38 | Effect.gen(function* () { |
| 39 | const clock = yield* TestClock.testClockWith(Effect.succeed); |
| 40 | const now = () => clock.currentTimeMillisUnsafe(); |
| 41 | const fiber = yield* Effect.forkChild( |
| 42 | effect.pipe( |
| 43 | Effect.provideService(FetchHttpClient.Fetch, fetch(now) as typeof globalThis.fetch) |
| 44 | ) |
| 45 | ); |
| 46 | const limit = Duration.toMillis(options.limit ?? '1 hour'); |
| 47 | while (fiber.pollUnsafe() === undefined && now() < limit) { |
| 48 | yield* TestClock.adjust(options.step ?? '5 seconds'); |
| 49 | yield* settle; |
| 50 | } |
| 51 | if (fiber.pollUnsafe() === undefined) { |
| 52 | yield* Fiber.interrupt(fiber); |
| 53 | return yield* Effect.die(new Error(`Still running after ${limit / 60_000} test minutes`)); |
| 54 | } |
| 55 | return yield* Fiber.join(fiber); |
| 56 | }).pipe(Effect.provide(TestClock.layer())) |
| 57 | ); |
| 58 | |
| 59 | export const json = (value: unknown, status = 200) => |
| 60 | new Response(JSON.stringify(value), { |
| 61 | status, |
| 62 | headers: { 'content-type': 'application/json' }, |
| 63 | }); |
| 64 | |
| 65 | /** Temporary directories created by a suite; call `cleanup` from `afterEach`. */ |
| 66 | export const temporaryDirectories = (prefix: string) => { |
| 67 | const created: string[] = []; |
| 68 | return { |
| 69 | create: () => |
| 70 | runRelease( |
| 71 | Effect.gen(function* () { |
| 72 | const fs = yield* FileSystem.FileSystem; |
| 73 | const directory = yield* fs.makeTempDirectory({ prefix }); |
| 74 | created.push(directory); |
| 75 | return directory; |
| 76 | }) |
| 77 | ), |
| 78 | cleanup: () => |
| 79 | runRelease( |
| 80 | Effect.gen(function* () { |
| 81 | const fs = yield* FileSystem.FileSystem; |
| 82 | yield* Effect.forEach(created.splice(0), directory => |
| 83 | fs.remove(directory, { recursive: true, force: true }) |
| 84 | ); |
| 85 | }) |
| 86 | ), |
| 87 | }; |
| 88 | }; |
| 89 | |
| 90 | export const writeText = (file: string, contents: string) => |
| 91 | runRelease( |
| 92 | Effect.gen(function* () { |
| 93 | const fs = yield* FileSystem.FileSystem; |
| 94 | const path = yield* Path.Path; |
| 95 | yield* fs.makeDirectory(path.dirname(file), { recursive: true }); |
| 96 | yield* fs.writeFileString(file, contents); |
| 97 | }) |
| 98 | ); |
| 99 | |
| 100 | export const readText = (file: string) => runRelease(readFile(file)); |
| 101 | |
| 102 | const withFileSystem = <A, E>(use: (fs: FileSystem.FileSystem) => Effect.Effect<A, E>) => |
| 103 | runRelease( |
| 104 | Effect.gen(function* () { |
| 105 | return yield* use(yield* FileSystem.FileSystem); |
| 106 | }) |
| 107 | ); |
| 108 | |
| 109 | export const exists = (file: string) => withFileSystem(fs => fs.exists(file)); |
| 110 | export const remove = (file: string) => withFileSystem(fs => fs.remove(file)); |
| 111 | export const symlink = (target: string, link: string) => |
| 112 | withFileSystem(fs => fs.symlink(target, link)); |
| 113 | export const makeExecutable = (file: string) => withFileSystem(fs => fs.chmod(file, 0o755)); |
| 114 | |
| 115 | export const resolvePath = (...segments: string[]) => |
| 116 | runRelease( |
| 117 | Effect.gen(function* () { |
| 118 | return (yield* Path.Path).resolve(...segments); |
| 119 | }) |
| 120 | ); |