@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 193 lines 8.44 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 * as ChildProcess from "effect/process/ChildProcess";
6 import { ChildProcessSpawner } from "effect/process/ChildProcessSpawner";
7 import { describe, expect, it } from "vitest";
8 import { NODE_BUNDLE_CONDITIONS } from "../../core/NodeServe.ts";
9 import { resolveVinextCli } from "../cli.ts";
10 import {
11 SERVER_ENTRY_NAME,
12 VINEXT_NODE_INSTALL,
13 makeNodeTarget,
14 makeVinextServeEntrySource,
15 target,
16 } from "../node.ts";
17
18 describe("makeNodeTarget", () => {
19 it.each([
20 { hybrid: false, inline: false },
21 { hybrid: true, inline: false },
22 { hybrid: false, inline: true },
23 { hybrid: true, inline: true },
24 ])(
25 "builds and serves without an Alchemy plugin (hybrid: $hybrid, inline config: $inline)",
26 ({ hybrid, inline }) =>
27 Effect.runPromise(
28 Effect.scoped(
29 Effect.gen(function* () {
30 const fs = yield* FileSystem.FileSystem;
31 const path = yield* Path.Path;
32 const example = yield* path.fromFileUrl(
33 new URL("../../../../../examples/prisma-website-vinext/", import.meta.url),
34 );
35 const root = yield* fs.makeTempDirectoryScoped({
36 prefix: "vinext-node-",
37 });
38 yield* fs.symlink(path.join(example, "node_modules"), path.join(root, "node_modules"));
39 yield* fs.writeFileString(path.join(root, "package.json"), '{"type":"module"}');
40 // Hybrid builds bundle Pages separately; it must still see the user's config.
41 const pageExtensions = inline && !hybrid ? ', pageExtensions: ["page.tsx"]' : "";
42 if (inline) {
43 yield* fs.writeFileString(
44 path.join(root, "vite.config.ts"),
45 `
46 import { fileURLToPath } from "node:url";
47 import vinext from "vinext";
48 export default {
49 resolve: { alias: { "@legacy-text": fileURLToPath(new URL("./legacy-text.ts", import.meta.url)) } },
50 plugins: [vinext({ prerender: true, nextConfig: { basePath: "/nested"${pageExtensions} } })],
51 };
52 `,
53 );
54 yield* fs.writeFileString(
55 path.join(root, "legacy-text.ts"),
56 'export const text = "Legacy page";',
57 );
58 }
59 yield* fs.makeDirectory(path.join(root, "pages"));
60 yield* fs.writeFileString(
61 path.join(root, pageExtensions ? "pages/legacy.page.tsx" : "pages/legacy.tsx"),
62 inline
63 ? 'import { text } from "@legacy-text";\nexport default function Page() { return <h1>{text}</h1>; }'
64 : "export default function Page() { return <h1>Legacy page</h1>; }",
65 );
66 if (hybrid) {
67 yield* fs.makeDirectory(path.join(root, "app"));
68 yield* fs.writeFileString(
69 path.join(root, "app/layout.tsx"),
70 "export default function Layout({children}) { return <html><body>{children}</body></html>; }",
71 );
72 yield* fs.writeFileString(
73 path.join(root, "app/page.tsx"),
74 "export default function Page() { return <h1>App page</h1>; }",
75 );
76 }
77 yield* fs.makeDirectory(path.join(root, "dist"));
78 yield* fs.writeFileString(path.join(root, "dist/stale.txt"), "old build");
79 const built = yield* makeNodeTarget().build!({
80 root,
81 framework: "vinext",
82 });
83 expect(yield* fs.exists(path.join(root, "vite.config.ts"))).toBe(inline);
84 if (inline) {
85 expect(
86 yield* fs.readFileString(path.join(root, "dist/server/vinext-prerender.json")),
87 ).toContain('"route": "/legacy"');
88 }
89 expect(
90 yield* fs.readFileString(path.join(built.distDirectory!, "server/entry.js")),
91 ).toContain("REDIS_URL");
92 expect(yield* fs.exists(path.join(root, "dist/stale.txt"))).toBe(false);
93 if (hybrid) {
94 const buildId = (yield* fs.readFileString(
95 path.join(root, "dist/server/BUILD_ID"),
96 )).trim();
97 const manifest = JSON.parse(
98 yield* fs.readFileString(path.join(root, "dist/server/vinext-server.json")),
99 );
100 for (const entry of ["index.js", "entry.js"]) {
101 const code = yield* fs.readFileString(path.join(root, "dist/server", entry));
102 expect(code).toContain(buildId);
103 expect(code).toContain(manifest.prerenderSecret);
104 }
105 const assets = yield* fs.readFileString(
106 path.join(root, "dist/server/ssr/vinext-client-assets.js"),
107 );
108 expect(assets).not.toContain('"clientEntry":null');
109 }
110 const spawner = yield* ChildProcessSpawner;
111 const child = yield* spawner.spawn(
112 ChildProcess.make(
113 "node",
114 [
115 "--input-type=module",
116 "--eval",
117 `
118 import assert from "node:assert/strict";
119 import { startProdServer } from "vinext/server/prod-server";
120 const { server } = await startProdServer({ port: 0, host: "127.0.0.1", outDir: ${JSON.stringify(built.distDirectory)} });
121 try {
122 const base = "http://127.0.0.1:" + server.address().port;
123 for (const [route, text] of ${JSON.stringify(
124 (hybrid
125 ? [
126 ["/legacy", "Legacy page"],
127 ["/", "App page"],
128 ]
129 : [["/legacy", "Legacy page"]]
130 ).map(([route, text]) => [inline ? `/nested${route}` : route, text]),
131 )}) {
132 const response = await fetch(base + route);
133 assert.equal(response.status, 200);
134 assert.ok((await response.text()).includes(text));
135 }
136 } finally { await new Promise((resolve) => server.close(resolve)); }
137 `,
138 ],
139 { cwd: root, stdout: "inherit", stderr: "inherit" },
140 ),
141 );
142 expect(yield* child.exitCode).toBe(0);
143 }),
144 ).pipe(Effect.provide(NodeServices.layer)),
145 ),
146 120_000,
147 );
148 it("resolves the installed CLI without requiring an exported CLI subpath", () =>
149 Effect.runPromise(
150 Effect.gen(function* () {
151 const fs = yield* FileSystem.FileSystem;
152 const path = yield* Path.Path;
153 const root = yield* path.fromFileUrl(
154 new URL("../../../../../examples/prisma-website-vinext/", import.meta.url),
155 );
156 const cli = yield* resolveVinextCli(root);
157 expect(cli).toMatch(/vinext\/dist\/cli\.js$/);
158 expect(yield* fs.exists(cli)).toBe(true);
159 }).pipe(Effect.provide(NodeServices.layer)),
160 ));
161 it("declares the node platform and a wholesale vinext build (not Cloudflare)", () => {
162 const node = makeNodeTarget();
163 expect(node.platform).toBe("node");
164 expect(node.build).toBeTypeOf("function");
165 expect(node.bundle?.conditions).toEqual([...NODE_BUNDLE_CONDITIONS]);
166 expect(node.bundle?.external ?? []).not.toContain("cloudflare:");
167 expect(node.bundle?.external ?? []).not.toContain("@aws-sdk/");
168 });
169
170 it("writes a startProdServer serve entry with /health, not a Worker fetch handler", () => {
171 const source = makeVinextServeEntrySource();
172 expect(SERVER_ENTRY_NAME).toBe("server/serve-node.mjs");
173 expect(source).toContain('from "vinext/server/prod-server"');
174 expect(source).toContain("startProdServer");
175 expect(source).toContain("/health");
176 expect(source).toContain("process.env.PORT");
177 expect(source).not.toContain("vinext/server/fetch-handler");
178 expect(source).not.toContain("aws-lambda");
179 expect(source).not.toContain("cloudflare");
180 expect(source).not.toContain("worker/index.ts");
181 });
182
183 it("installs vinext and React at runtime instead of bundling them", () => {
184 expect(VINEXT_NODE_INSTALL).toContain("vinext");
185 expect(VINEXT_NODE_INSTALL).toContain("react");
186 expect(VINEXT_NODE_INSTALL).toContain("react-dom");
187 expect(VINEXT_NODE_INSTALL).toContain("react-server-dom-webpack");
188 });
189
190 it("exposes the named `target` module export as the factory", () => {
191 expect(target).toBe(makeNodeTarget);
192 });
193 });