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

providerMaintenance.test.ts

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