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

providerMaintenance.test.ts

615 lines19.3 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import { expect, it } from "@effect/vitest";
3import * as NodeFS from "node:fs";
4import * as NodeServices from "@effect/platform-node/NodeServices";
5import * as NodeOS from "node:os";
6import * as NodePath from "node:path";
7import { ProviderDriverKind, ProviderInstanceId, type ServerProvider } from "@t3tools/contracts";
8import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
9import * as Crypto from "effect/Crypto";
10import * as Effect from "effect/Effect";
11import { HttpClient } from "effect/unstable/http";
12import {
13 createProviderVersionAdvisory,
14 enrichProviderSnapshotWithVersionAdvisory,
15 makePackageManagedProviderMaintenanceResolver,
16 makeProviderMaintenanceCapabilities,
17 makeStaticProviderMaintenanceResolver,
18 normalizeCommandPath,
19 ProviderVersionCache,
20 resolveLatestProviderVersion,
21 resolveProviderMaintenanceCapabilitiesEffect,
22} from "./providerMaintenance.ts";
23import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
24​
25const driver = (value: string) => ProviderDriverKind.make(value);
26// These write `#!/bin/sh` stubs and resolve them through a darwin-mocked
27// PATH walk; a Windows temp path cannot be split on `:`.
28const windowsHost = HostProcessPlatform.defaultValue() === "win32";
29​
30const makeTempDir = (name: string) =>
31 Crypto.Crypto.pipe(
32 Effect.flatMap((crypto) => crypto.randomUUIDv4),
33 Effect.map((id) => NodePath.join(NodeOS.tmpdir(), `${name}-${id}`)),
34 );
35const isNativeTestCommandPath =
36 (expectedPathSegment: string) =>
37 (commandPath: string): boolean =>
38 normalizeCommandPath(commandPath).includes(expectedPathSegment);
39const packageToolUpdate = makePackageManagedProviderMaintenanceResolver({
40 provider: driver("packageTool"),
41 npmPackageName: "@example/package-tool",
42 homebrewFormula: "package-tool",
43 nativeUpdate: null,
44});
45const nativePackageToolUpdate = makePackageManagedProviderMaintenanceResolver({
46 provider: driver("nativePackageTool"),
47 npmPackageName: "@example/native-package-tool",
48 homebrewFormula: "native-package-tool",
49 nativeUpdate: {
50 executable: "native-package-tool",
51 args: ["update"],
52 lockKey: "native-package-tool-native",
53 isCommandPath: isNativeTestCommandPath("/.local/bin/native-package-tool"),
54 },
55});
56const scopedPackageToolUpdate = makePackageManagedProviderMaintenanceResolver({
57 provider: driver("scopedPackageTool"),
58 npmPackageName: "@example/scoped-package-tool",
59 homebrewFormula: "example/tap/scoped-package-tool",
60 nativeUpdate: {
61 executable: "scoped-package-tool",
62 args: ["upgrade"],
63 lockKey: "scoped-package-tool-native",
64 isCommandPath: isNativeTestCommandPath("/.scoped-package-tool/bin/scoped-package-tool"),
65 },
66});
67const staticToolUpdate = makeStaticProviderMaintenanceResolver(
68 makeProviderMaintenanceCapabilities({
69 provider: driver("staticTool"),
70 packageName: null,
71 updateExecutable: "static-tool",
72 updateArgs: ["update"],
73 updateLockKey: "static-tool",
74 }),
75);
76const installedPackageToolProvider: ServerProvider = {
77 instanceId: ProviderInstanceId.make("packageTool"),
78 driver: driver("packageTool"),
79 enabled: true,
80 installed: true,
81 version: "1.0.0",
82 status: "ready",
83 auth: { status: "authenticated" },
84 checkedAt: "2026-04-10T00:00:00.000Z",
85 models: [],
86 slashCommands: [],
87 skills: [],
88};
89​
90it.layer(NodeServices.layer)("providerMaintenance", (it) => {
91 it.effect("reads cached versions through the injectable cache reference", () =>
92 resolveLatestProviderVersion(packageToolUpdate.resolve()).pipe(
93 Effect.provideService(
94 ProviderVersionCache,
95 new Map([
96 [
97 "@example/package-tool",
98 {
99 expiresAt: Number.MAX_SAFE_INTEGER,
100 version: "9.9.9",
101 },
102 ],
103 ]),
104 ),
105 Effect.provideService(
106 HttpClient.HttpClient,
107 HttpClient.make(() =>
108 Effect.die("cached provider version should not make an HTTP request"),
109 ),
110 ),
111 Effect.map((version) => {
112 expect(version).toBe("9.9.9");
113 }),
114 ),
115 );
116​
117 it.effect("does not fetch latest provider versions when update checks are disabled", () =>
118 enrichProviderSnapshotWithVersionAdvisory(
119 installedPackageToolProvider,
120 packageToolUpdate.resolve(),
121 {
122 enableProviderUpdateChecks: false,
123 },
124 ).pipe(
125 Effect.provideService(ProviderVersionCache, new Map()),
126 Effect.provideService(
127 HttpClient.HttpClient,
128 HttpClient.make(() =>
129 Effect.die("disabled provider update checks should not make an HTTP request"),
130 ),
131 ),
132 Effect.map((provider) => {
133 expect(provider.versionAdvisory).toMatchObject({
134 status: "unknown",
135 currentVersion: "1.0.0",
136 latestVersion: null,
137 checkedAt: "2026-04-10T00:00:00.000Z",
138 });
139 }),
140 ),
141 );
142​
143 it("marks providers with unknown current versions as unknown", () => {
144 expect(
145 createProviderVersionAdvisory({
146 driver: driver("packageTool"),
147 currentVersion: null,
148 latestVersion: "9.9.9",
149 }),
150 ).toMatchObject({
151 status: "unknown",
152 currentVersion: null,
153 latestVersion: "9.9.9",
154 });
155 });
156​
157 it("marks providers with unknown latest versions as unknown", () => {
158 expect(
159 createProviderVersionAdvisory({
160 driver: driver("packageTool"),
161 currentVersion: "1.0.0",
162 latestVersion: null,
163 }),
164 ).toMatchObject({
165 status: "unknown",
166 currentVersion: "1.0.0",
167 latestVersion: null,
168 message: null,
169 });
170 });
171​
172 it("marks installed providers behind latest when a newer provider version is available", () => {
173 expect(
174 createProviderVersionAdvisory({
175 driver: driver("nativePackageTool"),
176 currentVersion: "2.1.110",
177 latestVersion: "2.1.117",
178 maintenanceCapabilities: nativePackageToolUpdate.resolve(),
179 }),
180 ).toMatchObject({
181 status: "behind_latest",
182 currentVersion: "2.1.110",
183 latestVersion: "2.1.117",
184 updateCommand:
185 "npm install -g --allow-scripts=@example/native-package-tool @example/native-package-tool@latest",
186 canUpdate: true,
187 message: "Install the update now or review provider settings.",
188 });
189 });
190​
191 it("keeps update commands owned by provider maintenance capabilities", () => {
192 expect(staticToolUpdate.resolve()).toEqual({
193 provider: driver("staticTool"),
194 packageName: null,
195 update: {
196 command: "static-tool update",
197​
198 executable: "static-tool",
199​
200 args: ["update"],
201​
202 lockKey: "static-tool",
203 },
204 });
205 });
206​
207 it.effect.skipIf(windowsHost)(
208 "switches package-managed providers to vite-plus updates when the resolved binary lives in vite-plus global bin",
209 () =>
210 Effect.gen(function* () {
211 const tempDir = yield* makeTempDir("t3-vite-plus-capabilities");
212 const vitePlusBinDir = NodePath.join(tempDir, ".vite-plus", "bin");
213 NodeFS.mkdirSync(vitePlusBinDir, { recursive: true });
214 const packageToolPath = NodePath.join(vitePlusBinDir, "package-tool");
215 NodeFS.writeFileSync(packageToolPath, "#!/bin/sh\n");
216 NodeFS.chmodSync(packageToolPath, 0o755);
217​
218 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
219 packageToolUpdate,
220 {
221 binaryPath: "package-tool",
222 env: {
223 PATH: vitePlusBinDir,
224 },
225 },
226 ).pipe(Effect.provideService(HostProcessPlatform, "darwin"));
227​
228 expect(capabilities).toEqual({
229 provider: driver("packageTool"),
230 packageName: "@example/package-tool",
231 update: {
232 command: "vp i -g @example/package-tool",
233​
234 executable: "vp",
235​
236 args: ["i", "-g", "@example/package-tool"],
237​
238 lockKey: "vite-plus-global",
239 },
240 });
241 }),
242 );
243​
244 it.effect(
245 "switches package-managed providers to bun updates when the resolved binary lives in bun's global bin",
246 () =>
247 Effect.gen(function* () {
248 const tempDir = yield* makeTempDir("t3-bun-capabilities");
249 const bunBinDir = NodePath.join(tempDir, ".bun", "bin");
250 NodeFS.mkdirSync(bunBinDir, { recursive: true });
251 NodeFS.writeFileSync(NodePath.join(bunBinDir, "native-package-tool.exe"), "MZ");
252​
253 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
254 nativePackageToolUpdate,
255 {
256 binaryPath: "native-package-tool",
257 env: {
258 PATH: bunBinDir,
259 PATHEXT: ".COM;.EXE;.BAT;.CMD",
260 },
261 },
262 ).pipe(Effect.provideService(HostProcessPlatform, "win32"));
263​
264 expect(capabilities).toEqual({
265 provider: driver("nativePackageTool"),
266 packageName: "@example/native-package-tool",
267 update: {
268 command: "bun i -g @example/native-package-tool@latest",
269​
270 executable: "bun",
271​
272 args: ["i", "-g", "@example/native-package-tool@latest"],
273​
274 lockKey: "bun-global",
275 },
276 });
277 }),
278 );
279​
280 it.effect.skipIf(windowsHost)(
281 "switches package-managed providers to pnpm updates when the resolved binary lives in pnpm's global bin",
282 () =>
283 Effect.gen(function* () {
284 const tempDir = yield* makeTempDir("t3-pnpm-capabilities");
285 const pnpmHomeDir = NodePath.join(tempDir, ".local", "share", "pnpm");
286 NodeFS.mkdirSync(pnpmHomeDir, { recursive: true });
287 const scopedPackageToolPath = NodePath.join(pnpmHomeDir, "scoped-package-tool");
288 NodeFS.writeFileSync(scopedPackageToolPath, "#!/bin/sh\n");
289 NodeFS.chmodSync(scopedPackageToolPath, 0o755);
290​
291 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
292 scopedPackageToolUpdate,
293 {
294 binaryPath: "scoped-package-tool",
295 env: {
296 PATH: pnpmHomeDir,
297 },
298 },
299 ).pipe(Effect.provideService(HostProcessPlatform, "darwin"));
300​
301 expect(capabilities).toEqual({
302 provider: driver("scopedPackageTool"),
303 packageName: "@example/scoped-package-tool",
304 update: {
305 command: "pnpm add -g @example/scoped-package-tool@latest",
306​
307 executable: "pnpm",
308​
309 args: ["add", "-g", "@example/scoped-package-tool@latest"],
310​
311 lockKey: "pnpm-global",
312 },
313 });
314 }),
315 );
316​
317 it("switches package-tool to Homebrew updates when the binary resolves through Homebrew", () => {
318 expect(
319 packageToolUpdate.resolve({
320 binaryPath: "/opt/homebrew/bin/package-tool",
321 env: {
322 PATH: "",
323 },
324 }),
325 ).toEqual({
326 provider: driver("packageTool"),
327 packageName: "@example/package-tool",
328 update: {
329 command: "brew upgrade package-tool",
330​
331 executable: "brew",
332​
333 args: ["upgrade", "package-tool"],
334​
335 lockKey: "homebrew",
336 },
337 });
338 });
339​
340 it.effect.skipIf(windowsHost)(
341 "switches native-package-tool to native updates when the binary resolves through the native installer",
342 () =>
343 Effect.gen(function* () {
344 const tempDir = yield* makeTempDir("t3-native-package-tool-native-capabilities");
345 const nativeBinDir = NodePath.join(tempDir, ".local", "bin");
346 NodeFS.mkdirSync(nativeBinDir, { recursive: true });
347 const nativePackageToolPath = NodePath.join(nativeBinDir, "native-package-tool");
348 NodeFS.writeFileSync(nativePackageToolPath, "#!/bin/sh\n");
349 NodeFS.chmodSync(nativePackageToolPath, 0o755);
350​
351 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
352 nativePackageToolUpdate,
353 {
354 binaryPath: "native-package-tool",
355 env: {
356 PATH: nativeBinDir,
357 },
358 },
359 ).pipe(Effect.provideService(HostProcessPlatform, "darwin"));
360​
361 expect(capabilities).toEqual({
362 provider: driver("nativePackageTool"),
363 packageName: "@example/native-package-tool",
364 update: {
365 command: "native-package-tool update",
366​
367 executable: "native-package-tool",
368​
369 args: ["update"],
370​
371 lockKey: "native-package-tool-native",
372 },
373 });
374 }),
375 );
376​
377 it.effect.skipIf(windowsHost)(
378 "switches scoped-package-tool to native upgrades when the binary resolves through the standalone installer",
379 () =>
380 Effect.gen(function* () {
381 const tempDir = yield* makeTempDir("t3-scoped-package-tool-native-capabilities");
382 const nativeBinDir = NodePath.join(tempDir, ".scoped-package-tool", "bin");
383 NodeFS.mkdirSync(nativeBinDir, { recursive: true });
384 const scopedPackageToolPath = NodePath.join(nativeBinDir, "scoped-package-tool");
385 NodeFS.writeFileSync(scopedPackageToolPath, "#!/bin/sh\n");
386 NodeFS.chmodSync(scopedPackageToolPath, 0o755);
387​
388 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
389 scopedPackageToolUpdate,
390 {
391 binaryPath: "scoped-package-tool",
392 env: {
393 PATH: nativeBinDir,
394 },
395 },
396 ).pipe(Effect.provideService(HostProcessPlatform, "darwin"));
397​
398 expect(capabilities).toEqual({
399 provider: driver("scopedPackageTool"),
400 packageName: "@example/scoped-package-tool",
401 update: {
402 command: "scoped-package-tool upgrade",
403​
404 executable: "scoped-package-tool",
405​
406 args: ["upgrade"],
407​
408 lockKey: "scoped-package-tool-native",
409 },
410 });
411 }),
412 );
413​
414 it("switches native-package-tool to Homebrew updates when the binary resolves through Homebrew", () => {
415 expect(
416 nativePackageToolUpdate.resolve({
417 binaryPath: "/opt/homebrew/bin/native-package-tool",
418 env: {
419 PATH: "",
420 },
421 }),
422 ).toEqual({
423 provider: driver("nativePackageTool"),
424 packageName: "@example/native-package-tool",
425 update: {
426 command: "brew upgrade native-package-tool",
427​
428 executable: "brew",
429​
430 args: ["upgrade", "native-package-tool"],
431​
432 lockKey: "homebrew",
433 },
434 });
435 });
436​
437 it("switches scoped-package-tool to Homebrew updates when the binary resolves through Homebrew", () => {
438 expect(
439 scopedPackageToolUpdate.resolve({
440 binaryPath: "/opt/homebrew/bin/scoped-package-tool",
441 env: {
442 PATH: "",
443 },
444 }),
445 ).toEqual({
446 provider: driver("scopedPackageTool"),
447 packageName: "@example/scoped-package-tool",
448 update: {
449 command: "brew upgrade example/tap/scoped-package-tool",
450​
451 executable: "brew",
452​
453 args: ["upgrade", "example/tap/scoped-package-tool"],
454​
455 lockKey: "homebrew",
456 },
457 });
458 });
459​
460 it.effect.skipIf(!symlinksSupported)(
461 "keeps npm updates for binaries symlinked into npm's global node_modules tree",
462 () =>
463 Effect.gen(function* () {
464 const tempDir = yield* makeTempDir("t3-npm-capabilities");
465 const binDir = NodePath.join(tempDir, "bin");
466 const packageBinDir = NodePath.join(
467 tempDir,
468 "lib",
469 "node_modules",
470 "@example",
471 "package-tool",
472 "bin",
473 );
474 NodeFS.mkdirSync(binDir, { recursive: true });
475 NodeFS.mkdirSync(packageBinDir, { recursive: true });
476 const packageBinPath = NodePath.join(packageBinDir, "package-tool.js");
477 const symlinkPath = NodePath.join(binDir, "package-tool");
478 NodeFS.writeFileSync(packageBinPath, "#!/usr/bin/env node\n");
479 NodeFS.chmodSync(packageBinPath, 0o755);
480 NodeFS.symlinkSync(packageBinPath, symlinkPath);
481​
482 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
483 packageToolUpdate,
484 {
485 binaryPath: symlinkPath,
486 env: {
487 PATH: "",
488 },
489 },
490 );
491​
492 expect(capabilities).toEqual({
493 provider: driver("packageTool"),
494 packageName: "@example/package-tool",
495 update: {
496 command:
497 "npm install -g --allow-scripts=@example/package-tool @example/package-tool@latest",
498​
499 executable: "npm",
500​
501 args: [
502 "install",
503 "-g",
504 "--allow-scripts=@example/package-tool",
505 "@example/package-tool@latest",
506 ],
507​
508 lockKey: "npm-global",
509 },
510 });
511 }),
512 );
513​
514 it.effect.skipIf(!symlinksSupported)(
515 "uses Effect FileSystem realPath when detecting pnpm global symlinks",
516 () =>
517 Effect.gen(function* () {
518 const tempDir = yield* makeTempDir("t3-pnpm-realpath-capabilities");
519 const binDir = NodePath.join(tempDir, "bin");
520 const packageBinDir = NodePath.join(
521 tempDir,
522 ".local",
523 "share",
524 "pnpm",
525 "global",
526 "5",
527 "node_modules",
528 "@example",
529 "package-tool",
530 "bin",
531 );
532 NodeFS.mkdirSync(binDir, { recursive: true });
533 NodeFS.mkdirSync(packageBinDir, { recursive: true });
534 const packageBinPath = NodePath.join(packageBinDir, "package-tool.js");
535 const symlinkPath = NodePath.join(binDir, "package-tool");
536 NodeFS.writeFileSync(packageBinPath, "#!/usr/bin/env node\n");
537 NodeFS.chmodSync(packageBinPath, 0o755);
538 NodeFS.symlinkSync(packageBinPath, symlinkPath);
539​
540 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
541 packageToolUpdate,
542 {
543 binaryPath: symlinkPath,
544 env: {
545 PATH: "",
546 },
547 },
548 );
549​
550 expect(capabilities).toEqual({
551 provider: driver("packageTool"),
552 packageName: "@example/package-tool",
553 update: {
554 command: "pnpm add -g @example/package-tool@latest",
555​
556 executable: "pnpm",
557​
558 args: ["add", "-g", "@example/package-tool@latest"],
559​
560 lockKey: "pnpm-global",
561 },
562 });
563 }),
564 );
565​
566 it("allows the package's own install scripts in npm global updates", () => {
567 const claudeUpdate = makePackageManagedProviderMaintenanceResolver({
568 provider: driver("claudeAgent"),
569 npmPackageName: "@anthropic-ai/claude-code",
570 homebrewFormula: "claude-code",
571 nativeUpdate: {
572 executable: "claude",
573 args: ["update"],
574 lockKey: "claude-native",
575 isCommandPath: isNativeTestCommandPath("/.local/bin/claude"),
576 },
577 });
578​
579 expect(claudeUpdate.resolve()).toEqual({
580 provider: driver("claudeAgent"),
581 packageName: "@anthropic-ai/claude-code",
582 update: {
583 command:
584 "npm install -g --allow-scripts=@anthropic-ai/claude-code @anthropic-ai/claude-code@latest",
585​
586 executable: "npm",
587​
588 args: [
589 "install",
590 "-g",
591 "--allow-scripts=@anthropic-ai/claude-code",
592 "@anthropic-ai/claude-code@latest",
593 ],
594​
595 lockKey: "npm-global",
596 },
597 });
598 });
599​
600 it("disables one-click updates for explicit custom binary paths it cannot safely map", () => {
601 expect(
602 packageToolUpdate.resolve({
603 binaryPath: "C:\\Tools\\package-tool\\package-tool.exe",
604 env: {
605 PATH: "",
606 PATHEXT: ".COM;.EXE;.BAT;.CMD",
607 },
608 }),
609 ).toEqual({
610 provider: driver("packageTool"),
611 packageName: "@example/package-tool",
612 update: null,
613 });
614 });
615});