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

main
ts 119 lines 4.46 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 { fromHarnessOptions } from "../index.ts";
8 import { make, readNitroOutput, SERVER_ENTRY_NAME } from "../Nuxt.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("readNitroOutput", () => {
18 it("maps a nitro .output tree onto the BuildOutput contract, entry first", async () => {
19 const output = await runWithNode(
20 Effect.gen(function* () {
21 const fs = yield* FileSystem.FileSystem;
22 const path = yield* Path.Path;
23 const dir = yield* fs.makeTempDirectoryScoped({
24 prefix: "nuxt-output-",
25 });
26 const serverDir = path.join(dir, "server");
27 const publicDir = path.join(dir, "public");
28 yield* fs.makeDirectory(path.join(serverDir, "chunks"), {
29 recursive: true,
30 });
31 yield* fs.makeDirectory(publicDir, { recursive: true });
32 // alphabetically before index.mjs, to prove entry-first sorting
33 yield* fs.writeFileString(path.join(serverDir, "chunks", "a.mjs"), "export const a = 1;");
34 yield* fs.writeFileString(path.join(serverDir, "index.mjs"), "export default {};");
35 yield* fs.writeFileString(path.join(publicDir, "robots.txt"), "User-agent: *\n");
36 return yield* readNitroOutput({ dir, serverDir, publicDir });
37 }),
38 );
39 expect(output.serverModules?.map((module) => module.name)).toEqual([
40 SERVER_ENTRY_NAME,
41 "server/chunks/a.mjs",
42 ]);
43 expect(output.serverModules?.[0]?.hash).toMatch(/^[0-9a-f]{64}$/);
44 expect(output.clientDirectory).toBeDefined();
45 expect(output.distDirectory).toBeDefined();
46 expect(output.externalWorkspaces.size).toBe(0);
47 });
48
49 it("fails when the server directory has no index.mjs entry", async () => {
50 const result = await runWithNode(
51 Effect.gen(function* () {
52 const fs = yield* FileSystem.FileSystem;
53 const path = yield* Path.Path;
54 const dir = yield* fs.makeTempDirectoryScoped({
55 prefix: "nuxt-output-",
56 });
57 const serverDir = path.join(dir, "server");
58 yield* fs.makeDirectory(serverDir, { recursive: true });
59 yield* fs.writeFileString(path.join(serverDir, "other.mjs"), "export const x = 1;");
60 return yield* Effect.result(
61 readNitroOutput({
62 dir,
63 serverDir,
64 publicDir: path.join(dir, "public"),
65 }),
66 );
67 }),
68 );
69 expect(result._tag).toBe("Failure");
70 if (result._tag === "Failure") {
71 expect(String(result.failure)).toContain("server/index.mjs");
72 }
73 });
74 });
75
76 describe("make", () => {
77 it("dev fails with a descriptive FrameworkError outside a Nuxt project", async () => {
78 // /tmp has no nuxt install: resolving the project's kit is the first
79 // step of `dev` and must surface a FrameworkError (not a raw throw).
80 const result = await runWithNode(
81 Effect.gen(function* () {
82 const framework = yield* make({ root: "/tmp/does-not-matter" });
83 return yield* Effect.result(framework.dev());
84 }),
85 );
86 expect(result._tag).toBe("Failure");
87 if (result._tag === "Failure") {
88 expect(result.failure._tag).toBe("FrameworkError");
89 }
90 });
91 });
92
93 describe("fromHarnessOptions", () => {
94 it("prefers the target-scoped carriage over the deprecated vite alias", () => {
95 const options = fromHarnessOptions({
96 target: {
97 cloudflare: {
98 worker: {
99 compatibilityDate: "2026-03-10",
100 compatibilityFlags: ["nodejs_compat"],
101 main: "./worker-entry.ts",
102 },
103 },
104 },
105 vite: { compatibilityDate: "1999-01-01" },
106 });
107 expect(options.compatibilityDate).toBe("2026-03-10");
108 expect(options.compatibilityFlags).toEqual(["nodejs_compat"]);
109 expect(options.main).toBe("./worker-entry.ts");
110 });
111
112 it("falls back to the vite alias when no target carriage exists", () => {
113 const options = fromHarnessOptions({
114 vite: { compatibilityDate: "2026-03-10" },
115 });
116 expect(options.compatibilityDate).toBe("2026-03-10");
117 expect(options.main).toBeUndefined();
118 });
119 });