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 { sameStopConfig } from "@alchemy.run/fly/replicas"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as machines from "@distilled.cloud/fly-io/machines"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as FileSystem from "effect/FileSystem"; |
| 8 | import * as Path from "effect/Path"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import { cloneFixture } from "../../Cloudflare/Utils/Fixture.ts"; |
| 11 | import { |
| 12 | assertAppGone, |
| 13 | assertMachineGone, |
| 14 | assertOnlyMachine, |
| 15 | getText, |
| 16 | initialText, |
| 17 | startTraffic, |
| 18 | websiteChecks, |
| 19 | websiteServices, |
| 20 | } from "./fixtures/deployment.ts"; |
| 21 | |
| 22 | const { test } = Test.make({ providers: Fly.providers() }); |
| 23 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 24 | |
| 25 | test.provider( |
| 26 | "R08/R02 StaticSite: build v1 to v2 under un-retried traffic, forwarded policies, destroy, gone", |
| 27 | (stack) => |
| 28 | Effect.gen(function* () { |
| 29 | yield* stack.destroy(); |
| 30 | const fs = yield* FileSystem.FileSystem; |
| 31 | const path = yield* Path.Path; |
| 32 | const cwd = yield* cloneFixture( |
| 33 | path.resolve(import.meta.dirname, "../../Cloudflare/Website/staticsite-fixture"), |
| 34 | { |
| 35 | prefix: "alchemy-staticsite-fly-", |
| 36 | tempRoot: path.resolve(import.meta.dirname, "../../../.tmp"), |
| 37 | entries: ["src", "build.sh"], |
| 38 | }, |
| 39 | ); |
| 40 | const deploy = () => |
| 41 | stack.deploy( |
| 42 | Effect.gen(function* () { |
| 43 | const site = yield* Fly.Website.StaticSite("Blog", { |
| 44 | path: cwd, |
| 45 | build: { command: "bash build.sh", output: "dist" }, |
| 46 | memo: { include: ["src/**", "build.sh"] }, |
| 47 | deploy: { strategy: "bluegreen", healthTimeout: "90 seconds" }, |
| 48 | shutdown: { signal: "SIGTERM", timeout: "10 seconds" }, |
| 49 | checks: websiteChecks("/index.html"), |
| 50 | services: websiteServices("/index.html"), |
| 51 | }); |
| 52 | return { site }; |
| 53 | }), |
| 54 | ); |
| 55 | let appName: string | undefined; |
| 56 | yield* Effect.gen(function* () { |
| 57 | const first = yield* deploy(); |
| 58 | appName = first.site.app!.appName; |
| 59 | const url = first.site.url!; |
| 60 | const oldId = first.site.service!.machineId; |
| 61 | expect(url).toBe(`https://${appName}.fly.dev`); |
| 62 | const firstBody = yield* initialText(`${url}/`); |
| 63 | expect(firstBody).toContain("StaticSite fixture v1"); |
| 64 | const observed = yield* machines.getMachine({ |
| 65 | app_name: appName, |
| 66 | machine_id: oldId, |
| 67 | }); |
| 68 | expect(observed.config?.metadata?.["alchemy.phase"]).toBe("active"); |
| 69 | expect(observed.config?.stop_config?.signal).toBe("SIGTERM"); |
| 70 | expect( |
| 71 | sameStopConfig(observed.config?.stop_config, { |
| 72 | signal: "SIGTERM", |
| 73 | timeout: "10000ms", |
| 74 | }), |
| 75 | ).toBe(true); |
| 76 | expect(observed.config?.checks?.website?.path).toBe("/index.html"); |
| 77 | expect(observed.config?.services?.[0]?.checks?.[0]?.path).toBe("/index.html"); |
| 78 | expect(observed.config?.env?.ALCHEMY_FLY_SHUTDOWN_TIMEOUT_MS).toBeUndefined(); |
| 79 | const traffic = yield* startTraffic(`${url}/`); |
| 80 | yield* traffic.waitFor((body) => body.includes("StaticSite fixture v1")); |
| 81 | const index = path.join(cwd, "src/index.html"); |
| 82 | yield* fs.writeFileString( |
| 83 | index, |
| 84 | (yield* fs.readFileString(index)).replaceAll("fixture v1", "fixture v2"), |
| 85 | ); |
| 86 | const second = yield* deploy(); |
| 87 | const newId = second.site.service!.machineId; |
| 88 | expect(yield* fs.readFileString(path.join(cwd, "dist/index.html"))).toContain( |
| 89 | "StaticSite fixture v2", |
| 90 | ); |
| 91 | expect(second.site.service!.code.hash).not.toBe(first.site.service!.code.hash); |
| 92 | const updated = yield* machines.getMachine({ |
| 93 | app_name: appName, |
| 94 | machine_id: newId, |
| 95 | }); |
| 96 | expect(updated.config?.image).not.toBe(observed.config?.image); |
| 97 | expect(second.site.url).toBe(url); |
| 98 | expect(newId).not.toBe(oldId); |
| 99 | const secondBody = yield* getText(`${url}/`); |
| 100 | expect(secondBody).toContain("StaticSite fixture v2"); |
| 101 | yield* traffic.waitFor((body) => body.includes("StaticSite fixture v2")); |
| 102 | const samples = yield* traffic.finish; |
| 103 | for (const body of samples) expect([firstBody, secondBody]).toContain(body); |
| 104 | yield* assertMachineGone(appName, oldId); |
| 105 | yield* assertOnlyMachine(appName, newId); |
| 106 | }).pipe( |
| 107 | Effect.scoped, |
| 108 | Effect.ensuring( |
| 109 | Effect.gen(function* () { |
| 110 | yield* stack.destroy(); |
| 111 | if (appName) yield* assertAppGone(appName); |
| 112 | }).pipe(Effect.orDie), |
| 113 | ), |
| 114 | ); |
| 115 | }).pipe(logLevel), |
| 116 | // Two build-command/image deployments, proxy overlap, and final cloud census. |
| 117 | { |
| 118 | tags: ["provider:fly", "provider:fly:machine", "provider:fly:website", "live"], |
| 119 | timeout: 720_000, |
| 120 | }, |
| 121 | ); |