The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as fs from "node:fs/promises"; |
| 2 | import * as path from "node:path"; |
| 3 | import { pathToFileURL } from "node:url"; |
| 4 | import * as vite from "vite"; |
| 5 | import { expect, onTestFinished, test, vi } from "vitest"; |
| 6 | import * as Assets from "../../core/bindings/assets/Assets.ts"; |
| 7 | import cloudflareVitePlugin from "../plugin.ts"; |
| 8 | |
| 9 | test.each([ |
| 10 | ["selective Worker routes", ["/api/*"]], |
| 11 | ["explicitly excluded internal routes", ["/api/*", "!/cdn-cgi/alchemy/module-runner/*"]], |
| 12 | ["assets first", false], |
| 13 | ["Worker first", true], |
| 14 | ] as const)("starts the module runner with SPA fallback and %s", async (_, runWorkerFirst) => { |
| 15 | onTestFinished(() => { |
| 16 | vi.unstubAllEnvs(); |
| 17 | }); |
| 18 | // These tests use only the local runtime; no Cloudflare API calls are needed. |
| 19 | vi.stubEnv("CLOUDFLARE_API_TOKEN", "local-test-unused"); |
| 20 | vi.stubEnv("CLOUDFLARE_ACCOUNT_ID", "00000000000000000000000000000000"); |
| 21 | const tmpRoot = path.resolve(import.meta.dirname, "../.cache/test-roots"); |
| 22 | await fs.mkdir(tmpRoot, { recursive: true }); |
| 23 | const root = await fs.mkdtemp(path.join(tmpRoot, "dev-assets-")); |
| 24 | onTestFinished(() => fs.rm(root, { recursive: true, force: true })); |
| 25 | const entry = path.join(root, "worker.js"); |
| 26 | await fs.writeFile(path.join(root, "index.html"), "<html><body>SPA shell</body></html>"); |
| 27 | await fs.writeFile( |
| 28 | entry, |
| 29 | ` |
| 30 | export default { |
| 31 | fetch(request, env) { |
| 32 | if (new URL(request.url).pathname.startsWith("/api/")) { |
| 33 | return new Response("api"); |
| 34 | } |
| 35 | return env.ASSETS.fetch(request); |
| 36 | }, |
| 37 | }; |
| 38 | `, |
| 39 | ); |
| 40 | const server = await vite.createServer({ |
| 41 | root, |
| 42 | configFile: false, |
| 43 | cacheDir: path.join(root, ".vite"), |
| 44 | logLevel: "silent", |
| 45 | server: { host: "127.0.0.1", port: 0 }, |
| 46 | plugins: [ |
| 47 | cloudflareVitePlugin({ |
| 48 | main: entry, |
| 49 | worker: { |
| 50 | name: "vite-dev-assets-test", |
| 51 | bindings: [Assets.local("ASSETS")], |
| 52 | assets: { |
| 53 | runWorkerFirst: |
| 54 | typeof runWorkerFirst === "boolean" ? runWorkerFirst : [...runWorkerFirst], |
| 55 | notFoundHandling: "single-page-application", |
| 56 | }, |
| 57 | }, |
| 58 | }), |
| 59 | ], |
| 60 | }); |
| 61 | onTestFinished(() => server.close()); |
| 62 | await server.listen(); |
| 63 | const url = server.resolvedUrls!.local[0]; |
| 64 | const page = await fetch(new URL("/dashboard", url), { |
| 65 | signal: AbortSignal.timeout(5_000), |
| 66 | }); |
| 67 | expect(page.status).toBe(200); |
| 68 | expect(await page.text()).toContain("SPA shell"); |
| 69 | if (runWorkerFirst !== false) { |
| 70 | const api = await fetch(new URL("/api/hello", url), { |
| 71 | signal: AbortSignal.timeout(5_000), |
| 72 | }); |
| 73 | expect(api.status).toBe(200); |
| 74 | expect(await api.text()).toBe("api"); |
| 75 | } |
| 76 | }); |
| 77 | |
| 78 | test("a Worker reads HTML and binary files in Vite's publicDir through ASSETS", async () => { |
| 79 | onTestFinished(() => { |
| 80 | vi.unstubAllEnvs(); |
| 81 | }); |
| 82 | vi.stubEnv("CLOUDFLARE_API_TOKEN", "local-test-unused"); |
| 83 | vi.stubEnv("CLOUDFLARE_ACCOUNT_ID", "00000000000000000000000000000000"); |
| 84 | const tmpRoot = path.resolve(import.meta.dirname, "../.cache/test-roots"); |
| 85 | await fs.mkdir(tmpRoot, { recursive: true }); |
| 86 | const root = await fs.mkdtemp(path.join(tmpRoot, "dev-assets-public-")); |
| 87 | onTestFinished(() => fs.rm(root, { recursive: true, force: true })); |
| 88 | const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0xff]); |
| 89 | await fs.mkdir(path.join(root, "public")); |
| 90 | await fs.writeFile( |
| 91 | path.join(root, "public", "about.html"), |
| 92 | "<html><body>public page</body></html>", |
| 93 | ); |
| 94 | await fs.writeFile(path.join(root, "public", "og.png"), png); |
| 95 | const entry = path.join(root, "worker.js"); |
| 96 | await fs.writeFile( |
| 97 | entry, |
| 98 | ` |
| 99 | export default { |
| 100 | fetch(request, env) { |
| 101 | const { pathname } = new URL(request.url); |
| 102 | return env.ASSETS.fetch(new URL(pathname.slice("/read".length), request.url)); |
| 103 | }, |
| 104 | }; |
| 105 | `, |
| 106 | ); |
| 107 | const server = await vite.createServer({ |
| 108 | root, |
| 109 | configFile: false, |
| 110 | cacheDir: path.join(root, ".vite"), |
| 111 | logLevel: "silent", |
| 112 | server: { host: "127.0.0.1", port: 0 }, |
| 113 | plugins: [ |
| 114 | cloudflareVitePlugin({ |
| 115 | main: entry, |
| 116 | worker: { |
| 117 | name: "vite-dev-assets-public-test", |
| 118 | bindings: [Assets.local("ASSETS")], |
| 119 | assets: { runWorkerFirst: ["/read/*"] }, |
| 120 | }, |
| 121 | }), |
| 122 | ], |
| 123 | }); |
| 124 | onTestFinished(() => server.close()); |
| 125 | await server.listen(); |
| 126 | const url = server.resolvedUrls!.local[0]; |
| 127 | |
| 128 | const page = await fetch(new URL("/read/about", url), { signal: AbortSignal.timeout(5_000) }); |
| 129 | expect(page.status).toBe(200); |
| 130 | expect(page.headers.get("content-type")).toContain("text/html"); |
| 131 | expect(await page.text()).toBe("<html><body>public page</body></html>"); |
| 132 | |
| 133 | const image = await fetch(new URL("/read/og.png", url), { signal: AbortSignal.timeout(5_000) }); |
| 134 | expect(image.status).toBe(200); |
| 135 | expect(image.headers.get("content-type")).toBe("image/png"); |
| 136 | expect(Buffer.from(await image.arrayBuffer())).toEqual(png); |
| 137 | }); |
| 138 | |
| 139 | // https://github.com/alchemy-run/alchemy/issues/2250 |
| 140 | test("a Worker reads every byte of a publicDir file through ASSETS with SPA fallback", async () => { |
| 141 | onTestFinished(() => { |
| 142 | vi.unstubAllEnvs(); |
| 143 | }); |
| 144 | vi.stubEnv("CLOUDFLARE_API_TOKEN", "local-test-unused"); |
| 145 | vi.stubEnv("CLOUDFLARE_ACCOUNT_ID", "00000000000000000000000000000000"); |
| 146 | const tmpRoot = path.resolve(import.meta.dirname, "../.cache/test-roots"); |
| 147 | await fs.mkdir(tmpRoot, { recursive: true }); |
| 148 | const root = await fs.mkdtemp(path.join(tmpRoot, "dev-assets-binary-")); |
| 149 | onTestFinished(() => fs.rm(root, { recursive: true, force: true })); |
| 150 | const bytes = Buffer.from(Array.from({ length: 256 }, (_, i) => i)); |
| 151 | await fs.writeFile(path.join(root, "index.html"), "<html><body>SPA shell</body></html>"); |
| 152 | await fs.mkdir(path.join(root, "public")); |
| 153 | await fs.writeFile(path.join(root, "public", "sample.bin"), bytes); |
| 154 | const entry = path.join(root, "worker.js"); |
| 155 | await fs.writeFile( |
| 156 | entry, |
| 157 | ` |
| 158 | export default { |
| 159 | fetch(request, env) { |
| 160 | return env.ASSETS.fetch(new Request(new URL("/sample.bin", request.url), request)); |
| 161 | }, |
| 162 | }; |
| 163 | `, |
| 164 | ); |
| 165 | const server = await vite.createServer({ |
| 166 | root, |
| 167 | configFile: false, |
| 168 | cacheDir: path.join(root, ".vite"), |
| 169 | logLevel: "silent", |
| 170 | server: { host: "127.0.0.1", port: 0 }, |
| 171 | plugins: [ |
| 172 | cloudflareVitePlugin({ |
| 173 | main: entry, |
| 174 | worker: { |
| 175 | name: "vite-dev-assets-binary-test", |
| 176 | bindings: [Assets.local("ASSETS")], |
| 177 | assets: { runWorkerFirst: true, notFoundHandling: "single-page-application" }, |
| 178 | }, |
| 179 | }), |
| 180 | ], |
| 181 | }); |
| 182 | onTestFinished(() => server.close()); |
| 183 | await server.listen(); |
| 184 | const url = server.resolvedUrls!.local[0]; |
| 185 | |
| 186 | const response = await fetch(new URL("/dashboard", url), { signal: AbortSignal.timeout(5_000) }); |
| 187 | expect(response.status).toBe(200); |
| 188 | expect(response.headers.get("content-type")).toBe("application/octet-stream"); |
| 189 | expect(Buffer.from(await response.arrayBuffer())).toEqual(bytes); |
| 190 | }); |
| 191 | |
| 192 | // Vite resolves a disabled `publicDir` to `""`, which must not turn a |
| 193 | // requested pathname into an absolute path on the host's filesystem. |
| 194 | test("a Worker cannot read files outside the root through ASSETS when publicDir is disabled", async () => { |
| 195 | onTestFinished(() => { |
| 196 | vi.unstubAllEnvs(); |
| 197 | }); |
| 198 | vi.stubEnv("CLOUDFLARE_API_TOKEN", "local-test-unused"); |
| 199 | vi.stubEnv("CLOUDFLARE_ACCOUNT_ID", "00000000000000000000000000000000"); |
| 200 | const tmpRoot = path.resolve(import.meta.dirname, "../.cache/test-roots"); |
| 201 | await fs.mkdir(tmpRoot, { recursive: true }); |
| 202 | const root = await fs.mkdtemp(path.join(tmpRoot, "dev-assets-no-public-")); |
| 203 | const outside = await fs.mkdtemp(path.join(tmpRoot, "dev-assets-outside-")); |
| 204 | onTestFinished(() => fs.rm(root, { recursive: true, force: true })); |
| 205 | onTestFinished(() => fs.rm(outside, { recursive: true, force: true })); |
| 206 | const secret = path.join(outside, "secret.bin"); |
| 207 | await fs.writeFile(secret, "outside the root"); |
| 208 | const entry = path.join(root, "worker.js"); |
| 209 | await fs.writeFile( |
| 210 | entry, |
| 211 | ` |
| 212 | export default { |
| 213 | fetch(request, env) { |
| 214 | const { pathname } = new URL(request.url); |
| 215 | return env.ASSETS.fetch(new URL(pathname.slice("/read".length), request.url)); |
| 216 | }, |
| 217 | }; |
| 218 | `, |
| 219 | ); |
| 220 | const server = await vite.createServer({ |
| 221 | root, |
| 222 | configFile: false, |
| 223 | cacheDir: path.join(root, ".vite"), |
| 224 | publicDir: false, |
| 225 | logLevel: "silent", |
| 226 | server: { host: "127.0.0.1", port: 0 }, |
| 227 | plugins: [ |
| 228 | cloudflareVitePlugin({ |
| 229 | main: entry, |
| 230 | worker: { |
| 231 | name: "vite-dev-assets-no-public-test", |
| 232 | bindings: [Assets.local("ASSETS")], |
| 233 | assets: { runWorkerFirst: ["/read/*"] }, |
| 234 | }, |
| 235 | }), |
| 236 | ], |
| 237 | }); |
| 238 | onTestFinished(() => server.close()); |
| 239 | await server.listen(); |
| 240 | const url = server.resolvedUrls!.local[0]; |
| 241 | |
| 242 | const response = await fetch(new URL(`/read${pathToFileURL(secret).pathname}`, url), { |
| 243 | signal: AbortSignal.timeout(5_000), |
| 244 | }); |
| 245 | expect(response.status).toBe(404); |
| 246 | expect(await response.text()).not.toContain("outside the root"); |
| 247 | }); |
| 248 | |
| 249 | // `@cloudflare/vite-plugin` serves HTML through `transformIndexHtml` (`public/` |
| 250 | // HTML as is) and applies `server.headers` to asset responses. |
| 251 | test("serves HTML through Vite with the dev server's headers", async () => { |
| 252 | onTestFinished(() => { |
| 253 | vi.unstubAllEnvs(); |
| 254 | }); |
| 255 | vi.stubEnv("CLOUDFLARE_API_TOKEN", "local-test-unused"); |
| 256 | vi.stubEnv("CLOUDFLARE_ACCOUNT_ID", "00000000000000000000000000000000"); |
| 257 | const tmpRoot = path.resolve(import.meta.dirname, "../.cache/test-roots"); |
| 258 | await fs.mkdir(tmpRoot, { recursive: true }); |
| 259 | const root = await fs.mkdtemp(path.join(tmpRoot, "dev-assets-html-")); |
| 260 | onTestFinished(() => fs.rm(root, { recursive: true, force: true })); |
| 261 | await fs.writeFile(path.join(root, "index.html"), "<html><head></head><body>shell</body></html>"); |
| 262 | await fs.mkdir(path.join(root, "public")); |
| 263 | await fs.writeFile( |
| 264 | path.join(root, "public", "raw.html"), |
| 265 | "<html><head></head><body>raw</body></html>", |
| 266 | ); |
| 267 | const entry = path.join(root, "worker.js"); |
| 268 | await fs.writeFile( |
| 269 | entry, |
| 270 | `export default { fetch: (request, env) => env.ASSETS.fetch(request) };`, |
| 271 | ); |
| 272 | const server = await vite.createServer({ |
| 273 | root, |
| 274 | configFile: false, |
| 275 | cacheDir: path.join(root, ".vite"), |
| 276 | logLevel: "silent", |
| 277 | server: { host: "127.0.0.1", port: 0, headers: { "x-dev-header": "from-vite" } }, |
| 278 | plugins: [ |
| 279 | cloudflareVitePlugin({ |
| 280 | main: entry, |
| 281 | worker: { name: "vite-dev-assets-html-test", bindings: [Assets.local("ASSETS")] }, |
| 282 | }), |
| 283 | ], |
| 284 | }); |
| 285 | onTestFinished(() => server.close()); |
| 286 | await server.listen(); |
| 287 | const url = server.resolvedUrls!.local[0]; |
| 288 | |
| 289 | const index = await fetch(new URL("/", url), { signal: AbortSignal.timeout(5_000) }); |
| 290 | expect(index.status).toBe(200); |
| 291 | expect(index.headers.get("x-dev-header")).toBe("from-vite"); |
| 292 | expect(await index.text()).toContain("/@vite/client"); |
| 293 | |
| 294 | const raw = await fetch(new URL("/raw.html", url), { signal: AbortSignal.timeout(5_000) }); |
| 295 | expect(raw.status).toBe(200); |
| 296 | expect(raw.headers.get("x-dev-header")).toBe("from-vite"); |
| 297 | expect(await raw.text()).not.toContain("/@vite/client"); |
| 298 | }); |
| 299 | |
| 300 | // Additional modules load as Worker modules, not through the module runner, |
| 301 | // so editing one restarts the dev server, as `@cloudflare/vite-plugin` does. |
| 302 | test("restarts the dev server when an additional module changes", async () => { |
| 303 | onTestFinished(() => { |
| 304 | vi.unstubAllEnvs(); |
| 305 | }); |
| 306 | vi.stubEnv("CLOUDFLARE_API_TOKEN", "local-test-unused"); |
| 307 | vi.stubEnv("CLOUDFLARE_ACCOUNT_ID", "00000000000000000000000000000000"); |
| 308 | const tmpRoot = path.resolve(import.meta.dirname, "../.cache/test-roots"); |
| 309 | await fs.mkdir(tmpRoot, { recursive: true }); |
| 310 | const root = await fs.mkdtemp(path.join(tmpRoot, "dev-assets-module-")); |
| 311 | onTestFinished(() => fs.rm(root, { recursive: true, force: true })); |
| 312 | await fs.writeFile(path.join(root, "message.txt"), "one"); |
| 313 | const entry = path.join(root, "worker.js"); |
| 314 | await fs.writeFile( |
| 315 | entry, |
| 316 | `import message from "./message.txt";\nexport default { fetch: () => new Response(message) };`, |
| 317 | ); |
| 318 | const server = await vite.createServer({ |
| 319 | root, |
| 320 | configFile: false, |
| 321 | cacheDir: path.join(root, ".vite"), |
| 322 | logLevel: "silent", |
| 323 | server: { host: "127.0.0.1", port: 0 }, |
| 324 | plugins: [ |
| 325 | cloudflareVitePlugin({ main: entry, worker: { name: "vite-dev-module-test", bindings: [] } }), |
| 326 | ], |
| 327 | }); |
| 328 | onTestFinished(() => server.close()); |
| 329 | await server.listen(); |
| 330 | const url = server.resolvedUrls!.local[0]; |
| 331 | const first = await fetch(url, { signal: AbortSignal.timeout(10_000) }); |
| 332 | expect(await first.text()).toBe("one"); |
| 333 | const restart = vi.spyOn(server, "restart").mockResolvedValue(); |
| 334 | await fs.writeFile(path.join(root, "message.txt"), "two"); |
| 335 | await vi.waitFor(() => expect(restart).toHaveBeenCalled(), { timeout: 10_000 }); |
| 336 | }); |