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 { normalizeEndpoint } from "@alchemy.run/cloudflare/ApiShield/Operation"; |
| 3 | import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment"; |
| 4 | import { findZoneByName } from "@alchemy.run/cloudflare/Zone/lookup"; |
| 5 | import { expect } from "@alchemy.run/test"; |
| 6 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 7 | import * as apiGateway from "@distilled.cloud/cloudflare/api-gateway"; |
| 8 | import { adopt } from "alchemy/AdoptPolicy"; |
| 9 | import * as Provider from "alchemy/Provider"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import { MinimumLogLevel } from "effect/References"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | import * as Stream from "effect/Stream"; |
| 14 | |
| 15 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 16 | |
| 17 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 18 | |
| 19 | const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 20 | |
| 21 | // Deterministic per-test endpoints. Each test owns a disjoint path so reruns |
| 22 | // and parallel runs never collide (never derive identity from Date.now()). |
| 23 | const ENDPOINT_DEFAULT = "/alchemy-apishield/default/{thingId}"; |
| 24 | const ENDPOINT_REPLACE = "/alchemy-apishield/replace"; |
| 25 | const ENDPOINT_LIST = "/alchemy-apishield/list"; |
| 26 | |
| 27 | const resolveZoneId = Effect.gen(function* () { |
| 28 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 29 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 30 | if (!zone) { |
| 31 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 32 | } |
| 33 | return zone.id; |
| 34 | }); |
| 35 | |
| 36 | // The scoped API token the test harness mints propagates eventually- |
| 37 | // consistently — a fresh token intermittently 403s. Ride out the blips on |
| 38 | // the test's own out-of-band calls by retrying the typed `Forbidden` error. |
| 39 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 40 | |
| 41 | const findOperation = (zoneId: string, tuple: { method: string; host: string; endpoint: string }) => |
| 42 | apiGateway.listOperations.items({ zoneId, host: [tuple.host], method: [tuple.method] }).pipe( |
| 43 | Stream.filter( |
| 44 | (op) => |
| 45 | op.method === tuple.method && |
| 46 | op.host === tuple.host && |
| 47 | op.endpoint === normalizeEndpoint(tuple.endpoint), |
| 48 | ), |
| 49 | Stream.runCollect, |
| 50 | Effect.map((chunk) => Array.from(chunk)[0]), |
| 51 | Effect.retry({ |
| 52 | while: (e) => e._tag === "Forbidden", |
| 53 | schedule: forbiddenRetrySchedule, |
| 54 | times: 8, |
| 55 | }), |
| 56 | ); |
| 57 | |
| 58 | const getOperation = (zoneId: string, operationId: string) => |
| 59 | apiGateway.getOperation({ zoneId, operationId }).pipe( |
| 60 | Effect.retry({ |
| 61 | while: (e) => e._tag === "Forbidden", |
| 62 | schedule: forbiddenRetrySchedule, |
| 63 | times: 8, |
| 64 | }), |
| 65 | ); |
| 66 | |
| 67 | // Purge a tuple left over from interrupted runs so each test starts clean. |
| 68 | const purgeOperation = ( |
| 69 | zoneId: string, |
| 70 | tuple: { method: string; host: string; endpoint: string }, |
| 71 | ) => |
| 72 | findOperation(zoneId, tuple).pipe( |
| 73 | Effect.flatMap((op) => |
| 74 | op |
| 75 | ? apiGateway.deleteOperation({ zoneId, operationId: op.operationId }).pipe( |
| 76 | Effect.catchTag("OperationNotFound", () => Effect.void), |
| 77 | Effect.retry({ |
| 78 | while: (e) => e._tag === "Forbidden", |
| 79 | schedule: forbiddenRetrySchedule, |
| 80 | times: 8, |
| 81 | }), |
| 82 | ) |
| 83 | : Effect.void, |
| 84 | ), |
| 85 | ); |
| 86 | |
| 87 | test.provider( |
| 88 | "create, no-op redeploy, destroy an API Shield operation", |
| 89 | (stack) => |
| 90 | Effect.gen(function* () { |
| 91 | const zoneId = yield* resolveZoneId; |
| 92 | const tuple = { method: "GET", host: zoneName, endpoint: ENDPOINT_DEFAULT }; |
| 93 | |
| 94 | yield* stack.destroy(); |
| 95 | yield* purgeOperation(zoneId, tuple); |
| 96 | |
| 97 | const op = yield* stack.deploy( |
| 98 | Effect.gen(function* () { |
| 99 | return yield* Cloudflare.ApiShield.Operation("DefaultOp", { |
| 100 | zoneId, |
| 101 | method: "GET", |
| 102 | host: zoneName, |
| 103 | endpoint: ENDPOINT_DEFAULT, |
| 104 | }).pipe(adopt(true)); |
| 105 | }), |
| 106 | ); |
| 107 | |
| 108 | expect(op.operationId).toBeDefined(); |
| 109 | expect(op.zoneId).toEqual(zoneId); |
| 110 | expect(op.method).toEqual("GET"); |
| 111 | expect(op.host).toEqual(zoneName); |
| 112 | // Cloudflare normalizes `{thingId}` to `{var1}`. |
| 113 | expect(op.endpoint).toEqual("/alchemy-apishield/default/{var1}"); |
| 114 | |
| 115 | const live = yield* getOperation(zoneId, op.operationId); |
| 116 | expect(live.operationId).toEqual(op.operationId); |
| 117 | expect(live.endpoint).toEqual("/alchemy-apishield/default/{var1}"); |
| 118 | |
| 119 | // No-op redeploy of the same (raw) endpoint converges on the same |
| 120 | // physical operation — the normalized forms match. |
| 121 | const redeployed = yield* stack.deploy( |
| 122 | Effect.gen(function* () { |
| 123 | return yield* Cloudflare.ApiShield.Operation("DefaultOp", { |
| 124 | zoneId, |
| 125 | method: "GET", |
| 126 | host: zoneName, |
| 127 | endpoint: ENDPOINT_DEFAULT, |
| 128 | }).pipe(adopt(true)); |
| 129 | }), |
| 130 | ); |
| 131 | expect(redeployed.operationId).toEqual(op.operationId); |
| 132 | |
| 133 | yield* stack.destroy(); |
| 134 | |
| 135 | const gone = yield* findOperation(zoneId, tuple); |
| 136 | expect(gone).toBeUndefined(); |
| 137 | }).pipe(logLevel), |
| 138 | { |
| 139 | tags: [ |
| 140 | "provider:cloudflare", |
| 141 | "provider:cloudflare:apishield", |
| 142 | "provider:cloudflare:zone", |
| 143 | "live", |
| 144 | ], |
| 145 | }, |
| 146 | ); |
| 147 | |
| 148 | test.provider( |
| 149 | "changing the method triggers replacement", |
| 150 | (stack) => |
| 151 | Effect.gen(function* () { |
| 152 | const zoneId = yield* resolveZoneId; |
| 153 | const getTuple = { method: "GET", host: zoneName, endpoint: ENDPOINT_REPLACE }; |
| 154 | const postTuple = { ...getTuple, method: "POST" }; |
| 155 | |
| 156 | yield* stack.destroy(); |
| 157 | yield* purgeOperation(zoneId, getTuple); |
| 158 | yield* purgeOperation(zoneId, postTuple); |
| 159 | |
| 160 | const initial = yield* stack.deploy( |
| 161 | Effect.gen(function* () { |
| 162 | return yield* Cloudflare.ApiShield.Operation("ReplaceOp", { |
| 163 | zoneId, |
| 164 | method: "GET", |
| 165 | host: zoneName, |
| 166 | endpoint: ENDPOINT_REPLACE, |
| 167 | }).pipe(adopt(true)); |
| 168 | }), |
| 169 | ); |
| 170 | expect(initial.method).toEqual("GET"); |
| 171 | |
| 172 | const replaced = yield* stack.deploy( |
| 173 | Effect.gen(function* () { |
| 174 | return yield* Cloudflare.ApiShield.Operation("ReplaceOp", { |
| 175 | zoneId, |
| 176 | method: "POST", |
| 177 | host: zoneName, |
| 178 | endpoint: ENDPOINT_REPLACE, |
| 179 | }).pipe(adopt(true)); |
| 180 | }), |
| 181 | ); |
| 182 | |
| 183 | // The tuple is the operation's identity — a new physical operation. |
| 184 | expect(replaced.operationId).not.toEqual(initial.operationId); |
| 185 | expect(replaced.method).toEqual("POST"); |
| 186 | |
| 187 | // The old GET operation was deleted as part of the replacement. |
| 188 | const oldOp = yield* findOperation(zoneId, getTuple); |
| 189 | expect(oldOp).toBeUndefined(); |
| 190 | |
| 191 | yield* stack.destroy(); |
| 192 | |
| 193 | const gone = yield* findOperation(zoneId, postTuple); |
| 194 | expect(gone).toBeUndefined(); |
| 195 | }).pipe(logLevel), |
| 196 | { |
| 197 | tags: [ |
| 198 | "provider:cloudflare", |
| 199 | "provider:cloudflare:apishield", |
| 200 | "provider:cloudflare:zone", |
| 201 | "live", |
| 202 | ], |
| 203 | }, |
| 204 | ); |
| 205 | |
| 206 | test.provider( |
| 207 | "list enumerates the deployed API Shield operation", |
| 208 | (stack) => |
| 209 | Effect.gen(function* () { |
| 210 | const zoneId = yield* resolveZoneId; |
| 211 | const tuple = { method: "GET", host: zoneName, endpoint: ENDPOINT_LIST }; |
| 212 | |
| 213 | yield* stack.destroy(); |
| 214 | yield* purgeOperation(zoneId, tuple); |
| 215 | |
| 216 | const op = yield* stack.deploy( |
| 217 | Effect.gen(function* () { |
| 218 | return yield* Cloudflare.ApiShield.Operation("ListOp", { |
| 219 | zoneId, |
| 220 | method: "GET", |
| 221 | host: zoneName, |
| 222 | endpoint: ENDPOINT_LIST, |
| 223 | }).pipe(adopt(true)); |
| 224 | }), |
| 225 | ); |
| 226 | |
| 227 | const provider = yield* Provider.findProvider(Cloudflare.ApiShield.Operation); |
| 228 | const all = yield* provider.list(); |
| 229 | |
| 230 | // The deployed operation appears in the exhaustively-paginated, |
| 231 | // all-zones result with the exact `read` shape. |
| 232 | const found = all.find((x) => x.operationId === op.operationId); |
| 233 | expect(found).toBeDefined(); |
| 234 | expect(found?.zoneId).toEqual(zoneId); |
| 235 | expect(found?.method).toEqual("GET"); |
| 236 | expect(found?.host).toEqual(zoneName); |
| 237 | expect(found?.endpoint).toEqual("/alchemy-apishield/list"); |
| 238 | expect(found?.lastUpdated).toBeDefined(); |
| 239 | |
| 240 | yield* stack.destroy(); |
| 241 | |
| 242 | const gone = yield* findOperation(zoneId, tuple); |
| 243 | expect(gone).toBeUndefined(); |
| 244 | }).pipe(logLevel), |
| 245 | { |
| 246 | tags: [ |
| 247 | "provider:cloudflare", |
| 248 | "provider:cloudflare:apishield", |
| 249 | "provider:cloudflare:zone", |
| 250 | "live", |
| 251 | ], |
| 252 | }, |
| 253 | ); |