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 { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as workflows from "@distilled.cloud/cloudflare/workflows"; |
| 5 | import * as Alchemy from "alchemy"; |
| 6 | import * as State from "alchemy/State/State"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 11 | import * as Path from "effect/Path"; |
| 12 | import { MinimumLogLevel } from "effect/References"; |
| 13 | import * as Schedule from "effect/Schedule"; |
| 14 | import { requestWorker } from "../Utils/WorkerRequest.ts"; |
| 15 | import ExplicitNameWorkflowWorker, { |
| 16 | EXPLICIT_WORKFLOW_NAME, |
| 17 | } from "./fixtures/explicit-name-worker.ts"; |
| 18 | import WorkflowLocalWorker from "./fixtures/workflow-worker.ts"; |
| 19 | |
| 20 | // `dev: true` runs local providers behind the RPC sidecar proxy by default, |
| 21 | // matching the process topology of the real `alchemy dev` command (see |
| 22 | // MakeOptions.sidecar in Test/Core.ts). |
| 23 | const { test } = Test.make({ |
| 24 | providers: Cloudflare.providers(), |
| 25 | dev: true, |
| 26 | }); |
| 27 | |
| 28 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 29 | |
| 30 | class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{ |
| 31 | status: number; |
| 32 | }> {} |
| 33 | |
| 34 | class WorkflowInstanceNotVisible extends Data.TaggedError("WorkflowInstanceNotVisible")<{ |
| 35 | instanceId: string; |
| 36 | }> {} |
| 37 | |
| 38 | class WorkflowStatusFailed extends Data.TaggedError("WorkflowStatusFailed")<{ |
| 39 | status: number; |
| 40 | body: string; |
| 41 | }> { |
| 42 | override get message() { |
| 43 | return `Workflow status ${this.status}: ${this.body}`; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | class WorkflowLinkNotReady extends Data.TaggedError("WorkflowLinkNotReady")<{ |
| 48 | instanceId: string; |
| 49 | }> {} |
| 50 | |
| 51 | interface WorkflowStatus { |
| 52 | status: string; |
| 53 | output?: { |
| 54 | greeting: string; |
| 55 | retryAttempt: number; |
| 56 | retryConfig: Cloudflare.Workflows.WorkflowStepConfig; |
| 57 | timeoutConfig: Cloudflare.Workflows.WorkflowStepConfig; |
| 58 | defaultsOk: boolean; |
| 59 | workflowName: string; |
| 60 | instanceId: string; |
| 61 | }; |
| 62 | error?: { name?: string; message?: string } | null; |
| 63 | rollback?: { |
| 64 | outcome: "complete" | "failed"; |
| 65 | error: { message?: string } | null; |
| 66 | } | null; |
| 67 | } |
| 68 | |
| 69 | const isTerminal = (status: WorkflowStatus): boolean => |
| 70 | status.status === "complete" || status.status === "errored" || status.status === "terminated"; |
| 71 | |
| 72 | /** |
| 73 | * Start a workflow instance over HTTP, retrying while the freshly-served |
| 74 | * worker is still coming up (local workerd boots fast; a fresh workers.dev |
| 75 | * URL takes a few seconds to start serving 200s). |
| 76 | */ |
| 77 | const startInstance = (url: string, path = "/workflow/start/world") => |
| 78 | Effect.gen(function* () { |
| 79 | const client = yield* HttpClient.HttpClient; |
| 80 | const res = yield* client.post(`${url}${path}`).pipe( |
| 81 | Effect.flatMap((res) => |
| 82 | res.status === 200 |
| 83 | ? Effect.succeed(res) |
| 84 | : Effect.fail(new WorkerNotReady({ status: res.status })), |
| 85 | ), |
| 86 | Effect.retry({ |
| 87 | while: (e) => e._tag === "WorkerNotReady" && e.status === 404, |
| 88 | schedule: Schedule.spaced("2 seconds"), |
| 89 | times: 10, |
| 90 | }), |
| 91 | ); |
| 92 | const { instanceId } = (yield* res.json) as { instanceId: string }; |
| 93 | expect(instanceId).toBeTypeOf("string"); |
| 94 | return instanceId; |
| 95 | }); |
| 96 | |
| 97 | /** |
| 98 | * Poll one instance without retrying failed workflow executions. |
| 99 | * |
| 100 | * Cloudflare's `get(id).status()` can answer `(instance.not_found)` for an |
| 101 | * instance `create()` just returned: instance reads are eventually consistent, |
| 102 | * and on a freshly created live Workflow an early instance can stay invisible. |
| 103 | * That state is retried briefly and then surfaced as |
| 104 | * `WorkflowInstanceNotVisible`, which `runInstance` answers with a fresh |
| 105 | * instance. Every other status error fails. |
| 106 | */ |
| 107 | const waitForTerminal = (url: string, instanceId: string) => |
| 108 | requestWorker(HttpClientRequest.get(`${url}/workflow/status/${instanceId}`)).pipe( |
| 109 | Effect.flatMap((res) => |
| 110 | res.status === 200 |
| 111 | ? res.json.pipe(Effect.map((json) => json as unknown as WorkflowStatus)) |
| 112 | : res.text.pipe( |
| 113 | Effect.flatMap((body) => |
| 114 | Effect.fail( |
| 115 | res.status === 500 && body.includes("(instance.not_found)") |
| 116 | ? new WorkflowInstanceNotVisible({ instanceId }) |
| 117 | : new WorkflowStatusFailed({ status: res.status, body }), |
| 118 | ), |
| 119 | ), |
| 120 | ), |
| 121 | ), |
| 122 | Effect.retry({ |
| 123 | while: (error) => error instanceof WorkflowInstanceNotVisible, |
| 124 | schedule: Schedule.spaced("2 seconds"), |
| 125 | times: 5, |
| 126 | }), |
| 127 | Effect.repeat({ |
| 128 | schedule: Schedule.spaced("3 seconds"), |
| 129 | until: isTerminal, |
| 130 | times: 10, |
| 131 | }), |
| 132 | ); |
| 133 | |
| 134 | const runInstance = (url: string, path: string, live = false) => |
| 135 | Effect.gen(function* () { |
| 136 | const instanceId = yield* startInstance(url, path); |
| 137 | const status = yield* waitForTerminal(url, instanceId); |
| 138 | // A newly deployed live workflow can precede its worker/engine link. |
| 139 | if (live && status.status === "errored" && status.error?.message === "Worker not found.") { |
| 140 | return yield* Effect.fail(new WorkflowLinkNotReady({ instanceId })); |
| 141 | } |
| 142 | return { instanceId, status }; |
| 143 | }).pipe( |
| 144 | Effect.retry({ |
| 145 | while: (error) => |
| 146 | error instanceof WorkflowLinkNotReady || |
| 147 | (live && error instanceof WorkflowInstanceNotVisible), |
| 148 | schedule: Schedule.spaced("3 seconds"), |
| 149 | times: 3, |
| 150 | }), |
| 151 | ); |
| 152 | |
| 153 | const probeWorkflow = Effect.fn(function* (url: string) { |
| 154 | const ready = yield* Effect.gen(function* () { |
| 155 | const id = yield* startInstance(url, "/workflow/probe"); |
| 156 | const status = yield* waitForTerminal(url, id).pipe( |
| 157 | Effect.catchTag("WorkflowInstanceNotVisible", () => Effect.succeed(undefined)), |
| 158 | ); |
| 159 | if (status === undefined) return false; |
| 160 | yield* Effect.logInfo(`Workflow readiness probe ${id}: ${JSON.stringify(status)}`); |
| 161 | if ( |
| 162 | status.status === "errored" && |
| 163 | (status.error?.message === "Worker not found." || |
| 164 | (status.error?.name === "TypeError" && |
| 165 | (status.error.message === 'The RPC receiver does not implement the method "run".' || |
| 166 | status.error.message === |
| 167 | "The entrypoint name LocalTestWorkflow was not found in this worker. Ensure the worker exports an entrypoint with that name."))) |
| 168 | ) |
| 169 | return false; |
| 170 | expect(status).toMatchObject({ |
| 171 | status: "complete", |
| 172 | output: { ready: true }, |
| 173 | }); |
| 174 | return true; |
| 175 | }).pipe( |
| 176 | Effect.repeat({ |
| 177 | schedule: Schedule.spaced("1 second"), |
| 178 | times: 8, |
| 179 | until: (ready) => ready, |
| 180 | }), |
| 181 | Effect.timeout("45 seconds"), |
| 182 | ); |
| 183 | expect(ready, "Workflow entrypoint did not propagate").toBe(true); |
| 184 | }); |
| 185 | |
| 186 | const assertRollback = (url: string, live = false) => |
| 187 | Effect.gen(function* () { |
| 188 | const client = yield* HttpClient.HttpClient; |
| 189 | const { instanceId, status } = yield* runInstance(url, "/workflow/rollback", live); |
| 190 | expect(status).toMatchObject({ status: "errored" }); |
| 191 | expect(status.error?.message).toContain("rollback requested"); |
| 192 | // Local bindings omit rollback metadata; the persisted records verify execution. |
| 193 | if (live) { |
| 194 | expect(status.rollback).toEqual({ outcome: "complete", error: null }); |
| 195 | } |
| 196 | |
| 197 | const response = yield* client.get(`${url}/workflow/rollback-result/${instanceId}`); |
| 198 | expect(response.status).toBe(200); |
| 199 | expect(yield* response.json).toEqual( |
| 200 | ["undefined", "both", "timeout-only", "retries-only"].map((step) => ({ |
| 201 | output: { value: "reserved", step }, |
| 202 | error: "rollback requested", |
| 203 | })), |
| 204 | ); |
| 205 | }); |
| 206 | |
| 207 | const assertFailureScenarios = (url: string, live = false) => |
| 208 | Effect.gen(function* () { |
| 209 | const client = yield* HttpClient.HttpClient; |
| 210 | for (const scenario of [ |
| 211 | "timeout-zero", |
| 212 | "rollback-timeout-zero", |
| 213 | "retry-exhaustion", |
| 214 | "rollback-retry-exhaustion", |
| 215 | ]) { |
| 216 | const { instanceId, status } = yield* runInstance( |
| 217 | url, |
| 218 | `/workflow/scenario/${scenario}`, |
| 219 | live, |
| 220 | ); |
| 221 | const zeroTimeout = scenario.endsWith("timeout-zero"); |
| 222 | const response = yield* client.get( |
| 223 | `${url}/workflow/record/${instanceId}/${zeroTimeout ? "protected" : "attempts"}`, |
| 224 | ); |
| 225 | expect(response.status).toBe(200); |
| 226 | const record = yield* response.json; |
| 227 | yield* Effect.log("Workflow failure scenario", { |
| 228 | scenario, |
| 229 | status, |
| 230 | record, |
| 231 | }); |
| 232 | expect(status).toMatchObject({ status: "errored" }); |
| 233 | if (zeroTimeout) { |
| 234 | if (live && scenario === "rollback-timeout-zero") { |
| 235 | // Cloudflare validates rollback config on execution, without exposing its error. |
| 236 | expect(status.error?.message).toBe("rollback requested"); |
| 237 | expect(status.rollback).toEqual({ outcome: "failed", error: null }); |
| 238 | } else { |
| 239 | expect(status.error?.message).toContain("invalid format"); |
| 240 | } |
| 241 | expect(record).toBeNull(); |
| 242 | } else { |
| 243 | expect(status.error?.message).toContain( |
| 244 | scenario === "retry-exhaustion" ? "retry budget exhausted" : "rollback requested", |
| 245 | ); |
| 246 | expect(record).toEqual({ attempt: 2 }); |
| 247 | if (live && scenario === "rollback-retry-exhaustion") { |
| 248 | expect(status.rollback?.outcome).toBe("failed"); |
| 249 | expect(status.rollback?.error?.message).toContain("rollback budget exhausted"); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | }); |
| 254 | |
| 255 | /** |
| 256 | * Read the persisted state row of the nested `Cloudflare.Workflow` resource |
| 257 | * (the WorkflowResource the `Workflow` effect-class registers under the host |
| 258 | * worker) from the scratch stack's private state store. |
| 259 | */ |
| 260 | const readWorkflowRow = (stack: Test.ScratchStack) => |
| 261 | Effect.gen(function* () { |
| 262 | const state = yield* yield* State.State; |
| 263 | const fqns = yield* state.list({ stack: stack.name, stage: stack.stage }); |
| 264 | for (const fqn of fqns) { |
| 265 | const row = yield* state.get({ |
| 266 | stack: stack.name, |
| 267 | stage: stack.stage, |
| 268 | fqn, |
| 269 | }); |
| 270 | if (row && !State.isActionState(row) && row.resourceType === "Cloudflare.Workflow") { |
| 271 | return row; |
| 272 | } |
| 273 | } |
| 274 | return undefined; |
| 275 | }).pipe(Effect.provide(stack.state)); |
| 276 | |
| 277 | const asyncWorkflowMain = Effect.gen(function* () { |
| 278 | const path = yield* Path.Path; |
| 279 | return path.resolve(import.meta.dirname, "fixtures/async-workflow-worker.ts"); |
| 280 | }); |
| 281 | |
| 282 | /** |
| 283 | * Under `alchemy dev` the Workflow resource is emulated by the local provider |
| 284 | * (a `dev:` id, no cloud API calls) and the host worker's `workflow` binding |
| 285 | * is lowered onto the local workerd workflow engine. This exercises the full |
| 286 | * local roundtrip: create an instance through the binding and poll it to |
| 287 | * completion against the local simulator. |
| 288 | */ |
| 289 | test.provider( |
| 290 | "workflow runs to completion against the local simulator", |
| 291 | (stack) => |
| 292 | Effect.gen(function* () { |
| 293 | yield* stack.destroy(); |
| 294 | |
| 295 | const deployed = yield* stack.deploy( |
| 296 | Effect.gen(function* () { |
| 297 | const worker = yield* WorkflowLocalWorker; |
| 298 | return { worker }; |
| 299 | }), |
| 300 | ); |
| 301 | |
| 302 | // The worker serves from the local dev proxy — proof the stack ran in |
| 303 | // local mode. |
| 304 | expect(deployed.worker.url).toMatch(/^http:\/\/localhost:\d+$/); |
| 305 | |
| 306 | // The local provider fabricated a `dev:` workflow id (proof no cloud |
| 307 | // call ran) and the row is stamped with the local provider mode. |
| 308 | const row = yield* readWorkflowRow(stack); |
| 309 | expect(row).toBeDefined(); |
| 310 | expect(row!.attr?.workflowId).toMatch(/^dev:/); |
| 311 | expect(row!.providerMode).toBe("local"); |
| 312 | |
| 313 | // Drive the workflow through the binding against local workerd. |
| 314 | const url = deployed.worker.url!; |
| 315 | const instanceId = yield* startInstance(url); |
| 316 | const status = yield* waitForTerminal(url, instanceId); |
| 317 | |
| 318 | expect(status).toMatchObject({ status: "complete" }); |
| 319 | expect(status.output?.greeting).toBe("Hello, world!"); |
| 320 | expect(status.output?.instanceId).toBe(instanceId); |
| 321 | |
| 322 | expect(status.error).toBeFalsy(); |
| 323 | expect(status.output?.retryAttempt).toBe(2); |
| 324 | expect(status.output?.retryConfig.retries).toEqual({ |
| 325 | limit: 2, |
| 326 | delay: "1 second", |
| 327 | backoff: "constant", |
| 328 | }); |
| 329 | expect(status.output?.retryConfig.timeout).toBeDefined(); |
| 330 | expect(status.output?.timeoutConfig.timeout).toBe("30 seconds"); |
| 331 | expect(status.output?.timeoutConfig.retries).toBeDefined(); |
| 332 | expect(status.output?.defaultsOk).toBe(true); |
| 333 | yield* Effect.log("Workflow resolved configuration", status.output); |
| 334 | yield* assertRollback(url); |
| 335 | yield* assertFailureScenarios(url); |
| 336 | |
| 337 | const client = yield* HttpClient.HttpClient; |
| 338 | const events = yield* client |
| 339 | .get(`${url}/workflow/events/${instanceId}`) |
| 340 | .pipe(Effect.flatMap((response) => response.json)); |
| 341 | expect(events).toEqual([expect.objectContaining({ type: "workflow_queued" })]); |
| 342 | const deleted = yield* client |
| 343 | .post(`${url}/workflow/delete/${instanceId}`) |
| 344 | .pipe(Effect.flatMap((response) => response.json)); |
| 345 | expect(deleted).toEqual({ |
| 346 | deleted: [], |
| 347 | errors: [expect.objectContaining({ id: instanceId })], |
| 348 | }); |
| 349 | const batchId = yield* startInstance(url); |
| 350 | const batch = yield* client |
| 351 | .post(`${url}/workflow/delete-batch/${batchId}`) |
| 352 | .pipe(Effect.flatMap((response) => response.json)); |
| 353 | expect(batch).toEqual({ |
| 354 | deleted: [{ id: batchId }], |
| 355 | errors: [expect.objectContaining({ id: "missing-instance" })], |
| 356 | }); |
| 357 | |
| 358 | yield* stack.destroy(); |
| 359 | }).pipe(logLevel), |
| 360 | { |
| 361 | tags: [ |
| 362 | "provider:cloudflare", |
| 363 | "provider:cloudflare:r2", |
| 364 | "provider:cloudflare:worker", |
| 365 | "provider:cloudflare:workflow", |
| 366 | "local", |
| 367 | ], |
| 368 | timeout: 120_000, |
| 369 | }, |
| 370 | ); |
| 371 | |
| 372 | // Exercise physical names through real workerd bindings, not just metadata. |
| 373 | test.provider( |
| 374 | "async Worker binding preserves an explicit physical Workflow name", |
| 375 | (stack) => |
| 376 | Effect.gen(function* () { |
| 377 | yield* stack.destroy(); |
| 378 | |
| 379 | const workflowName = "existing-workflow-physical-name"; |
| 380 | const main = yield* asyncWorkflowMain; |
| 381 | const deployWith = (name?: string) => |
| 382 | stack.deploy( |
| 383 | Cloudflare.Worker("ExplicitWorkflowWorker", { |
| 384 | main, |
| 385 | env: { |
| 386 | WORKFLOW_NAME: name ?? workflowName, |
| 387 | EXISTING_WORKFLOW: Cloudflare.Workflow("ExistingWorkflow", { |
| 388 | workflowName: name, |
| 389 | }), |
| 390 | }, |
| 391 | }), |
| 392 | ); |
| 393 | const created = yield* deployWith(workflowName); |
| 394 | const workflowRow = yield* readWorkflowRow(stack); |
| 395 | expect(workflowRow?.providerMode).toBe("local"); |
| 396 | expect(workflowRow?.attr?.workflowId).toMatch(/^dev:/); |
| 397 | expect(workflowRow?.attr?.workflowName).toBe(workflowName); |
| 398 | const instance = yield* startInstance(created.url!); |
| 399 | expect((yield* waitForTerminal(created.url!, instance)).output?.workflowName).toBe( |
| 400 | workflowName, |
| 401 | ); |
| 402 | |
| 403 | yield* deployWith(); |
| 404 | const preserved = yield* readWorkflowRow(stack); |
| 405 | expect(preserved?.attr?.workflowId).toBe(workflowRow?.attr?.workflowId); |
| 406 | expect(preserved?.attr?.workflowName).toBe(workflowName); |
| 407 | |
| 408 | const renamed = yield* deployWith(`${workflowName}-renamed`); |
| 409 | const replaced = yield* readWorkflowRow(stack); |
| 410 | expect(replaced?.attr?.workflowId).not.toBe(workflowRow?.attr?.workflowId); |
| 411 | expect(replaced?.attr?.workflowName).toBe(`${workflowName}-renamed`); |
| 412 | const client = yield* HttpClient.HttpClient; |
| 413 | const ready = yield* client.get(`${renamed.url!}/workflow/name`).pipe( |
| 414 | Effect.flatMap((response) => response.text), |
| 415 | Effect.repeat({ |
| 416 | schedule: Schedule.spaced("1 second"), |
| 417 | until: (name) => name === `${workflowName}-renamed`, |
| 418 | times: 8, |
| 419 | }), |
| 420 | ); |
| 421 | expect(ready).toBe(`${workflowName}-renamed`); |
| 422 | const renamedInstance = yield* startInstance(renamed.url!); |
| 423 | expect((yield* waitForTerminal(renamed.url!, renamedInstance)).output?.workflowName).toBe( |
| 424 | `${workflowName}-renamed`, |
| 425 | ); |
| 426 | |
| 427 | yield* stack.destroy(); |
| 428 | }).pipe(logLevel), |
| 429 | { |
| 430 | tags: [ |
| 431 | "provider:cloudflare", |
| 432 | "provider:cloudflare:worker", |
| 433 | "provider:cloudflare:workflow", |
| 434 | "local", |
| 435 | ], |
| 436 | timeout: 120_000, |
| 437 | }, |
| 438 | ); |
| 439 | |
| 440 | test.provider( |
| 441 | "cross-script binding runs the host's explicit Workflow name", |
| 442 | (stack) => |
| 443 | Effect.gen(function* () { |
| 444 | yield* stack.destroy(); |
| 445 | |
| 446 | const workflowName = "existing-cross-script-workflow"; |
| 447 | const main = yield* asyncWorkflowMain; |
| 448 | const host = Cloudflare.Worker("ExplicitWorkflowHost", { |
| 449 | main, |
| 450 | env: { |
| 451 | EXISTING_WORKFLOW: Cloudflare.Workflow("ExistingWorkflow", { |
| 452 | workflowName, |
| 453 | }), |
| 454 | }, |
| 455 | }); |
| 456 | yield* stack.deploy(host); |
| 457 | const deployed = yield* stack.deploy( |
| 458 | Effect.gen(function* () { |
| 459 | const worker = yield* host; |
| 460 | return yield* Cloudflare.Worker("ExplicitWorkflowConsumer", { |
| 461 | main, |
| 462 | env: { |
| 463 | EXISTING_WORKFLOW: Cloudflare.Workflow("ExistingWorkflow", { |
| 464 | scriptName: worker.workerName, |
| 465 | workflowName, |
| 466 | }), |
| 467 | }, |
| 468 | }); |
| 469 | }), |
| 470 | ); |
| 471 | const instance = yield* startInstance(deployed.url!); |
| 472 | const status = yield* waitForTerminal(deployed.url!, instance); |
| 473 | expect(status.status).toBe("complete"); |
| 474 | expect(status.output?.workflowName).toBe(workflowName); |
| 475 | |
| 476 | yield* stack.destroy(); |
| 477 | }).pipe(logLevel), |
| 478 | { |
| 479 | tags: [ |
| 480 | "provider:cloudflare", |
| 481 | "provider:cloudflare:worker", |
| 482 | "provider:cloudflare:workflow", |
| 483 | "local", |
| 484 | ], |
| 485 | timeout: 120_000, |
| 486 | }, |
| 487 | ); |
| 488 | |
| 489 | test.provider( |
| 490 | "Effect-native Worker binding preserves an explicit physical Workflow name", |
| 491 | (stack) => |
| 492 | Effect.gen(function* () { |
| 493 | yield* stack.destroy(); |
| 494 | const deployed = yield* stack.deploy(ExplicitNameWorkflowWorker); |
| 495 | |
| 496 | const workflowRow = yield* readWorkflowRow(stack); |
| 497 | expect(workflowRow?.providerMode).toBe("local"); |
| 498 | expect(workflowRow?.attr?.workflowName).toBe(EXPLICIT_WORKFLOW_NAME); |
| 499 | const instance = yield* startInstance(deployed.url!); |
| 500 | const status = yield* waitForTerminal(deployed.url!, instance); |
| 501 | expect(status.status).toBe("complete"); |
| 502 | expect(status.output?.workflowName).toBe(EXPLICIT_WORKFLOW_NAME); |
| 503 | |
| 504 | yield* stack.destroy(); |
| 505 | }).pipe(logLevel), |
| 506 | { |
| 507 | tags: [ |
| 508 | "provider:cloudflare", |
| 509 | "provider:cloudflare:worker", |
| 510 | "provider:cloudflare:workflow", |
| 511 | "local", |
| 512 | ], |
| 513 | timeout: 120_000, |
| 514 | }, |
| 515 | ); |
| 516 | |
| 517 | test.provider( |
| 518 | "Alchemy.remote() preserves an explicit Workflow name and schedules", |
| 519 | (stack) => |
| 520 | Effect.gen(function* () { |
| 521 | yield* stack.destroy(); |
| 522 | const deployed = yield* stack.deploy(ExplicitNameWorkflowWorker.pipe(Alchemy.remote())); |
| 523 | expect(deployed.url).not.toMatch(/^http:\/\/localhost/); |
| 524 | |
| 525 | const row = yield* readWorkflowRow(stack); |
| 526 | expect(row?.providerMode).toBe("live"); |
| 527 | expect(row?.attr?.workflowId).not.toMatch(/^dev:/); |
| 528 | const workflowName = row!.attr!.workflowName; |
| 529 | const accountId = row!.attr!.accountId; |
| 530 | const observed = yield* workflows.getWorkflow({ |
| 531 | accountId, |
| 532 | workflowName, |
| 533 | }); |
| 534 | expect(observed.id).toBe(row!.attr!.workflowId); |
| 535 | expect(observed.name).toBe(EXPLICIT_WORKFLOW_NAME); |
| 536 | expect(observed.schedules?.map((schedule) => schedule.cron)).toEqual(["0 0 1 1 *"]); |
| 537 | |
| 538 | const { status } = yield* runInstance(deployed.url!, "/workflow/start/world", true); |
| 539 | expect(status).toMatchObject({ status: "complete" }); |
| 540 | expect(status.error).toBeFalsy(); |
| 541 | expect(status.output?.greeting).toBe("Hello, world!"); |
| 542 | expect(status.output?.workflowName).toBe(EXPLICIT_WORKFLOW_NAME); |
| 543 | |
| 544 | yield* stack.destroy(); |
| 545 | const gone = yield* workflows.getWorkflow({ accountId, workflowName }).pipe( |
| 546 | Effect.as(false), |
| 547 | Effect.catchTag("WorkflowNotFound", () => Effect.succeed(true)), |
| 548 | ); |
| 549 | expect(gone).toBe(true); |
| 550 | }).pipe(logLevel), |
| 551 | { |
| 552 | tags: [ |
| 553 | "provider:cloudflare", |
| 554 | "provider:cloudflare:worker", |
| 555 | "provider:cloudflare:workflow", |
| 556 | "live", |
| 557 | ], |
| 558 | timeout: 120_000, |
| 559 | }, |
| 560 | ); |
| 561 | |
| 562 | /** |
| 563 | * `Alchemy.remote()` opts the whole worker + hosted workflow OUT of local |
| 564 | * emulation: even under `alchemy dev` the worker deploys to real Cloudflare |
| 565 | * and `putWorkflow` registers a real account-level Workflow. The workflow |
| 566 | * class is hosted BY the worker script, so both must run live together — |
| 567 | * a live workflow cannot reference a script that only exists in local |
| 568 | * workerd. After destroy, an out-of-band `getWorkflow` proves the cloud |
| 569 | * workflow is gone (pins the stamped-mode delete path). |
| 570 | */ |
| 571 | test.provider( |
| 572 | "Alchemy.remote() worker + workflow run live in dev and delete on destroy", |
| 573 | (stack) => |
| 574 | Effect.gen(function* () { |
| 575 | yield* stack.destroy(); |
| 576 | |
| 577 | const deployed = yield* stack.deploy( |
| 578 | Effect.gen(function* () { |
| 579 | const worker = yield* WorkflowLocalWorker; |
| 580 | return { worker }; |
| 581 | }).pipe(Alchemy.remote()), |
| 582 | ); |
| 583 | |
| 584 | // Live deploy: a real workers.dev URL, not the local dev proxy. |
| 585 | expect(deployed.worker.url).not.toMatch(/^http:\/\/localhost/); |
| 586 | |
| 587 | // The workflow has a real Cloudflare id and a `live` mode stamp. |
| 588 | const row = yield* readWorkflowRow(stack); |
| 589 | expect(row).toBeDefined(); |
| 590 | expect(row!.attr?.workflowId).not.toMatch(/^dev:/); |
| 591 | expect(row!.providerMode).toBe("live"); |
| 592 | const workflowName = row!.attr!.workflowName; |
| 593 | const workflowAccountId = row!.attr!.accountId; |
| 594 | |
| 595 | // Out-of-band: the workflow exists on real Cloudflare. |
| 596 | const live = yield* workflows.getWorkflow({ |
| 597 | accountId: workflowAccountId, |
| 598 | workflowName, |
| 599 | }); |
| 600 | expect(live.id).toBe(row!.attr!.workflowId); |
| 601 | |
| 602 | const url = deployed.worker.url!; |
| 603 | yield* probeWorkflow(url); |
| 604 | const { status } = yield* runInstance(url, "/workflow/start/world", true); |
| 605 | expect(status).toMatchObject({ status: "complete" }); |
| 606 | expect(status.error).toBeFalsy(); |
| 607 | expect(status.output?.greeting).toBe("Hello, world!"); |
| 608 | expect(status.output?.retryAttempt).toBe(2); |
| 609 | expect(status.output?.retryConfig.retries).toEqual({ |
| 610 | limit: 2, |
| 611 | delay: "1 second", |
| 612 | backoff: "constant", |
| 613 | }); |
| 614 | expect(status.output?.retryConfig.timeout).toBeDefined(); |
| 615 | expect(status.output?.timeoutConfig.timeout).toBe("30 seconds"); |
| 616 | expect(status.output?.timeoutConfig.retries).toBeDefined(); |
| 617 | expect(status.output?.defaultsOk).toBe(true); |
| 618 | yield* Effect.log("Workflow resolved configuration", status.output); |
| 619 | yield* assertRollback(url, true); |
| 620 | yield* assertFailureScenarios(url, true); |
| 621 | |
| 622 | yield* stack.destroy(); |
| 623 | |
| 624 | // The live-stamped row was deleted through the live provider even in a |
| 625 | // dev run — the cloud workflow is gone. |
| 626 | const gone = yield* workflows |
| 627 | .getWorkflow({ accountId: workflowAccountId, workflowName }) |
| 628 | .pipe( |
| 629 | Effect.as(false), |
| 630 | Effect.catchTag("WorkflowNotFound", () => Effect.succeed(true)), |
| 631 | ); |
| 632 | expect(gone).toBe(true); |
| 633 | }).pipe(logLevel), |
| 634 | { |
| 635 | tags: [ |
| 636 | "provider:cloudflare", |
| 637 | "provider:cloudflare:r2", |
| 638 | "provider:cloudflare:worker", |
| 639 | "provider:cloudflare:workflow", |
| 640 | "live", |
| 641 | ], |
| 642 | timeout: 120_000, |
| 643 | }, |
| 644 | ); |