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 Provider from "alchemy/Provider"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: Fly.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | |
| 14 | const waitUntilVolumeGone = (appName: string, volumeId: string) => |
| 15 | machines |
| 16 | .getVolumeById({ |
| 17 | app_name: appName, |
| 18 | volume_id: volumeId, |
| 19 | }) |
| 20 | .pipe( |
| 21 | Effect.map((volume) => { |
| 22 | const state = volume.state; |
| 23 | return state === "destroyed" || |
| 24 | state === "pending_destroy" || |
| 25 | state === "scheduled_for_destruction" |
| 26 | ? ("gone" as const) |
| 27 | : ("found" as const); |
| 28 | }), |
| 29 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 30 | Effect.repeat({ |
| 31 | schedule: Schedule.spaced("2 seconds"), |
| 32 | until: (status) => status === "gone", |
| 33 | times: 10, |
| 34 | }), |
| 35 | ); |
| 36 | |
| 37 | const waitUntilMachineGone = (appName: string, machineId: string) => |
| 38 | machines |
| 39 | .getMachine({ |
| 40 | app_name: appName, |
| 41 | machine_id: machineId, |
| 42 | }) |
| 43 | .pipe( |
| 44 | Effect.map((machine) => |
| 45 | machine.state === "destroyed" ? ("gone" as const) : ("found" as const), |
| 46 | ), |
| 47 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 48 | Effect.repeat({ |
| 49 | schedule: Schedule.spaced("2 seconds"), |
| 50 | until: (status) => status === "gone", |
| 51 | times: 10, |
| 52 | }), |
| 53 | ); |
| 54 | |
| 55 | test.provider( |
| 56 | "create, extend, and delete a mounted disk", |
| 57 | (stack) => |
| 58 | Effect.gen(function* () { |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | const created = yield* stack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | const app = yield* Fly.App("Site"); |
| 64 | return yield* Fly.Machine("Box", { |
| 65 | app, |
| 66 | region: "iad", |
| 67 | image: "nginx:alpine", |
| 68 | guest: { cpus: 1, memoryMb: 256 }, |
| 69 | mounts: [{ path: "/data", sizeGb: 1, encrypted: true }], |
| 70 | }); |
| 71 | }), |
| 72 | ); |
| 73 | |
| 74 | expect(created.mounts).toHaveLength(1); |
| 75 | expect(created.mounts[0]?.path).toEqual("/data"); |
| 76 | expect(created.mounts[0]?.volumeId).toEqual(expect.any(String)); |
| 77 | expect(created.mounts[0]?.sizeGb).toEqual(1); |
| 78 | expect(created.mounts[0]?.name).toMatch(/^[a-z][a-z0-9_]*$/); |
| 79 | |
| 80 | const fetched = yield* machines.getVolumeById({ |
| 81 | app_name: created.appName, |
| 82 | volume_id: created.mounts[0]!.volumeId, |
| 83 | }); |
| 84 | expect(fetched.id).toEqual(created.mounts[0]?.volumeId); |
| 85 | expect(fetched.name).toEqual(created.mounts[0]?.name); |
| 86 | expect(fetched.region).toEqual("iad"); |
| 87 | expect(fetched.size_gb).toEqual(1); |
| 88 | expect(fetched.encrypted).toEqual(true); |
| 89 | expect(fetched.attached_machine_id).toEqual(created.machineId); |
| 90 | |
| 91 | const updated = yield* stack.deploy( |
| 92 | Effect.gen(function* () { |
| 93 | const app = yield* Fly.App("Site"); |
| 94 | return yield* Fly.Machine("Box", { |
| 95 | app, |
| 96 | region: "iad", |
| 97 | image: "nginx:alpine", |
| 98 | guest: { cpus: 1, memoryMb: 256 }, |
| 99 | mounts: [ |
| 100 | { |
| 101 | path: "/data", |
| 102 | sizeGb: 2, |
| 103 | encrypted: true, |
| 104 | autoBackupEnabled: false, |
| 105 | snapshotRetention: 2, |
| 106 | }, |
| 107 | ], |
| 108 | }); |
| 109 | }), |
| 110 | ); |
| 111 | |
| 112 | expect(updated.machineId).toEqual(created.machineId); |
| 113 | expect(updated.mounts[0]?.volumeId).toEqual(created.mounts[0]?.volumeId); |
| 114 | expect(updated.mounts[0]?.sizeGb).toEqual(2); |
| 115 | |
| 116 | const refetched = yield* machines.getVolumeById({ |
| 117 | app_name: updated.appName, |
| 118 | volume_id: updated.mounts[0]!.volumeId, |
| 119 | }); |
| 120 | expect(refetched.id).toEqual(created.mounts[0]?.volumeId); |
| 121 | expect(refetched.size_gb).toEqual(2); |
| 122 | expect(refetched.auto_backup_enabled).toEqual(false); |
| 123 | expect(refetched.snapshot_retention).toEqual(2); |
| 124 | |
| 125 | yield* stack.destroy(); |
| 126 | |
| 127 | const volumeGone = yield* waitUntilVolumeGone(created.appName, created.mounts[0]!.volumeId); |
| 128 | expect(volumeGone).toEqual("gone"); |
| 129 | const machineGone = yield* waitUntilMachineGone(created.appName, created.machineId); |
| 130 | expect(machineGone).toEqual("gone"); |
| 131 | }).pipe(logLevel), |
| 132 | { |
| 133 | tags: [ |
| 134 | "provider:fly", |
| 135 | "provider:fly:app", |
| 136 | "provider:fly:machine", |
| 137 | "provider:fly:volume", |
| 138 | "live", |
| 139 | ], |
| 140 | timeout: 180_000, |
| 141 | }, |
| 142 | ); |
| 143 | |
| 144 | test.provider( |
| 145 | "replace disks when the machine region changes", |
| 146 | (stack) => |
| 147 | Effect.gen(function* () { |
| 148 | yield* stack.destroy(); |
| 149 | |
| 150 | const created = yield* stack.deploy( |
| 151 | Effect.gen(function* () { |
| 152 | const app = yield* Fly.App("ReplaceSite"); |
| 153 | return yield* Fly.Machine("ReplaceBox", { |
| 154 | app, |
| 155 | region: "iad", |
| 156 | image: "nginx:alpine", |
| 157 | guest: { cpus: 1, memoryMb: 256 }, |
| 158 | mounts: [{ path: "/data", sizeGb: 1 }], |
| 159 | }); |
| 160 | }), |
| 161 | ); |
| 162 | |
| 163 | expect(created.region).toEqual("iad"); |
| 164 | const oldVolumeId = created.mounts[0]!.volumeId; |
| 165 | |
| 166 | const replaced = yield* stack.deploy( |
| 167 | Effect.gen(function* () { |
| 168 | const app = yield* Fly.App("ReplaceSite"); |
| 169 | return yield* Fly.Machine("ReplaceBox", { |
| 170 | app, |
| 171 | region: "ewr", |
| 172 | image: "nginx:alpine", |
| 173 | guest: { cpus: 1, memoryMb: 256 }, |
| 174 | mounts: [{ path: "/data", sizeGb: 1 }], |
| 175 | }); |
| 176 | }), |
| 177 | ); |
| 178 | |
| 179 | expect(replaced.machineId).not.toEqual(created.machineId); |
| 180 | expect(replaced.region).toEqual("ewr"); |
| 181 | expect(replaced.mounts[0]?.volumeId).not.toEqual(oldVolumeId); |
| 182 | expect(replaced.mounts[0]?.sizeGb).toEqual(1); |
| 183 | |
| 184 | const fetched = yield* machines.getVolumeById({ |
| 185 | app_name: replaced.appName, |
| 186 | volume_id: replaced.mounts[0]!.volumeId, |
| 187 | }); |
| 188 | expect(fetched.id).toEqual(replaced.mounts[0]?.volumeId); |
| 189 | expect(fetched.region).toEqual("ewr"); |
| 190 | |
| 191 | const oldGone = yield* waitUntilVolumeGone(created.appName, oldVolumeId); |
| 192 | expect(oldGone).toEqual("gone"); |
| 193 | |
| 194 | yield* stack.destroy(); |
| 195 | |
| 196 | const gone = yield* waitUntilVolumeGone(replaced.appName, replaced.mounts[0]!.volumeId); |
| 197 | expect(gone).toEqual("gone"); |
| 198 | }).pipe(logLevel), |
| 199 | { |
| 200 | tags: [ |
| 201 | "provider:fly", |
| 202 | "provider:fly:app", |
| 203 | "provider:fly:machine", |
| 204 | "provider:fly:volume", |
| 205 | "live", |
| 206 | ], |
| 207 | timeout: 180_000, |
| 208 | }, |
| 209 | ); |
| 210 | |
| 211 | test.provider( |
| 212 | "count creates one volume per replica", |
| 213 | (stack) => |
| 214 | Effect.gen(function* () { |
| 215 | yield* stack.destroy(); |
| 216 | |
| 217 | const created = yield* stack.deploy( |
| 218 | Effect.gen(function* () { |
| 219 | const app = yield* Fly.App("CountSite"); |
| 220 | return yield* Fly.Machine("CountBox", { |
| 221 | app, |
| 222 | region: "iad", |
| 223 | image: "nginx:alpine", |
| 224 | guest: { cpus: 1, memoryMb: 256 }, |
| 225 | count: 2, |
| 226 | mounts: [{ path: "/data", sizeGb: 1 }], |
| 227 | }); |
| 228 | }), |
| 229 | ); |
| 230 | |
| 231 | expect(created.count).toEqual(2); |
| 232 | expect(created.machineIds).toHaveLength(2); |
| 233 | expect(created.machineIds[0]).toEqual(created.machineId); |
| 234 | expect(created.replicas).toHaveLength(2); |
| 235 | expect(created.replicas[0]?.mounts[0]?.volumeId).toEqual(expect.any(String)); |
| 236 | expect(created.replicas[1]?.mounts[0]?.volumeId).toEqual(expect.any(String)); |
| 237 | expect(created.replicas[0]?.mounts[0]?.volumeId).not.toEqual( |
| 238 | created.replicas[1]?.mounts[0]?.volumeId, |
| 239 | ); |
| 240 | expect(created.replicas[0]?.mounts[0]?.name).toEqual(created.replicas[1]?.mounts[0]?.name); |
| 241 | |
| 242 | const left = yield* machines.getVolumeById({ |
| 243 | app_name: created.appName, |
| 244 | volume_id: created.replicas[0]!.mounts[0]!.volumeId, |
| 245 | }); |
| 246 | const right = yield* machines.getVolumeById({ |
| 247 | app_name: created.appName, |
| 248 | volume_id: created.replicas[1]!.mounts[0]!.volumeId, |
| 249 | }); |
| 250 | expect(left.attached_machine_id).toEqual(created.replicas[0]?.machineId); |
| 251 | expect(right.attached_machine_id).toEqual(created.replicas[1]?.machineId); |
| 252 | expect(left.name).toEqual(right.name); |
| 253 | |
| 254 | const scaled = yield* stack.deploy( |
| 255 | Effect.gen(function* () { |
| 256 | const app = yield* Fly.App("CountSite"); |
| 257 | return yield* Fly.Machine("CountBox", { |
| 258 | app, |
| 259 | region: "iad", |
| 260 | image: "nginx:alpine", |
| 261 | guest: { cpus: 1, memoryMb: 256 }, |
| 262 | count: 1, |
| 263 | mounts: [{ path: "/data", sizeGb: 1 }], |
| 264 | }); |
| 265 | }), |
| 266 | ); |
| 267 | |
| 268 | expect(scaled.count).toEqual(1); |
| 269 | expect(scaled.machineIds).toEqual([created.machineId]); |
| 270 | expect(scaled.mounts[0]?.volumeId).toEqual(created.replicas[0]?.mounts[0]?.volumeId); |
| 271 | |
| 272 | const extraGone = yield* waitUntilMachineGone( |
| 273 | created.appName, |
| 274 | created.replicas[1]!.machineId, |
| 275 | ); |
| 276 | expect(extraGone).toEqual("gone"); |
| 277 | const extraVolumeGone = yield* waitUntilVolumeGone( |
| 278 | created.appName, |
| 279 | created.replicas[1]!.mounts[0]!.volumeId, |
| 280 | ); |
| 281 | expect(extraVolumeGone).toEqual("gone"); |
| 282 | |
| 283 | const provider = yield* Provider.findProvider(Fly.Machine); |
| 284 | const all = yield* provider.list(); |
| 285 | const found = all.find((machine) => machine.machineId === scaled.machineId); |
| 286 | expect(found).toBeDefined(); |
| 287 | expect(found?.count).toEqual(1); |
| 288 | |
| 289 | yield* stack.destroy(); |
| 290 | }).pipe(logLevel), |
| 291 | { |
| 292 | tags: [ |
| 293 | "provider:fly", |
| 294 | "provider:fly:app", |
| 295 | "provider:fly:machine", |
| 296 | "provider:fly:volume", |
| 297 | "live", |
| 298 | ], |
| 299 | timeout: 180_000, |
| 300 | }, |
| 301 | ); |