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 Data from "effect/Data"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { requestWorker } from "../Utils/WorkerRequest.ts"; |
| 9 | import Stack from "./fixtures/stack.ts"; |
| 10 | |
| 11 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 12 | providers: Cloudflare.providers(), |
| 13 | }); |
| 14 | |
| 15 | const stack = beforeAll(deploy(Stack)); |
| 16 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack)); |
| 17 | |
| 18 | type Status = "none" | "held" | "released" | "expired"; |
| 19 | |
| 20 | /** A hold route answered with something other than 200. */ |
| 21 | class HoldRequestFailed extends Data.TaggedError("HoldRequestFailed")<{ |
| 22 | readonly status: number; |
| 23 | }> {} |
| 24 | |
| 25 | /** Call a hold route; right after a deploy, retry until the Worker serves it. */ |
| 26 | const call = ( |
| 27 | url: string, |
| 28 | action: "place" | "release" | "state" | "schedule" | "cancel", |
| 29 | id: string, |
| 30 | ) => |
| 31 | requestWorker( |
| 32 | (action === "state" ? HttpClientRequest.get : HttpClientRequest.post)( |
| 33 | `${url}/${action}/${id}`, |
| 34 | ).pipe(HttpClientRequest.setHeader("connection", "close")), |
| 35 | ).pipe( |
| 36 | Effect.filterOrFail( |
| 37 | (res) => res.status === 200, |
| 38 | (res) => new HoldRequestFailed({ status: res.status }), |
| 39 | ), |
| 40 | Effect.flatMap((res) => res.json), |
| 41 | Effect.retry({ schedule: Schedule.spaced("1 second"), times: 10 }), |
| 42 | ); |
| 43 | |
| 44 | /** Send a hold command, or read it; answers the hold's status. */ |
| 45 | const hold = (url: string, action: "place" | "release" | "state", id: string) => |
| 46 | call(url, action, id).pipe(Effect.map((body) => (body as { readonly status: Status }).status)); |
| 47 | |
| 48 | /** Cancel a hold's expiry directly, past the policy. */ |
| 49 | const cancel = (url: string, id: string) => |
| 50 | call(url, "cancel", id).pipe(Effect.map((body) => (body as { readonly cancel: string }).cancel)); |
| 51 | |
| 52 | test( |
| 53 | "an unscheduled command never fires on Durable Objects", |
| 54 | Effect.gen(function* () { |
| 55 | const { url } = yield* stack; |
| 56 | const base = url.replace(/\/+$/, ""); |
| 57 | const run = yield* Effect.sync(() => crypto.randomUUID()); |
| 58 | const released = `released-${run}`; |
| 59 | const kept = `kept-${run}`; |
| 60 | |
| 61 | // both holds schedule their expiry (HoldDeadline, on HoldPlaced); one is |
| 62 | // released, which unschedules its expiry (HoldDeadline, on HoldReleased) |
| 63 | expect(yield* hold(base, "place", released)).toBe("held"); |
| 64 | expect(yield* hold(base, "place", kept)).toBe("held"); |
| 65 | expect(yield* hold(base, "release", released)).toBe("released"); |
| 66 | |
| 67 | // the kept hold expires once its window has passed |
| 68 | yield* hold(base, "state", kept).pipe( |
| 69 | Effect.repeat({ |
| 70 | schedule: Schedule.spaced("2 seconds"), |
| 71 | until: (status) => status === "expired", |
| 72 | times: 30, |
| 73 | }), |
| 74 | ); |
| 75 | // the released hold was placed first, so its expiry would have fired by now |
| 76 | yield* Effect.sleep("3 seconds"); |
| 77 | expect(yield* hold(base, "state", kept)).toBe("expired"); |
| 78 | expect(yield* hold(base, "state", released)).toBe("released"); |
| 79 | |
| 80 | // cancelling again says why nothing was cancelled |
| 81 | expect(yield* cancel(base, kept)).toBe("AlreadyExecuted"); |
| 82 | expect(yield* cancel(base, released)).toBe("AlreadyCancelled"); |
| 83 | expect(yield* cancel(base, `never-${run}`)).toBe("NotScheduled"); |
| 84 | |
| 85 | // schedule answers with the command id to cancel by, and when it is due |
| 86 | const before = yield* Effect.sync(() => Date.now()); |
| 87 | const receipt = (yield* call(base, "schedule", `direct-${run}`)) as { |
| 88 | readonly commandId: string; |
| 89 | readonly at: number; |
| 90 | }; |
| 91 | expect(receipt.commandId).toBe("expire"); |
| 92 | // the Worker's clock may drift a little from ours |
| 93 | expect(receipt.at).toBeGreaterThan(before + 15_000 - 5_000); |
| 94 | expect(receipt.at).toBeLessThan(before + 15_000 + 30_000); |
| 95 | expect(yield* cancel(base, `direct-${run}`)).toBe("Cancelled"); |
| 96 | }), |
| 97 | { timeout: 180_000, tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"] }, |
| 98 | ); |