@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 290 lines 9.95 KB
Raw
1 import { expect } from "@alchemy.run/test";
2 import * as Test from "@alchemy.run/test/Alchemy";
3 import * as Effect from "effect/Effect";
4 import * as FileSystem from "effect/FileSystem";
5 import * as pathe from "pathe";
6 import * as Command from "@/Command";
7
8 const { test } = Test.make({ providers: Command.providers() });
9
10 const FIXTURE_DIR = pathe.resolve(import.meta.dirname, "fixture");
11
12 const makeTemporaryFixture = Effect.fn(function* () {
13 const fs = yield* FileSystem.FileSystem;
14 const tempDir = yield* fs.makeTempDirectoryScoped();
15 yield* fs.copy(FIXTURE_DIR, tempDir);
16 return {
17 cwd: tempDir,
18 outdir: pathe.join(tempDir, "dist"),
19 };
20 });
21
22 test.provider(
23 "create, skip, update, delete build with memoization",
24 (stack) =>
25 Effect.gen(function* () {
26 const fs = yield* FileSystem.FileSystem;
27
28 yield* stack.destroy();
29
30 const fixture = yield* makeTemporaryFixture();
31
32 const deploy = () =>
33 stack.deploy(
34 Command.Build("test-build", {
35 command: "bash build.sh",
36 cwd: fixture.cwd,
37 outdir: "dist",
38 }),
39 );
40
41 const build1 = yield* deploy();
42
43 // `outdir` is persisted relative to `process.cwd()` for portability;
44 // resolve it back to an absolute path before comparing.
45 expect(pathe.resolve(build1.outdir)).toBe(fixture.outdir);
46 expect(build1.hash).toMatchObject({
47 input: expect.any(String),
48 output: expect.any(String),
49 });
50
51 const distExists = yield* fs.exists(fixture.outdir);
52 expect(distExists).toBe(true);
53
54 const outputExists = yield* fs.exists(pathe.join(fixture.outdir, "output.txt"));
55 expect(outputExists).toBe(true);
56
57 const firstBuildOutput = yield* fs.readFileString(pathe.join(fixture.outdir, "output.txt"));
58
59 yield* Effect.sleep(1100);
60
61 const build2 = yield* deploy();
62
63 expect(build2.hash).toMatchObject(build1.hash);
64
65 const secondBuildOutput = yield* fs.readFileString(pathe.join(fixture.outdir, "output.txt"));
66 expect(secondBuildOutput).toBe(firstBuildOutput);
67
68 yield* fs.writeFileString(
69 pathe.join(fixture.cwd, "src", "main.ts"),
70 'export const message = "Updated!";\n',
71 );
72
73 const build3 = yield* deploy();
74
75 expect(build3.hash).not.toMatchObject(build1.hash);
76
77 const thirdBuildOutput = yield* fs.readFileString(pathe.join(fixture.outdir, "output.txt"));
78 expect(thirdBuildOutput).not.toBe(firstBuildOutput);
79
80 yield* fs.writeFileString(
81 pathe.join(fixture.cwd, "src", "main.ts"),
82 'export const message = "Hello, World!";\n',
83 );
84
85 yield* stack.destroy();
86
87 const distExistsAfterDestroy = yield* fs.exists(fixture.outdir);
88 expect(distExistsAfterDestroy).toBe(false);
89 }),
90 { tags: ["unit", "local"], timeout: 60000 },
91 );
92
93 test.provider(
94 "input hash folds in the build command and env",
95 (stack) =>
96 Effect.gen(function* () {
97 yield* stack.destroy();
98
99 const fixture = yield* makeTemporaryFixture();
100
101 const deploy = (props: Partial<Command.BuildProps>) =>
102 stack.deploy(
103 Command.Build("test-build", {
104 command: "bash build.sh",
105 cwd: fixture.cwd,
106 outdir: "dist",
107 ...props,
108 }),
109 );
110
111 const withEnvA = yield* deploy({ env: { API_URL: "https://a.example" } });
112
113 // Same source tree + outdir, only the env differs: the build must not be
114 // judged reusable, so the input hash must change.
115 const withEnvB = yield* deploy({ env: { API_URL: "https://b.example" } });
116 expect(withEnvB.hash.input).not.toBe(withEnvA.hash.input);
117
118 // Likewise a change to the command string busts the input hash.
119 const withCommand = yield* deploy({
120 command: "bash build.sh dummy",
121 env: { API_URL: "https://b.example" },
122 });
123 expect(withCommand.hash.input).not.toBe(withEnvB.hash.input);
124
125 yield* stack.destroy();
126 }),
127 { tags: ["unit", "local"], timeout: 60000 },
128 );
129
130 test.provider(
131 "rebuilds memoized output if outdir is missing",
132 (stack) =>
133 Effect.gen(function* () {
134 const fs = yield* FileSystem.FileSystem;
135
136 yield* stack.destroy();
137
138 const fixture = yield* makeTemporaryFixture();
139 expect(yield* fs.exists(fixture.outdir)).toBe(false);
140
141 const deploy = () =>
142 stack.deploy(
143 Command.Build("test-build", {
144 command: "bash build.sh",
145 cwd: fixture.cwd,
146 outdir: "dist",
147 }),
148 );
149
150 yield* deploy();
151 expect(yield* fs.exists(fixture.outdir)).toBe(true);
152
153 yield* fs.remove(fixture.outdir, { recursive: true });
154 expect(yield* fs.exists(fixture.outdir)).toBe(false);
155
156 yield* deploy();
157 expect(yield* fs.exists(fixture.outdir)).toBe(true);
158
159 yield* stack.destroy();
160 }),
161 { tags: ["unit", "local"] },
162 );
163
164 test.provider(
165 "memo include globs can reach outside cwd (monorepo workspace deps)",
166 (stack) =>
167 Effect.gen(function* () {
168 const fs = yield* FileSystem.FileSystem;
169
170 yield* stack.destroy();
171
172 // A monorepo shape: the app builds from `app/` and imports a sibling
173 // workspace package at `packages/env/` that the default memo scope
174 // (files under `cwd`) does not cover.
175 const tempDir = yield* fs.makeTempDirectoryScoped();
176 const appDir = pathe.join(tempDir, "app");
177 const siblingDir = pathe.join(tempDir, "packages", "env");
178 yield* fs.copy(FIXTURE_DIR, appDir);
179 yield* fs.makeDirectory(siblingDir, { recursive: true });
180 const siblingFile = pathe.join(siblingDir, "value.ts");
181 yield* fs.writeFileString(siblingFile, 'export const value = "a";\n');
182
183 const outputFile = pathe.join(appDir, "dist", "output.txt");
184 // Exclude `dist` from the memo hash: build.sh stamps `dist/output.txt`
185 // with $(date) (the test's rebuild detector), and the temp fixture has
186 // no .gitignore to filter it. With dist hashed, two rebuilds straddling
187 // a wall-clock second boundary would produce different "input" hashes.
188 const deploy = () =>
189 stack.deploy(
190 Command.Build("test-build", {
191 command: "bash build.sh",
192 cwd: appDir,
193 outdir: "dist",
194 memo: {
195 include: ["**/*", "../packages/env/**"],
196 exclude: ["dist/**"],
197 },
198 }),
199 );
200
201 const build1 = yield* deploy();
202 const firstOutput = yield* fs.readFileString(outputFile);
203
204 // Nothing changed (including the sibling): the build memoizes.
205 yield* Effect.sleep(1100);
206 const build2 = yield* deploy();
207 expect(build2.hash).toMatchObject(build1.hash);
208 expect(yield* fs.readFileString(outputFile)).toBe(firstOutput);
209
210 // Editing only the sibling package busts the input hash and rebuilds.
211 yield* fs.writeFileString(siblingFile, 'export const value = "b";\n');
212 const build3 = yield* deploy();
213 expect(build3.hash.input).not.toBe(build1.hash.input);
214 expect(yield* fs.readFileString(outputFile)).not.toBe(firstOutput);
215
216 // An *absolute* include glob matches the same files: its matches are
217 // normalized back to cwd-relative keys, so the input hash is identical
218 // to the `../` form — machine-specific path prefixes never leak into
219 // the hash (and the build memoizes instead of rerunning).
220 const build4 = yield* stack.deploy(
221 Command.Build("test-build", {
222 command: "bash build.sh",
223 cwd: appDir,
224 outdir: "dist",
225 memo: {
226 include: ["**/*", pathe.join(siblingDir, "**")],
227 exclude: ["dist/**"],
228 },
229 }),
230 );
231 expect(build4.hash.input).toBe(build3.hash.input);
232
233 yield* stack.destroy();
234 }),
235 { tags: ["unit", "local"], timeout: 60000 },
236 );
237
238 test.provider(
239 "default memo honors .gitignore files from the repository root down",
240 (stack) =>
241 Effect.gen(function* () {
242 const fs = yield* FileSystem.FileSystem;
243 yield* stack.destroy();
244
245 // A repository whose root `.gitignore` anchors `/app/generated/` to the
246 // root and ignores logs except `keep.log`, and whose app ignores a
247 // `data/` folder it cannot read.
248 const repo = yield* fs.makeTempDirectoryScoped();
249 const appDir = pathe.join(repo, "app");
250 yield* fs.makeDirectory(pathe.join(repo, ".git"));
251 yield* fs.writeFileString(
252 pathe.join(repo, ".gitignore"),
253 "/app/generated/\n*.log\n!keep.log\n",
254 );
255 yield* fs.copy(FIXTURE_DIR, appDir);
256 yield* fs.writeFileString(pathe.join(appDir, ".gitignore"), "data/\n");
257 const locked = pathe.join(appDir, "data", "postgres");
258 yield* fs.makeDirectory(locked, { recursive: true });
259 yield* Effect.acquireRelease(fs.chmod(locked, 0o000), () =>
260 fs.chmod(locked, 0o755).pipe(Effect.ignore),
261 );
262
263 const deploy = () =>
264 stack.deploy(
265 Command.Build("test-build", { command: "bash build.sh", cwd: appDir, outdir: "dist" }),
266 );
267 const outputFile = pathe.join(appDir, "dist", "output.txt");
268
269 const build1 = yield* deploy();
270 const firstOutput = yield* fs.readFileString(outputFile);
271
272 // Ignored files change: none are hashed, so the build memoizes.
273 yield* Effect.sleep(1100);
274 yield* fs.writeFileString(pathe.join(appDir, "debug.log"), "noise");
275 yield* fs.makeDirectory(pathe.join(appDir, "generated"));
276 yield* fs.writeFileString(pathe.join(appDir, "generated", "types.ts"), "noise");
277 const build2 = yield* deploy();
278 expect(build2.hash.input).toBe(build1.hash.input);
279 expect(yield* fs.readFileString(outputFile)).toBe(firstOutput);
280
281 // A re-included file is hashed: changing it rebuilds.
282 yield* fs.writeFileString(pathe.join(appDir, "keep.log"), "kept");
283 const build3 = yield* deploy();
284 expect(build3.hash.input).not.toBe(build1.hash.input);
285 expect(yield* fs.readFileString(outputFile)).not.toBe(firstOutput);
286
287 yield* stack.destroy();
288 }),
289 { tags: ["unit", "local"], timeout: 60000 },
290 );