The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as images from "@distilled.cloud/cloudflare/images"; |
| 6 | import { adopt } from "alchemy/AdoptPolicy"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { testName } from "../../names.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 14 | |
| 15 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 16 | |
| 17 | // Out-of-band read of a variant, riding out eventual-consistency blips |
| 18 | // where the freshly created variant is not yet visible (`VariantNotFound`, |
| 19 | // Cloudflare error code 5401). |
| 20 | const getVariant = ( |
| 21 | accountId: string, |
| 22 | variantId: string, |
| 23 | // Optional read-after-write predicate. Cloudflare's variant GET is |
| 24 | // eventually consistent, so an immediate read after a create/PATCH can |
| 25 | // return stale options; keep polling until the observed variant satisfies |
| 26 | // this before asserting on it. |
| 27 | until?: (res: images.GetV1VariantResponse) => boolean, |
| 28 | ) => |
| 29 | images.getV1Variant({ accountId, variantId }).pipe( |
| 30 | Effect.flatMap((res) => |
| 31 | !until || until(res) ? Effect.succeed(res) : Effect.fail({ _tag: "VariantStale" as const }), |
| 32 | ), |
| 33 | Effect.retry({ |
| 34 | // Ride out both "not yet visible" (`VariantNotFound`) and "visible but |
| 35 | // stale options" (`VariantStale`) read-after-write windows. Cloudflare's |
| 36 | // variant GET is served from an eventually-consistent read replica that, |
| 37 | // under full-suite parallel load, can lag a freshly created/patched |
| 38 | // variant well past a minute — so the budget is generous (~90s). |
| 39 | while: (e) => e._tag === "VariantNotFound" || e._tag === "VariantStale", |
| 40 | // Steady spacing (fixed, not exponential — an uncapped exponential |
| 41 | // reaches a 64s single delay by the 8th retry and blows the timeout). |
| 42 | // Polls every 3s rather than every 2s: under full-suite parallel load |
| 43 | // tighter polling only adds to the API request pressure that stretches |
| 44 | // these consistency windows in the first place. |
| 45 | schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(30)]), |
| 46 | }), |
| 47 | ); |
| 48 | |
| 49 | // Poll until the variant is gone — a missing variant surfaces as the typed |
| 50 | // `VariantNotFound`, which is the success condition here. |
| 51 | const expectGone = (accountId: string, variantId: string) => |
| 52 | images.getV1Variant({ accountId, variantId }).pipe( |
| 53 | Effect.flatMap(() => Effect.fail({ _tag: "VariantNotDeleted" } as const)), |
| 54 | Effect.catchTag("VariantNotFound", () => Effect.void), |
| 55 | Effect.retry({ |
| 56 | while: (e) => e._tag === "VariantNotDeleted", |
| 57 | // Bounded fixed spacing (~30s) — see `getVariant` on why the previous |
| 58 | // uncapped exponential could run past the test timeout. |
| 59 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(15)]), |
| 60 | }), |
| 61 | ); |
| 62 | |
| 63 | test.provider.skipIf(!!process.env.FAST)( |
| 64 | "create, update in place, and delete a variant", |
| 65 | (stack) => |
| 66 | Effect.gen(function* () { |
| 67 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 68 | |
| 69 | yield* stack.destroy(); |
| 70 | |
| 71 | // Variant names are alphanumeric only (no hyphens/underscores) — use |
| 72 | // the logical ID verbatim as the deterministic name. |
| 73 | // |
| 74 | // Adopt: variants carry no ownership markers, so a variant left in the |
| 75 | // cloud by a previously crashed run (state never persisted) is |
| 76 | // indistinguishable from a foreign one and `read` reports it `Unowned`. |
| 77 | // These tests use fixed names and always run the same way, so take over |
| 78 | // any such leftover rather than failing with `OwnedBySomeoneElse`. |
| 79 | const created = yield* stack.deploy( |
| 80 | Cloudflare.Images.Variant("alchemytestvariant", { |
| 81 | fit: "cover", |
| 82 | width: 100, |
| 83 | height: 100, |
| 84 | }).pipe(adopt(true)), |
| 85 | ); |
| 86 | |
| 87 | expect(created.variantName).toEqual("alchemytestvariant"); |
| 88 | expect(created.accountId).toEqual(accountId); |
| 89 | expect(created.fit).toEqual("cover"); |
| 90 | expect(created.width).toEqual(100); |
| 91 | expect(created.height).toEqual(100); |
| 92 | expect(created.metadata).toEqual("none"); |
| 93 | expect(created.neverRequireSignedURLs).toEqual(false); |
| 94 | |
| 95 | // Out-of-band verification against the live API (ride out read-after-write |
| 96 | // lag until the created options are observable). |
| 97 | const live = yield* getVariant( |
| 98 | accountId, |
| 99 | created.variantName, |
| 100 | (r) => r.variant?.options.fit === "cover" && r.variant?.options.width === 100, |
| 101 | ); |
| 102 | expect(live.variant?.id).toEqual("alchemytestvariant"); |
| 103 | expect(live.variant?.options.fit).toEqual("cover"); |
| 104 | expect(live.variant?.options.width).toEqual(100); |
| 105 | expect(live.variant?.options.height).toEqual(100); |
| 106 | |
| 107 | // Update mutable options in place — same variant name, PATCHed options. |
| 108 | const updated = yield* stack.deploy( |
| 109 | Cloudflare.Images.Variant("alchemytestvariant", { |
| 110 | fit: "contain", |
| 111 | width: 200, |
| 112 | height: 100, |
| 113 | metadata: "copyright", |
| 114 | neverRequireSignedURLs: true, |
| 115 | }).pipe(adopt(true)), |
| 116 | ); |
| 117 | |
| 118 | expect(updated.variantName).toEqual("alchemytestvariant"); |
| 119 | expect(updated.fit).toEqual("contain"); |
| 120 | expect(updated.width).toEqual(200); |
| 121 | expect(updated.height).toEqual(100); |
| 122 | expect(updated.metadata).toEqual("copyright"); |
| 123 | expect(updated.neverRequireSignedURLs).toEqual(true); |
| 124 | |
| 125 | const patched = yield* getVariant( |
| 126 | accountId, |
| 127 | updated.variantName, |
| 128 | (r) => |
| 129 | r.variant?.options.fit === "contain" && |
| 130 | r.variant?.options.width === 200 && |
| 131 | r.variant?.options.metadata === "copyright" && |
| 132 | r.variant?.neverRequireSignedURLs === true, |
| 133 | ); |
| 134 | expect(patched.variant?.options.fit).toEqual("contain"); |
| 135 | expect(patched.variant?.options.width).toEqual(200); |
| 136 | expect(patched.variant?.options.metadata).toEqual("copyright"); |
| 137 | expect(patched.variant?.neverRequireSignedURLs).toEqual(true); |
| 138 | |
| 139 | // No-op redeploy — identical props, still the same variant. |
| 140 | const noop = yield* stack.deploy( |
| 141 | Cloudflare.Images.Variant("alchemytestvariant", { |
| 142 | fit: "contain", |
| 143 | width: 200, |
| 144 | height: 100, |
| 145 | metadata: "copyright", |
| 146 | neverRequireSignedURLs: true, |
| 147 | }).pipe(adopt(true)), |
| 148 | ); |
| 149 | expect(noop.variantName).toEqual("alchemytestvariant"); |
| 150 | |
| 151 | yield* stack.destroy(); |
| 152 | |
| 153 | yield* expectGone(accountId, "alchemytestvariant"); |
| 154 | |
| 155 | // Destroy again — delete is idempotent. |
| 156 | yield* stack.destroy(); |
| 157 | }).pipe(logLevel), |
| 158 | // Two sequential out-of-band verifies (post-create, post-update) each ride |
| 159 | // out a read-replica lag that can run ~90s under full-suite parallel load; |
| 160 | // the 120s default can't hold both plus the deploys/destroys. |
| 161 | { |
| 162 | tags: ["provider:cloudflare", "provider:cloudflare:images", "live"], |
| 163 | timeout: 240_000, |
| 164 | }, |
| 165 | ); |
| 166 | |
| 167 | test.provider( |
| 168 | "replace a variant when the name changes", |
| 169 | (stack) => |
| 170 | Effect.gen(function* () { |
| 171 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 172 | |
| 173 | yield* stack.destroy(); |
| 174 | |
| 175 | const initial = yield* stack.deploy( |
| 176 | Cloudflare.Images.Variant("ReplaceVariant", { |
| 177 | name: testName("alchemyreplacea", ""), |
| 178 | fit: "cover", |
| 179 | width: 64, |
| 180 | height: 64, |
| 181 | }).pipe(adopt(true)), |
| 182 | ); |
| 183 | expect(initial.variantName).toEqual(testName("alchemyreplacea", "")); |
| 184 | |
| 185 | // Renaming the variant replaces it — the name is the API path id. |
| 186 | const replaced = yield* stack.deploy( |
| 187 | Cloudflare.Images.Variant("ReplaceVariant", { |
| 188 | name: testName("alchemyreplaceb", ""), |
| 189 | fit: "cover", |
| 190 | width: 64, |
| 191 | height: 64, |
| 192 | }).pipe(adopt(true)), |
| 193 | ); |
| 194 | expect(replaced.variantName).toEqual(testName("alchemyreplaceb", "")); |
| 195 | |
| 196 | const live = yield* getVariant(accountId, testName("alchemyreplaceb", "")); |
| 197 | expect(live.variant?.id).toEqual(testName("alchemyreplaceb", "")); |
| 198 | |
| 199 | // The old variant is gone after the replacement completes. |
| 200 | yield* expectGone(accountId, testName("alchemyreplacea", "")); |
| 201 | |
| 202 | yield* stack.destroy(); |
| 203 | yield* expectGone(accountId, testName("alchemyreplaceb", "")); |
| 204 | }).pipe(logLevel), |
| 205 | // Same eventual-consistency budget as above: the post-replace verify plus |
| 206 | // two gone-assertions can exceed the 120s default under parallel load. |
| 207 | { |
| 208 | tags: ["provider:cloudflare", "provider:cloudflare:images", "live"], |
| 209 | timeout: 240_000, |
| 210 | }, |
| 211 | ); |
| 212 | |
| 213 | // Canonical `list()` test (account collection): deploy a variant, resolve the |
| 214 | // provider with the typed `Provider.findProvider` helper, then assert the |
| 215 | // deployed variant appears in the exhaustively-enumerated result. Bracketed by |
| 216 | // `stack.destroy()` at both ends so the suite leaves no residue. |
| 217 | test.provider( |
| 218 | "list enumerates the deployed variant", |
| 219 | (stack) => |
| 220 | Effect.gen(function* () { |
| 221 | yield* stack.destroy(); |
| 222 | |
| 223 | const deployed = yield* stack.deploy( |
| 224 | Cloudflare.Images.Variant("alchemytestlistvariant", { |
| 225 | fit: "cover", |
| 226 | width: 120, |
| 227 | height: 120, |
| 228 | }).pipe(adopt(true)), |
| 229 | ); |
| 230 | |
| 231 | const provider = yield* Provider.findProvider(Cloudflare.Images.Variant); |
| 232 | const all = yield* provider.list(); |
| 233 | |
| 234 | expect(Array.isArray(all)).toBe(true); |
| 235 | expect(all.some((v) => v.variantName === deployed.variantName)).toBe(true); |
| 236 | |
| 237 | yield* stack.destroy(); |
| 238 | }).pipe(logLevel), |
| 239 | { tags: ["provider:cloudflare", "provider:cloudflare:images", "live"] }, |
| 240 | ); |