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

main
ts 169 lines 5.87 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Effect from "effect/Effect";
5 import * as HttpClient from "effect/http/HttpClient";
6 import { MinimumLogLevel } from "effect/References";
7 import * as Schedule from "effect/Schedule";
8 import { expectUrlContains } from "../Utils/Http.ts";
9 import PropsStack from "./fixtures/entrypoint-props/stack.ts";
10 import Stack from "./fixtures/worker-entrypoint-binding/stack.ts";
11
12 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
13 providers: Cloudflare.providers(),
14 });
15
16 const stack = beforeAll(deploy(Stack));
17 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
18
19 // ctx.props / ctx.exports: an Effect Worker and an async Worker, each with a Durable Object.
20 const propsStack = beforeAll(deploy(PropsStack));
21 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(PropsStack));
22
23 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
24
25 // Cold-start retry — fresh `workers.dev` URLs take a few seconds to start
26 // answering, capped at 3s so the doubling sleeps can't blow the timeout.
27 const coldStartRetry = Effect.retry({
28 schedule: Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("3 seconds")]),
29 times: 30,
30 });
31
32 test(
33 "target worker's default entrypoint responds",
34 Effect.gen(function* () {
35 const { targetUrl } = yield* stack;
36 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
37
38 const res = yield* client.get(targetUrl).pipe(coldStartRetry);
39 expect(yield* res.text).toBe("hello from EntrypointTargetWorker");
40 }),
41 {
42 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
43 timeout: 180_000,
44 },
45 );
46
47 test(
48 "caller reaches the NAMED entrypoint through the binding",
49 Effect.gen(function* () {
50 const { callerUrl } = yield* stack;
51 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
52
53 // The default entrypoint has no `greet` — a greeting proves the
54 // binding targeted the named `Api` class.
55 const res = yield* client.get(`${callerUrl}/greet?name=alice`).pipe(coldStartRetry);
56 expect(yield* res.text).toBe("hello alice from Api");
57 }),
58 {
59 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
60 timeout: 180_000,
61 },
62 );
63
64 test(
65 "a worker reaches its own NAMED entrypoint through WorkerEntrypoint(name)",
66 Effect.gen(function* () {
67 const { targetUrl } = yield* stack;
68 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
69
70 // A bare `Self` binding would hit the default export, which has no
71 // `greet`: the greeting proves the self binding carried `entrypoint`.
72 const res = yield* client.get(`${targetUrl}/self-greet?name=bob`).pipe(coldStartRetry);
73 expect(yield* res.text).toBe("hello bob from Api");
74 }),
75 {
76 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
77 timeout: 180_000,
78 },
79 );
80
81 test(
82 "a worker reaches its own default export through WorkerEntrypoint()",
83 Effect.gen(function* () {
84 const { targetUrl } = yield* stack;
85 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
86
87 const res = yield* client.get(`${targetUrl}/self-default`).pipe(coldStartRetry);
88 expect(yield* res.text).toBe("via self default: hello from EntrypointTargetWorker");
89 }),
90 {
91 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
92 timeout: 180_000,
93 },
94 );
95
96 // The Cloudflare API's service-binding schema does not carry `props` yet:
97 // the distilled `workers` service drops the field at encode, so deployed
98 // bindings deliver no ctx.props. Local dev already delivers them (see
99 // WorkerEntrypoint.local.test.ts). Flip this on when the distilled patch
100 // lands.
101 test.skip(
102 "props reach the named entrypoint's ctx.props",
103 Effect.gen(function* () {
104 const { callerUrl } = yield* stack;
105 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
106
107 const res = yield* client.get(`${callerUrl}/props`).pipe(coldStartRetry);
108 expect((yield* res.json) as Record<string, unknown>).toEqual({
109 tenant: "acme",
110 });
111 }),
112 {
113 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
114 timeout: 180_000,
115 },
116 );
117
118 const TENANT = `props=${JSON.stringify({ tenant: "acme" })}`;
119
120 const routes = (variant: "effect" | "async") => [
121 { name: "an external request has empty props", path: "/props", expected: "props={}" },
122 {
123 name: "a Worker handler mints a default entrypoint stub with props",
124 path: "/mint",
125 expected: `via-worker:${TENANT}`,
126 },
127 {
128 name: "a Durable Object mints a host entrypoint stub with props",
129 path: "/mint-do",
130 expected: `via-do:${TENANT}`,
131 },
132 // Effect Workers export only `default`; a named entrypoint is a native class.
133 ...(variant === "async"
134 ? [
135 {
136 name: "a Worker handler mints a named entrypoint stub with props",
137 path: "/mint-named",
138 expected: `via-named:${TENANT}`,
139 },
140 ]
141 : []),
142 {
143 name: "an entrypoint the Worker does not export is unavailable",
144 path: "/missing",
145 expected: variant === "effect" ? "EntrypointUnavailableError:Missing" : "undefined:Missing",
146 },
147 ];
148
149 for (const variant of ["effect", "async"] as const) {
150 describe(
151 `ctx.props and self entrypoint stubs (${variant})`,
152 { tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"] },
153 () => {
154 for (const route of routes(variant)) {
155 test(
156 route.name,
157 Effect.gen(function* () {
158 const urls = yield* propsStack;
159 const url = variant === "effect" ? urls.effectUrl : urls.asyncUrl;
160 yield* expectUrlContains(`${url}${route.path}`, route.expected, {
161 label: `${variant} ${route.path}`,
162 });
163 }).pipe(logLevel),
164 { timeout: 180_000 },
165 );
166 }
167 },
168 );
169 }