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

main
ts 108 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 type { HttpClientResponse } from "effect/http/HttpClientResponse";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10 import { SECRET_VALUE } from "./fixtures/secret.ts";
11 import Stack from "./fixtures/stack.ts";
12
13 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
14 providers: Cloudflare.providers(),
15 state: Cloudflare.state(),
16 });
17
18 const HOOK_TIMEOUT = 300_000;
19 const TEST_TIMEOUT = 120_000;
20
21 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
22
23 const stack = beforeAll(deploy(Stack), { timeout: HOOK_TIMEOUT });
24 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
25 timeout: HOOK_TIMEOUT,
26 });
27
28 // Fresh `workers.dev` URLs take a few seconds to start serving 200s, so the
29 // first request rides this bounded schedule. Capping at 30 recurs surfaces a
30 // genuine failure fast instead of an uncapped exponential blowing past the
31 // test timeout.
32 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
33 status: number;
34 }> {}
35
36 const ready = Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(30)]);
37
38 const fetchWhenReady = (url: string) =>
39 Effect.gen(function* () {
40 const client = yield* HttpClient.HttpClient;
41 return yield* client.get(url).pipe(
42 Effect.flatMap((res: HttpClientResponse) =>
43 res.status === 200
44 ? Effect.succeed(res)
45 : Effect.fail(new WorkerNotReady({ status: res.status })),
46 ),
47 Effect.retry({
48 while: (e): e is WorkerNotReady => e instanceof WorkerNotReady,
49 schedule: ready,
50 }),
51 );
52 });
53
54 const assertSecret = (url: string) =>
55 Effect.gen(function* () {
56 const res = yield* fetchWhenReady(`${url}/secret`);
57 expect(res.status).toBe(200);
58 const body = (yield* res.json) as {
59 value: string;
60 viaGet: string;
61 viaRaw: string;
62 };
63 expect(body.value).toBe(SECRET_VALUE);
64 expect(body.viaGet).toBe(SECRET_VALUE);
65 expect(body.viaRaw).toBe(SECRET_VALUE);
66 });
67
68 // Style (1): `Cloudflare.SecretsStore.ReadSecret(secret)` inside the Worker
69 // init, binding provided via `ReadSecretBinding`. Drives the full
70 // `ReadSecretClient` surface (direct Effect, `.get()`, `.raw`).
71 test(
72 "effect-worker: ReadSecret client round-trips the secret value at runtime",
73 Effect.gen(function* () {
74 const { effect } = yield* stack;
75 expect(effect).toBeTypeOf("string");
76 yield* assertSecret(effect);
77 }).pipe(logLevel),
78 {
79 tags: [
80 "provider:cloudflare",
81 "provider:cloudflare:secretsstore",
82 "provider:cloudflare:worker",
83 "live",
84 ],
85 timeout: TEST_TIMEOUT,
86 },
87 );
88
89 // Style (2): the same Secret declared on the Worker `env` async binding. The
90 // Worker provider must map it to a real `secrets_store_secret` binding so the
91 // runtime sees a `SecretsStoreSecret` with `.get()`, not a JSON blob.
92 test(
93 "async-worker: Secret on the Worker env round-trips as a SecretsStoreSecret",
94 Effect.gen(function* () {
95 const { async } = yield* stack;
96 expect(async).toBeTypeOf("string");
97 yield* assertSecret(async);
98 }).pipe(logLevel),
99 {
100 tags: [
101 "provider:cloudflare",
102 "provider:cloudflare:secretsstore",
103 "provider:cloudflare:worker",
104 "live",
105 ],
106 timeout: TEST_TIMEOUT,
107 },
108 );