The best way to control Claude Code, Codex and more from any computer or phone

main
ts 101 lines 3.9 KB
Raw
1 import { spawn } from "node:child_process";
2 import { once } from "node:events";
3 import * as fs from "node:fs/promises";
4 import * as net from "node:net";
5 import * as path from "node:path";
6 import { describe, expect, it, vi } from "vitest";
7 import { isDeployTarget } from "../../core/index.ts";
8 import { NODE_BUNDLE_CONDITIONS } from "../../core/NodeServe.ts";
9 import { makeProject, run } from "../../core/test/helpers.ts";
10 import { makeNodeAdapter, makeNodeTarget, target } from "../node.ts";
11
12 describe("makeNodeTarget", () => {
13 it("is a DeployTarget for the node platform with a finish pass", () => {
14 const node = makeNodeTarget({
15 adapter: { notFoundHandling: "single-page-application" },
16 });
17 expect(isDeployTarget(node)).toBe(true);
18 expect(node.platform).toBe("node");
19 expect(node.bundle?.conditions).toEqual([...NODE_BUNDLE_CONDITIONS]);
20 expect(node.bundle?.external ?? []).not.toContain("cloudflare:");
21 expect(node.bundle?.external ?? []).not.toContain("@aws-sdk/");
22 expect(node.build).toBeTypeOf("function");
23 expect(node.finish).toBeTypeOf("function");
24 });
25
26 it("produces the in-memory kit adapter from the adapter hook", () => {
27 const adapter = makeNodeAdapter();
28 expect(adapter.name).toBe("@alchemy.run/frontend-frameworks/sveltekit/node");
29 expect(adapter.result.current).toBeUndefined();
30 expect(typeof adapter.adapt).toBe("function");
31 });
32
33 it("exposes the named `target` module export as the factory", () => {
34 expect(target).toBe(makeNodeTarget);
35 });
36
37 it("serves SSR and static assets from a relocated dist directory", async () => {
38 const root = await makeProject({
39 "entry.js": 'export const handler = () => new Response("SSR home");',
40 ".svelte-kit/node/robots.txt": "User-agent: *",
41 ".svelte-kit/node/about.html": "Prerendered about",
42 ".svelte-kit/node/docs/index.html": "Prerendered docs",
43 ".svelte-kit/node/_app/immutable/app.css": "body { color: red; }",
44 "dist/client/stale.txt": "Removed in this build",
45 });
46 const output = await run(
47 makeNodeTarget().finish!(
48 {
49 distDirectory: path.join(root, "dist"),
50 clientDirectory: path.join(root, ".svelte-kit/node"),
51 serverModules: [],
52 externalWorkspaces: new Set(),
53 },
54 { root, entry: path.join(root, "entry.js") },
55 ),
56 );
57 expect(output.clientDirectory).toBe(path.join(root, "dist/client"));
58 await expect(fs.stat(path.join(root, "dist/client/stale.txt"))).rejects.toThrow();
59
60 const deployed = await makeProject({});
61 await fs.cp(path.join(root, "dist"), deployed, { recursive: true });
62 await fs.rm(root, { recursive: true, force: true });
63
64 const reservation = net.createServer();
65 reservation.listen(0, "127.0.0.1");
66 await once(reservation, "listening");
67 const port = (reservation.address() as net.AddressInfo).port;
68 await new Promise<void>((resolve, reject) =>
69 reservation.close((error) => (error ? reject(error) : resolve())),
70 );
71 const child = spawn(process.execPath, [path.join(deployed, output.serverModules![0]!.name)], {
72 cwd: deployed,
73 env: { ...process.env, PORT: String(port) },
74 stdio: "ignore",
75 });
76 const exited = once(child, "exit");
77 const base = `http://127.0.0.1:${port}`;
78 try {
79 await vi.waitFor(
80 async () => {
81 expect((await fetch(`${base}/health`)).status).toBe(200);
82 },
83 { timeout: 5_000, interval: 50 },
84 );
85 for (const [url, body] of [
86 ["/", "SSR home"],
87 ["/about", "Prerendered about"],
88 ["/docs/", "Prerendered docs"],
89 ["/robots.txt", "User-agent: *"],
90 ["/_app/immutable/app.css", "body { color: red; }"],
91 ]) {
92 const response = await fetch(`${base}${url}`);
93 expect(response.status).toBe(200);
94 expect(await response.text()).toBe(body);
95 }
96 } finally {
97 child.kill();
98 await exited;
99 }
100 });
101 });