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

main
ts 80 lines 2.73 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 RandomEnvWorker from "./fixtures/random-env/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 RandomEnvBody {
19 resolvedType: string;
20 isHexSecret: boolean;
21 }
22
23 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
24 status: number;
25 }> {}
26
27 /**
28 * Pins that an `Alchemy.Random` output bound during init resolves to its
29 * minted value under the LOCAL worker provider, the same as in a real
30 * deploy.
31 *
32 * Regression shape (beta.62, fixed by #829's request-time env reification):
33 * the runtime accessor read of an output-bound env entry came back
34 * `undefined` under `alchemy dev` while the same code worked deployed —
35 * the first consumer crashed far from the cause (e.g.
36 * `Redacted.value(undefined)` dying inside effect internals). Nothing else
37 * in the suite exercises a generated-secret Output accessor under the
38 * local provider, so this pins the deploy/dev parity directly.
39 */
40 test.provider(
41 "a Random output bound at init resolves under the local provider",
42 (stack) =>
43 Effect.gen(function* () {
44 yield* stack.destroy();
45
46 const { worker } = yield* stack.deploy(
47 Effect.gen(function* () {
48 const w = yield* RandomEnvWorker;
49 return { worker: w };
50 }),
51 );
52
53 const client = yield* HttpClient.HttpClient;
54 const body = yield* Effect.gen(function* () {
55 const res = yield* client.get(worker.url!).pipe(
56 Effect.flatMap((res) =>
57 res.status === 200
58 ? Effect.succeed(res)
59 : Effect.fail(new WorkerNotReady({ status: res.status })),
60 ),
61 Effect.retry({
62 while: (e): e is WorkerNotReady => e instanceof WorkerNotReady,
63 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
64 }),
65 );
66 return (yield* res.json) as unknown as RandomEnvBody;
67 }).pipe(Effect.orDie);
68
69 // The runtime read must observe the minted 32-byte hex secret —
70 // never `undefined`.
71 expect(body.resolvedType).toBe("string");
72 expect(body.isHexSecret).toBe(true);
73
74 yield* stack.destroy();
75 }).pipe(logLevel),
76 {
77 tags: ["provider:cloudflare", "provider:cloudflare:worker", "local"],
78 timeout: 240_000,
79 },
80 );