The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as ACME from "@alchemy.run/acme"; |
| 2 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 3 | import { CloudflareEnvironment } from "@alchemy.run/cloudflare"; |
| 4 | import { findZoneByName } from "@alchemy.run/cloudflare/Zone"; |
| 5 | import * as Fly from "@alchemy.run/fly"; |
| 6 | import { FlyAuth } from "@alchemy.run/fly/AuthProvider"; |
| 7 | import { fromAuthProvider } from "@alchemy.run/fly/Credentials"; |
| 8 | import { expect } from "@alchemy.run/test"; |
| 9 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 10 | import * as machines from "@distilled.cloud/fly-io/machines"; |
| 11 | import * as Alchemy from "alchemy"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import * as HttpClient from "effect/http/HttpClient"; |
| 14 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 15 | import * as Layer from "effect/Layer"; |
| 16 | import * as Redacted from "effect/Redacted"; |
| 17 | import * as Schedule from "effect/Schedule"; |
| 18 | import CertificatesApi, { CertIp, CertSite } from "./fixtures/certificates-api.ts"; |
| 19 | |
| 20 | /** |
| 21 | * Runtime certificate management on a Fly App: `request` a Fly-managed |
| 22 | * certificate, upload a Let's Encrypt staging certificate, check, get, and remove. |
| 23 | */ |
| 24 | const providers = Layer.mergeAll(Fly.providers(), Cloudflare.providers(), ACME.providers()); |
| 25 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ providers }); |
| 26 | const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 27 | const UPLOAD_HOST = `alchemy-wc-upload.${zoneName}`; |
| 28 | const REQUEST_HOST = `alchemy-wc-request.${zoneName}`; |
| 29 | |
| 30 | const Stack = Alchemy.Stack( |
| 31 | "FlyWriteCertificatesFixture", |
| 32 | { providers, state: Alchemy.localState() }, |
| 33 | Effect.gen(function* () { |
| 34 | const site = yield* CertSite; |
| 35 | yield* CertIp; |
| 36 | const api = yield* CertificatesApi; |
| 37 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 38 | const zone = yield* findZoneByName({ accountId, name: zoneName }).pipe(Effect.orDie); |
| 39 | if (!zone) return yield* Effect.die(new Error(`zone ${zoneName} not found`)); |
| 40 | const account = yield* ACME.Account("Issuer", { |
| 41 | ca: ACME.LetsEncryptStaging, |
| 42 | termsOfServiceAgreed: true, |
| 43 | }); |
| 44 | const certificate = yield* ACME.Certificate("Upload", { |
| 45 | account, |
| 46 | identifiers: [UPLOAD_HOST], |
| 47 | solver: Cloudflare.ACME.ZoneSolver({ zoneId: zone.id }), |
| 48 | revokeOnDelete: true, |
| 49 | }); |
| 50 | return { |
| 51 | appName: site.appName, |
| 52 | url: api.url, |
| 53 | chain: certificate.chain, |
| 54 | privateKey: certificate.privateKey, |
| 55 | }; |
| 56 | }), |
| 57 | ); |
| 58 | |
| 59 | // This hook builds/pushes the runtime image and issues a real certificate. |
| 60 | // The test body's timeout does not cover setup; reserve time for both here. |
| 61 | const stack = beforeAll(deploy(Stack), { timeout: 180_000 }); |
| 62 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack)); |
| 63 | |
| 64 | interface Reply { |
| 65 | ok: boolean; |
| 66 | value?: any; |
| 67 | error?: string; |
| 68 | } |
| 69 | |
| 70 | test( |
| 71 | "uploads, inspects and removes a custom certificate, and requests a Fly-managed one", |
| 72 | Effect.gen(function* () { |
| 73 | const { url, appName, chain, privateKey } = yield* stack; |
| 74 | const client = yield* HttpClient.HttpClient; |
| 75 | |
| 76 | const call = (path: string, body?: unknown) => |
| 77 | Effect.gen(function* () { |
| 78 | const request = |
| 79 | body === undefined |
| 80 | ? HttpClientRequest.get(`${url}${path}`) |
| 81 | : HttpClientRequest.post(`${url}${path}`).pipe(HttpClientRequest.bodyJsonUnsafe(body)); |
| 82 | const response = yield* client.execute(request); |
| 83 | const text = yield* response.text; |
| 84 | return JSON.parse(text || "null") as Reply; |
| 85 | }); |
| 86 | |
| 87 | const health = yield* call("/health").pipe( |
| 88 | Effect.retry({ |
| 89 | schedule: Schedule.spaced("2 seconds"), |
| 90 | times: 8, |
| 91 | }), |
| 92 | ); |
| 93 | expect(health.ok).toBe(true); |
| 94 | |
| 95 | const uploaded = yield* call("/upload", { |
| 96 | hostname: UPLOAD_HOST, |
| 97 | fullchain: chain, |
| 98 | privateKey: Redacted.value(privateKey), |
| 99 | }); |
| 100 | expect(uploaded.error).toBeUndefined(); |
| 101 | expect(uploaded.ok).toBe(true); |
| 102 | expect(uploaded.value?.hostname).toBe(UPLOAD_HOST); |
| 103 | const observed = yield* machines |
| 104 | .getAppCertificate({ |
| 105 | app_name: appName, |
| 106 | hostname: UPLOAD_HOST, |
| 107 | }) |
| 108 | .pipe(Effect.provide(fromAuthProvider().pipe(Layer.provide(FlyAuth)))); |
| 109 | expect(observed.hostname).toBe(UPLOAD_HOST); |
| 110 | |
| 111 | const fetched = yield* call(`/get?hostname=${UPLOAD_HOST}`); |
| 112 | expect(fetched.ok).toBe(true); |
| 113 | expect(fetched.value?.hostname).toBe(UPLOAD_HOST); |
| 114 | expect( |
| 115 | (fetched.value?.certificates ?? []).some((c: { source?: string }) => c.source === "custom"), |
| 116 | ).toBe(true); |
| 117 | |
| 118 | // Re-upload replaces in place (conflict → delete + create). |
| 119 | const reuploaded = yield* call("/upload", { |
| 120 | hostname: UPLOAD_HOST, |
| 121 | fullchain: chain, |
| 122 | privateKey: Redacted.value(privateKey), |
| 123 | }); |
| 124 | expect(reuploaded.ok).toBe(true); |
| 125 | |
| 126 | const checked = yield* call(`/check?hostname=${UPLOAD_HOST}`); |
| 127 | expect(checked.ok).toBe(true); |
| 128 | expect(checked.value?.hostname).toBe(UPLOAD_HOST); |
| 129 | |
| 130 | const requested = yield* call(`/request?hostname=${REQUEST_HOST}`); |
| 131 | expect(requested.ok).toBe(true); |
| 132 | const requestedDetail = yield* call(`/get?hostname=${REQUEST_HOST}`); |
| 133 | expect(requestedDetail.value?.acme_requested).toBe(true); |
| 134 | expect(requestedDetail.value?.dns_requirements).toBeDefined(); |
| 135 | |
| 136 | const removed = yield* call(`/remove?hostname=${UPLOAD_HOST}`); |
| 137 | expect(removed.ok).toBe(true); |
| 138 | yield* call(`/remove?hostname=${REQUEST_HOST}`); |
| 139 | const gone = yield* call(`/get?hostname=${UPLOAD_HOST}`).pipe( |
| 140 | Effect.repeat({ |
| 141 | schedule: Schedule.spaced("1 second"), |
| 142 | until: (reply) => reply.value === null, |
| 143 | times: 10, |
| 144 | }), |
| 145 | ); |
| 146 | expect(gone.value).toBeNull(); |
| 147 | // Removing again is a no-op. |
| 148 | expect((yield* call(`/remove?hostname=${UPLOAD_HOST}`)).ok).toBe(true); |
| 149 | }), |
| 150 | { |
| 151 | tags: [ |
| 152 | "provider:acme", |
| 153 | "provider:acme:account", |
| 154 | "provider:acme:certificate", |
| 155 | "provider:cloudflare", |
| 156 | "provider:cloudflare:dns", |
| 157 | "provider:cloudflare:zone", |
| 158 | "provider:fly", |
| 159 | "provider:fly:app", |
| 160 | "provider:fly:certificate", |
| 161 | "provider:fly:ipassignment", |
| 162 | "provider:fly:machine", |
| 163 | "provider:fly:service", |
| 164 | "live", |
| 165 | ], |
| 166 | timeout: 240_000, |
| 167 | }, |
| 168 | ); |