The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeFsPromises from "node:fs/promises"; |
| 2 | import * as NodePath from "node:path"; |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import type { AstroIntegration } from "astro"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import type * as ViteModule from "vite"; |
| 8 | import { describe, expect, it } from "vitest"; |
| 9 | import { Framework, type BuildOutput } from "../../core/index.ts"; |
| 10 | import { cacheCloudflare } from "../cache.ts"; |
| 11 | import cloudflareTarget, { |
| 12 | distilledCloudflare, |
| 13 | IMAGE_PASSTHROUGH_ENDPOINT, |
| 14 | makeIntegrationPluginOptions, |
| 15 | SERVER_ENTRYPOINT, |
| 16 | } from "../cloudflare.ts"; |
| 17 | import { createConfigPlugin } from "../config-plugin.ts"; |
| 18 | import framework, { |
| 19 | DEFAULT_TARGET_SPECIFIER, |
| 20 | isAstroTarget, |
| 21 | layer as toLayer, |
| 22 | makeAstroInlineConfig, |
| 23 | NODE_ENVIRONMENTS, |
| 24 | type AstroTarget, |
| 25 | } from "../index.ts"; |
| 26 | |
| 27 | const ROOT = "/project"; |
| 28 | |
| 29 | const TEST_ADAPTER: AstroIntegration = { name: "test-adapter", hooks: {} }; |
| 30 | |
| 31 | const flatten = (plugins: unknown): Array<ViteModule.Plugin> => |
| 32 | ((plugins ?? []) as Array<unknown>) |
| 33 | .flat(Infinity) |
| 34 | .filter( |
| 35 | (plugin): plugin is ViteModule.Plugin => |
| 36 | typeof plugin === "object" && plugin !== null && "name" in plugin, |
| 37 | ); |
| 38 | |
| 39 | type CapturedConfig = { |
| 40 | build?: { redirects?: boolean }; |
| 41 | vite?: { plugins?: unknown }; |
| 42 | image?: { |
| 43 | service?: { entrypoint?: string | URL }; |
| 44 | endpoint?: { entrypoint?: string | URL }; |
| 45 | }; |
| 46 | }; |
| 47 | |
| 48 | const noopLogger = { |
| 49 | info: () => {}, |
| 50 | warn: () => {}, |
| 51 | error: () => {}, |
| 52 | debug: () => {}, |
| 53 | }; |
| 54 | |
| 55 | const runConfigSetup = ( |
| 56 | integration: AstroIntegration, |
| 57 | command: "dev" | "build" | "sync", |
| 58 | config: Record<string, unknown> = {}, |
| 59 | ): CapturedConfig => { |
| 60 | let captured: CapturedConfig | undefined; |
| 61 | const hook = integration.hooks["astro:config:setup"]; |
| 62 | if (!hook) throw new Error("astro:config:setup hook missing"); |
| 63 | void hook({ |
| 64 | command, |
| 65 | config: { vite: {}, image: {}, ...config }, |
| 66 | updateConfig: (config: unknown) => { |
| 67 | captured = config as CapturedConfig; |
| 68 | return {} as never; |
| 69 | }, |
| 70 | logger: noopLogger, |
| 71 | } as never); |
| 72 | if (!captured) throw new Error("updateConfig was not called"); |
| 73 | return captured; |
| 74 | }; |
| 75 | |
| 76 | const objectHook = <A extends Array<unknown>, R>( |
| 77 | hook: unknown, |
| 78 | ): ((this: unknown, ...args: A) => R) => { |
| 79 | if (typeof hook === "function") return hook as never; |
| 80 | if (typeof hook === "object" && hook !== null && "handler" in hook) { |
| 81 | return (hook as { handler: (this: unknown, ...args: A) => R }).handler; |
| 82 | } |
| 83 | throw new Error("hook is neither a function nor an object hook"); |
| 84 | }; |
| 85 | |
| 86 | describe("cloudflare target module", () => { |
| 87 | it("default-exports a factory producing an AstroTarget for the cloudflare platform", () => { |
| 88 | const target = cloudflareTarget({}); |
| 89 | expect(target.platform).toBe("cloudflare"); |
| 90 | expect(isAstroTarget(target)).toBe(true); |
| 91 | expect(target.bundle?.conditions).toEqual(["workerd", "worker", "module", "browser"]); |
| 92 | expect(target.bundle?.external).toEqual(["cloudflare:"]); |
| 93 | expect(target.build).toBeUndefined(); |
| 94 | expect(target.serve).toBeUndefined(); |
| 95 | }); |
| 96 | |
| 97 | it("carries the config opaquely and builds the forked integration from it", () => { |
| 98 | const worker = { compatibilityDate: "2026-03-10" }; |
| 99 | const target = cloudflareTarget({ |
| 100 | worker, |
| 101 | prerenderEnvironment: "node", |
| 102 | sessionKVBindingName: "MY_SESSION", |
| 103 | }); |
| 104 | expect(target.config).toEqual({ |
| 105 | worker, |
| 106 | prerenderEnvironment: "node", |
| 107 | sessionKVBindingName: "MY_SESSION", |
| 108 | }); |
| 109 | const integration = target.integration(); |
| 110 | expect(integration.name).toBe("@alchemy.run/frontend-frameworks/astro"); |
| 111 | expect(integration.hooks["astro:config:setup"]).toBeDefined(); |
| 112 | }); |
| 113 | |
| 114 | it("is the default target specifier of the framework module", () => { |
| 115 | expect(DEFAULT_TARGET_SPECIFIER).toBe("@alchemy.run/frontend-frameworks/astro/cloudflare"); |
| 116 | }); |
| 117 | }); |
| 118 | |
| 119 | describe("isAstroTarget", () => { |
| 120 | it("accepts a DeployTarget with an integration hook and rejects others", () => { |
| 121 | expect(isAstroTarget(cloudflareTarget())).toBe(true); |
| 122 | expect(isAstroTarget({ platform: "cloudflare", config: {} })).toBe(false); |
| 123 | expect(isAstroTarget(undefined)).toBe(false); |
| 124 | expect(isAstroTarget("cloudflare")).toBe(false); |
| 125 | }); |
| 126 | }); |
| 127 | |
| 128 | describe("makeIntegrationPluginOptions", () => { |
| 129 | it("pins main, the ssr entry environment, and the node skipEnvironments", () => { |
| 130 | const options = makeIntegrationPluginOptions(); |
| 131 | expect(options.main).toBe(SERVER_ENTRYPOINT); |
| 132 | expect(options.viteEnvironments).toEqual({ entry: "ssr" }); |
| 133 | expect(options.skipEnvironments).toEqual([...NODE_ENVIRONMENTS]); |
| 134 | }); |
| 135 | |
| 136 | it("preserves user cloudflare options, including a custom worker main, but overrides the structural ones", () => { |
| 137 | const options = makeIntegrationPluginOptions({ |
| 138 | compatibilityDate: "2026-03-10", |
| 139 | compatibilityFlags: ["nodejs_compat"], |
| 140 | worker: { name: "fixtures-astro", bindings: [] }, |
| 141 | main: "/somewhere/else.ts", |
| 142 | viteEnvironments: { entry: "rsc" }, |
| 143 | skipEnvironments: ["custom", "prerender"], |
| 144 | }); |
| 145 | expect(options.compatibilityDate).toBe("2026-03-10"); |
| 146 | expect(options.compatibilityFlags).toEqual(["nodejs_compat"]); |
| 147 | expect(options.worker?.name).toBe("fixtures-astro"); |
| 148 | // A user `main` is the custom Worker entry that wraps the vendored |
| 149 | // server entrypoint's handler; it is kept. Astro still pins the worker |
| 150 | // environment name to "ssr". |
| 151 | expect(options.main).toBe("/somewhere/else.ts"); |
| 152 | expect(options.viteEnvironments).toEqual({ entry: "ssr" }); |
| 153 | expect(options.skipEnvironments).toEqual(["astro", "prerender", "custom"]); |
| 154 | }); |
| 155 | }); |
| 156 | |
| 157 | describe("makeAstroInlineConfig", () => { |
| 158 | it("pins root, leaves configFile undiscovered-default, injects the target integration; leaves output unset", () => { |
| 159 | const config = makeAstroInlineConfig({ |
| 160 | root: ROOT, |
| 161 | integration: TEST_ADAPTER, |
| 162 | }); |
| 163 | expect(config.root).toBe(ROOT); |
| 164 | // The user's astro.config.* must load natively: configFile stays |
| 165 | // undefined so astro's own discovery runs (and degrades gracefully to |
| 166 | // no file — the internal programmatic fallback). |
| 167 | expect(config.configFile).toBeUndefined(); |
| 168 | // The inline config merges OVER the config file, so a default `output` |
| 169 | // here would clobber the file's (e.g. an explicit "static"). It must |
| 170 | // only appear when passed explicitly via the user overrides. |
| 171 | expect("output" in config).toBe(false); |
| 172 | // The target integration rides in `integrations` (it self-registers as |
| 173 | // the adapter at astro:config:done), never in `adapter` — an inline |
| 174 | // `adapter` would deep-merge with a user-file adapter object. |
| 175 | expect(config.adapter).toBeUndefined(); |
| 176 | expect((config.integrations as Array<AstroIntegration>).map((i) => i.name)).toEqual([ |
| 177 | "test-adapter", |
| 178 | ]); |
| 179 | }); |
| 180 | |
| 181 | it("merges user config, appends the target integration after the user's", () => { |
| 182 | const userIntegration: AstroIntegration = { |
| 183 | name: "user-integration", |
| 184 | hooks: {}, |
| 185 | }; |
| 186 | const config = makeAstroInlineConfig({ |
| 187 | root: ROOT, |
| 188 | integration: TEST_ADAPTER, |
| 189 | userConfig: { |
| 190 | site: "https://example.com", |
| 191 | output: "static", |
| 192 | root: "/elsewhere", |
| 193 | integrations: [userIntegration], |
| 194 | devToolbar: { enabled: false }, |
| 195 | }, |
| 196 | }); |
| 197 | expect(config.site).toBe("https://example.com"); |
| 198 | expect(config.output).toBe("static"); |
| 199 | expect(config.devToolbar).toEqual({ enabled: false }); |
| 200 | expect(config.root).toBe(ROOT); |
| 201 | expect(config.configFile).toBeUndefined(); |
| 202 | expect((config.integrations as Array<AstroIntegration>).map((i) => i.name)).toEqual([ |
| 203 | "user-integration", |
| 204 | "test-adapter", |
| 205 | ]); |
| 206 | }); |
| 207 | |
| 208 | it("lets a user-supplied adapter flow through so astro:config:done rejects it with the actionable error", () => { |
| 209 | const config = makeAstroInlineConfig({ |
| 210 | root: ROOT, |
| 211 | integration: TEST_ADAPTER, |
| 212 | userConfig: { adapter: { name: "user-adapter", hooks: {} } }, |
| 213 | }); |
| 214 | expect((config.adapter as AstroIntegration).name).toBe("user-adapter"); |
| 215 | }); |
| 216 | |
| 217 | it("merges the dev port into server options", () => { |
| 218 | const config = makeAstroInlineConfig({ |
| 219 | root: ROOT, |
| 220 | integration: TEST_ADAPTER, |
| 221 | userConfig: { server: { host: "127.0.0.1" } }, |
| 222 | port: 3102, |
| 223 | }); |
| 224 | expect(config.server).toEqual({ host: "127.0.0.1", port: 3102 }); |
| 225 | }); |
| 226 | |
| 227 | it("appends extra vite plugins after the user's", () => { |
| 228 | const userPlugin: ViteModule.Plugin = { name: "user-plugin" }; |
| 229 | const collectorPlugin: ViteModule.Plugin = { name: "alchemy:build-output" }; |
| 230 | const config = makeAstroInlineConfig({ |
| 231 | root: ROOT, |
| 232 | integration: TEST_ADAPTER, |
| 233 | userConfig: { vite: { plugins: [userPlugin] } }, |
| 234 | extraVitePlugins: [collectorPlugin], |
| 235 | }); |
| 236 | const plugins = flatten(config.vite?.plugins); |
| 237 | const names = plugins.map((plugin) => plugin.name); |
| 238 | expect(names.indexOf("user-plugin")).toBeLessThan(names.indexOf("alchemy:build-output")); |
| 239 | }); |
| 240 | }); |
| 241 | |
| 242 | describe("distilledCloudflare astro:config:setup", () => { |
| 243 | it("injects the cloudflare plugins, config plugin, and prerender plugins in dev", () => { |
| 244 | // Default (workerd) prerendering: no node dev middleware, the workerd |
| 245 | // prerender environment plugin instead. |
| 246 | const workerdNames = flatten(runConfigSetup(distilledCloudflare(), "dev").vite?.plugins).map( |
| 247 | (plugin) => plugin.name, |
| 248 | ); |
| 249 | expect(workerdNames).toContain( |
| 250 | "@alchemy.run/frontend-frameworks/astro:workerd-prerender-environment", |
| 251 | ); |
| 252 | expect(workerdNames).not.toContain( |
| 253 | "@alchemy.run/frontend-frameworks/astro:dev-server-prerender-middleware", |
| 254 | ); |
| 255 | |
| 256 | const captured = runConfigSetup(distilledCloudflare({ prerenderEnvironment: "node" }), "dev"); |
| 257 | const plugins = flatten(captured.vite?.plugins); |
| 258 | const names = plugins.map((plugin) => plugin.name); |
| 259 | expect(names).toContain( |
| 260 | "@alchemy.run/frontend-frameworks/astro:dev-server-prerender-middleware", |
| 261 | ); |
| 262 | expect(names).toContain("@alchemy.run/frontend-frameworks/astro:cf-imports"); |
| 263 | expect(names).toContain("@alchemy.run/frontend-frameworks/astro:environment"); |
| 264 | expect(names).toContain("@alchemy.run/frontend-frameworks/astro:cf-externals"); |
| 265 | expect(names).toContain("virtual:astro-cloudflare:config"); |
| 266 | const cloudflare = plugins.filter((plugin) => plugin.name.startsWith("distilled-cloudflare")); |
| 267 | expect(cloudflare.length).toBeGreaterThan(0); |
| 268 | // Dev keeps the dev server hooks intact. |
| 269 | expect(cloudflare.some((plugin) => plugin.configureServer !== undefined)).toBe(true); |
| 270 | expect(captured.build?.redirects).toBe(false); |
| 271 | }); |
| 272 | |
| 273 | it("strips configureServer from the cloudflare plugins during build/sync typegen", () => { |
| 274 | for (const command of ["build", "sync"] as const) { |
| 275 | const captured = runConfigSetup(distilledCloudflare(), command); |
| 276 | const plugins = flatten(captured.vite?.plugins); |
| 277 | const names = plugins.map((plugin) => plugin.name); |
| 278 | expect(names).not.toContain( |
| 279 | "@alchemy.run/frontend-frameworks/astro:dev-server-prerender-middleware", |
| 280 | ); |
| 281 | const cloudflare = plugins.filter((plugin) => plugin.name.startsWith("distilled-cloudflare")); |
| 282 | expect(cloudflare.length).toBeGreaterThan(0); |
| 283 | expect(cloudflare.every((plugin) => plugin.configureServer === undefined)).toBe(true); |
| 284 | } |
| 285 | }); |
| 286 | |
| 287 | it("configures the passthrough image service with the phase-specific endpoint", () => { |
| 288 | const dev = runConfigSetup(distilledCloudflare(), "dev"); |
| 289 | expect(dev.image?.service?.entrypoint).toBeDefined(); |
| 290 | expect(dev.image?.endpoint).toEqual({ |
| 291 | entrypoint: "astro/assets/endpoint/generic", |
| 292 | }); |
| 293 | const build = runConfigSetup(distilledCloudflare(), "build"); |
| 294 | expect(build.image?.endpoint).toEqual({ |
| 295 | entrypoint: IMAGE_PASSTHROUGH_ENDPOINT, |
| 296 | }); |
| 297 | }); |
| 298 | |
| 299 | it("pre-bundles the server environments in dev but disables discovery during typegen", () => { |
| 300 | const findEnvironmentPlugin = (command: "dev" | "build") => { |
| 301 | const captured = runConfigSetup(distilledCloudflare(), command); |
| 302 | const plugin = flatten(captured.vite?.plugins).find( |
| 303 | (candidate) => candidate.name === "@alchemy.run/frontend-frameworks/astro:environment", |
| 304 | ); |
| 305 | if (!plugin) throw new Error("environment plugin missing"); |
| 306 | return objectHook< |
| 307 | [string, ViteModule.EnvironmentOptions], |
| 308 | { optimizeDeps?: ViteModule.DepOptimizationOptions } | undefined |
| 309 | >(plugin.configEnvironment); |
| 310 | }; |
| 311 | |
| 312 | const dev = findEnvironmentPlugin("dev"); |
| 313 | expect(dev.call({}, "ssr", {})?.optimizeDeps?.include).toContain(SERVER_ENTRYPOINT); |
| 314 | expect(dev.call({}, "client", {})?.optimizeDeps?.include).toContain( |
| 315 | "astro/runtime/client/dev-toolbar/entrypoint.js", |
| 316 | ); |
| 317 | expect(dev.call({}, "custom", {})).toBeUndefined(); |
| 318 | // Environments already configured with an explicit (no-discovery) |
| 319 | // optimizer are left alone. |
| 320 | expect(dev.call({}, "ssr", { optimizeDeps: { noDiscovery: true } })).toBeUndefined(); |
| 321 | |
| 322 | const typegen = findEnvironmentPlugin("build"); |
| 323 | expect(typegen.call({}, "ssr", {})?.optimizeDeps).toEqual({ |
| 324 | noDiscovery: true, |
| 325 | include: [], |
| 326 | }); |
| 327 | }); |
| 328 | |
| 329 | it("enables the cache provider only when the config uses cacheCloudflare()", () => { |
| 330 | const configModule = (config: Record<string, unknown>) => { |
| 331 | const captured = runConfigSetup(distilledCloudflare(), "dev", config); |
| 332 | const plugin = flatten(captured.vite?.plugins).find( |
| 333 | (candidate) => candidate.name === "virtual:astro-cloudflare:config", |
| 334 | ); |
| 335 | if (!plugin) throw new Error("config plugin missing"); |
| 336 | return objectHook<[string], string>(plugin.load).call( |
| 337 | { environment: { name: "ssr" } }, |
| 338 | "\0virtual:astro-cloudflare:config", |
| 339 | ); |
| 340 | }; |
| 341 | const ssrInclude = (config: Record<string, unknown>) => { |
| 342 | const captured = runConfigSetup(distilledCloudflare(), "dev", config); |
| 343 | const plugin = flatten(captured.vite?.plugins).find( |
| 344 | (candidate) => candidate.name === "@alchemy.run/frontend-frameworks/astro:environment", |
| 345 | ); |
| 346 | if (!plugin) throw new Error("environment plugin missing"); |
| 347 | return objectHook< |
| 348 | [string, ViteModule.EnvironmentOptions], |
| 349 | { optimizeDeps?: ViteModule.DepOptimizationOptions } | undefined |
| 350 | >(plugin.configEnvironment).call({}, "ssr", {})?.optimizeDeps?.include; |
| 351 | }; |
| 352 | |
| 353 | const cached = { cache: { provider: cacheCloudflare() } }; |
| 354 | expect(configModule(cached)).toContain("export const cacheProviderEnabled = true"); |
| 355 | expect(ssrInclude(cached)).toContain("@alchemy.run/frontend-frameworks/astro/cache/provider"); |
| 356 | |
| 357 | // Upstream's `@astrojs/cloudflare/cache` provider is detected by name. |
| 358 | const upstream = { |
| 359 | cache: { |
| 360 | provider: { name: "cloudflare", entrypoint: "@astrojs/cloudflare/cache/provider" }, |
| 361 | }, |
| 362 | }; |
| 363 | expect(configModule(upstream)).toContain("export const cacheProviderEnabled = true"); |
| 364 | |
| 365 | for (const config of [{}, { cache: { provider: { name: "memory", entrypoint: "x" } } }]) { |
| 366 | expect(configModule(config)).toContain("export const cacheProviderEnabled = false"); |
| 367 | expect(ssrInclude(config)).not.toContain( |
| 368 | "@alchemy.run/frontend-frameworks/astro/cache/provider", |
| 369 | ); |
| 370 | } |
| 371 | }); |
| 372 | |
| 373 | it("scopes cf-externals to the worker-resolved server environments", () => { |
| 374 | const captured = runConfigSetup(distilledCloudflare(), "build"); |
| 375 | const plugin = flatten(captured.vite?.plugins).find( |
| 376 | (candidate) => candidate.name === "@alchemy.run/frontend-frameworks/astro:cf-externals", |
| 377 | ); |
| 378 | if (!plugin) throw new Error("cf-externals plugin missing"); |
| 379 | const applies = plugin.applyToEnvironment as (environment: { name: string }) => boolean; |
| 380 | expect(applies({ name: "ssr" })).toBe(true); |
| 381 | expect(applies({ name: "prerender" })).toBe(true); |
| 382 | expect(applies({ name: "client" })).toBe(false); |
| 383 | const config = objectHook<[{ ssr?: { external?: unknown } }], void>(plugin.config); |
| 384 | const conf = { ssr: { external: ["some-dep"] } }; |
| 385 | config.call({}, conf); |
| 386 | expect(conf.ssr.external).toBeUndefined(); |
| 387 | }); |
| 388 | }); |
| 389 | |
| 390 | describe("distilledCloudflare astro:config:done", () => { |
| 391 | it("registers the adapter without a preview entrypoint", () => { |
| 392 | let adapter: Record<string, unknown> | undefined; |
| 393 | const integration = distilledCloudflare(); |
| 394 | const hook = integration.hooks["astro:config:done"]; |
| 395 | if (!hook) throw new Error("astro:config:done hook missing"); |
| 396 | void hook({ |
| 397 | buildOutput: "server", |
| 398 | config: { |
| 399 | base: "/", |
| 400 | build: { client: new URL("file:///project/dist/client/") }, |
| 401 | }, |
| 402 | injectTypes: () => new URL("file:///dev/null"), |
| 403 | setAdapter: (value: unknown) => { |
| 404 | adapter = value as Record<string, unknown>; |
| 405 | }, |
| 406 | } as never); |
| 407 | expect(adapter?.name).toBe("@alchemy.run/frontend-frameworks/astro"); |
| 408 | expect(adapter?.previewEntrypoint).toBeUndefined(); |
| 409 | expect(adapter?.serverEntrypoint).toBeUndefined(); |
| 410 | expect(adapter?.adapterFeatures).toMatchObject({ |
| 411 | buildOutput: "server", |
| 412 | middlewareMode: "classic", |
| 413 | preserveBuildClientDir: true, |
| 414 | preserveBuildServerDir: true, |
| 415 | }); |
| 416 | }); |
| 417 | |
| 418 | it("injects a hookless adapter marker at astro:config:setup when the config declares none", () => { |
| 419 | // Astro's build refuses server output unless `config.adapter` is set; |
| 420 | // the integration (injected via `integrations`) satisfies the check |
| 421 | // with an inert marker while `setAdapter` registers the real adapter. |
| 422 | const integration = distilledCloudflare(); |
| 423 | const updates: Array<Record<string, unknown>> = []; |
| 424 | const hook = integration.hooks["astro:config:setup"]; |
| 425 | if (!hook) throw new Error("astro:config:setup hook missing"); |
| 426 | void hook({ |
| 427 | command: "build", |
| 428 | config: { vite: {}, image: {} }, |
| 429 | updateConfig: (update: unknown) => { |
| 430 | updates.push(update as Record<string, unknown>); |
| 431 | return {} as never; |
| 432 | }, |
| 433 | logger: noopLogger, |
| 434 | } as never); |
| 435 | const marker = updates.find((update) => update.adapter !== undefined)?.adapter as |
| 436 | | { name: string; hooks: Record<string, unknown> } |
| 437 | | undefined; |
| 438 | expect(marker).toEqual({ |
| 439 | name: "@alchemy.run/frontend-frameworks/astro", |
| 440 | hooks: {}, |
| 441 | }); |
| 442 | // With the marker injected, astro:config:done registers the adapter. |
| 443 | const done = integration.hooks["astro:config:done"]; |
| 444 | if (!done) throw new Error("astro:config:done hook missing"); |
| 445 | let adapter: Record<string, unknown> | undefined; |
| 446 | void done({ |
| 447 | buildOutput: "server", |
| 448 | config: { |
| 449 | base: "/", |
| 450 | adapter: marker, |
| 451 | build: { client: new URL("file:///project/dist/client/") }, |
| 452 | }, |
| 453 | injectTypes: () => new URL("file:///dev/null"), |
| 454 | setAdapter: (value: unknown) => { |
| 455 | adapter = value as Record<string, unknown>; |
| 456 | }, |
| 457 | } as never); |
| 458 | expect(adapter?.name).toBe("@alchemy.run/frontend-frameworks/astro"); |
| 459 | }); |
| 460 | |
| 461 | it("does not inject the marker when the config declares an adapter, and fails at astro:config:done", () => { |
| 462 | const integration = distilledCloudflare(); |
| 463 | const updates: Array<Record<string, unknown>> = []; |
| 464 | const setup = integration.hooks["astro:config:setup"]; |
| 465 | if (!setup) throw new Error("astro:config:setup hook missing"); |
| 466 | void setup({ |
| 467 | command: "build", |
| 468 | config: { |
| 469 | vite: {}, |
| 470 | image: {}, |
| 471 | adapter: { name: "@astrojs/cloudflare", hooks: {} }, |
| 472 | }, |
| 473 | updateConfig: (update: unknown) => { |
| 474 | updates.push(update as Record<string, unknown>); |
| 475 | return {} as never; |
| 476 | }, |
| 477 | logger: noopLogger, |
| 478 | } as never); |
| 479 | expect(updates.every((update) => update.adapter === undefined)).toBe(true); |
| 480 | const done = integration.hooks["astro:config:done"]; |
| 481 | if (!done) throw new Error("astro:config:done hook missing"); |
| 482 | expect(() => |
| 483 | done({ |
| 484 | buildOutput: "server", |
| 485 | config: { |
| 486 | base: "/", |
| 487 | adapter: { name: "@astrojs/cloudflare", hooks: {} }, |
| 488 | build: { client: new URL("file:///project/dist/client/") }, |
| 489 | }, |
| 490 | injectTypes: () => new URL("file:///dev/null"), |
| 491 | setAdapter: () => {}, |
| 492 | } as never), |
| 493 | ).toThrow(/declares the adapter "@astrojs\/cloudflare"[\s\S]*Remove `adapter`/); |
| 494 | }); |
| 495 | }); |
| 496 | |
| 497 | describe("distilledCloudflare astro:build:setup", () => { |
| 498 | it("applies the server-target vite tweaks", () => { |
| 499 | const integration = distilledCloudflare(); |
| 500 | const hook = integration.hooks["astro:build:setup"]; |
| 501 | if (!hook) throw new Error("astro:build:setup hook missing"); |
| 502 | const viteConfig: Record<string, any> = {}; |
| 503 | void hook({ vite: viteConfig, target: "server" } as never); |
| 504 | expect(viteConfig.ssr?.noExternal).toBe(true); |
| 505 | expect(viteConfig.build?.rolldownOptions?.external).toEqual(["sharp"]); |
| 506 | expect(viteConfig.build?.rolldownOptions?.output?.banner).toContain("globalThis.process"); |
| 507 | expect(viteConfig.define?.["globalThis.__ASTRO_IMAGES_BINDING_NAME"]).toBe('"IMAGES"'); |
| 508 | |
| 509 | const clientConfig: Record<string, any> = {}; |
| 510 | void hook({ vite: clientConfig, target: "client" } as never); |
| 511 | expect(clientConfig.ssr).toBeUndefined(); |
| 512 | }); |
| 513 | }); |
| 514 | |
| 515 | describe("config plugin (virtual:astro-cloudflare:config)", () => { |
| 516 | const plugin = createConfigPlugin({ |
| 517 | sessionKVBindingName: "SESSION", |
| 518 | compileImageConfig: null, |
| 519 | cacheProviderEnabled: false, |
| 520 | }); |
| 521 | |
| 522 | it("resolves and loads the virtual module", () => { |
| 523 | const resolveId = objectHook<[string], string>(plugin.resolveId); |
| 524 | const resolved = resolveId.call({}, "virtual:astro-cloudflare:config"); |
| 525 | expect(resolved).toBe("\0virtual:astro-cloudflare:config"); |
| 526 | |
| 527 | const load = objectHook<[string], string>(plugin.load); |
| 528 | const code = load.call({ environment: { name: "ssr" } }, resolved); |
| 529 | expect(code).toContain('export const sessionKVBindingName = "SESSION"'); |
| 530 | expect(code).toContain("export const compileImageConfig = null"); |
| 531 | expect(code).toContain("export const cacheProviderEnabled = false"); |
| 532 | expect(code).toContain("export const isPrerender = false"); |
| 533 | }); |
| 534 | |
| 535 | it("marks isPrerender only in the prerender environment", () => { |
| 536 | const load = objectHook<[string], string>(plugin.load); |
| 537 | const code = load.call( |
| 538 | { environment: { name: "prerender" } }, |
| 539 | "\0virtual:astro-cloudflare:config", |
| 540 | ); |
| 541 | expect(code).toContain("export const isPrerender = true"); |
| 542 | }); |
| 543 | }); |
| 544 | |
| 545 | describe("vendored runtime purity", () => { |
| 546 | it("never imports wrangler, @astrojs/cloudflare, or @cloudflare/vite-plugin", async () => { |
| 547 | const runtimeDir = NodePath.resolve(import.meta.dirname, "../runtime"); |
| 548 | const entries = await NodeFsPromises.readdir(runtimeDir, { |
| 549 | recursive: true, |
| 550 | withFileTypes: true, |
| 551 | }); |
| 552 | const files = entries |
| 553 | .filter((entry) => entry.isFile() && entry.name.endsWith(".ts")) |
| 554 | .map((entry) => NodePath.join(entry.parentPath, entry.name)); |
| 555 | expect(files.length).toBeGreaterThan(0); |
| 556 | for (const file of files) { |
| 557 | const source = await NodeFsPromises.readFile(file, "utf8"); |
| 558 | expect(source, file).not.toMatch(/["']wrangler["']|["']wrangler\//); |
| 559 | expect(source, file).not.toMatch(/["']@astrojs\/cloudflare/); |
| 560 | expect(source, file).not.toMatch(/["']@cloudflare\/vite-plugin/); |
| 561 | } |
| 562 | }); |
| 563 | }); |
| 564 | |
| 565 | describe("framework factory", () => { |
| 566 | it("default-exports a factory producing a Layer<Framework>", () => { |
| 567 | expect(Layer.isLayer(framework({}))).toBe(true); |
| 568 | expect(Layer.isLayer(toLayer())).toBe(true); |
| 569 | }); |
| 570 | |
| 571 | it("accepts the harness's target-scoped carriage and the deprecated vite alias", () => { |
| 572 | expect( |
| 573 | Layer.isLayer( |
| 574 | framework({ |
| 575 | target: { |
| 576 | cloudflare: { worker: { compatibilityDate: "2026-03-10" } }, |
| 577 | }, |
| 578 | }), |
| 579 | ), |
| 580 | ).toBe(true); |
| 581 | expect(Layer.isLayer(framework({ vite: { compatibilityDate: "2026-03-10" } }))).toBe(true); |
| 582 | }); |
| 583 | }); |
| 584 | |
| 585 | describe("deploy-target resolution", () => { |
| 586 | const OUTPUT: BuildOutput = { |
| 587 | clientDirectory: undefined, |
| 588 | serverModules: [{ name: "entry.mjs", content: "export default {}", hash: "hash" }], |
| 589 | externalWorkspaces: new Set<string>(), |
| 590 | }; |
| 591 | |
| 592 | const run = <A, E>( |
| 593 | layer: Layer.Layer<Framework, unknown, never>, |
| 594 | effect: Effect.Effect<A, E, Framework>, |
| 595 | ) => Effect.runPromise(effect.pipe(Effect.provide(layer)) as Effect.Effect<A, E, never>); |
| 596 | |
| 597 | const build = Effect.flatMap(Framework, (service) => service.build()); |
| 598 | |
| 599 | it("delegates the build wholesale when the target defines `build`", async () => { |
| 600 | const contexts: Array<{ root: string; framework?: string | undefined }> = []; |
| 601 | const target: AstroTarget = { |
| 602 | platform: "test", |
| 603 | config: {}, |
| 604 | integration: () => TEST_ADAPTER, |
| 605 | build: (context) => { |
| 606 | contexts.push({ root: context.root, framework: context.framework }); |
| 607 | return Effect.succeed(OUTPUT); |
| 608 | }, |
| 609 | }; |
| 610 | const layer = toLayer({ target }).pipe(Layer.provide(NodeServices.layer)); |
| 611 | const output = await run(layer, build); |
| 612 | expect(output).toEqual(OUTPUT); |
| 613 | expect(contexts).toHaveLength(1); |
| 614 | expect(contexts[0]?.framework).toBe("astro"); |
| 615 | expect(NodePath.isAbsolute(contexts[0]?.root ?? "")).toBe(true); |
| 616 | }); |
| 617 | |
| 618 | it("fails with a FrameworkError when the resolved target is not an AstroTarget", async () => { |
| 619 | const layer = toLayer({ |
| 620 | target: { platform: "test", config: {} } as unknown as AstroTarget, |
| 621 | }).pipe(Layer.provide(NodeServices.layer)); |
| 622 | const error = await run(layer, Effect.flip(build)); |
| 623 | expect(error).toMatchObject({ |
| 624 | _tag: "FrameworkError", |
| 625 | framework: "astro", |
| 626 | }); |
| 627 | expect(String((error as { message: string }).message)).toContain("AstroTarget"); |
| 628 | }); |
| 629 | |
| 630 | it("applies a target factory to targetConfig", async () => { |
| 631 | const configs: Array<unknown> = []; |
| 632 | const layer = toLayer({ |
| 633 | target: (config: { flag: boolean }) => { |
| 634 | configs.push(config); |
| 635 | return { |
| 636 | platform: "test", |
| 637 | config, |
| 638 | integration: () => TEST_ADAPTER, |
| 639 | build: () => Effect.succeed(OUTPUT), |
| 640 | } satisfies AstroTarget<{ flag: boolean }>; |
| 641 | }, |
| 642 | targetConfig: { flag: true }, |
| 643 | }).pipe(Layer.provide(NodeServices.layer)); |
| 644 | const output = await run(layer, build); |
| 645 | expect(output).toEqual(OUTPUT); |
| 646 | expect(configs).toEqual([{ flag: true }]); |
| 647 | }); |
| 648 | }); |