@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 106 lines 3.99 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 CronTestWorker from "./fixtures/cron/cron-worker.ts";
10
11 // `dev: true` runs local providers behind the RPC sidecar proxy by default,
12 // matching the process topology of the real `alchemy dev` command.
13 const { test } = Test.make({
14 providers: Cloudflare.providers(),
15 dev: true,
16 });
17
18 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
19
20 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
21 status: number;
22 }> {}
23
24 /**
25 * Under `alchemy dev` the Worker's cron triggers are wired into the local
26 * runtime: cloudflare-runtime starts a Node-side timer per expression AND
27 * exposes Miniflare's manual trigger route
28 * (`/cdn-cgi/handler/scheduled?cron=...&time=...`) on the local worker URL.
29 *
30 * The local timer for `* * * * *` only fires on minute boundaries, so this
31 * test drives the manual route instead of waiting — the route exercises the
32 * exact same entry-worker -> user-worker `scheduled()` dispatch the timer
33 * uses (the timer itself is covered in cloudflare-runtime's own suite,
34 * `test/globals/Scheduled.test.ts`).
35 */
36 test.provider(
37 "local cron worker fires scheduled() via the manual trigger route",
38 (stack) =>
39 Effect.gen(function* () {
40 yield* stack.destroy();
41
42 const deployed = yield* stack.deploy(
43 Effect.gen(function* () {
44 const worker = yield* CronTestWorker;
45 return { worker };
46 }),
47 );
48
49 // The dev URL is the local proxy — proof no cloud deploy ran.
50 expect(deployed.worker.url).toMatch(/^http:\/\/localhost:\d+$/);
51 // The cron registered by the `cron()` event source is recorded on the
52 // local attributes, exactly like the live provider records it.
53 expect(deployed.worker.crons).toContain("* * * * *");
54
55 const url = deployed.worker.url;
56 const client = yield* HttpClient.HttpClient;
57
58 // Reset the counter DO. Doubles as a readiness probe for the first
59 // request against a freshly started workerd.
60 yield* Effect.gen(function* () {
61 const res = yield* client.post(`${url}/reset`);
62 if (res.status !== 200) {
63 return yield* Effect.fail(new WorkerNotReady({ status: res.status }));
64 }
65 }).pipe(
66 Effect.retry({
67 schedule: Schedule.exponential("500 millis"),
68 times: 10,
69 }),
70 );
71
72 // Trigger the fire manually. `cron` must match the registered
73 // expression exactly — the runtime bridge dispatches scheduled events
74 // to listeners by expression.
75 const scheduledTime = Date.now();
76 const trigger = yield* client.post(
77 `${url}/cdn-cgi/handler/scheduled?cron=${encodeURIComponent("* * * * *")}&time=${scheduledTime}`,
78 );
79 expect(trigger.status).toBe(200);
80 expect(yield* trigger.text).toBe("ok");
81
82 // The handler records `controller.scheduledTime` on the DO; poll the
83 // worker's /times route until the fire shows up (bounded).
84 const times = yield* Effect.gen(function* () {
85 const res = yield* client.get(`${url}/times`);
86 if (res.status !== 200) return [];
87 const body = (yield* res.json) as { times?: unknown };
88 return Array.isArray(body.times) ? body.times : [];
89 }).pipe(
90 Effect.catch(() => Effect.succeed([] as unknown[])),
91 Effect.repeat({
92 schedule: Schedule.spaced("500 millis"),
93 until: (times): boolean => times.length > 0,
94 times: 10,
95 }),
96 );
97
98 expect(times).toContain(scheduledTime);
99
100 yield* stack.destroy();
101 }).pipe(logLevel),
102 {
103 tags: ["provider:cloudflare", "provider:cloudflare:worker", "local"],
104 timeout: 180_000,
105 },
106 );