@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, 03:37 UTC

main
ts 531 lines 19.9 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off
2 import * as NodeChildProcess from "node:child_process";
3 import * as NodeFS from "node:fs";
4 import * as NodeOS from "node:os";
5 import * as NodePath from "node:path";
6 import { expect } from "vite-plus/test";
7 import * as NodeServices from "@effect/platform-node/NodeServices";
8 import * as Option from "effect/Option";
9 import { afterAll, assert, describe, it } from "@effect/vitest";
10 import * as Clock from "effect/Clock";
11 import * as Effect from "effect/Effect";
12 import * as Fiber from "effect/Fiber";
13 import * as Sink from "effect/Sink";
14 import * as Stream from "effect/Stream";
15 import { ChildProcess, ChildProcessSpawner } from "effect/process";
16 import * as TestClock from "effect/testing/TestClock";
17
18 import {
19 comparisonBase,
20 defaultConcurrency,
21 docReaderTests,
22 gitLines,
23 ignoredCheckInputs,
24 runSteps,
25 splitTests,
26 type Step,
27 } from "./check-changed.ts";
28 import { decide, readWorkspace } from "../.github/scripts/ci-changes.cjs";
29
30 const GiB = 1024 ** 3;
31
32 describe("check:changed", () => {
33 it("bounds the default concurrency by the scarcer of cores and available memory", () => {
34 assert.strictEqual(defaultConcurrency(32, 128 * GiB), 16);
35 assert.strictEqual(defaultConcurrency(32, 20 * GiB), 3);
36 assert.strictEqual(defaultConcurrency(4, 15 * GiB), 2);
37 assert.strictEqual(defaultConcurrency(4, 2 * GiB), 1);
38 assert.strictEqual(defaultConcurrency(1, 64 * GiB), 1);
39 });
40
41 it("runs the server tests for the one guide they read and leaves other docs inert", () => {
42 const packages = readWorkspace();
43 assert.isTrue(
44 decide(["docs/user/t3-connect-troubleshooting.md"], packages, false).outputs.test_server,
45 );
46 assert.deepStrictEqual(docReaderTests(["docs/user/t3-connect-troubleshooting.md"]), [
47 "apps/server/src/cloud/ConnectReachability.test.ts",
48 ]);
49 assert.deepStrictEqual(decide(["docs/user/install.md"], packages, false).affected, []);
50 });
51
52 it.effect(
53 "keeps weighted steps within the budget, in order, and serial steps one at a time",
54 () =>
55 Effect.gen(function* () {
56 const startedAt: Record<string, number> = {};
57 let active = 0;
58 let maxActive = 0;
59 let serialActive = 0;
60 let maxSerial = 0;
61 const step = (
62 name: string,
63 options: { weight?: number; serial?: boolean } = {},
64 ): Step<never> => {
65 const slots = Math.min(options.weight ?? 1, 3);
66 return {
67 name,
68 ...options,
69 run: Effect.gen(function* () {
70 startedAt[name] = (yield* Clock.currentTimeMillis) / 1000;
71 active += slots;
72 maxActive = Math.max(maxActive, active);
73 if (options.serial) maxSerial = Math.max(maxSerial, ++serialActive);
74 yield* Effect.sleep("1 second");
75 active -= slots;
76 if (options.serial) serialActive--;
77 return { ok: true, output: "" };
78 }),
79 };
80 };
81 const fiber = yield* runSteps(
82 [
83 step("light 1"),
84 // Above the budget, so it holds every slot rather than never starting.
85 step("heavy", { weight: 5 }),
86 step("pair", { weight: 2 }),
87 step("light 2"),
88 step("test 1", { serial: true }),
89 step("test 2", { serial: true }),
90 ],
91 { concurrency: 3, timeoutSeconds: 60 },
92 ).pipe(Effect.forkChild);
93 yield* TestClock.adjust("10 seconds");
94 const results = yield* Fiber.join(fiber);
95
96 assert.deepStrictEqual(
97 results.map((result) => result.outcome),
98 ["pass", "pass", "pass", "pass", "pass", "pass"],
99 );
100 assert.strictEqual(maxActive, 3);
101 // Lighter steps queued behind the heavy one never start ahead of it.
102 assert.deepStrictEqual(startedAt, {
103 "light 1": 0,
104 heavy: 1,
105 pair: 2,
106 "light 2": 2,
107 "test 1": 3,
108 "test 2": 4,
109 });
110 assert.strictEqual(maxSerial, 1);
111 }),
112 );
113
114 it.effect("interrupts a step at its timeout and reports TIMEOUT, apart from FAIL", () =>
115 Effect.gen(function* () {
116 let interrupted = false;
117 const fiber = yield* runSteps(
118 [
119 {
120 name: "hangs",
121 run: Effect.never.pipe(
122 Effect.onInterrupt(() => Effect.sync(() => (interrupted = true))),
123 ),
124 },
125 { name: "fails", run: Effect.succeed({ ok: false, output: "error" }) },
126 { name: "passes", run: Effect.succeed({ ok: true, output: "" }) },
127 ],
128 { concurrency: 3, timeoutSeconds: 10 },
129 ).pipe(Effect.forkChild);
130 yield* TestClock.adjust("9 seconds");
131 assert.isFalse(interrupted);
132 yield* TestClock.adjust("1 second");
133 const results = yield* Fiber.join(fiber);
134
135 assert.isTrue(interrupted);
136 assert.deepStrictEqual(
137 results.map(({ name, outcome, time }) => [name, outcome, time]),
138 [
139 ["hangs", "TIMEOUT", "killed after 10s"],
140 ["fails", "FAIL", "0.0s"],
141 ["passes", "pass", "0.0s"],
142 ],
143 );
144 }),
145 );
146
147 it("runs the .github/scripts node:test files with node --test, not vitest", () => {
148 assert.deepStrictEqual(
149 splitTests([
150 "apps/server/src/ws.test.ts",
151 ".github/scripts/ci-changes.test.cjs",
152 ".github\\scripts\\thread-transfer-report.test.cjs",
153 "scripts/check-changed.test.ts",
154 ]),
155 {
156 vitestTests: ["apps/server/src/ws.test.ts", "scripts/check-changed.test.ts"],
157 nodeTests: [
158 ".github/scripts/ci-changes.test.cjs",
159 ".github\\scripts\\thread-transfer-report.test.cjs",
160 ],
161 },
162 );
163 });
164
165 it.effect("fails instead of returning an empty file list when git exits nonzero", () => {
166 const spawner = (exitCode: number) =>
167 ChildProcessSpawner.make(() =>
168 Effect.succeed(
169 ChildProcessSpawner.makeHandle({
170 pid: ChildProcessSpawner.ProcessId(1),
171 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(exitCode)),
172 isRunning: Effect.succeed(false),
173 kill: () => Effect.void,
174 unref: Effect.succeed(Effect.void),
175 stdin: Sink.drain,
176 stdout: Stream.encodeText(Stream.make(exitCode === 0 ? "a.ts\nb.ts\n" : "")),
177 stderr: Stream.encodeText(
178 Stream.make(exitCode === 0 ? "" : "fatal: index file corrupt"),
179 ),
180 all: Stream.empty,
181 getInputFd: () => Sink.drain,
182 getOutputFd: () => Stream.empty,
183 }),
184 ),
185 );
186 return Effect.gen(function* () {
187 const listed = yield* gitLines("/repo", ["ls-files"]).pipe(
188 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner(0)),
189 );
190 assert.deepStrictEqual(listed, ["a.ts", "b.ts"]);
191 const error = yield* gitLines("/repo", ["ls-files"]).pipe(
192 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner(128)),
193 Effect.flip,
194 );
195 assert.strictEqual(
196 error,
197 "check:changed: git ls-files exited with 128: fatal: index file corrupt",
198 );
199 });
200 });
201
202 it("opens the built desktop app only for startup, settlement, and sidebar changes", () => {
203 const packages = readWorkspace();
204 const launches = (file: string) => decide([file], packages, false).outputs.desktop_launch;
205 for (const file of [
206 "apps/server/src/orchestration-v2/ThreadSettlementService.ts",
207 "apps/server/src/persistence/Migrations/054_ProjectionThreadsAutoSettleDisabledAt.ts",
208 "apps/web/src/components/sidebar/SidebarThreadRow.tsx",
209 "apps/desktop/src/backend/DesktopBackendManager.ts",
210 "apps/desktop/src/electron/ElectronProtocol.ts",
211 "packages/contracts/src/settings.ts",
212 "scripts/fixtures/desktop-launch/statev2.sqlite",
213 ]) {
214 assert.isTrue(launches(file), file);
215 }
216 for (const file of [
217 "apps/server/src/orchestration-v2/ThreadSettlementService.test.ts",
218 "apps/web/src/components/chat/ChatView.tsx",
219 "apps/desktop/src/updates/DesktopRemoteUpdates.ts",
220 "packages/contracts/src/orchestration.ts",
221 ]) {
222 assert.isFalse(launches(file), file);
223 }
224 // A script-only change still builds the app it opens.
225 assert.isTrue(decide(["scripts/smoke-desktop-app.ts"], packages, false).outputs.build);
226 });
227
228 it.each([
229 "packages/shared/src/testing/longTempDir.ts",
230 "packages/shared/src/testing/providerEnv.ts",
231 "packages/shared/src/testing/gitEnv.ts",
232 "packages/shared/src/testing/gitRepositoryEnv.ts",
233 ])("tests every package when the shared test setup file %s changes", (file) => {
234 assert.strictEqual(decide([file], readWorkspace(), false).outputs.test_packages, "");
235 });
236 });
237
238 // Hooks export GIT_DIR and GIT_INDEX_FILE, and user config or the default
239 // ~/.config/git/ignore can ignore or sign fixture files.
240 // Git for Windows rejects the null device as a config path, so use an empty file.
241 const emptyConfigDir = NodeFS.mkdtempSync(
242 NodePath.join(NodeOS.tmpdir(), "t3-check-changed-config-"),
243 );
244 const emptyConfig = NodePath.join(emptyConfigDir, "empty.gitconfig");
245 NodeFS.writeFileSync(emptyConfig, "");
246 afterAll(() => NodeFS.rmSync(emptyConfigDir, { recursive: true, force: true }));
247 const fixtureConfig = {
248 "core.excludesFile": emptyConfig,
249 "user.name": "Fixture",
250 "user.email": "fixture@example.invalid",
251 };
252 const env = {
253 ...Object.fromEntries(Object.entries(process.env).filter(([name]) => !name.startsWith("GIT_"))),
254 GIT_CONFIG_GLOBAL: emptyConfig,
255 GIT_CONFIG_NOSYSTEM: "1",
256 GIT_CONFIG_COUNT: String(Object.keys(fixtureConfig).length),
257 ...Object.fromEntries(
258 Object.entries(fixtureConfig).flatMap(([key, value], index) => [
259 [`GIT_CONFIG_KEY_${index}`, key],
260 [`GIT_CONFIG_VALUE_${index}`, value],
261 ]),
262 ),
263 };
264
265 // History A -> B -> C, with HEAD at C. Each remote's main points at A or B.
266 function fixture(remotes: Record<string, "A" | "B">) {
267 const root = NodeFS.realpathSync(
268 NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-check-changed-")),
269 );
270 const commits: Record<string, string> = {};
271 try {
272 const write = (file: string, content: string) => {
273 const path = NodePath.join(root, file);
274 NodeFS.mkdirSync(NodePath.dirname(path), { recursive: true });
275 NodeFS.writeFileSync(path, content);
276 };
277 const git = (...args: Array<string>) =>
278 NodeChildProcess.execFileSync("git", args, { cwd: root, encoding: "utf8", env }).trim();
279 git("init");
280 for (const name of ["A", "B", "C"]) {
281 write(`${name}.txt`, name);
282 git("add", ".");
283 git("commit", "-m", name);
284 commits[name] = git("rev-parse", "HEAD");
285 }
286 for (const [remote, commit] of Object.entries(remotes)) {
287 git("remote", "add", remote, NodePath.join(root, "missing"));
288 git("update-ref", `refs/remotes/${remote}/main`, commits[commit]!);
289 }
290 } catch (error) {
291 NodeFS.rmSync(root, { recursive: true, force: true });
292 throw error;
293 }
294 return {
295 commits,
296 root,
297 [Symbol.dispose]() {
298 NodeFS.rmSync(root, { recursive: true, force: true });
299 },
300 };
301 }
302
303 const inFixture = <A, E>(effect: Effect.Effect<A, E, ChildProcessSpawner.ChildProcessSpawner>) =>
304 Effect.gen(function* () {
305 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
306 return yield* effect.pipe(
307 Effect.provideService(
308 ChildProcessSpawner.ChildProcessSpawner,
309 ChildProcessSpawner.make((command) => spawner.spawn(ChildProcess.setEnv(command, env))),
310 ),
311 );
312 }).pipe(Effect.provide(NodeServices.layer), Effect.runPromise);
313 const compare = (root: string, ref: Option.Option<string>) => inFixture(comparisonBase(root, ref));
314
315 it.each(["upstream", "team/upstream"])(
316 "compares against %s/main when the fork's origin/main lags it",
317 async (remote) => {
318 using project = fixture({ origin: "A", [remote]: "B" });
319 const result = await compare(project.root, Option.none());
320 assert.strictEqual(result.base, project.commits.B);
321 assert.strictEqual(result.ref, remote + "/main");
322 },
323 );
324
325 it("compares against an explicit ref instead of the closest remote main", async () => {
326 using project = fixture({ origin: "A", upstream: "B" });
327 const result = await compare(project.root, Option.some("origin/main"));
328 assert.strictEqual(result.base, project.commits.A);
329 assert.strictEqual(result.ref, "origin/main");
330 });
331
332 it("lists the ignored inputs that main's pathspecs list, without entering node_modules", async () => {
333 using project = fixture({});
334 const git = (...args: Array<string>) =>
335 NodeChildProcess.execFileSync("git", args, { cwd: project.root, encoding: "utf8", env });
336 const write = (file: string, content = "") => {
337 NodeFS.mkdirSync(NodePath.dirname(NodePath.join(project.root, file)), { recursive: true });
338 NodeFS.writeFileSync(NodePath.join(project.root, file), content);
339 };
340 write(".gitignore", "node_modules/\ndist/\n.generated/\n.env*\n*.gen.d.ts\nreplaced/\nskipped/");
341 for (const file of ["replaced", "skipped"]) write(file);
342 git("add", ".");
343 git("commit", "-m", "files that directories replace");
344 git("update-index", "--skip-worktree", "skipped");
345 for (const file of ["replaced", "skipped"]) NodeFS.rmSync(NodePath.join(project.root, file));
346 for (const file of [
347 ".env.local",
348 "src/types.gen.d.ts",
349 "src/.generated/routes.ts",
350 "dist/index.d.ts",
351 "dist/node_modules/pkg/index.d.ts",
352 "node_modules/pkg/index.d.ts",
353 // A package folder left behind by a branch switch.
354 "old/dist/a.d.ts",
355 "old/node_modules/pkg/index.d.ts",
356 "replaced/sub/a.d.ts",
357 "skipped/a.d.ts",
358 ])
359 write(file);
360 const command =
361 "ls-files -o -i --exclude-standard -- :(glob)**/.generated/** :(glob)**/.astro/** :(glob)**/*.d.ts :(glob)**/.env* :(exclude,glob)**/node_modules/**";
362 const old = git(...command.split(" "))
363 .split("\n")
364 .filter(Boolean)
365 .sort();
366 assert.includeMembers(old, ["old/dist/a.d.ts", "replaced/sub/a.d.ts", "skipped/a.d.ts"]);
367 assert.deepStrictEqual(await inFixture(ignoredCheckInputs(project.root)), old);
368 });
369
370 function launcherFixture() {
371 const root = NodeFS.realpathSync(
372 NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3 checks & (fixture) ")),
373 );
374 const write = (file: string, content: string) => {
375 const path = NodePath.join(root, file);
376 NodeFS.mkdirSync(NodePath.dirname(path), { recursive: true });
377 NodeFS.writeFileSync(path, content);
378 };
379 for (const file of [
380 "scripts/check-changed.ts",
381 "scripts/check-changed-command.ts",
382 ".github/scripts/ci-changes.cjs",
383 ]) {
384 write(file, NodeFS.readFileSync(NodePath.join(import.meta.dirname, "..", file), "utf8"));
385 }
386 write(".gitignore", "node_modules/\nrecords/\n");
387 write("pnpm-workspace.yaml", "packages:\n - packages/*\n");
388 write(
389 "package.json",
390 JSON.stringify({
391 type: "module",
392 scripts: {
393 "knip:check:files": "knip --include files",
394 "knip:check:exports": "knip --exports",
395 },
396 }),
397 );
398 write("packages/example/package.json", JSON.stringify({ name: "example" }));
399 const source = "packages/example/space & (draft) %PATH% ! 研究.ts";
400 write(source, "export const value = 1;\n");
401 write(
402 "node_modules/vite-plus/package.json",
403 JSON.stringify({ type: "module", exports: { "./bin": "./cli.js" } }),
404 );
405 write(
406 "node_modules/vite-plus/cli.js",
407 `
408 import fs from 'node:fs';
409 import path from 'node:path';
410 const args = process.argv.slice(2);
411 fs.writeFileSync(path.join(process.env.RECORDS, process.pid + '.json'), JSON.stringify({ args, cwd: process.cwd() }));
412 console.log('fixture stdout: ' + args[0]);
413 console.error('fixture stderr: ' + args[0]);
414 process.exit(args[0] === process.env.FAIL_STEP ? 23 : 0);
415 `,
416 );
417 // Select the Windows launcher without changing the Effect runtime's host platform.
418 write(
419 "scripts/check-changed.ts",
420 NodeFS.readFileSync(NodePath.join(import.meta.dirname, "check-changed.ts"), "utf8").replace(
421 "vitePlusCommand(args, platform)",
422 'vitePlusCommand(args, "win32")',
423 ),
424 );
425 for (const name of ["effect", "@effect/platform-node", "@t3tools/shared"]) {
426 const dest = NodePath.join(root, "node_modules", name);
427 NodeFS.mkdirSync(NodePath.dirname(dest), { recursive: true });
428 NodeFS.symlinkSync(
429 NodeFS.realpathSync(NodePath.join(import.meta.dirname, "node_modules", name)),
430 dest,
431 "junction",
432 );
433 }
434 const git = (...args: Array<string>) =>
435 NodeChildProcess.execFileSync("git", args, { cwd: root, stdio: "pipe", env });
436 git("init");
437 git("add", ".");
438 git(
439 "-c",
440 "user.name=Fixture",
441 "-c",
442 "user.email=fixture@example.invalid",
443 "-c",
444 "commit.gpgsign=false",
445 "-c",
446 "core.hooksPath=",
447 "commit",
448 "-m",
449 "fixture",
450 );
451 const records = NodePath.join(root, "records");
452 NodeFS.mkdirSync(records);
453 return {
454 root,
455 source,
456 write,
457 run(failStep = "") {
458 return NodeChildProcess.spawnSync(process.execPath, ["scripts/check-changed.ts", "HEAD"], {
459 cwd: root,
460 encoding: "utf8",
461 env: { ...env, TEMP: NodeOS.tmpdir(), RECORDS: records, FAIL_STEP: failStep },
462 });
463 },
464 calls: (): Array<{ args: Array<string>; cwd: string }> =>
465 NodeFS.readdirSync(records).map((file) =>
466 JSON.parse(NodeFS.readFileSync(NodePath.join(records, file), "utf8")),
467 ),
468 [Symbol.dispose]() {
469 NodeFS.rmSync(root, { recursive: true, force: true });
470 },
471 };
472 }
473
474 it("checks changed source paths through the Windows launcher", () => {
475 using project = launcherFixture();
476 project.write(project.source, "export const value = 2;\n");
477 const result = project.run();
478 expect(result.status, result.stdout + result.stderr).toBe(0);
479 const calls = project.calls();
480 expect(calls.find(({ args }) => args[0] === "lint")).toEqual({
481 args: ["lint", "--no-error-on-unmatched-pattern", project.source],
482 cwd: project.root,
483 });
484 expect(calls.some(({ args }) => args[0] === "fmt" && args.includes(project.source))).toBe(true);
485 expect(calls.some(({ args }) => args[0] === "run" && args.includes("typecheck"))).toBe(true);
486 expect(calls.some(({ args }) => args[0] === "install")).toBe(false);
487 expect(result.stdout).toContain("fixture stdout: lint");
488 expect(result.stdout).toContain("fixture stderr: lint");
489 });
490
491 it("lints an Astro component and runs knip when only it changed", () => {
492 using project = launcherFixture();
493 const page = "packages/example/Page.astro";
494 project.write(page, "---\nconst title = 1;\n---\n<h1>{title}</h1>\n");
495 const result = project.run();
496 expect(result.status, result.stdout + result.stderr).toBe(0);
497 const calls = project.calls();
498 expect(calls.find(({ args }) => args[0] === "lint")?.args).toEqual([
499 "lint",
500 "--no-error-on-unmatched-pattern",
501 page,
502 ]);
503 const commands = calls.map(({ args }) => args);
504 expect(commands).toContainEqual(["exec", "knip", "--include", "files"]);
505 expect(commands).toContainEqual(["exec", "knip", "--exports"]);
506 });
507
508 it.each(["install", "lint"])("reports a failing %s step with its output", (step) => {
509 using project = launcherFixture();
510 project.write(project.source, "export const value = 2;\n");
511 project.write(
512 "packages/example/package.json",
513 JSON.stringify({ name: "example", version: "1.0.1" }),
514 );
515 const result = project.run(step);
516 expect(result.status, result.stdout + result.stderr).toBe(1);
517 const install = project.calls().find(({ args }) => args[0] === "install");
518 expect(install?.args, result.stdout + result.stderr).toEqual([
519 "install",
520 "--lockfile-only",
521 "--ignore-scripts",
522 ]);
523 expect(install?.cwd).not.toBe(project.root);
524 expect(result.stdout + result.stderr).toContain(`fixture stdout: ${step}`);
525 expect(result.stdout + result.stderr).toContain(`fixture stderr: ${step}`);
526 expect(result.stdout).toContain(step === "install" ? "FAIL unused patches" : "FAIL lint");
527 if (step === "lint") expect(result.stdout).toContain("pass unused patches");
528 expect(
529 NodeFS.existsSync(NodePath.join(project.root, "node_modules/.cache/check-changed.json")),
530 ).toBe(false);
531 });