The best way to control Claude Code, Codex and more from any computer or phone

main
ts 106 lines 3.52 KB
Raw
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 Data from "effect/Data";
5 import * as Effect from "effect/Effect";
6 import * as HttpClient from "effect/http/HttpClient";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import PauseWorkflowWorker from "./fixtures/workflow-pause/worker.ts";
10
11 const { test } = Test.make({
12 providers: Cloudflare.providers(),
13 dev: true,
14 });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 interface WorkflowStatus {
19 status: string;
20 error?: { message?: string } | null;
21 }
22
23 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
24 status: number;
25 }> {}
26
27 const isSettledAfterPause = (status: WorkflowStatus) =>
28 status.status === "paused" ||
29 status.status === "errored" ||
30 status.status === "complete" ||
31 status.status === "terminated";
32
33 /**
34 * End-to-end regression for #930: under local `alchemy dev`, pausing during an
35 * active `Cloudflare.Workflows.task` must leave the instance `paused` once that
36 * task finishes — not `errored` with `UnknownError` from `Effect.tryPromise`.
37 */
38 test.provider(
39 "pausing during an active task settles as paused under local workerd",
40 (stack) =>
41 Effect.gen(function* () {
42 yield* stack.destroy();
43
44 const { worker } = yield* stack.deploy(
45 Effect.gen(function* () {
46 const w = yield* PauseWorkflowWorker;
47 return { worker: w };
48 }),
49 );
50
51 const client = yield* HttpClient.HttpClient;
52 const url = worker.url!;
53 expect(url).toBeTypeOf("string");
54
55 const { instanceId } = yield* client.post(`${url}/workflow/start/world`).pipe(
56 Effect.flatMap((res) =>
57 res.status === 200
58 ? res.json.pipe(
59 Effect.flatMap((body) => {
60 const instanceId = (body as { instanceId?: unknown }).instanceId;
61 return typeof instanceId === "string"
62 ? Effect.succeed({ instanceId })
63 : Effect.fail(new Error("Worker returned no workflow id"));
64 }),
65 )
66 : Effect.fail(new WorkerNotReady({ status: res.status })),
67 ),
68 Effect.retry({
69 while: (e): e is WorkerNotReady => e instanceof WorkerNotReady,
70 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(15)]),
71 }),
72 );
73
74 const pauseRes = yield* client.post(`${url}/workflow/pause/${instanceId}`);
75 expect(pauseRes.status).toBe(200);
76
77 // The active task sleeps 8s; give the engine time to finish the step and
78 // apply the pending pause (or incorrectly mark the run errored).
79 const lastStatus = yield* client.get(`${url}/workflow/status/${instanceId}`).pipe(
80 Effect.flatMap((res) =>
81 res.status === 200
82 ? res.json.pipe(Effect.map((json) => json as unknown as WorkflowStatus))
83 : Effect.succeed({ status: "pending" } as WorkflowStatus),
84 ),
85 Effect.repeat({
86 schedule: Schedule.spaced("500 millis"),
87 until: isSettledAfterPause,
88 times: 40,
89 }),
90 );
91
92 expect(lastStatus.status).toBe("paused");
93 expect(lastStatus.error).toBeFalsy();
94
95 yield* stack.destroy();
96 }).pipe(logLevel),
97 {
98 tags: [
99 "provider:cloudflare",
100 "provider:cloudflare:worker",
101 "provider:cloudflare:workflow",
102 "local",
103 ],
104 timeout: 180_000,
105 },
106 );