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 NodeChildProcess from "node:child_process"; |
| 2 | import * as NodeNet from "node:net"; |
| 3 | import { fileURLToPath } from "node:url"; |
| 4 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as FileSystem from "effect/FileSystem"; |
| 7 | import * as Path from "effect/Path"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import type * as Scope from "effect/Scope"; |
| 10 | import { describe, expect, it } from "vitest"; |
| 11 | import { makeNeonServeEntrySource } from "../../core/NeonServe.ts"; |
| 12 | import { NODE_BUNDLE_CONDITIONS, makeNodeServeEntrySource } from "../../core/NodeServe.ts"; |
| 13 | import { |
| 14 | NEXT_PRODUCTION_APP_SOURCE, |
| 15 | SERVER_ENTRY_NAME, |
| 16 | makeNodeTarget, |
| 17 | nextNodeServeHandler, |
| 18 | target, |
| 19 | } from "../node.ts"; |
| 20 | |
| 21 | describe("makeNodeTarget", () => { |
| 22 | it("declares the node platform and a wholesale next build (not OpenNext)", () => { |
| 23 | const node = makeNodeTarget(); |
| 24 | expect(node.platform).toBe("node"); |
| 25 | expect(node.build).toBeTypeOf("function"); |
| 26 | expect(node.bundle?.conditions).toEqual([...NODE_BUNDLE_CONDITIONS]); |
| 27 | expect(node.bundle?.external ?? []).not.toContain("cloudflare:"); |
| 28 | expect(node.bundle?.external ?? []).not.toContain("@aws-sdk/"); |
| 29 | }); |
| 30 | |
| 31 | it("starts Next from the built config before preparing", () => { |
| 32 | const source = NEXT_PRODUCTION_APP_SOURCE; |
| 33 | expect(source).toContain('path.join(dir, ".next", "required-server-files.json")'); |
| 34 | expect(source.indexOf("__NEXT_PRIVATE_STANDALONE_CONFIG")).toBeLessThan( |
| 35 | source.indexOf("next({ dev: false, dir })"), |
| 36 | ); |
| 37 | expect(source.indexOf("next({ dev: false, dir })")).toBeLessThan( |
| 38 | source.indexOf("await app.prepare()"), |
| 39 | ); |
| 40 | }); |
| 41 | |
| 42 | it("wires the built-config startup into the shared Node and Neon entries", () => { |
| 43 | const node = makeNodeServeEntrySource({ handler: nextNodeServeHandler }); |
| 44 | expect(SERVER_ENTRY_NAME).toBe("serve-node.mjs"); |
| 45 | expect(node).toContain(NEXT_PRODUCTION_APP_SOURCE); |
| 46 | expect(node).toContain('import * as fs from "node:fs"'); |
| 47 | expect(node).toContain("/health"); |
| 48 | expect(node).toContain("process.env.PORT"); |
| 49 | expect(node).not.toContain("opennext"); |
| 50 | expect(node).not.toContain("cloudflare"); |
| 51 | const neon = makeNeonServeEntrySource({ handler: nextNodeServeHandler }); |
| 52 | expect(neon).toContain(NEXT_PRODUCTION_APP_SOURCE); |
| 53 | }); |
| 54 | |
| 55 | it("exposes the named `target` module export as the factory", () => { |
| 56 | expect(target).toBe(makeNodeTarget); |
| 57 | }); |
| 58 | }); |
| 59 | |
| 60 | const fixture = (files: Record<string, string>) => |
| 61 | Effect.gen(function* () { |
| 62 | const fs = yield* FileSystem.FileSystem; |
| 63 | const path = yield* Path.Path; |
| 64 | const workspace = yield* Effect.sync(() => |
| 65 | fileURLToPath(new URL("../../../../../", import.meta.url)), |
| 66 | ); |
| 67 | const base = path.join(workspace, ".alchemy", "nextjs-node-tests"); |
| 68 | yield* fs.makeDirectory(base, { recursive: true }); |
| 69 | const root = yield* fs.makeTempDirectoryScoped({ directory: base, prefix: "nextjs-" }); |
| 70 | yield* fs.symlink( |
| 71 | path.join(workspace, "examples", "aws-website-nextjs", "node_modules"), |
| 72 | path.join(root, "node_modules"), |
| 73 | ); |
| 74 | for (const [name, contents] of Object.entries({ |
| 75 | "package.json": '{"private":true,"type":"module"}', |
| 76 | ...files, |
| 77 | })) { |
| 78 | const file = path.join(root, name); |
| 79 | yield* fs.makeDirectory(path.dirname(file), { recursive: true }); |
| 80 | yield* fs.writeFileString(file, contents); |
| 81 | } |
| 82 | return root; |
| 83 | }); |
| 84 | |
| 85 | const freePort = Effect.callback<number>((resume) => { |
| 86 | const server = NodeNet.createServer(); |
| 87 | server.listen(0, "127.0.0.1", () => { |
| 88 | const address = server.address() as NodeNet.AddressInfo; |
| 89 | server.close(() => resume(Effect.succeed(address.port))); |
| 90 | }); |
| 91 | }); |
| 92 | |
| 93 | const serve = (entry: string, port: number) => |
| 94 | Effect.acquireRelease( |
| 95 | Effect.sync(() => { |
| 96 | let output = ""; |
| 97 | const child = NodeChildProcess.spawn("node", [entry], { |
| 98 | env: { ...process.env, PORT: String(port), HOST: "127.0.0.1" }, |
| 99 | stdio: ["ignore", "pipe", "pipe"], |
| 100 | }); |
| 101 | child.stdout.on("data", (chunk) => (output += String(chunk))); |
| 102 | child.stderr.on("data", (chunk) => (output += String(chunk))); |
| 103 | return { child, output: () => output }; |
| 104 | }), |
| 105 | ({ child }) => Effect.sync(() => child.kill("SIGKILL")), |
| 106 | ); |
| 107 | |
| 108 | const run = <A, E>(effect: Effect.Effect<A, E, FileSystem.FileSystem | Path.Path | Scope.Scope>) => |
| 109 | Effect.runPromise(effect.pipe(Effect.scoped, Effect.provide(NodeServices.layer))); |
| 110 | |
| 111 | describe("Next.js Node serve entry", { concurrent: false }, () => { |
| 112 | it( |
| 113 | "serves a real build without reloading next.config.ts at runtime", |
| 114 | () => |
| 115 | run( |
| 116 | Effect.gen(function* () { |
| 117 | const fs = yield* FileSystem.FileSystem; |
| 118 | const path = yield* Path.Path; |
| 119 | const root = yield* fixture({ |
| 120 | "app/layout.jsx": |
| 121 | "export default function Layout({children}) {return <html><body>{children}</body></html>}", |
| 122 | "app/page.jsx": "export default function Home() {return <h1>Node Next</h1>}", |
| 123 | "app/api/echo/route.js": |
| 124 | "export async function POST(request) {return Response.json({value:await request.text()})}", |
| 125 | "next.config.ts": |
| 126 | 'import type { NextConfig } from "next";\nconst config: NextConfig = { basePath: "/app", experimental: { cpus: 1 } };\nexport default config;\n', |
| 127 | }); |
| 128 | const output = yield* makeNodeTarget().build!({ root, framework: "nextjs" }); |
| 129 | expect(output.serverModules?.[0]?.name).toBe(SERVER_ENTRY_NAME); |
| 130 | // Any runtime load of the config now fails the server loudly. |
| 131 | yield* fs.writeFileString( |
| 132 | path.join(root, "next.config.ts"), |
| 133 | 'throw new Error("next.config.ts was reloaded at runtime");\n', |
| 134 | ); |
| 135 | const port = yield* freePort; |
| 136 | const server = yield* serve(path.join(root, SERVER_ENTRY_NAME), port); |
| 137 | const get = (pathname: string, init?: RequestInit) => |
| 138 | Effect.tryPromise(() => fetch(`http://127.0.0.1:${port}${pathname}`, init)); |
| 139 | const health = yield* get("/health").pipe( |
| 140 | Effect.retry({ |
| 141 | schedule: Schedule.spaced("250 millis"), |
| 142 | times: 80, |
| 143 | while: () => server.child.exitCode === null, |
| 144 | }), |
| 145 | Effect.mapError( |
| 146 | (cause) => |
| 147 | new Error(`serve-node.mjs never became ready:\n${server.output()}`, { cause }), |
| 148 | ), |
| 149 | ); |
| 150 | expect(health.status).toBe(200); |
| 151 | const page = yield* get("/app"); |
| 152 | const html = yield* Effect.tryPromise(() => page.text()); |
| 153 | expect(page.status, server.output()).toBe(200); |
| 154 | expect(html).toContain("Node Next"); |
| 155 | const post = yield* get("/app/api/echo", { method: "POST", body: "value" }); |
| 156 | expect(post.status).toBe(200); |
| 157 | expect(yield* Effect.tryPromise(() => post.json())).toEqual({ value: "value" }); |
| 158 | expect(server.output()).not.toContain("reloaded at runtime"); |
| 159 | }), |
| 160 | ), |
| 161 | 180_000, |
| 162 | ); |
| 163 | it( |
| 164 | 'rejects `output: "export"` at build time', |
| 165 | () => |
| 166 | run( |
| 167 | Effect.gen(function* () { |
| 168 | const root = yield* fixture({ |
| 169 | "app/layout.jsx": |
| 170 | "export default function Layout({children}) {return <html><body>{children}</body></html>}", |
| 171 | "app/page.jsx": "export default function Home() {return <h1>Static Next</h1>}", |
| 172 | "next.config.mjs": 'export default { output: "export", experimental: { cpus: 1 } };', |
| 173 | }); |
| 174 | const error = yield* makeNodeTarget().build!({ root, framework: "nextjs" }).pipe( |
| 175 | Effect.flip, |
| 176 | ); |
| 177 | expect(error.message).toContain('`output: "export"`'); |
| 178 | expect(error.message).toContain("cannot run on the Node target"); |
| 179 | }), |
| 180 | ), |
| 181 | 180_000, |
| 182 | ); |
| 183 | }); |