The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { |
| 3 | appendHeaders, |
| 4 | generateAssetsIgnore, |
| 5 | generateHeaders, |
| 6 | generateRedirects, |
| 7 | generateWorkerShim, |
| 8 | makeCloudflareAdapter, |
| 9 | makeDevPlatformEmulator, |
| 10 | type DevPlatformProxy, |
| 11 | type OpenDevPlatformProxy, |
| 12 | type OpenDevPlatformProxyOptions, |
| 13 | } from "../cloudflare.ts"; |
| 14 | |
| 15 | describe("generateWorkerShim", () => { |
| 16 | const shim = generateWorkerShim({ |
| 17 | serverImport: "./../output/server/index.js", |
| 18 | manifestImport: "./../cloudflare-tmp/manifest.js", |
| 19 | assetsBinding: "ASSETS", |
| 20 | }); |
| 21 | |
| 22 | it("imports the server and manifest through real relative paths", () => { |
| 23 | expect(shim).toContain(`import { server } from "./../output/server/index.js";`); |
| 24 | expect(shim).toContain( |
| 25 | `import { assets, app_path, prerendered, base_path, routes } from "./../cloudflare-tmp/manifest.js";`, |
| 26 | ); |
| 27 | }); |
| 28 | |
| 29 | it("uses module-level env from cloudflare:workers", () => { |
| 30 | expect(shim).toContain(`import { env } from 'cloudflare:workers';`); |
| 31 | }); |
| 32 | |
| 33 | it("serves static and prerendered files through the assets binding", () => { |
| 34 | expect(shim).toContain("env.ASSETS.fetch(req)"); |
| 35 | expect(shim).toContain("env.ASSETS.fetch(url)"); |
| 36 | }); |
| 37 | |
| 38 | it("respects a custom assets binding name", () => { |
| 39 | const custom = generateWorkerShim({ |
| 40 | serverImport: "./server/index.js", |
| 41 | manifestImport: "./manifest.js", |
| 42 | assetsBinding: "STATIC", |
| 43 | }); |
| 44 | expect(custom).toContain("env.STATIC.fetch(req)"); |
| 45 | expect(custom).not.toContain("env.ASSETS.fetch"); |
| 46 | }); |
| 47 | |
| 48 | it("replaces worktop with a caches.default pragma cache", () => { |
| 49 | expect(shim).not.toContain("worktop"); |
| 50 | expect(shim).toContain("caches.default"); |
| 51 | expect(shim).toContain("cache_lookup"); |
| 52 | expect(shim).toContain("cache_save"); |
| 53 | }); |
| 54 | |
| 55 | it("exposes the platform to server.respond", () => { |
| 56 | expect(shim).toContain("platform: { env, ctx, caches, cf: req.cf }"); |
| 57 | }); |
| 58 | |
| 59 | describe("SPA not-found deferral", () => { |
| 60 | const spa = generateWorkerShim({ |
| 61 | serverImport: "./../output/server/index.js", |
| 62 | manifestImport: "./../cloudflare-tmp/manifest.js", |
| 63 | assetsBinding: "ASSETS", |
| 64 | notFoundHandling: "single-page-application", |
| 65 | }); |
| 66 | |
| 67 | it("emits no deferral by default (upstream parity)", () => { |
| 68 | expect(shim).not.toContain("matches_kit_route"); |
| 69 | expect(shim).not.toContain("res.status === 404"); |
| 70 | }); |
| 71 | |
| 72 | it("emits no deferral for 404-page (upstream parity)", () => { |
| 73 | const fourOhFour = generateWorkerShim({ |
| 74 | serverImport: "./server/index.js", |
| 75 | manifestImport: "./manifest.js", |
| 76 | assetsBinding: "ASSETS", |
| 77 | notFoundHandling: "404-page", |
| 78 | }); |
| 79 | expect(fourOhFour).not.toContain("matches_kit_route"); |
| 80 | expect(fourOhFour).not.toContain("res.status === 404"); |
| 81 | }); |
| 82 | |
| 83 | it("defers router-level 404s to the assets binding for SPA builds", () => { |
| 84 | expect(spa).toContain("const matches_kit_route = (pathname)"); |
| 85 | expect(spa).toContain("res.status === 404"); |
| 86 | expect(spa).toContain("!matches_kit_route(pathname)"); |
| 87 | }); |
| 88 | |
| 89 | it("only defers navigation-shaped requests (GET/HEAD accepting text/html)", () => { |
| 90 | expect(spa).toContain("(req.method === 'GET' || req.method === 'HEAD')"); |
| 91 | expect(spa).toContain("(req.headers.get('accept') || '').includes('text/html')"); |
| 92 | }); |
| 93 | |
| 94 | it("probes kit's route patterns with the base path stripped", () => { |
| 95 | expect(spa).toContain("path.slice(base_path.length) || '/'"); |
| 96 | expect(spa).toContain("route.pattern.test(path)"); |
| 97 | expect(spa).toContain("for (const route of routes)"); |
| 98 | }); |
| 99 | |
| 100 | it("defers through the configured assets binding", () => { |
| 101 | const custom = generateWorkerShim({ |
| 102 | serverImport: "./server/index.js", |
| 103 | manifestImport: "./manifest.js", |
| 104 | assetsBinding: "STATIC", |
| 105 | notFoundHandling: "single-page-application", |
| 106 | }); |
| 107 | expect(custom).toContain("res = await env.STATIC.fetch(req);"); |
| 108 | expect(custom).not.toContain("env.ASSETS.fetch"); |
| 109 | }); |
| 110 | }); |
| 111 | }); |
| 112 | |
| 113 | describe("appendHeaders", () => { |
| 114 | it("adds a new rule block", () => { |
| 115 | const content = appendHeaders("/_app/*", ["X-Robots-Tag: noindex"], ""); |
| 116 | expect(content).toContain("/_app/*\n X-Robots-Tag: noindex"); |
| 117 | expect(content).toContain("AUTOGENERATED SVELTE IMMUTABLE HEADERS"); |
| 118 | }); |
| 119 | |
| 120 | it("merges into an existing block for the same URL", () => { |
| 121 | const existing = "/_app/*\n X-Existing: yes"; |
| 122 | const content = appendHeaders("/_app/*", ["X-Robots-Tag: noindex"], existing); |
| 123 | expect(content).toBe("/_app/*\n X-Robots-Tag: noindex\n X-Existing: yes"); |
| 124 | }); |
| 125 | }); |
| 126 | |
| 127 | describe("generateHeaders", () => { |
| 128 | it("emits noindex and immutable cache rules for the app dir", () => { |
| 129 | const content = generateHeaders("_app"); |
| 130 | expect(content).toContain("/_app/*\n X-Robots-Tag: noindex\n Cache-Control: no-cache"); |
| 131 | expect(content).toContain( |
| 132 | "/_app/immutable/*\n ! Cache-Control\n Cache-Control: public, immutable, max-age=31536000", |
| 133 | ); |
| 134 | }); |
| 135 | |
| 136 | it("preserves user-authored headers", () => { |
| 137 | const content = generateHeaders("_app", "/api/*\n X-Fixture: yes"); |
| 138 | expect(content).toContain("/api/*\n X-Fixture: yes"); |
| 139 | expect(content).toContain("/_app/immutable/*"); |
| 140 | }); |
| 141 | }); |
| 142 | |
| 143 | describe("generateRedirects", () => { |
| 144 | it("returns undefined when there is nothing to write", () => { |
| 145 | expect(generateRedirects(new Map())).toBeUndefined(); |
| 146 | }); |
| 147 | |
| 148 | it("emits prerendered redirect rules", () => { |
| 149 | const content = generateRedirects(new Map([["/old", { status: 301, location: "/new" }]])); |
| 150 | expect(content).toContain("/old /new 301"); |
| 151 | expect(content).toContain("AUTOGENERATED SVELTE PRERENDERED REDIRECTS"); |
| 152 | }); |
| 153 | |
| 154 | it("prepends user-authored redirects", () => { |
| 155 | const content = generateRedirects( |
| 156 | new Map([["/old", { status: 301, location: "/new" }]]), |
| 157 | "/legacy /modern 302", |
| 158 | ); |
| 159 | expect(content?.startsWith("/legacy /modern 302")).toBe(true); |
| 160 | expect(content).toContain("/old /new 301"); |
| 161 | }); |
| 162 | }); |
| 163 | |
| 164 | describe("generateAssetsIgnore", () => { |
| 165 | it("excludes the worker entry and metadata files from assets", () => { |
| 166 | const content = generateAssetsIgnore(); |
| 167 | expect(content.split("\n").filter(Boolean)).toEqual([ |
| 168 | "_worker.js", |
| 169 | "_routes.json", |
| 170 | "_headers", |
| 171 | "_redirects", |
| 172 | ]); |
| 173 | }); |
| 174 | }); |
| 175 | |
| 176 | interface FakeProxyHarness { |
| 177 | readonly openProxy: OpenDevPlatformProxy; |
| 178 | readonly opens: Array<OpenDevPlatformProxyOptions>; |
| 179 | readonly disposals: () => number; |
| 180 | } |
| 181 | |
| 182 | const makeFakeProxy = (env: Record<string, unknown>): FakeProxyHarness => { |
| 183 | const opens: Array<OpenDevPlatformProxyOptions> = []; |
| 184 | let disposed = 0; |
| 185 | const proxy: DevPlatformProxy = { |
| 186 | env, |
| 187 | cf: { colo: "TST", country: "US" }, |
| 188 | ctx: { waitUntil: (_promise: Promise<unknown>) => {} }, |
| 189 | caches: { default: "fake-cache" }, |
| 190 | dispose: async () => { |
| 191 | disposed += 1; |
| 192 | }, |
| 193 | }; |
| 194 | return { |
| 195 | openProxy: async (options) => { |
| 196 | opens.push(options); |
| 197 | return proxy; |
| 198 | }, |
| 199 | opens, |
| 200 | disposals: () => disposed, |
| 201 | }; |
| 202 | }; |
| 203 | |
| 204 | type PlatformShape = { |
| 205 | env: Record<string, unknown>; |
| 206 | ctx: { waitUntil: (promise: Promise<unknown>) => void }; |
| 207 | caches: unknown; |
| 208 | cf: Record<string, unknown>; |
| 209 | }; |
| 210 | |
| 211 | describe("makeDevPlatformEmulator", () => { |
| 212 | it("opens the proxy lazily and serves env, ctx, caches, and cf from it", async () => { |
| 213 | const fake = makeFakeProxy({ FROM_PROXY: "proxied" }); |
| 214 | const emulator = makeDevPlatformEmulator({ |
| 215 | bindings: ["binding-hook"], |
| 216 | compatibilityDate: "2026-03-10", |
| 217 | compatibilityFlags: ["nodejs_compat"], |
| 218 | openProxy: fake.openProxy, |
| 219 | }); |
| 220 | // nothing opened until the first platform() call |
| 221 | expect(fake.opens).toHaveLength(0); |
| 222 | const platform = (await emulator.platform?.({ |
| 223 | config: {}, |
| 224 | prerender: false, |
| 225 | })) as PlatformShape; |
| 226 | expect(fake.opens).toHaveLength(1); |
| 227 | expect(fake.opens[0]).toMatchObject({ |
| 228 | name: "sveltekit-dev-platform-proxy", |
| 229 | compatibilityDate: "2026-03-10", |
| 230 | compatibilityFlags: ["nodejs_compat"], |
| 231 | bindings: ["binding-hook"], |
| 232 | }); |
| 233 | expect(platform.env["FROM_PROXY"]).toBe("proxied"); |
| 234 | expect(platform.cf["colo"]).toBe("TST"); |
| 235 | expect(platform.caches).toEqual({ default: "fake-cache" }); |
| 236 | expect(() => platform.ctx.waitUntil(Promise.resolve())).not.toThrow(); |
| 237 | }); |
| 238 | |
| 239 | it("opens a single proxy across platform() calls", async () => { |
| 240 | const fake = makeFakeProxy({}); |
| 241 | const emulator = makeDevPlatformEmulator({ openProxy: fake.openProxy }); |
| 242 | await emulator.platform?.({ config: {}, prerender: false }); |
| 243 | await emulator.platform?.({ config: {}, prerender: true }); |
| 244 | await emulator.platform?.({ config: {}, prerender: false }); |
| 245 | expect(fake.opens).toHaveLength(1); |
| 246 | }); |
| 247 | |
| 248 | it("applies literal env overrides on top of proxied values", async () => { |
| 249 | const fake = makeFakeProxy({ SHARED: "proxied", FROM_PROXY: "proxied" }); |
| 250 | const emulator = makeDevPlatformEmulator({ |
| 251 | env: { SHARED: "literal-wins", LITERAL_ONLY: "literal" }, |
| 252 | openProxy: fake.openProxy, |
| 253 | }); |
| 254 | const platform = (await emulator.platform?.({ |
| 255 | config: {}, |
| 256 | prerender: false, |
| 257 | })) as PlatformShape; |
| 258 | expect(platform.env).toEqual({ |
| 259 | FROM_PROXY: "proxied", |
| 260 | SHARED: "literal-wins", |
| 261 | LITERAL_ONLY: "literal", |
| 262 | }); |
| 263 | }); |
| 264 | |
| 265 | it("throws on env access in prerenderable routes", async () => { |
| 266 | const fake = makeFakeProxy({ FROM_PROXY: "proxied" }); |
| 267 | const emulator = makeDevPlatformEmulator({ |
| 268 | env: { LITERAL: "value" }, |
| 269 | openProxy: fake.openProxy, |
| 270 | }); |
| 271 | const platform = (await emulator.platform?.({ |
| 272 | config: {}, |
| 273 | prerender: true, |
| 274 | })) as PlatformShape; |
| 275 | expect(() => platform.env["FROM_PROXY"]).toThrow( |
| 276 | "Cannot access platform.env.FROM_PROXY in a prerenderable route", |
| 277 | ); |
| 278 | expect(() => platform.env["LITERAL"]).toThrow( |
| 279 | "Cannot access platform.env.LITERAL in a prerenderable route", |
| 280 | ); |
| 281 | }); |
| 282 | |
| 283 | it("dispose tears down an opened proxy and is a no-op before open", async () => { |
| 284 | const untouched = makeFakeProxy({}); |
| 285 | const idle = makeDevPlatformEmulator({ openProxy: untouched.openProxy }); |
| 286 | await idle.dispose(); |
| 287 | expect(untouched.opens).toHaveLength(0); |
| 288 | expect(untouched.disposals()).toBe(0); |
| 289 | |
| 290 | const fake = makeFakeProxy({}); |
| 291 | const emulator = makeDevPlatformEmulator({ openProxy: fake.openProxy }); |
| 292 | await emulator.platform?.({ config: {}, prerender: false }); |
| 293 | await emulator.dispose(); |
| 294 | expect(fake.disposals()).toBe(1); |
| 295 | }); |
| 296 | }); |
| 297 | |
| 298 | describe("makeCloudflareAdapter emulation", () => { |
| 299 | it("serves the proxy-backed platform when dev platform options are present", async () => { |
| 300 | const fake = makeFakeProxy({ FROM_PROXY: "proxied" }); |
| 301 | const adapter = makeCloudflareAdapter({ |
| 302 | platform: { bindings: ["hook"], openProxy: fake.openProxy }, |
| 303 | }); |
| 304 | const emulator = await adapter.emulate?.(); |
| 305 | const platform = (await emulator?.platform?.({ |
| 306 | config: {}, |
| 307 | prerender: false, |
| 308 | })) as PlatformShape; |
| 309 | expect(platform.env["FROM_PROXY"]).toBe("proxied"); |
| 310 | await adapter.dispose(); |
| 311 | expect(fake.disposals()).toBe(1); |
| 312 | }); |
| 313 | |
| 314 | it("memoizes the emulator across emulate() calls", async () => { |
| 315 | const fake = makeFakeProxy({}); |
| 316 | const adapter = makeCloudflareAdapter({ |
| 317 | platform: { openProxy: fake.openProxy }, |
| 318 | }); |
| 319 | const first = await adapter.emulate?.(); |
| 320 | const second = await adapter.emulate?.(); |
| 321 | expect(first).toBe(second); |
| 322 | await first?.platform?.({ config: {}, prerender: false }); |
| 323 | await second?.platform?.({ config: {}, prerender: false }); |
| 324 | expect(fake.opens).toHaveLength(1); |
| 325 | }); |
| 326 | |
| 327 | it("serves an inert empty platform for production builds (no proxy)", async () => { |
| 328 | const adapter = makeCloudflareAdapter({}); |
| 329 | const emulator = await adapter.emulate?.(); |
| 330 | const platform = (await emulator?.platform?.({ |
| 331 | config: {}, |
| 332 | prerender: false, |
| 333 | })) as PlatformShape & { |
| 334 | caches: { default: { match: () => Promise<undefined> } }; |
| 335 | }; |
| 336 | expect(platform.env).toEqual({}); |
| 337 | expect(platform.cf).toEqual({}); |
| 338 | await expect(platform.caches.default.match()).resolves.toBeUndefined(); |
| 339 | // disposing without a proxy is a no-op |
| 340 | await adapter.dispose(); |
| 341 | }); |
| 342 | }); |