The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as FileSystem from "effect/FileSystem"; |
| 4 | import * as Path from "effect/Path"; |
| 5 | import type * as Scope from "effect/Scope"; |
| 6 | import { describe, expect, it } from "vitest"; |
| 7 | import { NODE_BUNDLE_CONDITIONS, NODE_SERVE_ENTRY_FILE_NAME } from "../../core/NodeServe.ts"; |
| 8 | import { SERVER_ENTRY_FILE_NAME, makeNodeTarget, target } from "../node.ts"; |
| 9 | import { DEFAULT_SERVER_BUILD_FILE } from "../ReactRouter.ts"; |
| 10 | |
| 11 | const runWithNode = <A, E>( |
| 12 | effect: Effect.Effect<A, E, FileSystem.FileSystem | Path.Path | Scope.Scope>, |
| 13 | ): Promise<A> => |
| 14 | Effect.runPromise( |
| 15 | Effect.scoped(effect).pipe(Effect.provide(NodeServices.layer)) as Effect.Effect<A, E>, |
| 16 | ); |
| 17 | |
| 18 | describe("makeNodeTarget", () => { |
| 19 | it("declares the node platform, Node bundle conditions, and a finish pass", () => { |
| 20 | const node = makeNodeTarget(); |
| 21 | expect(node.platform).toBe("node"); |
| 22 | expect(node.serverEntryFileName).toBe(DEFAULT_SERVER_BUILD_FILE); |
| 23 | expect(SERVER_ENTRY_FILE_NAME).toBe("index.js"); |
| 24 | expect(node.bundle?.conditions).toEqual([...NODE_BUNDLE_CONDITIONS]); |
| 25 | expect(node.bundle?.external ?? []).not.toContain("cloudflare:"); |
| 26 | expect(node.bundle?.external ?? []).not.toContain("@aws-sdk/"); |
| 27 | expect(node.bundle?.conditions ?? []).not.toContain("workerd"); |
| 28 | expect(node.finish).toBeTypeOf("function"); |
| 29 | expect(node.build).toBeTypeOf("function"); |
| 30 | }); |
| 31 | |
| 32 | it("carries the buildDirectory override on its config", () => { |
| 33 | expect(makeNodeTarget({ buildDirectory: "dist" }).config.buildDirectory).toBe("dist"); |
| 34 | }); |
| 35 | |
| 36 | it("exposes the named `target` module export as the factory", () => { |
| 37 | expect(target).toBe(makeNodeTarget); |
| 38 | }); |
| 39 | }); |
| 40 | |
| 41 | describe("finish", () => { |
| 42 | it("emits a Node serve entry around the fetch handler, not a Lambda adapter", async () => { |
| 43 | const output = await runWithNode( |
| 44 | Effect.gen(function* () { |
| 45 | const fs = yield* FileSystem.FileSystem; |
| 46 | const path = yield* Path.Path; |
| 47 | const dir = yield* fs.makeTempDirectoryScoped({ |
| 48 | prefix: "react-router-node-finish-", |
| 49 | }); |
| 50 | const serverDir = path.join(dir, "server"); |
| 51 | const clientDir = path.join(dir, "client"); |
| 52 | yield* fs.makeDirectory(serverDir, { recursive: true }); |
| 53 | yield* fs.makeDirectory(clientDir, { recursive: true }); |
| 54 | yield* fs.writeFileString( |
| 55 | path.join(serverDir, SERVER_ENTRY_FILE_NAME), |
| 56 | "export default { fetch() {} };", |
| 57 | ); |
| 58 | const node = makeNodeTarget(); |
| 59 | return yield* node.finish!( |
| 60 | { |
| 61 | distDirectory: dir, |
| 62 | clientDirectory: clientDir, |
| 63 | serverModules: [], |
| 64 | externalWorkspaces: new Set<string>(), |
| 65 | }, |
| 66 | { |
| 67 | root: dir, |
| 68 | framework: "react-router", |
| 69 | entry: path.join(serverDir, SERVER_ENTRY_FILE_NAME), |
| 70 | }, |
| 71 | ); |
| 72 | }), |
| 73 | ); |
| 74 | const names = output.serverModules?.map((module) => module.name) ?? []; |
| 75 | expect(names[0]).toBe(`server/${NODE_SERVE_ENTRY_FILE_NAME}`); |
| 76 | const serve = output.serverModules?.[0]; |
| 77 | const source = String(serve?.content); |
| 78 | expect(source).toContain("/health"); |
| 79 | expect(source).toContain("process.env.PORT"); |
| 80 | expect(source).toContain(`from "./${SERVER_ENTRY_FILE_NAME}"`); |
| 81 | expect(source).toContain("fetchHandler"); |
| 82 | expect(source).not.toContain("aws-lambda"); |
| 83 | expect(source).not.toContain("streamifyResponse"); |
| 84 | expect(source).not.toContain("workerd"); |
| 85 | expect(source).not.toContain("@aws-sdk/"); |
| 86 | }); |
| 87 | |
| 88 | it("fails when the framework produced no on-disk entry", async () => { |
| 89 | const error = await runWithNode( |
| 90 | Effect.gen(function* () { |
| 91 | const node = makeNodeTarget(); |
| 92 | return yield* node.finish!( |
| 93 | { |
| 94 | distDirectory: "/tmp", |
| 95 | clientDirectory: "/tmp/client", |
| 96 | serverModules: [], |
| 97 | externalWorkspaces: new Set<string>(), |
| 98 | }, |
| 99 | { root: "/tmp", framework: "react-router" }, |
| 100 | ).pipe(Effect.flip); |
| 101 | }), |
| 102 | ); |
| 103 | expect(error._tag).toBe("DeployTargetError"); |
| 104 | expect(error.message).toContain("no on-disk server entry"); |
| 105 | }); |
| 106 | }); |