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

main
ts 203 lines 6.29 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 Namespace from "alchemy/Namespace";
5 import * as Stack from "alchemy/Stack";
6 import { Stage } from "alchemy/Stage";
7 import { inMemoryState, type State } from "alchemy/State";
8 import * as Context from "effect/Context";
9 import * as Effect from "effect/Effect";
10 import * as Layer from "effect/Layer";
11
12 const { test } = Test.make({
13 providers: Layer.empty,
14 state: inMemoryState(),
15 });
16
17 // A resource declared once, at module scope, and referenced from a site's
18 // `env` — the pattern `examples/cloudflare-website-tanstack-start` uses.
19 const Cache = Cloudflare.KV.Namespace("Cache", {});
20
21 /** Compile the stack and return its registered resources. */
22 const compile = <A, Err = never, Req = never>(
23 effect: Effect.Effect<A, Err, Req>,
24 ): Effect.Effect<Stack.CompiledStack["resources"], Err, State> =>
25 effect.pipe(
26 // @ts-expect-error - Stack.make's typing erases R unsoundly here
27 Stack.make({
28 name: "test",
29 providers: Layer.empty,
30 state: inMemoryState(),
31 }),
32 Effect.provideService(Stage, "test"),
33 Effect.map((stack: Stack.CompiledStack) => stack.resources),
34 );
35
36 /** Compile the stack and return the FQN of every registered resource. */
37 const fqns = <A, Err = never, Req = never>(
38 effect: Effect.Effect<A, Err, Req>,
39 ): Effect.Effect<string[], Err, State> =>
40 compile(effect).pipe(Effect.map((resources) => Object.keys(resources).sort()));
41
42 test(
43 "StaticSite declares env resources in the caller's namespace",
44 Effect.gen(function* () {
45 const keys = yield* fqns(
46 Effect.gen(function* () {
47 yield* Cache;
48 yield* Cloudflare.Website.StaticSite("Site", {
49 command: "echo build",
50 outdir: "dist",
51 main: "./worker.ts",
52 env: { CACHE: Cache },
53 });
54 }),
55 );
56 // Only the build sub-resource is namespaced; the Worker is the site
57 // itself and `Cache` stays where the caller declared it.
58 expect(keys).toEqual(["Cache", "Site", "Site/Build"]);
59 }),
60 {
61 tags: [
62 "unit",
63 "provider:cloudflare",
64 "provider:cloudflare:kv",
65 "provider:cloudflare:website",
66 "local",
67 ],
68 },
69 );
70
71 test(
72 "StaticSite claims its pre-#1053 `<id>/Worker` FQN, including when nested",
73 Effect.gen(function* () {
74 const resources = yield* compile(
75 Effect.gen(function* () {
76 // Top-level site.
77 yield* Cloudflare.Website.StaticSite("Site", {
78 command: "echo build",
79 outdir: "dist",
80 main: "./worker.ts",
81 });
82 // Site nested inside a caller namespace — the former FQN must
83 // resolve under the same `App/` prefix as the site itself.
84 yield* Cloudflare.Website.StaticSite("Nested", {
85 command: "echo build",
86 outdir: "dist",
87 main: "./worker.ts",
88 }).pipe(Namespace.push("App"));
89 }),
90 );
91 expect(resources["Site"]?.FormerFqns).toEqual(["Site/Worker"]);
92 expect(resources["App/Nested"]?.FormerFqns).toEqual(["App/Nested/Worker"]);
93 }),
94 {
95 tags: ["unit", "provider:cloudflare", "provider:cloudflare:website", "local"],
96 },
97 );
98
99 test(
100 "Vite declares env resources in the caller's namespace",
101 Effect.gen(function* () {
102 const keys = yield* fqns(
103 Effect.gen(function* () {
104 yield* Cache;
105 yield* Cloudflare.Website.Vite("Site", {
106 main: "./worker.ts",
107 env: { CACHE: Cache },
108 });
109 }),
110 );
111 expect(keys).toEqual(["Cache", "Site"]);
112 }),
113 {
114 tags: [
115 "unit",
116 "provider:cloudflare",
117 "provider:cloudflare:kv",
118 "provider:cloudflare:website",
119 "local",
120 ],
121 },
122 );
123
124 test(
125 "Astro forwards the prerender environment to its source provider",
126 Effect.gen(function* () {
127 const resources = yield* compile(
128 Cloudflare.Website.Astro("Astro", {
129 prerenderEnvironment: "node",
130 sessionKVBindingName: false,
131 }),
132 );
133 expect(resources["Astro"]?.Props.source).toMatchObject({
134 provider: "@alchemy.run/frontend-frameworks/astro/source",
135 devMode: "server",
136 options: {
137 prerenderEnvironment: "node",
138 },
139 });
140 }),
141 {
142 tags: ["unit", "provider:cloudflare", "provider:cloudflare:website", "local"],
143 },
144 );
145
146 class WebsiteRoot extends Context.Service<WebsiteRoot, string>()("WebsiteRoot") {}
147
148 const astroProps = Effect.gen(function* () {
149 const rootDir = yield* WebsiteRoot;
150 return {
151 rootDir,
152 astro: { output: "static" as const },
153 assets: { notFoundHandling: "404-page" as const },
154 };
155 });
156
157 class AstroFromEffect extends Cloudflare.Website.Astro<AstroFromEffect>()(
158 "AstroClass",
159 astroProps,
160 ) {}
161
162 const AstroFromEffectFunction = Cloudflare.Website.Astro("AstroFunction", astroProps);
163
164 // Both overloads must retain the props Effect's requirements without adding an error.
165 const functionRequiresRoot: WebsiteRoot extends Effect.Services<typeof AstroFromEffectFunction>
166 ? true
167 : false = true;
168 const classRequiresRoot: WebsiteRoot extends Effect.Services<typeof AstroFromEffect>
169 ? true
170 : false = true;
171 const functionIsInfallible: [Effect.Error<typeof AstroFromEffectFunction>] extends [never]
172 ? true
173 : false = true;
174 const classIsInfallible: [Effect.Error<typeof AstroFromEffect>] extends [never] ? true : false =
175 true;
176
177 test(
178 "Astro function and class constructors evaluate typed props Effects before declaring resources",
179 Effect.gen(function* () {
180 expect(
181 functionRequiresRoot && classRequiresRoot && functionIsInfallible && classIsInfallible,
182 ).toBe(true);
183 const resources = yield* compile(
184 Effect.all([AstroFromEffectFunction, AstroFromEffect]).pipe(
185 Effect.provideService(WebsiteRoot, "./typed-astro"),
186 ),
187 );
188 // Static output must suppress auto-provisioning the session namespace.
189 expect(Object.keys(resources).sort()).toEqual(["AstroClass", "AstroFunction"]);
190 for (const id of ["AstroClass", "AstroFunction"]) {
191 expect(resources[id]?.Props.source).toMatchObject({
192 rootDir: "./typed-astro",
193 options: { astro: { output: "static" } },
194 });
195 expect(resources[id]?.Props.assets).toEqual({
196 notFoundHandling: "404-page",
197 });
198 }
199 }),
200 {
201 tags: ["unit", "provider:cloudflare", "provider:cloudflare:website", "local"],
202 },
203 );