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 { generateLocalId, isLiveId } from "@alchemy.run/cloudflare/LocalRuntime"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as queues from "@distilled.cloud/cloudflare/queues"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 8 | import { State } from "alchemy/State"; |
| 9 | import type { CreatedResourceState } from "alchemy/State/ResourceState"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as Exit from "effect/Exit"; |
| 12 | import { MinimumLogLevel } from "effect/References"; |
| 13 | import * as Schedule from "effect/Schedule"; |
| 14 | import * as pathe from "pathe"; |
| 15 | |
| 16 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 17 | |
| 18 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 19 | |
| 20 | const main = pathe.resolve(import.meta.dirname, "consumer-worker.ts"); |
| 21 | |
| 22 | /** |
| 23 | * Lifecycle: create the consumer, change settings (in-place update), |
| 24 | * change script (replace), then destroy. |
| 25 | * |
| 26 | * Verifies the diff matrix end-to-end and that updateConsumer is |
| 27 | * issued every reconcile (so settings drift gets corrected even when |
| 28 | * `olds.settings` matches `news.settings`). |
| 29 | */ |
| 30 | test.provider( |
| 31 | "create, update settings, replace script, delete", |
| 32 | (stack) => |
| 33 | Effect.gen(function* () { |
| 34 | const env = yield* CloudflareEnvironment; |
| 35 | const { accountId } = yield* env; |
| 36 | |
| 37 | yield* stack.destroy(); |
| 38 | |
| 39 | const initial = yield* stack.deploy( |
| 40 | Effect.gen(function* () { |
| 41 | const queue = yield* Cloudflare.Queues.Queue("Q"); |
| 42 | const workerA = yield* Cloudflare.Worker("WorkerA", { |
| 43 | main, |
| 44 | compatibility: { date: "2024-01-01" }, |
| 45 | }); |
| 46 | const consumer = yield* Cloudflare.Queues.Consumer("Consumer", { |
| 47 | queueId: queue.queueId, |
| 48 | scriptName: workerA.workerName, |
| 49 | settings: { batchSize: 5, maxRetries: 3 }, |
| 50 | }); |
| 51 | return { queue, workerA, consumer }; |
| 52 | }), |
| 53 | ); |
| 54 | |
| 55 | expect(initial.consumer.consumerId).toBeTypeOf("string"); |
| 56 | expect(initial.consumer.scriptName).toEqual(initial.workerA.workerName); |
| 57 | |
| 58 | const live = yield* queues.getConsumer({ |
| 59 | accountId, |
| 60 | queueId: initial.queue.queueId, |
| 61 | consumerId: initial.consumer.consumerId, |
| 62 | }); |
| 63 | expect("scriptName" in live ? live.scriptName : undefined).toEqual( |
| 64 | initial.workerA.workerName, |
| 65 | ); |
| 66 | |
| 67 | // Settings-only change is an update, not a replace — consumerId |
| 68 | // must remain stable. |
| 69 | const updated = yield* stack.deploy( |
| 70 | Effect.gen(function* () { |
| 71 | const queue = yield* Cloudflare.Queues.Queue("Q"); |
| 72 | const workerA = yield* Cloudflare.Worker("WorkerA", { |
| 73 | main, |
| 74 | compatibility: { date: "2024-01-01" }, |
| 75 | }); |
| 76 | const consumer = yield* Cloudflare.Queues.Consumer("Consumer", { |
| 77 | queueId: queue.queueId, |
| 78 | scriptName: workerA.workerName, |
| 79 | settings: { batchSize: 25, maxRetries: 7 }, |
| 80 | }); |
| 81 | return { queue, workerA, consumer }; |
| 82 | }), |
| 83 | ); |
| 84 | |
| 85 | expect(updated.consumer.consumerId).toEqual(initial.consumer.consumerId); |
| 86 | |
| 87 | const liveUpdated = yield* queues.getConsumer({ |
| 88 | accountId, |
| 89 | queueId: updated.queue.queueId, |
| 90 | consumerId: updated.consumer.consumerId, |
| 91 | }); |
| 92 | expect(liveUpdated.settings?.batchSize).toEqual(25); |
| 93 | expect(liveUpdated.settings?.maxRetries).toEqual(7); |
| 94 | |
| 95 | // Script change is a delete-first replace: Cloudflare's |
| 96 | // updateConsumer silently ignores script_name on an existing |
| 97 | // consumer, and the platform allows only one Worker consumer |
| 98 | // per queue, so the engine must tear the old consumer down |
| 99 | // before creating the new one. WorkerA stays yielded across |
| 100 | // the deploy so it isn't garbage-collected mid-replace and |
| 101 | // race the Worker.delete with Cloudflare's queue↔script sync. |
| 102 | const replaced = yield* stack.deploy( |
| 103 | Effect.gen(function* () { |
| 104 | const queue = yield* Cloudflare.Queues.Queue("Q"); |
| 105 | yield* Cloudflare.Worker("WorkerA", { main, compatibility: { date: "2024-01-01" } }); |
| 106 | const workerB = yield* Cloudflare.Worker("WorkerB", { |
| 107 | main, |
| 108 | compatibility: { date: "2024-01-01" }, |
| 109 | }); |
| 110 | const consumer = yield* Cloudflare.Queues.Consumer("Consumer", { |
| 111 | queueId: queue.queueId, |
| 112 | scriptName: workerB.workerName, |
| 113 | settings: { batchSize: 25, maxRetries: 7 }, |
| 114 | }); |
| 115 | return { queue, workerB, consumer }; |
| 116 | }), |
| 117 | ); |
| 118 | |
| 119 | expect(replaced.consumer.consumerId).not.toEqual(initial.consumer.consumerId); |
| 120 | expect(replaced.consumer.scriptName).toEqual(replaced.workerB.workerName); |
| 121 | |
| 122 | const liveReplaced = yield* queues.getConsumer({ |
| 123 | accountId, |
| 124 | queueId: replaced.queue.queueId, |
| 125 | consumerId: replaced.consumer.consumerId, |
| 126 | }); |
| 127 | expect("scriptName" in liveReplaced ? liveReplaced.scriptName : undefined).toEqual( |
| 128 | replaced.workerB.workerName, |
| 129 | ); |
| 130 | |
| 131 | // The original consumer must be gone after the replace. |
| 132 | const oldExit = yield* Effect.exit( |
| 133 | queues.getConsumer({ |
| 134 | accountId, |
| 135 | queueId: replaced.queue.queueId, |
| 136 | consumerId: initial.consumer.consumerId, |
| 137 | }), |
| 138 | ); |
| 139 | expect(Exit.isFailure(oldExit)).toBe(true); |
| 140 | |
| 141 | yield* stack.destroy(); |
| 142 | |
| 143 | // Post-destroy: the new consumer must be gone on Cloudflare too. |
| 144 | const exit = yield* Effect.exit( |
| 145 | queues.getConsumer({ |
| 146 | accountId, |
| 147 | queueId: replaced.queue.queueId, |
| 148 | consumerId: replaced.consumer.consumerId, |
| 149 | }), |
| 150 | ); |
| 151 | expect(Exit.isFailure(exit)).toBe(true); |
| 152 | }).pipe(logLevel), |
| 153 | { |
| 154 | tags: [ |
| 155 | "provider:cloudflare", |
| 156 | "provider:cloudflare:queue", |
| 157 | "provider:cloudflare:worker", |
| 158 | "live", |
| 159 | ], |
| 160 | }, |
| 161 | ); |
| 162 | |
| 163 | /** |
| 164 | * Recovery from out-of-band consumer deletion. After a manual |
| 165 | * `deleteConsumer` via the API, the reconciler must observe that |
| 166 | * the consumer is missing and recreate it instead of failing on a |
| 167 | * stale `output.consumerId` from local state. |
| 168 | * |
| 169 | * The redeploy bumps `settings` so the diff returns `update` and |
| 170 | * the engine actually invokes reconcile (a no-prop redeploy is a |
| 171 | * `noop` and skips drift detection by design — drift correction |
| 172 | * only happens when something the user-controlled changes). |
| 173 | */ |
| 174 | test.provider( |
| 175 | "recreates consumer after out-of-band delete", |
| 176 | (stack) => |
| 177 | Effect.gen(function* () { |
| 178 | const env = yield* CloudflareEnvironment; |
| 179 | const { accountId } = yield* env; |
| 180 | |
| 181 | yield* stack.destroy(); |
| 182 | |
| 183 | const initial = yield* stack.deploy( |
| 184 | Effect.gen(function* () { |
| 185 | const queue = yield* Cloudflare.Queues.Queue("Q"); |
| 186 | const worker = yield* Cloudflare.Worker("Worker", { |
| 187 | main, |
| 188 | compatibility: { date: "2024-01-01" }, |
| 189 | }); |
| 190 | const consumer = yield* Cloudflare.Queues.Consumer("Consumer", { |
| 191 | queueId: queue.queueId, |
| 192 | scriptName: worker.workerName, |
| 193 | settings: { batchSize: 5 }, |
| 194 | }); |
| 195 | return { queue, worker, consumer }; |
| 196 | }), |
| 197 | ); |
| 198 | |
| 199 | // Out-of-band delete via the SDK directly. |
| 200 | yield* queues.deleteConsumer({ |
| 201 | accountId, |
| 202 | queueId: initial.queue.queueId, |
| 203 | consumerId: initial.consumer.consumerId, |
| 204 | }); |
| 205 | |
| 206 | const recovered = yield* stack.deploy( |
| 207 | Effect.gen(function* () { |
| 208 | const queue = yield* Cloudflare.Queues.Queue("Q"); |
| 209 | const worker = yield* Cloudflare.Worker("Worker", { |
| 210 | main, |
| 211 | compatibility: { date: "2024-01-01" }, |
| 212 | }); |
| 213 | const consumer = yield* Cloudflare.Queues.Consumer("Consumer", { |
| 214 | queueId: queue.queueId, |
| 215 | scriptName: worker.workerName, |
| 216 | settings: { batchSize: 11 }, |
| 217 | }); |
| 218 | return { queue, worker, consumer }; |
| 219 | }), |
| 220 | ); |
| 221 | |
| 222 | expect(recovered.consumer.consumerId).toBeTypeOf("string"); |
| 223 | // The new consumer must be reachable on Cloudflare — the previous |
| 224 | // implementation died with "already exists but could not be found" |
| 225 | // because listConsumers was single-page and ConsumerAlreadyExists |
| 226 | // was caught by a generic `Effect.catch`. A freshly-created consumer |
| 227 | // can briefly 404 from this out-of-band read under load, so ride out |
| 228 | // the read-after-create lag before asserting. |
| 229 | const live = yield* queues |
| 230 | .getConsumer({ |
| 231 | accountId, |
| 232 | queueId: recovered.queue.queueId, |
| 233 | consumerId: recovered.consumer.consumerId, |
| 234 | }) |
| 235 | .pipe( |
| 236 | Effect.retry({ |
| 237 | while: (e) => e._tag === "ConsumerNotFound", |
| 238 | schedule: Schedule.exponential("500 millis"), |
| 239 | times: 8, |
| 240 | }), |
| 241 | ); |
| 242 | expect("scriptName" in live ? live.scriptName : undefined).toEqual( |
| 243 | recovered.worker.workerName, |
| 244 | ); |
| 245 | |
| 246 | yield* stack.destroy(); |
| 247 | }).pipe(logLevel), |
| 248 | { |
| 249 | tags: [ |
| 250 | "provider:cloudflare", |
| 251 | "provider:cloudflare:queue", |
| 252 | "provider:cloudflare:worker", |
| 253 | "live", |
| 254 | ], |
| 255 | }, |
| 256 | ); |
| 257 | |
| 258 | /** |
| 259 | * State-loss adoption. Wipe the local state for the Consumer, |
| 260 | * leaving the Cloudflare consumer in place. On redeploy the engine |
| 261 | * calls `provider.read`, which now falls back to listConsumers when |
| 262 | * `output.consumerId` is missing — so the consumer is adopted instead |
| 263 | * of producing a duplicate-create attempt. |
| 264 | */ |
| 265 | test.provider( |
| 266 | "adopts existing consumer after local state loss", |
| 267 | (stack) => |
| 268 | Effect.gen(function* () { |
| 269 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 270 | |
| 271 | yield* stack.destroy(); |
| 272 | |
| 273 | const initial = yield* stack.deploy( |
| 274 | Effect.gen(function* () { |
| 275 | const queue = yield* Cloudflare.Queues.Queue("Q"); |
| 276 | const worker = yield* Cloudflare.Worker("Worker", { |
| 277 | main, |
| 278 | compatibility: { date: "2024-01-01" }, |
| 279 | }); |
| 280 | const consumer = yield* Cloudflare.Queues.Consumer("Consumer", { |
| 281 | queueId: queue.queueId, |
| 282 | scriptName: worker.workerName, |
| 283 | }); |
| 284 | return { queue, worker, consumer }; |
| 285 | }), |
| 286 | ); |
| 287 | |
| 288 | // Wipe just the Consumer entry — Queue and Worker stay so the |
| 289 | // redeploy reuses the same queueId / scriptName. |
| 290 | yield* Effect.gen(function* () { |
| 291 | const state = yield* yield* State; |
| 292 | yield* state.delete({ stack: stack.name, stage: stack.stage, fqn: "Consumer" }); |
| 293 | }).pipe(Effect.provide(stack.state)); |
| 294 | |
| 295 | const adopted = yield* stack.deploy( |
| 296 | Effect.gen(function* () { |
| 297 | const queue = yield* Cloudflare.Queues.Queue("Q"); |
| 298 | const worker = yield* Cloudflare.Worker("Worker", { |
| 299 | main, |
| 300 | compatibility: { date: "2024-01-01" }, |
| 301 | }); |
| 302 | const consumer = yield* Cloudflare.Queues.Consumer("Consumer", { |
| 303 | queueId: queue.queueId, |
| 304 | scriptName: worker.workerName, |
| 305 | }); |
| 306 | return { queue, worker, consumer }; |
| 307 | }), |
| 308 | ); |
| 309 | |
| 310 | // Adoption: the consumerId from Cloudflare equals the one we |
| 311 | // created originally — we did not duplicate-create. |
| 312 | expect(adopted.consumer.consumerId).toEqual(initial.consumer.consumerId); |
| 313 | |
| 314 | const live = yield* queues.getConsumer({ |
| 315 | accountId, |
| 316 | queueId: adopted.queue.queueId, |
| 317 | consumerId: adopted.consumer.consumerId, |
| 318 | }); |
| 319 | expect("scriptName" in live ? live.scriptName : undefined).toEqual(adopted.worker.workerName); |
| 320 | |
| 321 | yield* stack.destroy(); |
| 322 | }).pipe(logLevel), |
| 323 | { |
| 324 | tags: [ |
| 325 | "provider:cloudflare", |
| 326 | "provider:cloudflare:queue", |
| 327 | "provider:cloudflare:worker", |
| 328 | "live", |
| 329 | ], |
| 330 | }, |
| 331 | ); |
| 332 | |
| 333 | /** |
| 334 | * Conflict: queue already has a worker consumer pointing at a |
| 335 | * *different* script. This is the regression for the user-reported |
| 336 | * "already exists but could not be found" error — the previous |
| 337 | * implementation filtered listConsumers by `news.scriptName`, so a |
| 338 | * collision with a different script left the find empty and the |
| 339 | * reconciler died with a misleading message. |
| 340 | * |
| 341 | * The new behaviour: detect the foreign worker consumer and fail with |
| 342 | * a clear, actionable error naming both the existing script and the |
| 343 | * desired one. |
| 344 | */ |
| 345 | test.provider( |
| 346 | "fails clearly when queue has consumer for different script", |
| 347 | (stack) => |
| 348 | Effect.gen(function* () { |
| 349 | yield* stack.destroy(); |
| 350 | |
| 351 | // Phase 1: deploy worker A as the queue's consumer, then wipe just |
| 352 | // the Consumer state so the next deploy thinks it's a |
| 353 | // greenfield create. |
| 354 | const initial = yield* stack.deploy( |
| 355 | Effect.gen(function* () { |
| 356 | const queue = yield* Cloudflare.Queues.Queue("Q"); |
| 357 | const workerA = yield* Cloudflare.Worker("WorkerA", { |
| 358 | main, |
| 359 | compatibility: { date: "2024-01-01" }, |
| 360 | }); |
| 361 | const consumer = yield* Cloudflare.Queues.Consumer("Consumer", { |
| 362 | queueId: queue.queueId, |
| 363 | scriptName: workerA.workerName, |
| 364 | }); |
| 365 | return { queue, workerA, consumer }; |
| 366 | }), |
| 367 | ); |
| 368 | |
| 369 | yield* Effect.gen(function* () { |
| 370 | const state = yield* yield* State; |
| 371 | yield* state.delete({ stack: stack.name, stage: stack.stage, fqn: "Consumer" }); |
| 372 | }).pipe(Effect.provide(stack.state)); |
| 373 | |
| 374 | // Phase 2: redeploy with a different scriptName under the same |
| 375 | // logical id. Cloudflare's queue still has worker A as consumer. |
| 376 | const exit = yield* Effect.exit( |
| 377 | stack.deploy( |
| 378 | Effect.gen(function* () { |
| 379 | const queue = yield* Cloudflare.Queues.Queue("Q"); |
| 380 | const workerB = yield* Cloudflare.Worker("WorkerB", { |
| 381 | main, |
| 382 | compatibility: { date: "2024-01-01" }, |
| 383 | }); |
| 384 | const consumer = yield* Cloudflare.Queues.Consumer("Consumer", { |
| 385 | queueId: queue.queueId, |
| 386 | scriptName: workerB.workerName, |
| 387 | }); |
| 388 | return { queue, workerB, consumer }; |
| 389 | }), |
| 390 | ), |
| 391 | ); |
| 392 | |
| 393 | expect(Exit.isFailure(exit)).toBe(true); |
| 394 | const message = JSON.stringify(exit); |
| 395 | // The error must name both the colliding existing script and the |
| 396 | // requested one — that is the difference between "user can fix |
| 397 | // this" and "what does this mean". |
| 398 | expect(message).toContain(initial.workerA.workerName); |
| 399 | expect(message).toContain("only one worker consumer"); |
| 400 | |
| 401 | // Cleanup: re-introduce the Consumer entry pointing at workerA so |
| 402 | // destroy can remove the cloud consumer. |
| 403 | yield* stack.deploy( |
| 404 | Effect.gen(function* () { |
| 405 | const queue = yield* Cloudflare.Queues.Queue("Q"); |
| 406 | const workerA = yield* Cloudflare.Worker("WorkerA", { |
| 407 | main, |
| 408 | compatibility: { date: "2024-01-01" }, |
| 409 | }); |
| 410 | yield* Cloudflare.Queues.Consumer("Consumer", { |
| 411 | queueId: queue.queueId, |
| 412 | scriptName: workerA.workerName, |
| 413 | }); |
| 414 | return queue; |
| 415 | }), |
| 416 | ); |
| 417 | |
| 418 | yield* stack.destroy(); |
| 419 | }).pipe(logLevel), |
| 420 | { |
| 421 | tags: [ |
| 422 | "provider:cloudflare", |
| 423 | "provider:cloudflare:queue", |
| 424 | "provider:cloudflare:worker", |
| 425 | "live", |
| 426 | ], |
| 427 | }, |
| 428 | ); |
| 429 | |
| 430 | /** |
| 431 | * Adoption of a stranded prior-generation consumer. A Worker replacement |
| 432 | * mints a new physical script name; when the consumer's state entry is |
| 433 | * lost across that replacement (the beta.64→beta.67 upgrade wedge), the |
| 434 | * queue keeps a worker consumer pointing at the *old* generation's script |
| 435 | * while the resource is configured for the new one. Both scripts carry |
| 436 | * the same `alchemy:stack/stage/id` ownership tags, which proves the |
| 437 | * stranded consumer is this resource's own wiring — the reconciler must |
| 438 | * detach it and rebuild for the new script instead of dying with the |
| 439 | * foreign-consumer error (which stays reserved for scripts of a |
| 440 | * *different* logical worker — see the previous test). |
| 441 | */ |
| 442 | test.provider( |
| 443 | "adopts stranded consumer from a replaced generation of the same worker", |
| 444 | (stack) => |
| 445 | Effect.gen(function* () { |
| 446 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 447 | |
| 448 | yield* stack.destroy(); |
| 449 | |
| 450 | const initial = yield* stack.deploy( |
| 451 | Effect.gen(function* () { |
| 452 | const queue = yield* Cloudflare.Queues.Queue("Q"); |
| 453 | const worker = yield* Cloudflare.Worker("Worker", { |
| 454 | main, |
| 455 | compatibility: { date: "2024-01-01" }, |
| 456 | }); |
| 457 | const consumer = yield* Cloudflare.Queues.Consumer("Consumer", { |
| 458 | queueId: queue.queueId, |
| 459 | scriptName: worker.workerName, |
| 460 | }); |
| 461 | return { queue, worker, consumer }; |
| 462 | }), |
| 463 | ); |
| 464 | |
| 465 | // Lose the consumer's state entry — the live consumer stays |
| 466 | // attached to the queue, pointing at the current script. |
| 467 | yield* Effect.gen(function* () { |
| 468 | const state = yield* yield* State; |
| 469 | yield* state.delete({ stack: stack.name, stage: stack.stage, fqn: "Consumer" }); |
| 470 | }).pipe(Effect.provide(stack.state)); |
| 471 | |
| 472 | // Replace the worker's physical script (explicit `name` forces a |
| 473 | // replacement) in the same deploy that recreates the consumer. |
| 474 | // Reconcile observes the stranded consumer on the old script — |
| 475 | // same stack/stage/id tags — and must rebuild instead of dying. |
| 476 | // Previews cap script names at 54 chars — trim before suffixing. |
| 477 | const replacedName = `${initial.worker.workerName.slice(0, 48)}-v2`; |
| 478 | const recovered = yield* stack.deploy( |
| 479 | Effect.gen(function* () { |
| 480 | const queue = yield* Cloudflare.Queues.Queue("Q"); |
| 481 | const worker = yield* Cloudflare.Worker("Worker", { |
| 482 | main, |
| 483 | name: replacedName, |
| 484 | compatibility: { date: "2024-01-01" }, |
| 485 | }); |
| 486 | const consumer = yield* Cloudflare.Queues.Consumer("Consumer", { |
| 487 | queueId: queue.queueId, |
| 488 | scriptName: worker.workerName, |
| 489 | }); |
| 490 | return { queue, worker, consumer }; |
| 491 | }), |
| 492 | ); |
| 493 | |
| 494 | expect(recovered.worker.workerName).toEqual(replacedName); |
| 495 | expect(recovered.consumer.scriptName).toEqual(replacedName); |
| 496 | |
| 497 | const live = yield* queues |
| 498 | .getConsumer({ |
| 499 | accountId, |
| 500 | queueId: recovered.queue.queueId, |
| 501 | consumerId: recovered.consumer.consumerId, |
| 502 | }) |
| 503 | .pipe( |
| 504 | Effect.retry({ |
| 505 | while: (e) => e._tag === "ConsumerNotFound", |
| 506 | schedule: Schedule.exponential("500 millis"), |
| 507 | times: 8, |
| 508 | }), |
| 509 | ); |
| 510 | expect("scriptName" in live ? live.scriptName : undefined).toEqual(replacedName); |
| 511 | |
| 512 | yield* stack.destroy(); |
| 513 | }).pipe(logLevel), |
| 514 | { |
| 515 | tags: [ |
| 516 | "provider:cloudflare", |
| 517 | "provider:cloudflare:queue", |
| 518 | "provider:cloudflare:worker", |
| 519 | "live", |
| 520 | ], |
| 521 | }, |
| 522 | ); |
| 523 | |
| 524 | /** |
| 525 | * Suppressed delete: a consumer that only ever existed in dev (its |
| 526 | * persisted `consumerId` is a `dev:` mock id) has no Cloudflare |
| 527 | * counterpart. Destroying it must NOT issue a `deleteConsumer` against |
| 528 | * Cloudflare — the `dev:` ids are not valid and the request URL would be |
| 529 | * malformed. The live provider's `delete` short-circuits on the non-live |
| 530 | * id, so destroy succeeds and the state row is removed cleanly. |
| 531 | */ |
| 532 | test.provider( |
| 533 | "suppresses deletion of a dev-only consumer", |
| 534 | (stack) => |
| 535 | Effect.gen(function* () { |
| 536 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 537 | |
| 538 | yield* stack.destroy(); |
| 539 | |
| 540 | const devQueueId = generateLocalId(); |
| 541 | const devConsumerId = generateLocalId(); |
| 542 | |
| 543 | yield* Effect.gen(function* () { |
| 544 | const state = yield* yield* State; |
| 545 | yield* state.set({ |
| 546 | stack: stack.name, |
| 547 | stage: stack.stage, |
| 548 | fqn: "Consumer", |
| 549 | value: { |
| 550 | kind: "resource", |
| 551 | status: "created", |
| 552 | resourceType: "Cloudflare.Queues.Consumer", |
| 553 | namespace: undefined, |
| 554 | fqn: "Consumer", |
| 555 | logicalId: "Consumer", |
| 556 | instanceId: "00000000000000000000000000000002", |
| 557 | providerVersion: 0, |
| 558 | bindings: [], |
| 559 | downstream: [], |
| 560 | props: { queueId: devQueueId, scriptName: "dev-worker" }, |
| 561 | attr: { |
| 562 | consumerId: devConsumerId, |
| 563 | queueId: devQueueId, |
| 564 | scriptName: "dev-worker", |
| 565 | accountId, |
| 566 | }, |
| 567 | } satisfies CreatedResourceState, |
| 568 | }); |
| 569 | }); |
| 570 | |
| 571 | // Destroy must not attempt a (malformed) live delete against the dev ids. |
| 572 | const exit = yield* Effect.exit(stack.destroy()); |
| 573 | expect(Exit.isSuccess(exit)).toBe(true); |
| 574 | |
| 575 | // The dev-only resource is removed from state. |
| 576 | const persisted = yield* Effect.gen(function* () { |
| 577 | const state = yield* yield* State; |
| 578 | return yield* state.get({ stack: stack.name, stage: stack.stage, fqn: "Consumer" }); |
| 579 | }); |
| 580 | expect(persisted).toBeUndefined(); |
| 581 | }).pipe(logLevel), |
| 582 | { tags: ["provider:cloudflare", "provider:cloudflare:queue", "live"] }, |
| 583 | ); |
| 584 | |
| 585 | /** |
| 586 | * Promotion: a consumer whose persisted `consumerId` is a `dev:` mock id |
| 587 | * must be promoted to a real Cloudflare consumer on a live deploy. |
| 588 | * |
| 589 | * We first deploy a real queue + worker + consumer, then rewrite the |
| 590 | * persisted consumer's `consumerId` back to a `dev:` id (leaving the |
| 591 | * `queueId` live — in a real dev→live transition the upstream Queue is |
| 592 | * promoted first, so the consumer's queue ref is already a live id while |
| 593 | * its own id is still the dev one it was minted with). On redeploy the |
| 594 | * live provider's `diff` sees the `dev:` consumerId and returns `update` |
| 595 | * (not `noop`), so `reconcile` runs, re-observes the live consumer, and |
| 596 | * the persisted id is healed back to the real one. |
| 597 | */ |
| 598 | test.provider( |
| 599 | "promotes a dev consumer to a live consumer on deploy", |
| 600 | (stack) => |
| 601 | Effect.gen(function* () { |
| 602 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 603 | |
| 604 | yield* stack.destroy(); |
| 605 | |
| 606 | const buildStack = Effect.gen(function* () { |
| 607 | const queue = yield* Cloudflare.Queues.Queue("Q"); |
| 608 | const worker = yield* Cloudflare.Worker("Worker", { |
| 609 | main, |
| 610 | compatibility: { date: "2024-01-01" }, |
| 611 | }); |
| 612 | const consumer = yield* Cloudflare.Queues.Consumer("Consumer", { |
| 613 | queueId: queue.queueId, |
| 614 | scriptName: worker.workerName, |
| 615 | settings: { batchSize: 5 }, |
| 616 | }); |
| 617 | return { queue, worker, consumer }; |
| 618 | }); |
| 619 | |
| 620 | const initial = yield* stack.deploy(buildStack); |
| 621 | expect(isLiveId(initial.consumer.consumerId)).toBe(true); |
| 622 | |
| 623 | // Rewrite the persisted consumerId back to a dev id, simulating a |
| 624 | // consumer that was minted in `alchemy dev`. |
| 625 | const devQueueId = generateLocalId(); |
| 626 | const devConsumerId = generateLocalId(); |
| 627 | yield* Effect.gen(function* () { |
| 628 | const state = yield* yield* State; |
| 629 | const currentConsumer = (yield* state.get({ |
| 630 | stack: stack.name, |
| 631 | stage: stack.stage, |
| 632 | fqn: "Consumer", |
| 633 | })) as CreatedResourceState; |
| 634 | yield* state.set({ |
| 635 | stack: stack.name, |
| 636 | stage: stack.stage, |
| 637 | fqn: "Consumer", |
| 638 | value: { |
| 639 | ...currentConsumer, |
| 640 | attr: { ...currentConsumer.attr, queueId: devQueueId, consumerId: devConsumerId }, |
| 641 | }, |
| 642 | }); |
| 643 | }); |
| 644 | |
| 645 | const promoted = yield* stack.deploy(buildStack); |
| 646 | |
| 647 | // The dev id was promoted back to the real, live consumer id. |
| 648 | expect(isLiveId(promoted.consumer.consumerId)).toBe(true); |
| 649 | expect(isLiveId(promoted.queue.queueId)).toBe(true); |
| 650 | expect(promoted.consumer.consumerId).not.toEqual(devConsumerId); |
| 651 | expect(promoted.consumer.consumerId).toEqual(initial.consumer.consumerId); |
| 652 | expect(promoted.queue.queueId).toEqual(initial.queue.queueId); |
| 653 | |
| 654 | const live = yield* queues.getConsumer({ |
| 655 | accountId, |
| 656 | queueId: promoted.queue.queueId, |
| 657 | consumerId: promoted.consumer.consumerId, |
| 658 | }); |
| 659 | expect("scriptName" in live ? live.scriptName : undefined).toEqual( |
| 660 | promoted.worker.workerName, |
| 661 | ); |
| 662 | |
| 663 | yield* stack.destroy(); |
| 664 | }).pipe(logLevel), |
| 665 | { |
| 666 | tags: [ |
| 667 | "provider:cloudflare", |
| 668 | "provider:cloudflare:queue", |
| 669 | "provider:cloudflare:worker", |
| 670 | "live", |
| 671 | ], |
| 672 | }, |
| 673 | ); |
| 674 | |
| 675 | /** |
| 676 | * Canonical `list()` test (parent fan-out): queue consumers have no |
| 677 | * account-wide enumeration API, so `list()` enumerates every queue in the |
| 678 | * account and lists each queue's worker consumer. Deploy a queue + worker + |
| 679 | * consumer, then assert the consumer is present in the exhaustively- |
| 680 | * paginated result, hydrated into the same `Attributes` shape `read` returns. |
| 681 | */ |
| 682 | test.provider( |
| 683 | "list enumerates the deployed consumer", |
| 684 | (stack) => |
| 685 | Effect.gen(function* () { |
| 686 | yield* stack.destroy(); |
| 687 | |
| 688 | const deployed = yield* stack.deploy( |
| 689 | Effect.gen(function* () { |
| 690 | const queue = yield* Cloudflare.Queues.Queue("Q"); |
| 691 | const worker = yield* Cloudflare.Worker("Worker", { |
| 692 | main, |
| 693 | compatibility: { date: "2024-01-01" }, |
| 694 | }); |
| 695 | const consumer = yield* Cloudflare.Queues.Consumer("Consumer", { |
| 696 | queueId: queue.queueId, |
| 697 | scriptName: worker.workerName, |
| 698 | settings: { batchSize: 7 }, |
| 699 | }); |
| 700 | return { queue, worker, consumer }; |
| 701 | }), |
| 702 | ); |
| 703 | |
| 704 | const provider = yield* Provider.findProvider(Cloudflare.Queues.Consumer); |
| 705 | const all = yield* provider.list(); |
| 706 | |
| 707 | const found = all.find((c) => c.consumerId === deployed.consumer.consumerId); |
| 708 | expect(found).toBeDefined(); |
| 709 | expect(found?.queueId).toEqual(deployed.queue.queueId); |
| 710 | expect(found?.scriptName).toEqual(deployed.worker.workerName); |
| 711 | expect(found?.accountId).toBeTypeOf("string"); |
| 712 | |
| 713 | yield* stack.destroy(); |
| 714 | }).pipe(logLevel), |
| 715 | { |
| 716 | tags: [ |
| 717 | "provider:cloudflare", |
| 718 | "provider:cloudflare:queue", |
| 719 | "provider:cloudflare:worker", |
| 720 | "live", |
| 721 | ], |
| 722 | }, |
| 723 | ); |