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 { |
| 8 | generateLambdaEntry, |
| 9 | LAMBDA_ADAPTER_FILE_NAME, |
| 10 | LAMBDA_ENTRY_FILE_NAME, |
| 11 | makeAwsTarget, |
| 12 | SERVER_ENTRY_FILE_NAME, |
| 13 | } from "../aws.ts"; |
| 14 | import { fromHarnessOptions } from "../index.ts"; |
| 15 | import { |
| 16 | DEFAULT_TARGET_SPECIFIER, |
| 17 | inlineBuildConfig, |
| 18 | make, |
| 19 | readTanStackStartOutput, |
| 20 | selectServerEntryName, |
| 21 | type TanStackStartTarget, |
| 22 | } from "../TanStackStart.ts"; |
| 23 | |
| 24 | const runWithNode = <A, E>( |
| 25 | effect: Effect.Effect<A, E, FileSystem.FileSystem | Path.Path | Scope.Scope>, |
| 26 | ): Promise<A> => |
| 27 | Effect.runPromise( |
| 28 | Effect.scoped(effect).pipe(Effect.provide(NodeServices.layer)) as Effect.Effect<A, E>, |
| 29 | ); |
| 30 | |
| 31 | describe("readTanStackStartOutput", () => { |
| 32 | it("maps a dist tree onto the BuildOutput contract, entry first", async () => { |
| 33 | const output = await runWithNode( |
| 34 | Effect.gen(function* () { |
| 35 | const fs = yield* FileSystem.FileSystem; |
| 36 | const path = yield* Path.Path; |
| 37 | const dir = yield* fs.makeTempDirectoryScoped({ |
| 38 | prefix: "tanstack-start-output-", |
| 39 | }); |
| 40 | const serverDir = path.join(dir, "server"); |
| 41 | const clientDir = path.join(dir, "client"); |
| 42 | yield* fs.makeDirectory(path.join(serverDir, "assets"), { |
| 43 | recursive: true, |
| 44 | }); |
| 45 | yield* fs.makeDirectory(clientDir, { recursive: true }); |
| 46 | // alphabetically before server.js, to prove entry-first sorting |
| 47 | yield* fs.writeFileString( |
| 48 | path.join(serverDir, "assets", "router-abc.js"), |
| 49 | "export const a = 1;", |
| 50 | ); |
| 51 | yield* fs.writeFileString( |
| 52 | path.join(serverDir, "server.js"), |
| 53 | "export default { fetch() {} };", |
| 54 | ); |
| 55 | yield* fs.writeFileString(path.join(clientDir, "robots.txt"), "User-agent: *\n"); |
| 56 | return yield* readTanStackStartOutput({ dir, serverDir, clientDir }); |
| 57 | }), |
| 58 | ); |
| 59 | expect(output.serverModules?.map((module) => module.name)).toEqual([ |
| 60 | "server/server.js", |
| 61 | "server/assets/router-abc.js", |
| 62 | ]); |
| 63 | expect(output.serverModules?.[0]?.hash).toMatch(/^[0-9a-f]{64}$/); |
| 64 | expect(output.clientDirectory?.endsWith("client")).toBe(true); |
| 65 | expect(output.distDirectory).toBeDefined(); |
| 66 | expect(output.externalWorkspaces.size).toBe(0); |
| 67 | }); |
| 68 | |
| 69 | it("falls back to the single top-level module when the entry is renamed", async () => { |
| 70 | const output = await runWithNode( |
| 71 | Effect.gen(function* () { |
| 72 | const fs = yield* FileSystem.FileSystem; |
| 73 | const path = yield* Path.Path; |
| 74 | const dir = yield* fs.makeTempDirectoryScoped({ |
| 75 | prefix: "tanstack-start-output-", |
| 76 | }); |
| 77 | const serverDir = path.join(dir, "server"); |
| 78 | yield* fs.makeDirectory(path.join(serverDir, "assets"), { |
| 79 | recursive: true, |
| 80 | }); |
| 81 | yield* fs.writeFileString( |
| 82 | path.join(serverDir, "assets", "chunk-abc.js"), |
| 83 | "export const a = 1;", |
| 84 | ); |
| 85 | yield* fs.writeFileString( |
| 86 | path.join(serverDir, "entry-server.js"), |
| 87 | "export default { fetch() {} };", |
| 88 | ); |
| 89 | return yield* readTanStackStartOutput({ |
| 90 | dir, |
| 91 | serverDir, |
| 92 | clientDir: path.join(dir, "client"), |
| 93 | }); |
| 94 | }), |
| 95 | ); |
| 96 | expect(output.serverModules?.[0]?.name).toBe("server/entry-server.js"); |
| 97 | }); |
| 98 | |
| 99 | it("fails actionably when no server entry can be identified", async () => { |
| 100 | const error = await runWithNode( |
| 101 | Effect.gen(function* () { |
| 102 | const fs = yield* FileSystem.FileSystem; |
| 103 | const path = yield* Path.Path; |
| 104 | const dir = yield* fs.makeTempDirectoryScoped({ |
| 105 | prefix: "tanstack-start-output-", |
| 106 | }); |
| 107 | const serverDir = path.join(dir, "server"); |
| 108 | yield* fs.makeDirectory(serverDir, { recursive: true }); |
| 109 | yield* fs.writeFileString(path.join(serverDir, "one.js"), "export const a = 1;"); |
| 110 | yield* fs.writeFileString(path.join(serverDir, "two.js"), "export const b = 1;"); |
| 111 | return yield* readTanStackStartOutput({ |
| 112 | dir, |
| 113 | serverDir, |
| 114 | clientDir: path.join(dir, "client"), |
| 115 | }).pipe(Effect.flip); |
| 116 | }), |
| 117 | ); |
| 118 | expect(error._tag).toBe("FrameworkError"); |
| 119 | expect(error.message).toContain('no "server/server.js" entry'); |
| 120 | }); |
| 121 | |
| 122 | it("fails when the build produced no server modules", async () => { |
| 123 | const error = await runWithNode( |
| 124 | Effect.gen(function* () { |
| 125 | const fs = yield* FileSystem.FileSystem; |
| 126 | const path = yield* Path.Path; |
| 127 | const dir = yield* fs.makeTempDirectoryScoped({ |
| 128 | prefix: "tanstack-start-output-", |
| 129 | }); |
| 130 | const serverDir = path.join(dir, "server"); |
| 131 | yield* fs.makeDirectory(serverDir, { recursive: true }); |
| 132 | return yield* readTanStackStartOutput({ |
| 133 | dir, |
| 134 | serverDir, |
| 135 | clientDir: path.join(dir, "client"), |
| 136 | }).pipe(Effect.flip); |
| 137 | }), |
| 138 | ); |
| 139 | expect(error.message).toContain("no server modules"); |
| 140 | }); |
| 141 | }); |
| 142 | |
| 143 | describe("selectServerEntryName", () => { |
| 144 | it("prefers the expected name over a lone sibling", () => { |
| 145 | expect(selectServerEntryName(["server/server.js", "server/other.js"], "server/server.js")).toBe( |
| 146 | "server/server.js", |
| 147 | ); |
| 148 | }); |
| 149 | |
| 150 | it("ignores nested chunks when counting top-level modules", () => { |
| 151 | expect( |
| 152 | selectServerEntryName( |
| 153 | ["server/assets/a.js", "server/assets/b.js", "server/entry.js"], |
| 154 | "server/server.js", |
| 155 | ), |
| 156 | ).toBe("server/entry.js"); |
| 157 | }); |
| 158 | |
| 159 | it("is undefined when the choice is ambiguous", () => { |
| 160 | expect( |
| 161 | selectServerEntryName(["server/a.js", "server/b.js"], "server/server.js"), |
| 162 | ).toBeUndefined(); |
| 163 | }); |
| 164 | }); |
| 165 | |
| 166 | describe("inlineBuildConfig", () => { |
| 167 | it("forces a self-contained SSR bundle", () => { |
| 168 | const config = inlineBuildConfig("/app", undefined) as { |
| 169 | environments: { ssr: { resolve: { noExternal: boolean } } }; |
| 170 | build?: unknown; |
| 171 | root: string; |
| 172 | }; |
| 173 | // Vite externalizes node_modules from SSR bundles by default; a Lambda |
| 174 | // ships the server directory alone, so it must be self-contained. |
| 175 | expect(config.environments.ssr.resolve.noExternal).toBe(true); |
| 176 | expect(config.root).toBe("/app"); |
| 177 | expect(config.build).toBeUndefined(); |
| 178 | }); |
| 179 | |
| 180 | it("forwards an outDir override", () => { |
| 181 | const config = inlineBuildConfig("/app", "build") as { |
| 182 | build: { outDir: string }; |
| 183 | }; |
| 184 | expect(config.build.outDir).toBe("build"); |
| 185 | }); |
| 186 | }); |
| 187 | |
| 188 | describe("generateLambdaEntry", () => { |
| 189 | it("wraps the server entry's fetch handler for streaming by default", () => { |
| 190 | const source = generateLambdaEntry({ |
| 191 | streaming: true, |
| 192 | serverEntryFileName: SERVER_ENTRY_FILE_NAME, |
| 193 | }); |
| 194 | expect(source).toContain(`import * as serverEntry from "./server.js"`); |
| 195 | expect(source).toContain(`import { toLambdaHandler } from "./${LAMBDA_ADAPTER_FILE_NAME}"`); |
| 196 | expect(source).toContain("export const handler = toLambdaHandler("); |
| 197 | // A custom server entry may default-export the bare fetch function. |
| 198 | expect(source).toContain('typeof entry === "function"'); |
| 199 | }); |
| 200 | |
| 201 | it("emits the buffered wrapper when streaming is disabled", () => { |
| 202 | const source = generateLambdaEntry({ |
| 203 | streaming: false, |
| 204 | serverEntryFileName: "entry-server.js", |
| 205 | }); |
| 206 | expect(source).toContain("toBufferedLambdaHandler"); |
| 207 | expect(source).not.toContain("toLambdaHandler(fetchHandler)"); |
| 208 | expect(source).toContain(`from "./entry-server.js"`); |
| 209 | }); |
| 210 | }); |
| 211 | |
| 212 | describe("makeAwsTarget", () => { |
| 213 | it("declares the Lambda packaging seams", () => { |
| 214 | const target = makeAwsTarget(); |
| 215 | expect(target.platform).toBe("aws"); |
| 216 | expect(target.serverEntryFileName).toBe(SERVER_ENTRY_FILE_NAME); |
| 217 | expect(target.bundle?.conditions).toEqual(["node", "import", "module"]); |
| 218 | expect(target.bundle?.external).toEqual(["@aws-sdk/"]); |
| 219 | expect(typeof target.finish).toBe("function"); |
| 220 | // The wholesale build hook runs the build in a disposable child process. |
| 221 | expect(typeof target.build).toBe("function"); |
| 222 | }); |
| 223 | |
| 224 | it("carries the outDir override on its config", () => { |
| 225 | expect(makeAwsTarget({ outDir: "build" }).config.outDir).toBe("build"); |
| 226 | }); |
| 227 | }); |
| 228 | |
| 229 | describe("finish", () => { |
| 230 | it("emits the Lambda entry, the ESM marker, and the adapter copy", async () => { |
| 231 | const output = await runWithNode( |
| 232 | Effect.gen(function* () { |
| 233 | const fs = yield* FileSystem.FileSystem; |
| 234 | const path = yield* Path.Path; |
| 235 | const dir = yield* fs.makeTempDirectoryScoped({ |
| 236 | prefix: "tanstack-start-finish-", |
| 237 | }); |
| 238 | const serverDir = path.join(dir, "server"); |
| 239 | yield* fs.makeDirectory(serverDir, { recursive: true }); |
| 240 | yield* fs.writeFileString( |
| 241 | path.join(serverDir, SERVER_ENTRY_FILE_NAME), |
| 242 | "export default { fetch() {} };", |
| 243 | ); |
| 244 | const target = makeAwsTarget(); |
| 245 | return yield* target.finish!( |
| 246 | { |
| 247 | distDirectory: dir, |
| 248 | clientDirectory: path.join(dir, "client"), |
| 249 | serverModules: [], |
| 250 | externalWorkspaces: new Set<string>(), |
| 251 | }, |
| 252 | { |
| 253 | root: dir, |
| 254 | framework: "tanstack-start", |
| 255 | entry: path.join(serverDir, SERVER_ENTRY_FILE_NAME), |
| 256 | }, |
| 257 | ); |
| 258 | }), |
| 259 | ); |
| 260 | const names = output.serverModules?.map((module) => module.name) ?? []; |
| 261 | // The Lambda entry sorts first — it becomes the deployed handler module. |
| 262 | expect(names[0]).toBe(`server/${LAMBDA_ENTRY_FILE_NAME}`); |
| 263 | expect(names).toContain("server/package.json"); |
| 264 | expect(names).toContain(`server/${LAMBDA_ADAPTER_FILE_NAME}`); |
| 265 | expect(names).toContain(`server/${SERVER_ENTRY_FILE_NAME}`); |
| 266 | const manifest = output.serverModules?.find((module) => module.name === "server/package.json"); |
| 267 | expect(String(manifest?.content)).toContain('"type":"module"'); |
| 268 | }); |
| 269 | |
| 270 | it("fails when the framework produced no on-disk entry", async () => { |
| 271 | const error = await runWithNode( |
| 272 | Effect.gen(function* () { |
| 273 | const target = makeAwsTarget(); |
| 274 | return yield* target.finish!( |
| 275 | { |
| 276 | distDirectory: "/tmp", |
| 277 | clientDirectory: undefined, |
| 278 | serverModules: [], |
| 279 | externalWorkspaces: new Set<string>(), |
| 280 | }, |
| 281 | { root: "/tmp", framework: "tanstack-start" }, |
| 282 | ).pipe(Effect.flip); |
| 283 | }), |
| 284 | ); |
| 285 | expect(error._tag).toBe("DeployTargetError"); |
| 286 | expect(error.message).toContain("no on-disk server entry"); |
| 287 | }); |
| 288 | }); |
| 289 | |
| 290 | describe("make", () => { |
| 291 | it("defaults to this package's AWS deploy target", () => { |
| 292 | expect(DEFAULT_TARGET_SPECIFIER).toBe("@alchemy.run/frontend-frameworks/tanstack-start/aws"); |
| 293 | }); |
| 294 | |
| 295 | it("build fails with a descriptive FrameworkError outside a Vite project", async () => { |
| 296 | // /tmp has no vite install: loading the project's Vite is the first step |
| 297 | // after the target resolves and must surface a FrameworkError. |
| 298 | const result = await runWithNode( |
| 299 | Effect.gen(function* () { |
| 300 | const framework = yield* make({ |
| 301 | root: "/tmp/does-not-matter", |
| 302 | target: finishOnly(makeAwsTarget()), |
| 303 | }); |
| 304 | return yield* Effect.result(framework.build({ root: "/tmp/does-not-matter" })); |
| 305 | }), |
| 306 | ); |
| 307 | expect(result._tag).toBe("Failure"); |
| 308 | if (result._tag === "Failure") { |
| 309 | expect(result.failure._tag).toBe("FrameworkError"); |
| 310 | expect(result.failure.framework).toBe("tanstack-start"); |
| 311 | } |
| 312 | }); |
| 313 | |
| 314 | it("exposes the framework contract (build + dev)", async () => { |
| 315 | const framework = await runWithNode(make({ root: "/tmp/does-not-matter" })); |
| 316 | expect(typeof framework.build).toBe("function"); |
| 317 | expect(typeof framework.dev).toBe("function"); |
| 318 | }); |
| 319 | }); |
| 320 | |
| 321 | describe("fromHarnessOptions", () => { |
| 322 | it("forwards the harness's outDir override", () => { |
| 323 | expect(fromHarnessOptions({ tanstackStart: { outDir: "build" } }).outDir).toBe("build"); |
| 324 | expect(fromHarnessOptions({}).outDir).toBeUndefined(); |
| 325 | }); |
| 326 | }); |
| 327 | |
| 328 | /** Drop the wholesale `build` hook so the in-process pipeline runs. */ |
| 329 | const finishOnly = (target: TanStackStartTarget): TanStackStartTarget => { |
| 330 | const { build: _build, ...rest } = target; |
| 331 | return rest; |
| 332 | }; |