The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeFs from "node:fs/promises"; |
| 2 | import * as NodeHttp from "node:http"; |
| 3 | import type { AddressInfo } from "node:net"; |
| 4 | import * as NodeOs from "node:os"; |
| 5 | import * as NodePath from "node:path"; |
| 6 | import * as vite from "vite"; |
| 7 | import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest"; |
| 8 | import cloudflareVitePlugin from "../plugin.ts"; |
| 9 | import type { PreviewWorkerBuild } from "../preview-server.ts"; |
| 10 | |
| 11 | // Stand-in for the workerd preview runtime: a plain HTTP server; every |
| 12 | // proxied request responds with "worker". |
| 13 | let runtimeAddress: string; |
| 14 | let startedBuilds: Array<PreviewWorkerBuild> = []; |
| 15 | let closed = 0; |
| 16 | |
| 17 | vi.mock("../preview-server.ts", () => ({ |
| 18 | startPreviewServer: async (_options: unknown, build: PreviewWorkerBuild) => { |
| 19 | startedBuilds.push(build); |
| 20 | return { |
| 21 | address: new URL(runtimeAddress), |
| 22 | proxySharedSecret: "preview-secret", |
| 23 | close: async () => { |
| 24 | closed += 1; |
| 25 | }, |
| 26 | }; |
| 27 | }, |
| 28 | })); |
| 29 | |
| 30 | const listen = (server: NodeHttp.Server): Promise<string> => |
| 31 | new Promise((resolve, reject) => { |
| 32 | server.once("error", reject); |
| 33 | server.listen(0, "127.0.0.1", () => { |
| 34 | const { port } = server.address() as AddressInfo; |
| 35 | resolve(`http://127.0.0.1:${port}`); |
| 36 | }); |
| 37 | }); |
| 38 | |
| 39 | describe("preview plugin", () => { |
| 40 | let runtimeServer: NodeHttp.Server; |
| 41 | let root: string; |
| 42 | |
| 43 | beforeAll(async () => { |
| 44 | runtimeServer = NodeHttp.createServer((_req, res) => { |
| 45 | res.end("worker"); |
| 46 | }); |
| 47 | runtimeAddress = await listen(runtimeServer); |
| 48 | // A fake built project: the entry environment's outDir with the entry |
| 49 | // chunk (`index`, as a string `main` builds), and a client outDir with a |
| 50 | // static asset. |
| 51 | // Canonicalize the temp root: Windows may hand back an 8.3 short path |
| 52 | // (C:\Users\RUNNER~1\...) that the plugin keeps while realpath expands it. |
| 53 | root = await NodeFs.realpath( |
| 54 | await NodeFs.mkdtemp(NodePath.join(NodeOs.tmpdir(), "distilled-preview-")), |
| 55 | ); |
| 56 | await NodeFs.mkdir(NodePath.join(root, "dist", "ssr"), { recursive: true }); |
| 57 | await NodeFs.mkdir(NodePath.join(root, "dist", "client"), { |
| 58 | recursive: true, |
| 59 | }); |
| 60 | await NodeFs.writeFile( |
| 61 | NodePath.join(root, "dist", "ssr", "index.js"), |
| 62 | "export default { fetch: () => new Response('built worker') };", |
| 63 | ); |
| 64 | await NodeFs.writeFile(NodePath.join(root, "dist", "client", "index.html"), "<html></html>"); |
| 65 | }); |
| 66 | |
| 67 | afterAll(async () => { |
| 68 | runtimeServer.closeAllConnections(); |
| 69 | await new Promise((resolve) => runtimeServer.close(resolve)); |
| 70 | await NodeFs.rm(root, { recursive: true, force: true }); |
| 71 | }); |
| 72 | |
| 73 | afterEach(() => { |
| 74 | startedBuilds = []; |
| 75 | closed = 0; |
| 76 | }); |
| 77 | |
| 78 | const createPreviewServer = (plugins: vite.PluginOption) => |
| 79 | vite.preview({ |
| 80 | configFile: false, |
| 81 | root, |
| 82 | logLevel: "silent", |
| 83 | plugins: [plugins], |
| 84 | preview: { port: 0, host: "127.0.0.1" }, |
| 85 | }); |
| 86 | |
| 87 | const serverOrigin = (server: vite.PreviewServer): string => { |
| 88 | const { port } = server.httpServer.address() as AddressInfo; |
| 89 | return `http://127.0.0.1:${port}`; |
| 90 | }; |
| 91 | |
| 92 | it("serves the built worker through the runtime proxy", async () => { |
| 93 | const server = await createPreviewServer(cloudflareVitePlugin({ main: "./worker-entry.ts" })); |
| 94 | try { |
| 95 | const response = await fetch(`${serverOrigin(server)}/anything`); |
| 96 | expect(response.status).toBe(200); |
| 97 | expect(await response.text()).toBe("worker"); |
| 98 | } finally { |
| 99 | await server.close(); |
| 100 | } |
| 101 | }); |
| 102 | |
| 103 | it("resolves the built worker output from the entry environment", async () => { |
| 104 | const server = await createPreviewServer(cloudflareVitePlugin({ main: "./worker-entry.ts" })); |
| 105 | try { |
| 106 | expect(startedBuilds).toHaveLength(1); |
| 107 | const build = startedBuilds[0]!; |
| 108 | expect(build.directory).toBe(await NodeFs.realpath(NodePath.join(root, "dist", "ssr"))); |
| 109 | expect(build.entryModule).toBe("index.js"); |
| 110 | expect(build.assetsDirectory).toBe( |
| 111 | await NodeFs.realpath(NodePath.join(root, "dist", "client")), |
| 112 | ); |
| 113 | } finally { |
| 114 | await server.close(); |
| 115 | } |
| 116 | }); |
| 117 | |
| 118 | it("closes the runtime when the preview server closes", async () => { |
| 119 | const server = await createPreviewServer(cloudflareVitePlugin({ main: "./worker-entry.ts" })); |
| 120 | await server.close(); |
| 121 | expect(closed).toBe(1); |
| 122 | }); |
| 123 | |
| 124 | it("stays out of the way in SPA mode (no worker entry)", async () => { |
| 125 | // With no Worker, the client builds to Vite's own outDir, as with |
| 126 | // `@cloudflare/vite-plugin`. |
| 127 | await NodeFs.writeFile(NodePath.join(root, "dist", "index.html"), "<html></html>"); |
| 128 | const server = await createPreviewServer(cloudflareVitePlugin({})); |
| 129 | try { |
| 130 | expect(startedBuilds).toHaveLength(0); |
| 131 | // Vite's own static serving handles the request. |
| 132 | const response = await fetch(`${serverOrigin(server)}/index.html`); |
| 133 | expect(response.status).toBe(200); |
| 134 | expect(await response.text()).toContain("<html>"); |
| 135 | } finally { |
| 136 | await server.close(); |
| 137 | } |
| 138 | }); |
| 139 | |
| 140 | it("fails fast when the built entry chunk is missing", async () => { |
| 141 | const entry = NodePath.join(root, "dist", "ssr", "index.js"); |
| 142 | await NodeFs.rename(entry, `${entry}.bak`); |
| 143 | try { |
| 144 | await expect( |
| 145 | createPreviewServer(cloudflareVitePlugin({ main: "./worker-entry.ts" })), |
| 146 | ).rejects.toThrow(/index\.js/); |
| 147 | } finally { |
| 148 | await NodeFs.rename(`${entry}.bak`, entry); |
| 149 | } |
| 150 | }); |
| 151 | }); |