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 machines from "@distilled.cloud/fly-io/machines"; |
| 5 | import * as mpg from "@distilled.cloud/fly-io/mpg"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import { MinimumLogLevel } from "effect/References"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import PostgresApi, { Db, MpgIp, MpgSite } from "./fixtures/postgres-api.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: Fly.providers() }); |
| 15 | |
| 16 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 17 | |
| 18 | const waitUntilClusterGone = (clusterId: string) => |
| 19 | mpg.getClusterById({ id: clusterId }).pipe( |
| 20 | Effect.map((res) => |
| 21 | res.data === undefined || res.data.status === "deleted" |
| 22 | ? ("gone" as const) |
| 23 | : ("found" as const), |
| 24 | ), |
| 25 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 26 | Effect.repeat({ |
| 27 | schedule: Schedule.spaced("2 seconds"), |
| 28 | until: (status) => status === "gone", |
| 29 | times: 10, |
| 30 | }), |
| 31 | ); |
| 32 | |
| 33 | const waitUntilReady = (clusterId: string) => |
| 34 | mpg.getClusterById({ id: clusterId }).pipe( |
| 35 | Effect.map((res) => res.data?.status), |
| 36 | Effect.catchTag("NotFound", () => Effect.succeed(undefined)), |
| 37 | Effect.repeat({ |
| 38 | schedule: Schedule.spaced("5 seconds"), |
| 39 | until: (status) => status === "ready" || status === "error", |
| 40 | times: 24, |
| 41 | }), |
| 42 | ); |
| 43 | |
| 44 | const waitUntilAppGone = (appName: string) => |
| 45 | machines.getApp({ app_name: appName }).pipe( |
| 46 | Effect.as("found" as const), |
| 47 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 48 | Effect.repeat({ |
| 49 | schedule: Schedule.spaced("1 second"), |
| 50 | until: (status) => status === "gone", |
| 51 | times: 10, |
| 52 | }), |
| 53 | ); |
| 54 | |
| 55 | const secretNames = (appName: string) => |
| 56 | machines |
| 57 | .listSecrets({ |
| 58 | app_name: appName, |
| 59 | show_secrets: false, |
| 60 | }) |
| 61 | .pipe( |
| 62 | Effect.map((res) => (res.secrets ?? []).map((secret) => secret.name)), |
| 63 | Effect.catchTag(["NotFound", "Forbidden"], () => |
| 64 | Effect.succeed([] as Array<string | undefined>), |
| 65 | ), |
| 66 | ); |
| 67 | |
| 68 | test.provider( |
| 69 | "createCluster with an invalid region is a typed BadRequest", |
| 70 | (stack) => |
| 71 | Effect.gen(function* () { |
| 72 | yield* stack.destroy(); |
| 73 | |
| 74 | const orgSlug = yield* Fly.currentOrgSlug(); |
| 75 | const error = yield* mpg |
| 76 | .createCluster({ |
| 77 | org_slug: orgSlug, |
| 78 | name: "alchemy-mpg-invalid-region", |
| 79 | region: "not-a-region", |
| 80 | plan: "basic", |
| 81 | storage_in_gb: 10, |
| 82 | }) |
| 83 | .pipe(Effect.flip); |
| 84 | |
| 85 | expect(error._tag).toEqual("BadRequest"); |
| 86 | expect(error.message).toEqual("region is invalid"); |
| 87 | |
| 88 | yield* stack.destroy(); |
| 89 | }).pipe(logLevel), |
| 90 | { tags: ["provider:fly", "provider:fly:postgres", "live"], timeout: 90_000 }, |
| 91 | ); |
| 92 | |
| 93 | test.provider( |
| 94 | "create, attach, list, and destroy a managed postgres cluster", |
| 95 | (stack) => |
| 96 | Effect.gen(function* () { |
| 97 | yield* stack.destroy(); |
| 98 | |
| 99 | const created = yield* stack.deploy( |
| 100 | Effect.gen(function* () { |
| 101 | const app = yield* Fly.App("MpgSite"); |
| 102 | const db = yield* Fly.Postgres("Db", { |
| 103 | region: "iad", |
| 104 | plan: "basic", |
| 105 | volumeSizeGb: 10, |
| 106 | }); |
| 107 | return { app, db }; |
| 108 | }), |
| 109 | ); |
| 110 | |
| 111 | expect(created.db.clusterId).toEqual(expect.any(String)); |
| 112 | expect(created.db.clusterId.length).toBeGreaterThan(0); |
| 113 | expect(created.db.name).toEqual(expect.any(String)); |
| 114 | expect(created.db.region).toEqual("iad"); |
| 115 | expect(created.db.plan).toEqual("basic"); |
| 116 | |
| 117 | const status = yield* waitUntilReady(created.db.clusterId); |
| 118 | expect(status).toEqual("ready"); |
| 119 | |
| 120 | const fetched = yield* mpg.getClusterById({ |
| 121 | id: created.db.clusterId, |
| 122 | }); |
| 123 | expect(fetched.data?.id).toEqual(created.db.clusterId); |
| 124 | expect(fetched.data?.name).toEqual(created.db.name); |
| 125 | expect(fetched.data?.region).toEqual("iad"); |
| 126 | expect(fetched.data?.status).toEqual("ready"); |
| 127 | |
| 128 | const updated = yield* stack.deploy( |
| 129 | Effect.gen(function* () { |
| 130 | const app = yield* Fly.App("MpgSite"); |
| 131 | const db = yield* Fly.Postgres("Db", { |
| 132 | region: "iad", |
| 133 | plan: "basic", |
| 134 | volumeSizeGb: 10, |
| 135 | }); |
| 136 | return { app, db }; |
| 137 | }), |
| 138 | ); |
| 139 | expect(updated.db.clusterId).toEqual(created.db.clusterId); |
| 140 | expect(updated.db.name).toEqual(created.db.name); |
| 141 | expect(created.db.pooledConnectionUri.length).toBeGreaterThan(0); |
| 142 | |
| 143 | const provider = yield* Provider.findProvider(Fly.Postgres); |
| 144 | const all = yield* provider.list(); |
| 145 | const listed = all.find((row) => row.clusterId === created.db.clusterId); |
| 146 | expect(listed).toBeDefined(); |
| 147 | expect(listed?.name).toEqual(created.db.name); |
| 148 | expect(listed?.region).toEqual("iad"); |
| 149 | |
| 150 | yield* stack.destroy(); |
| 151 | |
| 152 | const clusterGone = yield* waitUntilClusterGone(created.db.clusterId); |
| 153 | expect(clusterGone).toEqual("gone"); |
| 154 | const appGone = yield* waitUntilAppGone(created.app.appName); |
| 155 | expect(appGone).toEqual("gone"); |
| 156 | }).pipe(logLevel), |
| 157 | { |
| 158 | tags: [ |
| 159 | "provider:fly", |
| 160 | "provider:fly:app", |
| 161 | "provider:fly:machine", |
| 162 | "provider:fly:postgres", |
| 163 | "live", |
| 164 | ], |
| 165 | timeout: 180_000, |
| 166 | }, |
| 167 | ); |
| 168 | |
| 169 | test.provider( |
| 170 | "a Service connects and SELECTs through ConnectPostgres", |
| 171 | (stack) => |
| 172 | Effect.gen(function* () { |
| 173 | yield* stack.destroy(); |
| 174 | |
| 175 | const deployed = yield* stack.deploy( |
| 176 | Effect.gen(function* () { |
| 177 | const app = yield* MpgSite; |
| 178 | const db = yield* Db; |
| 179 | const ip = yield* MpgIp; |
| 180 | const api = yield* PostgresApi; |
| 181 | return { app, db, ip, api }; |
| 182 | }), |
| 183 | ); |
| 184 | |
| 185 | expect(deployed.db.clusterId).toEqual(expect.any(String)); |
| 186 | expect(deployed.api.url).toEqual(`https://${deployed.app.appName}.fly.dev`); |
| 187 | |
| 188 | const names = yield* secretNames(deployed.app.appName); |
| 189 | expect(names).toContain("DATABASE_URL"); |
| 190 | |
| 191 | yield* Effect.logInfo("ConnectPostgres deployed", { |
| 192 | url: deployed.api.url, |
| 193 | machineId: deployed.api.machineId, |
| 194 | state: deployed.api.state, |
| 195 | }); |
| 196 | |
| 197 | class NotReady extends Data.TaggedError("NotReady")<{ |
| 198 | status: number; |
| 199 | body?: unknown; |
| 200 | }> { |
| 201 | override get message() { |
| 202 | return this.body === undefined |
| 203 | ? `status ${this.status}` |
| 204 | : `status ${this.status}: ${JSON.stringify(this.body)}`; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | const diagnose = Effect.gen(function* () { |
| 209 | const appName = deployed.api.appName; |
| 210 | const machineId = deployed.api.machineId; |
| 211 | const machine = yield* machines.getMachine({ |
| 212 | app_name: appName, |
| 213 | machine_id: machineId, |
| 214 | }); |
| 215 | const events = yield* machines.listMachineEvents({ |
| 216 | app_name: appName, |
| 217 | machine_id: machineId, |
| 218 | limit: 20, |
| 219 | }); |
| 220 | const listed = yield* machines.listMachineProcesses({ |
| 221 | app_name: appName, |
| 222 | machine_id: machineId, |
| 223 | }); |
| 224 | const processes = (Array.isArray(listed) ? listed : []).filter( |
| 225 | (proc) => typeof proc.command === "string" && proc.command.length > 0, |
| 226 | ); |
| 227 | const ips = yield* machines.listAppIPAssignments({ |
| 228 | app_name: appName, |
| 229 | }); |
| 230 | const exec = yield* machines |
| 231 | .execMachine({ |
| 232 | app_name: appName, |
| 233 | machine_id: machineId, |
| 234 | command: [ |
| 235 | "bun", |
| 236 | "-e", |
| 237 | "const r = await fetch('http://127.0.0.1:3000/ping'); console.log(r.status, await r.text())", |
| 238 | ], |
| 239 | timeout: 10, |
| 240 | }) |
| 241 | .pipe( |
| 242 | Effect.catchCause((cause) => |
| 243 | Effect.succeed({ |
| 244 | stdout: undefined, |
| 245 | stderr: String(cause), |
| 246 | exit_code: -1, |
| 247 | }), |
| 248 | ), |
| 249 | ); |
| 250 | yield* Effect.logError("ConnectPostgres HTTP diagnose", { |
| 251 | state: machine.state, |
| 252 | checks: machine.checks, |
| 253 | events, |
| 254 | processes, |
| 255 | ips, |
| 256 | exec, |
| 257 | }); |
| 258 | }).pipe(Effect.ignore); |
| 259 | |
| 260 | const getJson = (path: string) => |
| 261 | HttpClient.get(`${deployed.api.url}${path}`).pipe( |
| 262 | Effect.flatMap((res) => |
| 263 | res.json.pipe( |
| 264 | Effect.flatMap((body) => |
| 265 | res.status === 200 |
| 266 | ? Effect.succeed(body as { ok?: boolean; rows?: unknown }) |
| 267 | : Effect.fail(new NotReady({ status: res.status, body })), |
| 268 | ), |
| 269 | ), |
| 270 | ), |
| 271 | Effect.timeoutOrElse({ |
| 272 | duration: "8 seconds", |
| 273 | orElse: () => Effect.fail(new NotReady({ status: 0 })), |
| 274 | }), |
| 275 | Effect.retry({ |
| 276 | while: (e) => |
| 277 | e._tag === "NotReady" && |
| 278 | (e.status === 0 || e.status === 404 || e.status === 502 || e.status === 503), |
| 279 | schedule: Schedule.spaced("3 seconds"), |
| 280 | times: 8, |
| 281 | }), |
| 282 | Effect.timeout("60 seconds"), |
| 283 | Effect.tapError(() => diagnose), |
| 284 | ); |
| 285 | |
| 286 | const ping = yield* getJson("/ping"); |
| 287 | expect(ping.ok).toEqual(true); |
| 288 | |
| 289 | for (const path of ["/health", "/direct"]) { |
| 290 | const health = yield* getJson(path); |
| 291 | const executed = health.rows; |
| 292 | const list = Array.isArray(executed) |
| 293 | ? executed |
| 294 | : executed !== null && |
| 295 | typeof executed === "object" && |
| 296 | Array.isArray((executed as { rows?: unknown }).rows) |
| 297 | ? (executed as { rows: unknown[] }).rows |
| 298 | : []; |
| 299 | const first = list[0] as { ok?: unknown } | undefined; |
| 300 | expect(Number(first?.ok)).toEqual(1); |
| 301 | } |
| 302 | |
| 303 | yield* stack.destroy(); |
| 304 | |
| 305 | const clusterGone = yield* waitUntilClusterGone(deployed.db.clusterId); |
| 306 | expect(clusterGone).toEqual("gone"); |
| 307 | const appGone = yield* waitUntilAppGone(deployed.app.appName); |
| 308 | expect(appGone).toEqual("gone"); |
| 309 | }).pipe(logLevel), |
| 310 | { |
| 311 | tags: [ |
| 312 | "provider:fly", |
| 313 | "provider:fly:app", |
| 314 | "provider:fly:ipassignment", |
| 315 | "provider:fly:machine", |
| 316 | "provider:fly:postgres", |
| 317 | "provider:fly:service", |
| 318 | "live", |
| 319 | ], |
| 320 | timeout: 300_000, |
| 321 | }, |
| 322 | ); |