Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

providerMaintenance.test.ts

736 lines25.4 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import { expect, it } from "@effect/vitest";
3import * as NodeChildProcess from "node:child_process";
4import * as NodeFS from "node:fs";
5import * as NodeServices from "@effect/platform-node/NodeServices";
6import * as NodeOS from "node:os";
7import * as NodePath from "node:path";
8import { ProviderDriverKind, ProviderInstanceId, type ServerProvider } from "@t3tools/contracts";
9import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
10import * as Crypto from "effect/Crypto";
11import * as Effect from "effect/Effect";
12import * as Sink from "effect/Sink";
13import * as Stream from "effect/Stream";
14import { HttpClient } from "effect/unstable/http";
15import { ChildProcessSpawner } from "effect/unstable/process";
16import {
17 createProviderVersionAdvisory,
18 enrichProviderSnapshotWithVersionAdvisory,
19 homebrewOwnershipFromCommandPath,
20 makeCachedProviderMaintenanceResolution,
21 makePackageManagedProviderMaintenanceResolver,
22 makeProviderMaintenanceCapabilities,
23 normalizeCommandPath,
24 npmGlobalPrefixFromCommandPath,
25 parseHomebrewLatestVersion,
26 ProviderVersionCache,
27 resolveLatestProviderVersion,
28 resolveProviderMaintenanceCapabilitiesEffect,
29 type ProviderMaintenanceCapabilities,
30} from "./providerMaintenance.ts";
31import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
32​
33const driver = (value: string) => ProviderDriverKind.make(value);
34// These write `#!/bin/sh` stubs and evaluate them with darwin/linux path
35// semantics; a Windows temp path cannot be split on `:`.
36const windowsHost = HostProcessPlatform.defaultValue() === "win32";
37const makeTempDir = (name: string) =>
38 Crypto.Crypto.pipe(
39 Effect.flatMap((crypto) => crypto.randomUUIDv4),
40 Effect.map((id) => NodePath.join(NodeOS.tmpdir(), `${name}-${id}`)),
41 );
42const isNativeTestCommandPath =
43 (expectedPathSegment: string) =>
44 (commandPath: string): boolean =>
45 normalizeCommandPath(commandPath).includes(expectedPathSegment);
46const packageToolUpdate = makePackageManagedProviderMaintenanceResolver({
47 provider: driver("packageTool"),
48 npmPackageName: "@example/package-tool",
49 nativeUpdate: null,
50});
51const nativePackageToolUpdate = makePackageManagedProviderMaintenanceResolver({
52 provider: driver("nativePackageTool"),
53 npmPackageName: "@example/native-package-tool",
54 nativeUpdate: {
55 args: ["update"],
56 isCommandPath: isNativeTestCommandPath("/.local/bin/native-package-tool"),
57 },
58});
59const installedPackageToolProvider: ServerProvider = {
60 instanceId: ProviderInstanceId.make("packageTool"),
61 driver: driver("packageTool"),
62 enabled: true,
63 installed: true,
64 version: "1.0.0",
65 status: "ready",
66 auth: { status: "authenticated" },
67 checkedAt: "2026-04-10T00:00:00.000Z",
68 models: [],
69 slashCommands: [],
70 skills: [],
71};
72const manualPackageTool: ProviderMaintenanceCapabilities = {
73 provider: driver("packageTool"),
74 packageName: "@example/package-tool",
75 update: null,
76};
77​
78function writeExecutable(path: string) {
79 NodeFS.mkdirSync(NodePath.dirname(path), { recursive: true });
80 NodeFS.writeFileSync(path, "#!/bin/sh\n");
81 NodeFS.chmodSync(path, 0o755);
83​
84/** Symlink `<tempDir>/bin/<name>` into a package entry point, like npm/pnpm do. */
85function linkIntoPackage(tempDir: string, name: string, packageSegments: ReadonlyArray<string>) {
86 const target = NodePath.join(tempDir, ...packageSegments, "bin", `${name}.js`);
87 writeExecutable(target);
88 const link = NodePath.join(tempDir, "bin", name);
89 NodeFS.mkdirSync(NodePath.dirname(link), { recursive: true });
90 NodeFS.symlinkSync(target, link);
91 return link;
93​
94const noSpawn = ChildProcessSpawner.make(() =>
95 Effect.die("maintenance resolution should not spawn a process here"),
96);
97​
98function stdoutSpawner(onSpawn: (command: string, args: ReadonlyArray<string>) => string) {
99 return ChildProcessSpawner.make((command) => {
100 const { command: executable, args } = command as unknown as {
101 readonly command: string;
102 readonly args: ReadonlyArray<string>;
103 };
104 return Effect.succeed(
105 ChildProcessSpawner.makeHandle({
106 pid: ChildProcessSpawner.ProcessId(1),
107 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
108 isRunning: Effect.succeed(false),
109 kill: () => Effect.void,
110 unref: Effect.succeed(Effect.void),
111 stdin: Sink.drain,
112 stdout: Stream.encodeText(Stream.make(onSpawn(executable, args))),
113 stderr: Stream.empty,
114 all: Stream.empty,
115 getInputFd: () => Sink.drain,
116 getOutputFd: () => Stream.empty,
117 }),
118 );
119 });
121​
122it.layer(NodeServices.layer)("providerMaintenance", (it) => {
123 it.effect("reads cached versions through the injectable cache reference", () =>
124 resolveLatestProviderVersion(manualPackageTool).pipe(
125 Effect.provideService(
126 ProviderVersionCache,
127 new Map([
128 [
129 "@example/package-tool",
130 {
131 expiresAt: Number.MAX_SAFE_INTEGER,
132 version: "9.9.9",
133 },
134 ],
135 ]),
136 ),
137 Effect.provideService(
138 HttpClient.HttpClient,
139 HttpClient.make(() =>
140 Effect.die("cached provider version should not make an HTTP request"),
141 ),
142 ),
143 Effect.map((version) => {
144 expect(version).toBe("9.9.9");
145 }),
146 ),
147 );
148​
149 it.effect("prefers the installer's own latest version over the npm registry", () =>
150 resolveLatestProviderVersion({ ...manualPackageTool, latestVersion: "1.2.0" }).pipe(
151 Effect.provideService(ProviderVersionCache, new Map()),
152 Effect.provideService(
153 HttpClient.HttpClient,
154 HttpClient.make(() =>
155 Effect.die("installer-reported latest should not make an HTTP request"),
156 ),
157 ),
158 Effect.map((version) => {
159 expect(version).toBe("1.2.0");
160 }),
161 ),
162 );
163​
164 it.effect("does not fetch latest provider versions when update checks are disabled", () =>
165 enrichProviderSnapshotWithVersionAdvisory(installedPackageToolProvider, manualPackageTool, {
166 enableProviderUpdateChecks: false,
167 }).pipe(
168 Effect.provideService(ProviderVersionCache, new Map()),
169 Effect.provideService(
170 HttpClient.HttpClient,
171 HttpClient.make(() =>
172 Effect.die("disabled provider update checks should not make an HTTP request"),
173 ),
174 ),
175 Effect.map((provider) => {
176 expect(provider.versionAdvisory).toMatchObject({
177 status: "unknown",
178 currentVersion: "1.0.0",
179 latestVersion: null,
180 checkedAt: "2026-04-10T00:00:00.000Z",
181 });
182 }),
183 ),
184 );
185​
186 it("marks providers with unknown current versions as unknown", () => {
187 expect(
188 createProviderVersionAdvisory({
189 driver: driver("packageTool"),
190 currentVersion: null,
191 latestVersion: "9.9.9",
192 }),
193 ).toMatchObject({
194 status: "unknown",
195 currentVersion: null,
196 latestVersion: "9.9.9",
197 });
198 });
199​
200 it("marks providers with unknown latest versions as unknown", () => {
201 expect(
202 createProviderVersionAdvisory({
203 driver: driver("packageTool"),
204 currentVersion: "1.0.0",
205 latestVersion: null,
206 }),
207 ).toMatchObject({
208 status: "unknown",
209 currentVersion: "1.0.0",
210 latestVersion: null,
211 message: null,
212 });
213 });
214​
215 it("keeps the manual update hint when the install is behind but unowned", () => {
216 expect(
217 createProviderVersionAdvisory({
218 driver: driver("packageTool"),
219 currentVersion: "2.1.110",
220 latestVersion: "2.1.117",
221 maintenanceCapabilities: manualPackageTool,
222 }),
223 ).toMatchObject({
224 status: "behind_latest",
225 latestVersion: "2.1.117",
226 updateCommand: null,
227 canUpdate: false,
228 message: "Install the update now or review provider settings.",
229 });
230 });
231​
232 it.effect("stays manual-only when the binary cannot be located", () =>
233 resolveProviderMaintenanceCapabilitiesEffect(packageToolUpdate, {
234 binaryPath: "package-tool",
235 env: { PATH: "" },
236 }).pipe(
237 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn),
238 Effect.map((capabilities) => {
239 expect(capabilities).toEqual(manualPackageTool);
240 }),
241 ),
242 );
243​
244 it.effect.skipIf(!symlinksSupported)(
245 "pins npm updates to the global prefix that owns the package",
246 () =>
247 Effect.gen(function* () {
248 const tempDir = yield* makeTempDir("t3-npm-capabilities");
249 const link = linkIntoPackage(tempDir, "package-tool", [
250 "lib",
251 "node_modules",
252 "@example",
253 "package-tool",
254 ]);
255​
256 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
257 packageToolUpdate,
258 {
259 binaryPath: link,
260 env: { PATH: "" },
261 },
262 ).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn));
263​
264 expect(capabilities).toEqual({
265 provider: driver("packageTool"),
266 packageName: "@example/package-tool",
267 update: {
268 command: `npm install -g --prefix ${tempDir} --allow-scripts=@example/package-tool @example/package-tool@latest`,
269 executable: "npm",
270 args: [
271 "install",
272 "-g",
273 "--prefix",
274 tempDir,
275 "--allow-scripts=@example/package-tool",
276 "@example/package-tool@latest",
277 ],
278 lockKey: `npm-global:${normalizeCommandPath(tempDir)}`,
279 },
280 });
281 }),
282 );
283​
284 it("derives the npm prefix only from the global lib/node_modules layout", () => {
285 expect(
286 npmGlobalPrefixFromCommandPath(
287 "/usr/local/lib/node_modules/@openai/codex/bin/codex.js",
288 "@openai/codex",
289 ),
290 ).toBe("/usr/local");
291 // A copy nested inside another package is not a global install.
292 expect(
293 npmGlobalPrefixFromCommandPath(
294 "/usr/local/lib/node_modules/other/node_modules/@openai/codex/bin/codex.js",
295 "@openai/codex",
296 ),
297 ).toBeNull();
298 expect(
299 npmGlobalPrefixFromCommandPath(
300 "/lib/node_modules/@openai/codex/bin/codex.js",
301 "@openai/codex",
302 ),
303 ).toBe("/");
304 // Neither is a project-local dependency.
305 expect(
306 npmGlobalPrefixFromCommandPath(
307 "/work/app/node_modules/@openai/codex/bin/codex.js",
308 "@openai/codex",
309 ),
310 ).toBeNull();
311 });
312​
313 it.effect("proves Windows npm ownership from the package manifest beside the shim", () =>
314 Effect.gen(function* () {
315 const tempDir = yield* makeTempDir("t3-npm-windows-capabilities");
316 const shim = NodePath.join(tempDir, "package-tool.cmd");
317 NodeFS.mkdirSync(tempDir, { recursive: true });
318 NodeFS.writeFileSync(shim, "@echo off\r\n");
319 NodeFS.mkdirSync(NodePath.join(tempDir, "node_modules", "@example", "package-tool"), {
320 recursive: true,
321 });
322 NodeFS.writeFileSync(
323 NodePath.join(tempDir, "node_modules", "@example", "package-tool", "package.json"),
324 "{}",
325 );
326​
327 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(packageToolUpdate, {
328 binaryPath: shim,
329 env: { PATH: "", PATHEXT: ".COM;.EXE;.BAT;.CMD" },
330 }).pipe(
331 Effect.provideService(HostProcessPlatform, "win32"),
332 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn),
333 );
334​
335 expect(capabilities.update).toMatchObject({
336 executable: "npm",
337 args: ["install", "-g", "--prefix", tempDir, expect.any(String), expect.any(String)],
338 });
339​
340 // The same layout on POSIX is a project checkout, not a global install.
341 const script = NodePath.join(tempDir, "package-tool");
342 writeExecutable(script);
343 const posix = yield* resolveProviderMaintenanceCapabilitiesEffect(packageToolUpdate, {
344 binaryPath: script,
345 env: { PATH: "" },
346 }).pipe(
347 Effect.provideService(HostProcessPlatform, "linux"),
348 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn),
349 );
350 expect(posix.update).toBeNull();
351 }),
352 );
353​
354 it.effect.skipIf(!symlinksSupported)(
355 "switches to pnpm updates when the real path lives in pnpm's global store",
356 () =>
357 Effect.gen(function* () {
358 const tempDir = yield* makeTempDir("t3-pnpm-capabilities");
359 const link = linkIntoPackage(tempDir, "package-tool", [
360 ".local",
361 "share",
362 "pnpm",
363 "global",
364 "5",
365 "node_modules",
366 "@example",
367 "package-tool",
368 ]);
369​
370 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
371 packageToolUpdate,
372 {
373 binaryPath: link,
374 env: { PATH: "" },
375 },
376 ).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn));
377​
378 expect(capabilities.update).toMatchObject({
379 command: "pnpm add -g @example/package-tool@latest",
380 lockKey: "pnpm-global",
381 });
382 }),
383 );
384​
385 it.effect.skipIf(windowsHost)(
386 "switches to bun updates when the resolved binary lives in bun's global bin",
387 () =>
388 Effect.gen(function* () {
389 const tempDir = yield* makeTempDir("t3-bun-capabilities");
390 const bunBinDir = NodePath.join(tempDir, ".bun", "bin");
391 writeExecutable(NodePath.join(bunBinDir, "package-tool"));
392​
393 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
394 packageToolUpdate,
395 {
396 binaryPath: "package-tool",
397 env: { PATH: bunBinDir },
398 },
399 ).pipe(
400 Effect.provideService(HostProcessPlatform, "darwin"),
401 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn),
402 );
403​
404 expect(capabilities.update).toMatchObject({
405 command: "bun i -g @example/package-tool@latest",
406 lockKey: "bun-global",
407 });
408 }),
409 );
410​
411 it.effect.skipIf(windowsHost)("switches to native updates and runs the resolved executable", () =>
412 Effect.gen(function* () {
413 const tempDir = yield* makeTempDir("t3-native-capabilities");
414 const nativeBinDir = NodePath.join(tempDir, ".local", "bin");
415 const nativePath = NodePath.join(nativeBinDir, "native-package-tool");
416 writeExecutable(nativePath);
417​
418 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
419 nativePackageToolUpdate,
420 {
421 binaryPath: "native-package-tool",
422 env: { PATH: nativeBinDir },
423 },
424 ).pipe(
425 Effect.provideService(HostProcessPlatform, "darwin"),
426 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn),
427 );
428​
429 expect(capabilities).toEqual({
430 provider: driver("nativePackageTool"),
431 packageName: "@example/native-package-tool",
432 update: {
433 command: `${nativePath} update`,
434 executable: nativePath,
435 args: ["update"],
436 lockKey: "nativePackageTool-native",
437 },
438 });
439 }),
440 );
441​
442 // Regression for #9850: an explicit native path outside PATH, with spaces,
443 // must be what actually gets spawned.
444 it.effect.skipIf(windowsHost)("runs an explicit native updater outside PATH", () =>
445 Effect.gen(function* () {
446 const tempDir = yield* makeTempDir("t3-native-update");
447 const nativePath = NodePath.join(
448 tempDir,
449 "with spaces",
450 ".local",
451 "bin",
452 "native-package-tool",
453 );
454 NodeFS.mkdirSync(NodePath.dirname(nativePath), { recursive: true });
455 NodeFS.writeFileSync(nativePath, "#!/bin/sh\nprintf '%s' \"$1\"\n");
456 NodeFS.chmodSync(nativePath, 0o755);
457​
458 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
459 nativePackageToolUpdate,
460 { binaryPath: nativePath, env: { PATH: "" } },
461 ).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn));
462​
463 expect(capabilities.update?.executable).toBe(nativePath);
464 const result = NodeChildProcess.spawnSync(
465 capabilities.update!.executable,
466 capabilities.update!.args,
467 { env: { PATH: "" }, encoding: "utf8" },
468 );
469 expect(result.status).toBe(0);
470 expect(result.stdout).toBe("update");
471 }),
472 );
473​
474 it.effect.skipIf(!symlinksSupported)(
475 "prefers npm ownership over the Node keg the package lives under",
476 () =>
477 Effect.gen(function* () {
478 // `brew install node` keeps npm globals inside the node keg.
479 const tempDir = yield* makeTempDir("t3-homebrew-node-capabilities");
480 const keg = NodePath.join(tempDir, "Cellar", "node", "22.1.0");
481 const target = NodePath.join(
482 keg,
483 "lib",
484 "node_modules",
485 "@example",
486 "package-tool",
487 "bin",
488 "package-tool.js",
489 );
490 writeExecutable(target);
491 const link = NodePath.join(tempDir, "bin", "package-tool");
492 NodeFS.mkdirSync(NodePath.dirname(link), { recursive: true });
493 NodeFS.symlinkSync(target, link);
494​
495 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
496 packageToolUpdate,
497 {
498 binaryPath: link,
499 env: { PATH: "" },
500 },
501 ).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn));
502​
503 expect(capabilities.update).toMatchObject({
504 executable: "npm",
505 args: expect.arrayContaining(["--prefix", keg]),
506 lockKey: `npm-global:${normalizeCommandPath(keg)}`,
507 });
508 }),
509 );
510​
511 it("quotes copyable command words the shell would split", () => {
512 const posix = makeProviderMaintenanceCapabilities({
513 provider: driver("packageTool"),
514 packageName: "@example/package-tool",
515 updateExecutable: "npm",
516 updateArgs: [
517 "install",
518 "-g",
519 "--prefix",
520 "/Users/Jane Doe/.npm-global",
521 "@example/package-tool@latest",
522 ],
523 updateLockKey: "npm-global",
524 platform: "darwin",
525 });
526 expect(posix.update?.command).toBe(
527 "npm install -g --prefix '/Users/Jane Doe/.npm-global' @example/package-tool@latest",
528 );
529 const windows = makeProviderMaintenanceCapabilities({
530 provider: driver("packageTool"),
531 packageName: null,
532 updateExecutable: "C:\\Program Files\\Tool\\tool.exe",
533 updateArgs: ["update"],
534 updateLockKey: "tool",
535 platform: "win32",
536 });
537 expect(windows.update?.command).toBe("& 'C:\\Program Files\\Tool\\tool.exe' update");
538 });
539​
540 it.effect.skipIf(windowsHost)("carries the native updater's environment into the action", () =>
541 Effect.gen(function* () {
542 const tempDir = yield* makeTempDir("t3-native-env");
543 const nativePath = NodePath.join(tempDir, ".local", "bin", "native-package-tool");
544 writeExecutable(nativePath);
545 const resolver = makePackageManagedProviderMaintenanceResolver({
546 provider: driver("nativePackageTool"),
547 npmPackageName: "@example/native-package-tool",
548 nativeUpdate: {
549 args: ["update"],
550 isCommandPath: isNativeTestCommandPath("/.local/bin/native-package-tool"),
551 env: { TOOL_HOME: tempDir },
552 },
553 });
554​
555 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(resolver, {
556 binaryPath: nativePath,
557 env: { PATH: "" },
558 }).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn));
559​
560 expect(capabilities.update?.env).toEqual({ TOOL_HOME: tempDir });
561 }),
562 );
563​
564 it("recognizes Homebrew kegs and casks from the real executable path", () => {
565 expect(
566 homebrewOwnershipFromCommandPath("/opt/homebrew/Cellar/claude-code@latest/2.1.0/bin/claude"),
567 ).toEqual({ kind: "formula", name: "claude-code@latest", prefix: "/opt/homebrew" });
568 expect(homebrewOwnershipFromCommandPath("/usr/local/Caskroom/codex/0.148.0/codex")).toEqual({
569 kind: "cask",
570 name: "codex",
571 prefix: "/usr/local",
572 });
573 // A plain /usr/local/bin binary is not evidence of Homebrew (#8832).
574 expect(homebrewOwnershipFromCommandPath("/usr/local/bin/codex")).toBeNull();
575 // A keg elsewhere reports its prefix so the resolver can reject it against
576 // `brew --prefix`.
577 expect(homebrewOwnershipFromCommandPath("/srv/Cellar/claude/1.0.0/bin/claude")).toMatchObject({
578 prefix: "/srv",
579 });
580 });
581​
582 it.effect.skipIf(windowsHost)(
583 "stays manual-only for an explicit binary path that does not exist",
584 () =>
585 Effect.gen(function* () {
586 const tempDir = yield* makeTempDir("t3-missing-native-capabilities");
587 const missingPath = NodePath.join(tempDir, ".local", "bin", "native-package-tool");
588​
589 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
590 nativePackageToolUpdate,
591 { binaryPath: missingPath, env: { PATH: "" } },
592 ).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn));
593​
594 expect(capabilities.update).toBeNull();
595 }),
596 );
597​
598 it.effect.skipIf(!symlinksSupported)(
599 "upgrades the Homebrew cask that owns the binary and compares against its version",
600 () =>
601 Effect.gen(function* () {
602 const tempDir = yield* makeTempDir("t3-homebrew-capabilities");
603 const brewBinDir = NodePath.join(tempDir, "brew-bin");
604 const brewPath = NodePath.join(brewBinDir, "brew");
605 writeExecutable(brewPath);
606 const caskBinary = NodePath.join(
607 tempDir,
608 "Caskroom",
609 "package-tool",
610 "0.148.0",
611 "package-tool",
612 );
613 writeExecutable(caskBinary);
614 const link = NodePath.join(tempDir, "bin", "package-tool");
615 NodeFS.mkdirSync(NodePath.dirname(link), { recursive: true });
616 NodeFS.symlinkSync(caskBinary, link);
617 const spawned: Array<ReadonlyArray<string>> = [];
618​
619 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
620 packageToolUpdate,
621 {
622 binaryPath: link,
623 env: { PATH: brewBinDir },
624 },
625 ).pipe(
626 Effect.provideService(HostProcessPlatform, "darwin"),
627 Effect.provideService(
628 ChildProcessSpawner.ChildProcessSpawner,
629 stdoutSpawner((command, args) => {
630 spawned.push([command, ...args]);
631 return args[0] === "--prefix"
632 ? `${tempDir}\n`
633 : JSON.stringify({ casks: [{ version: "0.148.0,42" }] });
634 }),
635 ),
636 );
637​
638 expect(spawned).toEqual([
639 [brewPath, "--prefix"],
640 [brewPath, "info", "--json=v2", "package-tool"],
641 ]);
642 expect(capabilities).toEqual({
643 provider: driver("packageTool"),
644 packageName: "@example/package-tool",
645 latestVersion: "0.148.0",
646 update: {
647 command: "brew upgrade --cask package-tool",
648 executable: brewPath,
649 args: ["upgrade", "--cask", "package-tool"],
650 lockKey: "homebrew",
651 },
652 });
653 }),
654 );
655​
656 it.effect.skipIf(windowsHost)(
657 "stays manual-only when the keg is not under the resolved brew's prefix",
658 () =>
659 Effect.gen(function* () {
660 const tempDir = yield* makeTempDir("t3-homebrew-foreign-prefix");
661 const brewBinDir = NodePath.join(tempDir, "brew-bin");
662 writeExecutable(NodePath.join(brewBinDir, "brew"));
663 const kegBinary = NodePath.join(
664 tempDir,
665 "elsewhere",
666 "Cellar",
667 "package-tool",
668 "1.0.0",
669 "bin",
670 "package-tool",
671 );
672 writeExecutable(kegBinary);
673​
674 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
675 packageToolUpdate,
676 {
677 binaryPath: kegBinary,
678 env: { PATH: brewBinDir },
679 },
680 ).pipe(
681 Effect.provideService(HostProcessPlatform, "darwin"),
682 Effect.provideService(
683 ChildProcessSpawner.ChildProcessSpawner,
684 stdoutSpawner(() => "/opt/homebrew\n"),
685 ),
686 );
687​
688 expect(capabilities).toEqual(manualPackageTool);
689 }),
690 );
691​
692 it("reads the stable formula version from brew info", () => {
693 const info = JSON.stringify({ formulae: [{ versions: { stable: "2.1.5" } }] });
694 const formula = { kind: "formula", name: "claude-code", prefix: "/opt/homebrew" } as const;
695 expect(parseHomebrewLatestVersion(info, formula)).toBe("2.1.5");
696 expect(parseHomebrewLatestVersion("not json", formula)).toBeNull();
697 });
698​
699 it.effect.skipIf(windowsHost)(
700 "disables one-click updates for explicit custom binary paths it cannot safely map",
701 () =>
702 Effect.gen(function* () {
703 const tempDir = yield* makeTempDir("t3-custom-capabilities");
704 const customPath = NodePath.join(tempDir, "tools", "package-tool");
705 writeExecutable(customPath);
706​
707 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
708 packageToolUpdate,
709 {
710 binaryPath: customPath,
711 env: { PATH: "" },
712 },
713 ).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn));
714​
715 expect(capabilities).toEqual(manualPackageTool);
716 }),
717 );
718​
719 it.effect("caches resolution until a fresh read is requested", () =>
720 Effect.gen(function* () {
721 let resolutions = 0;
722 const resolve = yield* makeCachedProviderMaintenanceResolution(
723 Effect.sync(() => {
724 resolutions += 1;
725 return manualPackageTool;
726 }),
727 );
728 yield* resolve();
729 yield* resolve();
730 expect(resolutions).toBe(1);
731 yield* resolve({ fresh: true });
732 yield* resolve();
733 expect(resolutions).toBe(2);
734 }),
735 );
736});