| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { |
| 3 | makeCloudflareTarget, |
| 4 | NITRO_HANDLER_SPECIFIER, |
| 5 | NITRO_PRESET, |
| 6 | } from "../cloudflare.ts"; |
| 7 | import type { NitroConfigSlice } from "../UserConfig.ts"; |
| 8 | |
| 9 | describe("makeCloudflareTarget", () => { |
| 10 | it("declares the cloudflare platform, the nitro preset, and workerd bundle settings", () => { |
| 11 | const target = makeCloudflareTarget(); |
| 12 | expect(target.platform).toBe("cloudflare"); |
| 13 | expect(target.nitroPreset).toBe(NITRO_PRESET); |
| 14 | expect(target.bundle?.conditions).toEqual([ |
| 15 | "workerd", |
| 16 | "worker", |
| 17 | "module", |
| 18 | "browser", |
| 19 | ]); |
| 20 | expect(target.bundle?.external).toEqual(["cloudflare:"]); |
| 21 | // No wholesale build and no finishing pass: nitro's cloudflare_module |
| 22 | // output is already workerd ESM. |
| 23 | expect(target.build).toBeUndefined(); |
| 24 | expect(target.finish).toBeUndefined(); |
| 25 | }); |
| 26 | |
| 27 | it("surfaces a configured main through the generic user-entry carriage", () => { |
| 28 | expect(makeCloudflareTarget().entry).toBeUndefined(); |
| 29 | expect(makeCloudflareTarget({ main: "./worker-entry.ts" }).entry).toEqual({ |
| 30 | main: "./worker-entry.ts", |
| 31 | }); |
| 32 | }); |
| 33 | |
| 34 | it("enforces wrangler-free nitro config: deployConfig off, nodeCompat on", () => { |
| 35 | const target = makeCloudflareTarget(); |
| 36 | const config: NitroConfigSlice = {}; |
| 37 | target.configureNitro?.(config, { root: "/project" }); |
| 38 | expect(config.cloudflare).toEqual({ |
| 39 | deployConfig: false, |
| 40 | nodeCompat: true, |
| 41 | }); |
| 42 | expect(config.entry).toBeUndefined(); |
| 43 | }); |
| 44 | |
| 45 | it("preserves foreign cloudflare keys while overriding the owned ones", () => { |
| 46 | const target = makeCloudflareTarget(); |
| 47 | const config: NitroConfigSlice = { |
| 48 | cloudflare: { deployConfig: true, pages: { routes: [] } }, |
| 49 | }; |
| 50 | target.configureNitro?.(config, { root: "/project" }); |
| 51 | expect(config.cloudflare).toEqual({ |
| 52 | deployConfig: false, |
| 53 | nodeCompat: true, |
| 54 | pages: { routes: [] }, |
| 55 | }); |
| 56 | }); |
| 57 | |
| 58 | it("honors an explicit nodeCompat opt-out", () => { |
| 59 | const target = makeCloudflareTarget({ nodeCompat: false }); |
| 60 | const config: NitroConfigSlice = {}; |
| 61 | target.configureNitro?.(config, { root: "/project" }); |
| 62 | expect(config.cloudflare).toEqual({ |
| 63 | deployConfig: false, |
| 64 | nodeCompat: false, |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | it("does NOT wire the user entry at the config level (prerender-clone safety)", () => { |
| 69 | // The framework package sets the entry on the nitro INSTANCE at |
| 70 | // `nitro:init`; a config-level entry would leak into the prerenderer's |
| 71 | // Node-preset clone, which cannot load `cloudflare:` imports. |
| 72 | const target = makeCloudflareTarget({ main: "./worker-entry.ts" }); |
| 73 | const config: NitroConfigSlice = {}; |
| 74 | target.configureNitro?.(config, { |
| 75 | root: "/project", |
| 76 | entry: "/project/worker-entry.ts", |
| 77 | }); |
| 78 | expect(config.entry).toBeUndefined(); |
| 79 | }); |
| 80 | |
| 81 | it("exports the wrappable-handler specifier for user entries", () => { |
| 82 | expect(NITRO_HANDLER_SPECIFIER).toBe( |
| 83 | "nitropack/presets/cloudflare/runtime/cloudflare-module", |
| 84 | ); |
| 85 | }); |
| 86 | }); |