@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

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