The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Fly from "@alchemy.run/fly"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as addons from "@distilled.cloud/fly-io/addons"; |
| 5 | import * as machines from "@distilled.cloud/fly-io/machines"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import BucketApi, { BucketIp, BucketSite, Data, OBJECT_BODY } from "./fixtures/bucket-api.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: Fly.providers() }); |
| 14 | |
| 15 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 16 | |
| 17 | const listTigris = () => |
| 18 | Effect.gen(function* () { |
| 19 | const rows: Array<{ id: string; name: string | null; options: unknown }> = []; |
| 20 | let after: string | undefined; |
| 21 | for (let i = 0; i < 8; i++) { |
| 22 | const page = yield* addons.addOns({ |
| 23 | type: "tigris", |
| 24 | first: 50, |
| 25 | after, |
| 26 | }); |
| 27 | for (const edge of page.edges ?? []) { |
| 28 | if (edge?.node != null) rows.push(edge.node); |
| 29 | } |
| 30 | if (!page.pageInfo.hasNextPage) break; |
| 31 | after = page.pageInfo.endCursor ?? undefined; |
| 32 | if (after === undefined || after.length === 0) break; |
| 33 | } |
| 34 | return rows; |
| 35 | }); |
| 36 | |
| 37 | const findAddOn = (addOnId: string, name: string) => |
| 38 | listTigris().pipe( |
| 39 | Effect.map( |
| 40 | (addOns) => |
| 41 | addOns.find((addOn) => addOn.id === addOnId) ?? addOns.find((addOn) => addOn.name === name), |
| 42 | ), |
| 43 | ); |
| 44 | |
| 45 | const waitUntilBucketGone = (addOnId: string, name: string) => |
| 46 | findAddOn(addOnId, name).pipe( |
| 47 | Effect.map((addOn) => (addOn === undefined ? ("gone" as const) : ("found" as const))), |
| 48 | Effect.repeat({ |
| 49 | schedule: Schedule.spaced("2 seconds"), |
| 50 | until: (status) => status === "gone", |
| 51 | times: 10, |
| 52 | }), |
| 53 | ); |
| 54 | |
| 55 | const waitUntilAppGone = (appName: string) => |
| 56 | machines.getApp({ app_name: appName }).pipe( |
| 57 | Effect.as("found" as const), |
| 58 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 59 | Effect.repeat({ |
| 60 | schedule: Schedule.spaced("1 second"), |
| 61 | until: (status) => status === "gone", |
| 62 | times: 10, |
| 63 | }), |
| 64 | ); |
| 65 | |
| 66 | const listedSecrets = (appName: string) => |
| 67 | machines |
| 68 | .listSecrets({ |
| 69 | app_name: appName, |
| 70 | show_secrets: false, |
| 71 | }) |
| 72 | .pipe( |
| 73 | Effect.map((res) => new Set((res.secrets ?? []).map((secret) => secret.name))), |
| 74 | Effect.catchTag("NotFound", () => Effect.succeed(new Set<string>())), |
| 75 | ); |
| 76 | |
| 77 | test.provider( |
| 78 | "create, update, and destroy a tigris bucket", |
| 79 | (stack) => |
| 80 | Effect.gen(function* () { |
| 81 | yield* stack.destroy(); |
| 82 | |
| 83 | const created = yield* stack.deploy( |
| 84 | Effect.gen(function* () { |
| 85 | return yield* Fly.Bucket("Data"); |
| 86 | }), |
| 87 | ); |
| 88 | |
| 89 | expect(created.addOnId).toEqual(expect.any(String)); |
| 90 | expect(created.addOnId.length).toBeGreaterThan(0); |
| 91 | expect(created.name).toEqual(expect.any(String)); |
| 92 | expect(created.name.length).toBeGreaterThan(0); |
| 93 | expect(created.public).toEqual(false); |
| 94 | |
| 95 | const fetched = yield* findAddOn(created.addOnId, created.name); |
| 96 | expect(fetched).toBeDefined(); |
| 97 | expect(fetched?.id).toEqual(created.addOnId); |
| 98 | expect(fetched?.name).toEqual(created.name); |
| 99 | |
| 100 | const updated = yield* stack.deploy( |
| 101 | Effect.gen(function* () { |
| 102 | return yield* Fly.Bucket("Data", { |
| 103 | public: true, |
| 104 | }); |
| 105 | }), |
| 106 | ); |
| 107 | |
| 108 | expect(updated.addOnId).toEqual(created.addOnId); |
| 109 | expect(updated.name).toEqual(created.name); |
| 110 | |
| 111 | const refetched = yield* findAddOn(updated.addOnId, updated.name); |
| 112 | expect(refetched).toBeDefined(); |
| 113 | expect(refetched?.id).toEqual(created.addOnId); |
| 114 | |
| 115 | const provider = yield* Provider.findProvider(Fly.Bucket); |
| 116 | const all = yield* provider.list(); |
| 117 | const listed = all.find((row) => row.addOnId === created.addOnId); |
| 118 | expect(listed).toBeDefined(); |
| 119 | expect(listed?.name).toEqual(created.name); |
| 120 | |
| 121 | yield* stack.destroy(); |
| 122 | |
| 123 | const gone = yield* waitUntilBucketGone(created.addOnId, created.name); |
| 124 | expect(gone).toEqual("gone"); |
| 125 | }).pipe(logLevel), |
| 126 | { tags: ["provider:fly", "provider:fly:bucket", "live"], timeout: 120_000 }, |
| 127 | ); |
| 128 | |
| 129 | test.provider( |
| 130 | "replace when the name changes", |
| 131 | (stack) => |
| 132 | Effect.gen(function* () { |
| 133 | yield* stack.destroy(); |
| 134 | |
| 135 | const created = yield* stack.deploy( |
| 136 | Effect.gen(function* () { |
| 137 | return yield* Fly.Bucket("Data"); |
| 138 | }), |
| 139 | ); |
| 140 | |
| 141 | const nextName = sanitizeReplaceName(created.name); |
| 142 | const replaced = yield* stack.deploy( |
| 143 | Effect.gen(function* () { |
| 144 | return yield* Fly.Bucket("Data", { |
| 145 | name: nextName, |
| 146 | }); |
| 147 | }), |
| 148 | ); |
| 149 | |
| 150 | expect(replaced.name).toEqual(nextName); |
| 151 | expect(replaced.addOnId).not.toEqual(created.addOnId); |
| 152 | |
| 153 | const oldGone = yield* findAddOn(created.addOnId, created.name); |
| 154 | expect(oldGone).toBeUndefined(); |
| 155 | const next = yield* findAddOn(replaced.addOnId, replaced.name); |
| 156 | expect(next).toBeDefined(); |
| 157 | expect(next?.name).toEqual(nextName); |
| 158 | |
| 159 | yield* stack.destroy(); |
| 160 | |
| 161 | const gone = yield* waitUntilBucketGone(replaced.addOnId, replaced.name); |
| 162 | expect(gone).toEqual("gone"); |
| 163 | }).pipe(logLevel), |
| 164 | { tags: ["provider:fly", "provider:fly:bucket", "live"], timeout: 120_000 }, |
| 165 | ); |
| 166 | |
| 167 | test.provider( |
| 168 | "a Service puts and gets an object on Tigris", |
| 169 | (stack) => |
| 170 | Effect.gen(function* () { |
| 171 | yield* stack.destroy(); |
| 172 | |
| 173 | const out = yield* stack.deploy( |
| 174 | Effect.gen(function* () { |
| 175 | const app = yield* BucketSite; |
| 176 | const bucket = yield* Data; |
| 177 | const ip = yield* BucketIp; |
| 178 | const api = yield* BucketApi; |
| 179 | return { app, bucket, ip, api }; |
| 180 | }), |
| 181 | ); |
| 182 | |
| 183 | expect(out.api.url).toEqual(`https://${out.app.appName}.fly.dev`); |
| 184 | |
| 185 | const names = yield* listedSecrets(out.app.appName).pipe( |
| 186 | Effect.flatMap((set) => |
| 187 | set.has("BUCKET_NAME") && set.has("AWS_ACCESS_KEY_ID") |
| 188 | ? Effect.succeed(set) |
| 189 | : Effect.fail(new Error(`missing Tigris secrets: ${[...set].join(",") || "(none)"}`)), |
| 190 | ), |
| 191 | Effect.retry({ |
| 192 | schedule: Schedule.spaced("2 seconds"), |
| 193 | times: 8, |
| 194 | }), |
| 195 | ); |
| 196 | expect(names.has("BUCKET_NAME")).toEqual(true); |
| 197 | expect(names.has("AWS_ACCESS_KEY_ID")).toEqual(true); |
| 198 | |
| 199 | const untilOk = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 200 | effect.pipe( |
| 201 | Effect.retry({ |
| 202 | schedule: Schedule.spaced("4 seconds"), |
| 203 | times: 10, |
| 204 | }), |
| 205 | ); |
| 206 | |
| 207 | const put = yield* untilOk( |
| 208 | HttpClient.get(`${out.api.url}/put`).pipe( |
| 209 | Effect.flatMap((res) => |
| 210 | res.status === 200 ? res.json : Effect.fail(new Error(`api returned ${res.status}`)), |
| 211 | ), |
| 212 | Effect.map((value) => value as { ok: boolean }), |
| 213 | ), |
| 214 | ); |
| 215 | expect(put.ok).toEqual(true); |
| 216 | |
| 217 | const got = yield* untilOk( |
| 218 | HttpClient.get(`${out.api.url}/get`).pipe( |
| 219 | Effect.flatMap((res) => |
| 220 | res.status === 200 ? res.json : Effect.fail(new Error(`api returned ${res.status}`)), |
| 221 | ), |
| 222 | Effect.map((value) => value as { ok: boolean; text: string }), |
| 223 | ), |
| 224 | ); |
| 225 | expect(got.ok).toEqual(true); |
| 226 | expect(got.text).toEqual(OBJECT_BODY); |
| 227 | |
| 228 | yield* stack.destroy(); |
| 229 | |
| 230 | const bucketGone = yield* waitUntilBucketGone(out.bucket.addOnId, out.bucket.name); |
| 231 | expect(bucketGone).toEqual("gone"); |
| 232 | const appGone = yield* waitUntilAppGone(out.app.appName); |
| 233 | expect(appGone).toEqual("gone"); |
| 234 | }).pipe(logLevel), |
| 235 | { |
| 236 | tags: [ |
| 237 | "provider:fly", |
| 238 | "provider:fly:app", |
| 239 | "provider:fly:bucket", |
| 240 | "provider:fly:ipassignment", |
| 241 | "provider:fly:machine", |
| 242 | "provider:fly:service", |
| 243 | "live", |
| 244 | ], |
| 245 | // Deploys a Fly Service (image build + push + machine health checks) |
| 246 | // and a Tigris bucket, then destroys both: measured 336–380s. |
| 247 | timeout: 600_000, |
| 248 | }, |
| 249 | ); |
| 250 | |
| 251 | const sanitizeReplaceName = (name: string): string => { |
| 252 | const clipped = name.length >= 30 ? name.slice(0, 29) : name; |
| 253 | return `${clipped}x`.replace(/[^a-z0-9-]/g, "-").slice(0, 63); |
| 254 | }; |