@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, 06:00 UTC

main
ts 170 lines 6.61 KB
Raw
1 import * as NodeFs from "node:fs";
2 import * as NodeServices from "@effect/platform-node/NodeServices";
3 import * as Effect from "effect/Effect";
4 import * as FileSystem from "effect/FileSystem";
5 import * as Path from "effect/Path";
6 import type * as Scope from "effect/Scope";
7 import { describe, expect, it } from "vitest";
8 import { makeDeployTarget } from "../../core/index.ts";
9 import { makeAwsTarget } from "../aws.ts";
10 import { make, readViteOutput } from "../Vite.ts";
11
12 const runWithNode = <A, E>(
13 effect: Effect.Effect<A, E, FileSystem.FileSystem | Path.Path | Scope.Scope>,
14 ): Promise<A> =>
15 Effect.runPromise(
16 Effect.scoped(effect).pipe(Effect.provide(NodeServices.layer)) as Effect.Effect<A, E>,
17 );
18
19 /** A minimal html-entry Vite project written into a scoped temp directory. */
20 const writeFixtureProject = Effect.fn(function* (marker: string) {
21 const fs = yield* FileSystem.FileSystem;
22 const path = yield* Path.Path;
23 const root = yield* fs.makeTempDirectoryScoped({ prefix: "vite-app-" });
24 yield* fs.writeFileString(
25 path.join(root, "package.json"),
26 JSON.stringify({ name: "vite-fixture", private: true, type: "module" }),
27 );
28 yield* fs.writeFileString(
29 path.join(root, "index.html"),
30 `<!doctype html><html><head><title>fixture</title></head><body><div id="app">${marker}</div><script type="module" src="/src/main.ts"></script></body></html>`,
31 );
32 yield* fs.makeDirectory(path.join(root, "src"), { recursive: true });
33 yield* fs.writeFileString(
34 path.join(root, "src", "main.ts"),
35 `document.querySelector("#app")!.setAttribute("data-ready", "true");\n`,
36 );
37 return root;
38 });
39
40 /**
41 * Fully canonical form of an existing path. Effect's `fs.realPath` delegates
42 * to node's `fs.realpath`, which resolves symlinks (macOS `/var` ->
43 * `/private/var`) but NOT Windows 8.3 short names (`RUNNER~1` vs
44 * `runneradmin`); only `realpath.native` does both.
45 */
46 const canonicalPath = (p: string) => Effect.sync(() => NodeFs.realpathSync.native(p));
47
48 /** An adapter-less in-process target (no build child) for direct testing. */
49 const inProcessTarget = makeDeployTarget({
50 platform: "aws",
51 config: {},
52 });
53
54 describe("readViteOutput", () => {
55 it("maps an assets directory onto the assets-only BuildOutput contract", async () => {
56 const output = await runWithNode(
57 Effect.gen(function* () {
58 const fs = yield* FileSystem.FileSystem;
59 const path = yield* Path.Path;
60 const dir = yield* fs.makeTempDirectoryScoped({
61 prefix: "vite-output-",
62 });
63 yield* fs.writeFileString(path.join(dir, "index.html"), "<!doctype html>");
64 return yield* readViteOutput({ outDir: dir }).pipe(
65 Effect.map((output) => ({
66 distDirectory: output.distDirectory,
67 clientDirectory: output.clientDirectory,
68 serverModules: output.serverModules,
69 externalWorkspaces: [...output.externalWorkspaces],
70 dir,
71 })),
72 );
73 }),
74 );
75 expect(output.distDirectory).toBe(output.dir);
76 expect(output.clientDirectory).toBe(output.dir);
77 expect(output.serverModules).toBeUndefined();
78 expect(output.externalWorkspaces).toEqual([]);
79 });
80
81 it("fails actionably when the output directory is missing", async () => {
82 const error = await runWithNode(
83 Effect.flip(readViteOutput({ outDir: "/nonexistent/vite-dist" })),
84 );
85 expect(error._tag).toBe("FrameworkError");
86 expect(error.message).toContain("no output directory");
87 });
88 });
89
90 describe("make().build", () => {
91 it("drives the project's vite build and returns an assets-only output", async () => {
92 const result = await runWithNode(
93 Effect.gen(function* () {
94 const fs = yield* FileSystem.FileSystem;
95 const path = yield* Path.Path;
96 const root = yield* writeFixtureProject("VITE_BUILD_MARKER");
97 const framework = yield* make({ root, target: inProcessTarget });
98 const output = yield* framework.build({ root });
99 const indexHtml = yield* fs.readFileString(
100 path.join(output.clientDirectory!, "index.html"),
101 );
102 // vite's resolveConfig canonicalizes the root, so compare fully
103 // canonical paths on both sides.
104 return {
105 clientDirectory: yield* canonicalPath(output.clientDirectory!),
106 distDirectory: yield* canonicalPath(output.distDirectory!),
107 serverModules: output.serverModules,
108 indexHtml,
109 expectedOutDir: yield* canonicalPath(path.join(root, "dist")),
110 };
111 }),
112 );
113 expect(result.clientDirectory).toBe(result.expectedOutDir);
114 expect(result.distDirectory).toBe(result.expectedOutDir);
115 expect(result.serverModules).toBeUndefined();
116 expect(result.indexHtml).toContain("VITE_BUILD_MARKER");
117 }, 120_000);
118
119 it("honors an explicit outDir override", async () => {
120 const result = await runWithNode(
121 Effect.gen(function* () {
122 const fs = yield* FileSystem.FileSystem;
123 const path = yield* Path.Path;
124 const root = yield* writeFixtureProject("VITE_OUTDIR_MARKER");
125 const framework = yield* make({
126 root,
127 target: inProcessTarget,
128 vite: { outDir: "build-output" },
129 });
130 const output = yield* framework.build({ root });
131 return {
132 clientDirectory: output.clientDirectory,
133 expected: path.join(root, "build-output"),
134 exists: yield* fs.exists(path.join(root, "build-output", "index.html")),
135 };
136 }),
137 );
138 expect(result.clientDirectory).toBe(result.expected);
139 expect(result.exists).toBe(true);
140 }, 120_000);
141 });
142
143 describe("make().dev", () => {
144 it("serves the project through vite's own dev server, scoped", async () => {
145 const body = await runWithNode(
146 Effect.gen(function* () {
147 const root = yield* writeFixtureProject("VITE_DEV_MARKER");
148 const framework = yield* make({ root, target: inProcessTarget });
149 const { url } = yield* framework.dev({ root });
150 expect(url).toMatch(/^http:\/\/(localhost|127\.0\.0\.1)/);
151 return yield* Effect.tryPromise(async () => {
152 const response = await fetch(url);
153 return await response.text();
154 });
155 }),
156 );
157 expect(body).toContain("VITE_DEV_MARKER");
158 }, 120_000);
159 });
160
161 describe("makeAwsTarget", () => {
162 it("is an assets-only target whose only seam is the wholesale child build", () => {
163 const target = makeAwsTarget({ vite: { outDir: "dist" } });
164 expect(target.platform).toBe("aws");
165 expect(target.build).toBeTypeOf("function");
166 expect(target.finish).toBeUndefined();
167 expect(target.entry).toBeUndefined();
168 expect(target.config).toEqual({ vite: { outDir: "dist" } });
169 });
170 });