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

main
ts 142 lines 4.33 KB
Raw
1 import { describe, expect, it } from "vitest";
2 import { sortServerModules, type OutputFile } from "../../core/index.ts";
3 import {
4 listEdgeFunctions,
5 DEFAULT_COMPATIBILITY_DATE,
6 hasDoQueueClass,
7 makeRunnerConfig,
8 toRuntimeModules,
9 WORKER_ENTRY_MODULE,
10 } from "../Nextjs.ts";
11
12 const file = (name: string, content: string | Uint8Array): OutputFile => ({
13 name,
14 content,
15 hash: "test",
16 });
17
18 describe("makeRunnerConfig", () => {
19 it("applies defaults", () => {
20 const config = makeRunnerConfig("/app");
21 expect(config).toEqual({
22 appDir: "/app",
23 configPath: undefined,
24 cache: "static-assets",
25 compatibilityDate: DEFAULT_COMPATIBILITY_DATE,
26 skipNextBuild: false,
27 minify: false,
28 debug: false,
29 buildCommand: undefined,
30 });
31 });
32
33 it("honors options", () => {
34 const config = makeRunnerConfig("/app", {
35 vite: { compatibilityDate: "2026-01-01" },
36 nextjs: {
37 configPath: "custom.config.ts",
38 cache: "kv",
39 buildCommand: "npx next build --debug",
40 skipNextBuild: true,
41 minify: true,
42 debug: true,
43 },
44 });
45 expect(config).toEqual({
46 appDir: "/app",
47 configPath: "custom.config.ts",
48 cache: "kv",
49 compatibilityDate: "2026-01-01",
50 skipNextBuild: true,
51 minify: true,
52 debug: true,
53 buildCommand: "npx next build --debug",
54 });
55 });
56 });
57
58 describe("toRuntimeModules", () => {
59 it("maps extensions to cloudflare-runtime module types", () => {
60 const modules = toRuntimeModules([
61 file("worker/worker.js", "entry"),
62 file("worker/chunks/a.mjs", "chunk"),
63 file("worker/legacy.cjs", "cjs"),
64 file("worker/data.json", `{"a":1}`),
65 file("worker/wasm.wasm", new Uint8Array([0, 97, 115, 109])),
66 file("worker/blob.bin", new Uint8Array([1, 2, 3])),
67 file("worker/readme.txt", "text"),
68 ]);
69 expect(modules.map((module) => [module.name, module.type])).toEqual([
70 ["worker/worker.js", "ESModule"],
71 ["worker/chunks/a.mjs", "ESModule"],
72 ["worker/legacy.cjs", "CommonJsModule"],
73 ["worker/data.json", "Json"],
74 ["worker/wasm.wasm", "Wasm"],
75 ["worker/blob.bin", "Data"],
76 ["worker/readme.txt", "Text"],
77 ]);
78 });
79
80 it("drops sourcemaps", () => {
81 expect(toRuntimeModules([file("worker/worker.js.map", "{}")])).toEqual([]);
82 });
83
84 it("normalizes buffered text content to strings and text to bytes", () => {
85 const [text] = toRuntimeModules([file("worker/worker.js", Buffer.from("hello"))]);
86 expect(text).toEqual({
87 name: "worker/worker.js",
88 type: "ESModule",
89 content: "hello",
90 });
91 const [bytes] = toRuntimeModules([file("worker/blob.bin", "abc")]);
92 expect(bytes?.type).toBe("Data");
93 expect(bytes?.content).toBeInstanceOf(Uint8Array);
94 });
95 });
96
97 describe("hasDoQueueClass", () => {
98 it("detects the DO queue class in string and buffered entries", () => {
99 expect(hasDoQueueClass(file("worker/worker.js", "export { DOQueueHandler }"))).toBe(true);
100 expect(hasDoQueueClass(file("worker/worker.js", Buffer.from("class DOQueueHandler")))).toBe(
101 true,
102 );
103 expect(hasDoQueueClass(file("worker/worker.js", "export default {}"))).toBe(false);
104 expect(hasDoQueueClass(undefined)).toBe(false);
105 });
106 });
107
108 describe("server module ordering", () => {
109 it("sorts the worker entry first", () => {
110 const sorted = sortServerModules(
111 [
112 file("worker/chunks/a.js", "a"),
113 file(WORKER_ENTRY_MODULE, "entry"),
114 file("worker/chunks/b.js", "b"),
115 ],
116 WORKER_ENTRY_MODULE,
117 );
118 expect(sorted[0]?.name).toBe(WORKER_ENTRY_MODULE);
119 expect(WORKER_ENTRY_MODULE).toBe("worker/worker.js");
120 });
121 });
122
123 describe("listEdgeFunctions", () => {
124 it("returns edge function paths from the middleware manifest", () => {
125 expect(
126 listEdgeFunctions({
127 middleware: { "/": { files: [] } },
128 functions: {
129 "/api/edge/route": { files: [] },
130 "/edge-page": { files: [] },
131 },
132 }),
133 ).toEqual(["/api/edge/route", "/edge-page"]);
134 });
135
136 it("treats middleware-only manifests (and junk) as edge-free", () => {
137 expect(listEdgeFunctions({ middleware: { "/": { files: [] } } })).toEqual([]);
138 expect(listEdgeFunctions({})).toEqual([]);
139 expect(listEdgeFunctions(undefined)).toEqual([]);
140 expect(listEdgeFunctions("nope")).toEqual([]);
141 });
142 });