The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 r2 from "@distilled.cloud/cloudflare/r2"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | // The scoped API token the test harness mints propagates eventually- |
| 16 | // consistently across Cloudflare's edge — ride out 403 blips (`Forbidden`, |
| 17 | // declared in the distilled error union) on out-of-band verification calls. |
| 18 | const getNotification = (accountId: string, bucketName: string, queueId: string) => |
| 19 | r2.getBucketEventNotification({ accountId, bucketName, queueId }).pipe( |
| 20 | Effect.retry({ |
| 21 | while: (e) => e._tag === "Forbidden", |
| 22 | schedule: Schedule.exponential("500 millis"), |
| 23 | times: 8, |
| 24 | }), |
| 25 | ); |
| 26 | |
| 27 | // A deleted configuration surfaces as `EventNotificationConfigNotFound` |
| 28 | // (code 11011) or `NoEventNotificationConfig` (code 11015) — and once the |
| 29 | // stack's bucket/queue are destroyed, as `BucketNotFound`/`QueueNotFound`. |
| 30 | // All of those are the success condition here. |
| 31 | const expectGone = (accountId: string, bucketName: string, queueId: string) => |
| 32 | getNotification(accountId, bucketName, queueId).pipe( |
| 33 | Effect.flatMap(() => Effect.fail({ _tag: "NotificationNotDeleted" } as const)), |
| 34 | Effect.catchTag( |
| 35 | [ |
| 36 | "NoEventNotificationConfig", |
| 37 | "EventNotificationConfigNotFound", |
| 38 | "BucketNotFound", |
| 39 | "QueueNotFound", |
| 40 | ], |
| 41 | () => Effect.void, |
| 42 | ), |
| 43 | Effect.retry({ |
| 44 | while: (e) => e._tag === "NotificationNotDeleted", |
| 45 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]), |
| 46 | }), |
| 47 | ); |
| 48 | |
| 49 | // One program deploying the bucket, the queue, and the notification joining |
| 50 | // them. The notification's props reference both resources' outputs, so the |
| 51 | // engine orders notification-last on deploy (and first on destroy). |
| 52 | const program = (opts: { rules: Cloudflare.R2.BucketEventNotificationRule[] }) => |
| 53 | Effect.gen(function* () { |
| 54 | const bucket = yield* Cloudflare.R2.Bucket("EventBucket", { |
| 55 | forceDestroy: true, |
| 56 | }); |
| 57 | const queue = yield* Cloudflare.Queues.Queue("EventQueueA"); |
| 58 | const notification = yield* Cloudflare.R2.BucketEventNotification("Notification", { |
| 59 | bucketName: bucket.bucketName, |
| 60 | queueId: queue.queueId, |
| 61 | rules: opts.rules, |
| 62 | }); |
| 63 | return { bucket, queue, notification }; |
| 64 | }); |
| 65 | |
| 66 | // Replacement variant — both queues stay deployed across both steps; only |
| 67 | // the notification's target queue flips, so the replacement is isolated to |
| 68 | // the notification itself. |
| 69 | const replacementProgram = (opts: { |
| 70 | rules: Cloudflare.R2.BucketEventNotificationRule[]; |
| 71 | target: "A" | "B"; |
| 72 | }) => |
| 73 | Effect.gen(function* () { |
| 74 | const bucket = yield* Cloudflare.R2.Bucket("EventBucket", { |
| 75 | forceDestroy: true, |
| 76 | }); |
| 77 | const queueA = yield* Cloudflare.Queues.Queue("EventQueueA"); |
| 78 | const queueB = yield* Cloudflare.Queues.Queue("EventQueueB"); |
| 79 | const target = opts.target === "B" ? queueB : queueA; |
| 80 | const notification = yield* Cloudflare.R2.BucketEventNotification("Notification", { |
| 81 | bucketName: bucket.bucketName, |
| 82 | queueId: target.queueId, |
| 83 | rules: opts.rules, |
| 84 | }); |
| 85 | return { bucket, queueA, queueB, notification }; |
| 86 | }); |
| 87 | |
| 88 | test.provider( |
| 89 | "create, update rules in place, and delete", |
| 90 | (stack) => |
| 91 | Effect.gen(function* () { |
| 92 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 93 | |
| 94 | yield* stack.destroy(); |
| 95 | |
| 96 | // Create — a single prefix-scoped rule. |
| 97 | const initial = yield* stack.deploy( |
| 98 | program({ |
| 99 | rules: [ |
| 100 | { |
| 101 | actions: ["PutObject", "DeleteObject"], |
| 102 | prefix: "incoming/", |
| 103 | }, |
| 104 | ], |
| 105 | }), |
| 106 | ); |
| 107 | |
| 108 | expect(initial.notification.accountId).toEqual(accountId); |
| 109 | expect(initial.notification.bucketName).toEqual(initial.bucket.bucketName); |
| 110 | expect(initial.notification.queueId).toEqual(initial.queue.queueId); |
| 111 | expect(initial.notification.jurisdiction).toEqual("default"); |
| 112 | expect(initial.notification.rules).toHaveLength(1); |
| 113 | expect([...initial.notification.rules[0].actions].sort()).toEqual([ |
| 114 | "DeleteObject", |
| 115 | "PutObject", |
| 116 | ]); |
| 117 | expect(initial.notification.rules[0].prefix).toEqual("incoming/"); |
| 118 | expect(initial.notification.rules[0].ruleId).toBeDefined(); |
| 119 | |
| 120 | const live = yield* getNotification( |
| 121 | accountId, |
| 122 | initial.bucket.bucketName, |
| 123 | initial.queue.queueId, |
| 124 | ); |
| 125 | // The endpoint echoes the queue ID in dashed-UUID form. |
| 126 | expect(live.queueId?.replace(/-/g, "")).toEqual(initial.queue.queueId); |
| 127 | expect(live.rules ?? []).toHaveLength(1); |
| 128 | expect(live.rules?.[0].prefix).toEqual("incoming/"); |
| 129 | |
| 130 | // Update — replace the rule set in place (same bucket + queue pair). |
| 131 | const updated = yield* stack.deploy( |
| 132 | program({ |
| 133 | rules: [ |
| 134 | { |
| 135 | actions: ["PutObject", "CompleteMultipartUpload"], |
| 136 | prefix: "images/", |
| 137 | suffix: ".png", |
| 138 | description: "new PNG images", |
| 139 | }, |
| 140 | { |
| 141 | actions: ["DeleteObject", "LifecycleDeletion"], |
| 142 | // R2 rejects rules whose key ranges overlap ("invalid |
| 143 | // overlap"), even with disjoint actions — scope this rule |
| 144 | // to a separate prefix. |
| 145 | prefix: "logs/", |
| 146 | }, |
| 147 | ], |
| 148 | }), |
| 149 | ); |
| 150 | |
| 151 | // Same configuration mutated in place — not a replacement. |
| 152 | expect(updated.notification.bucketName).toEqual(initial.notification.bucketName); |
| 153 | expect(updated.notification.queueId).toEqual(initial.notification.queueId); |
| 154 | expect(updated.notification.rules).toHaveLength(2); |
| 155 | |
| 156 | const liveUpdated = yield* getNotification( |
| 157 | accountId, |
| 158 | updated.bucket.bucketName, |
| 159 | updated.queue.queueId, |
| 160 | ); |
| 161 | const liveRules = [...(liveUpdated.rules ?? [])].sort((a, b) => |
| 162 | (a.prefix ?? "").localeCompare(b.prefix ?? ""), |
| 163 | ); |
| 164 | expect(liveRules).toHaveLength(2); |
| 165 | expect(liveRules[0].prefix).toEqual("images/"); |
| 166 | expect(liveRules[0].suffix).toEqual(".png"); |
| 167 | expect(liveRules[0].description).toEqual("new PNG images"); |
| 168 | expect(liveRules[1].prefix).toEqual("logs/"); |
| 169 | expect([...liveRules[1].actions].sort()).toEqual(["DeleteObject", "LifecycleDeletion"]); |
| 170 | |
| 171 | // Redeploying identical props is a no-op (same rule IDs survive). |
| 172 | const noop = yield* stack.deploy( |
| 173 | program({ |
| 174 | rules: [ |
| 175 | { |
| 176 | actions: ["PutObject", "CompleteMultipartUpload"], |
| 177 | prefix: "images/", |
| 178 | suffix: ".png", |
| 179 | description: "new PNG images", |
| 180 | }, |
| 181 | { |
| 182 | actions: ["DeleteObject", "LifecycleDeletion"], |
| 183 | // R2 rejects rules whose key ranges overlap ("invalid |
| 184 | // overlap"), even with disjoint actions — scope this rule |
| 185 | // to a separate prefix. |
| 186 | prefix: "logs/", |
| 187 | }, |
| 188 | ], |
| 189 | }), |
| 190 | ); |
| 191 | expect(noop.notification.rules.map((rule) => rule.ruleId).sort()).toEqual( |
| 192 | updated.notification.rules.map((rule) => rule.ruleId).sort(), |
| 193 | ); |
| 194 | |
| 195 | yield* stack.destroy(); |
| 196 | |
| 197 | yield* expectGone(accountId, initial.bucket.bucketName, initial.queue.queueId); |
| 198 | |
| 199 | // Destroy again — delete must be idempotent (already gone). |
| 200 | yield* stack.destroy(); |
| 201 | }).pipe(logLevel), |
| 202 | { |
| 203 | tags: ["provider:cloudflare", "provider:cloudflare:queue", "provider:cloudflare:r2", "live"], |
| 204 | timeout: 300_000, |
| 205 | }, |
| 206 | ); |
| 207 | |
| 208 | test.provider( |
| 209 | "changing the queue triggers a replacement", |
| 210 | (stack) => |
| 211 | Effect.gen(function* () { |
| 212 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 213 | |
| 214 | yield* stack.destroy(); |
| 215 | |
| 216 | const initial = yield* stack.deploy( |
| 217 | replacementProgram({ |
| 218 | target: "A", |
| 219 | rules: [{ actions: ["PutObject"] }], |
| 220 | }), |
| 221 | ); |
| 222 | expect(initial.notification.queueId).toEqual(initial.queueA.queueId); |
| 223 | |
| 224 | // Point the notification at a different queue — the (bucket, queue) |
| 225 | // pair is the configuration's identity, so this replaces it. |
| 226 | const replaced = yield* stack.deploy( |
| 227 | replacementProgram({ |
| 228 | target: "B", |
| 229 | rules: [{ actions: ["PutObject"] }], |
| 230 | }), |
| 231 | ); |
| 232 | |
| 233 | expect(replaced.queueB.queueId).not.toEqual(initial.queueA.queueId); |
| 234 | expect(replaced.notification.queueId).toEqual(replaced.queueB.queueId); |
| 235 | |
| 236 | // The old pair's configuration is gone; the new pair's is live. |
| 237 | yield* expectGone(accountId, initial.bucket.bucketName, initial.queueA.queueId); |
| 238 | const live = yield* getNotification( |
| 239 | accountId, |
| 240 | replaced.bucket.bucketName, |
| 241 | replaced.queueB.queueId, |
| 242 | ); |
| 243 | // The endpoint echoes the queue ID in dashed-UUID form. |
| 244 | expect(live.queueId?.replace(/-/g, "")).toEqual(replaced.queueB.queueId); |
| 245 | |
| 246 | yield* stack.destroy(); |
| 247 | |
| 248 | yield* expectGone(accountId, replaced.bucket.bucketName, replaced.queueB.queueId); |
| 249 | }).pipe(logLevel), |
| 250 | { |
| 251 | tags: ["provider:cloudflare", "provider:cloudflare:queue", "provider:cloudflare:r2", "live"], |
| 252 | timeout: 300_000, |
| 253 | }, |
| 254 | ); |
| 255 | |
| 256 | test.provider( |
| 257 | "list enumerates deployed bucket event notifications", |
| 258 | (stack) => |
| 259 | Effect.gen(function* () { |
| 260 | yield* stack.destroy(); |
| 261 | |
| 262 | const deployed = yield* stack.deploy( |
| 263 | program({ |
| 264 | rules: [{ actions: ["PutObject", "DeleteObject"], prefix: "incoming/" }], |
| 265 | }), |
| 266 | ); |
| 267 | |
| 268 | // Parent fan-out over every R2 bucket, then each bucket's |
| 269 | // event-notification queues — the deployed (bucket, queue) pair must |
| 270 | // appear, hydrated into the exact `read` Attributes shape. |
| 271 | const provider = yield* Provider.findProvider(Cloudflare.R2.BucketEventNotification); |
| 272 | const all = yield* provider.list(); |
| 273 | |
| 274 | const found = all.find( |
| 275 | (n) => n.bucketName === deployed.bucket.bucketName && n.queueId === deployed.queue.queueId, |
| 276 | ); |
| 277 | expect(found).toBeDefined(); |
| 278 | expect(found?.accountId).toBeDefined(); |
| 279 | expect(found?.jurisdiction).toEqual("default"); |
| 280 | expect(found?.rules.length).toBeGreaterThanOrEqual(1); |
| 281 | expect(found?.rules.some((rule) => rule.prefix === "incoming/")).toBe(true); |
| 282 | |
| 283 | yield* stack.destroy(); |
| 284 | }).pipe(logLevel), |
| 285 | { |
| 286 | tags: ["provider:cloudflare", "provider:cloudflare:queue", "provider:cloudflare:r2", "live"], |
| 287 | timeout: 300_000, |
| 288 | }, |
| 289 | ); |