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

providerMaintenance.test.ts

559 lines17.5 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: "npm install -g @example/native-package-tool@latest",
180 canUpdate: true,
181 message: "Install the update now or review provider settings.",
182 });
183 });
184​
185 it("keeps update commands owned by provider maintenance capabilities", () => {
186 expect(staticToolUpdate.resolve()).toEqual({
187 provider: driver("staticTool"),
188 packageName: null,
189 update: {
190 command: "static-tool update",
191​
192 executable: "static-tool",
193​
194 args: ["update"],
195​
196 lockKey: "static-tool",
197 },
198 });
199 });
200​
201 it.effect(
202 "switches package-managed providers to vite-plus updates when the resolved binary lives in vite-plus global bin",
203 () =>
204 Effect.gen(function* () {
205 const tempDir = yield* makeTempDir("t3-vite-plus-capabilities");
206 const vitePlusBinDir = NodePath.join(tempDir, ".vite-plus", "bin");
207 NodeFS.mkdirSync(vitePlusBinDir, { recursive: true });
208 const packageToolPath = NodePath.join(vitePlusBinDir, "package-tool");
209 NodeFS.writeFileSync(packageToolPath, "#!/bin/sh\n");
210 NodeFS.chmodSync(packageToolPath, 0o755);
211​
212 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
213 packageToolUpdate,
214 {
215 binaryPath: "package-tool",
216 env: {
217 PATH: vitePlusBinDir,
218 },
219 },
220 ).pipe(Effect.provideService(HostProcessPlatform, "darwin"));
221​
222 expect(capabilities).toEqual({
223 provider: driver("packageTool"),
224 packageName: "@example/package-tool",
225 update: {
226 command: "vp i -g @example/package-tool",
227​
228 executable: "vp",
229​
230 args: ["i", "-g", "@example/package-tool"],
231​
232 lockKey: "vite-plus-global",
233 },
234 });
235 }),
236 );
237​
238 it.effect(
239 "switches package-managed providers to bun updates when the resolved binary lives in bun's global bin",
240 () =>
241 Effect.gen(function* () {
242 const tempDir = yield* makeTempDir("t3-bun-capabilities");
243 const bunBinDir = NodePath.join(tempDir, ".bun", "bin");
244 NodeFS.mkdirSync(bunBinDir, { recursive: true });
245 NodeFS.writeFileSync(NodePath.join(bunBinDir, "native-package-tool.exe"), "MZ");
246​
247 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
248 nativePackageToolUpdate,
249 {
250 binaryPath: "native-package-tool",
251 env: {
252 PATH: bunBinDir,
253 PATHEXT: ".COM;.EXE;.BAT;.CMD",
254 },
255 },
256 ).pipe(Effect.provideService(HostProcessPlatform, "win32"));
257​
258 expect(capabilities).toEqual({
259 provider: driver("nativePackageTool"),
260 packageName: "@example/native-package-tool",
261 update: {
262 command: "bun i -g @example/native-package-tool@latest",
263​
264 executable: "bun",
265​
266 args: ["i", "-g", "@example/native-package-tool@latest"],
267​
268 lockKey: "bun-global",
269 },
270 });
271 }),
272 );
273​
274 it.effect(
275 "switches package-managed providers to pnpm updates when the resolved binary lives in pnpm's global bin",
276 () =>
277 Effect.gen(function* () {
278 const tempDir = yield* makeTempDir("t3-pnpm-capabilities");
279 const pnpmHomeDir = NodePath.join(tempDir, ".local", "share", "pnpm");
280 NodeFS.mkdirSync(pnpmHomeDir, { recursive: true });
281 const scopedPackageToolPath = NodePath.join(pnpmHomeDir, "scoped-package-tool");
282 NodeFS.writeFileSync(scopedPackageToolPath, "#!/bin/sh\n");
283 NodeFS.chmodSync(scopedPackageToolPath, 0o755);
284​
285 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
286 scopedPackageToolUpdate,
287 {
288 binaryPath: "scoped-package-tool",
289 env: {
290 PATH: pnpmHomeDir,
291 },
292 },
293 ).pipe(Effect.provideService(HostProcessPlatform, "darwin"));
294​
295 expect(capabilities).toEqual({
296 provider: driver("scopedPackageTool"),
297 packageName: "@example/scoped-package-tool",
298 update: {
299 command: "pnpm add -g @example/scoped-package-tool@latest",
300​
301 executable: "pnpm",
302​
303 args: ["add", "-g", "@example/scoped-package-tool@latest"],
304​
305 lockKey: "pnpm-global",
306 },
307 });
308 }),
309 );
310​
311 it("switches package-tool to Homebrew updates when the binary resolves through Homebrew", () => {
312 expect(
313 packageToolUpdate.resolve({
314 binaryPath: "/opt/homebrew/bin/package-tool",
315 env: {
316 PATH: "",
317 },
318 }),
319 ).toEqual({
320 provider: driver("packageTool"),
321 packageName: "@example/package-tool",
322 update: {
323 command: "brew upgrade package-tool",
324​
325 executable: "brew",
326​
327 args: ["upgrade", "package-tool"],
328​
329 lockKey: "homebrew",
330 },
331 });
332 });
333​
334 it.effect(
335 "switches native-package-tool to native updates when the binary resolves through the native installer",
336 () =>
337 Effect.gen(function* () {
338 const tempDir = yield* makeTempDir("t3-native-package-tool-native-capabilities");
339 const nativeBinDir = NodePath.join(tempDir, ".local", "bin");
340 NodeFS.mkdirSync(nativeBinDir, { recursive: true });
341 const nativePackageToolPath = NodePath.join(nativeBinDir, "native-package-tool");
342 NodeFS.writeFileSync(nativePackageToolPath, "#!/bin/sh\n");
343 NodeFS.chmodSync(nativePackageToolPath, 0o755);
344​
345 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
346 nativePackageToolUpdate,
347 {
348 binaryPath: "native-package-tool",
349 env: {
350 PATH: nativeBinDir,
351 },
352 },
353 ).pipe(Effect.provideService(HostProcessPlatform, "darwin"));
354​
355 expect(capabilities).toEqual({
356 provider: driver("nativePackageTool"),
357 packageName: "@example/native-package-tool",
358 update: {
359 command: "native-package-tool update",
360​
361 executable: "native-package-tool",
362​
363 args: ["update"],
364​
365 lockKey: "native-package-tool-native",
366 },
367 });
368 }),
369 );
370​
371 it.effect(
372 "switches scoped-package-tool to native upgrades when the binary resolves through the standalone installer",
373 () =>
374 Effect.gen(function* () {
375 const tempDir = yield* makeTempDir("t3-scoped-package-tool-native-capabilities");
376 const nativeBinDir = NodePath.join(tempDir, ".scoped-package-tool", "bin");
377 NodeFS.mkdirSync(nativeBinDir, { recursive: true });
378 const scopedPackageToolPath = NodePath.join(nativeBinDir, "scoped-package-tool");
379 NodeFS.writeFileSync(scopedPackageToolPath, "#!/bin/sh\n");
380 NodeFS.chmodSync(scopedPackageToolPath, 0o755);
381​
382 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
383 scopedPackageToolUpdate,
384 {
385 binaryPath: "scoped-package-tool",
386 env: {
387 PATH: nativeBinDir,
388 },
389 },
390 ).pipe(Effect.provideService(HostProcessPlatform, "darwin"));
391​
392 expect(capabilities).toEqual({
393 provider: driver("scopedPackageTool"),
394 packageName: "@example/scoped-package-tool",
395 update: {
396 command: "scoped-package-tool upgrade",
397​
398 executable: "scoped-package-tool",
399​
400 args: ["upgrade"],
401​
402 lockKey: "scoped-package-tool-native",
403 },
404 });
405 }),
406 );
407​
408 it("switches native-package-tool to Homebrew updates when the binary resolves through Homebrew", () => {
409 expect(
410 nativePackageToolUpdate.resolve({
411 binaryPath: "/opt/homebrew/bin/native-package-tool",
412 env: {
413 PATH: "",
414 },
415 }),
416 ).toEqual({
417 provider: driver("nativePackageTool"),
418 packageName: "@example/native-package-tool",
419 update: {
420 command: "brew upgrade native-package-tool",
421​
422 executable: "brew",
423​
424 args: ["upgrade", "native-package-tool"],
425​
426 lockKey: "homebrew",
427 },
428 });
429 });
430​
431 it("switches scoped-package-tool to Homebrew updates when the binary resolves through Homebrew", () => {
432 expect(
433 scopedPackageToolUpdate.resolve({
434 binaryPath: "/opt/homebrew/bin/scoped-package-tool",
435 env: {
436 PATH: "",
437 },
438 }),
439 ).toEqual({
440 provider: driver("scopedPackageTool"),
441 packageName: "@example/scoped-package-tool",
442 update: {
443 command: "brew upgrade example/tap/scoped-package-tool",
444​
445 executable: "brew",
446​
447 args: ["upgrade", "example/tap/scoped-package-tool"],
448​
449 lockKey: "homebrew",
450 },
451 });
452 });
453​
454 it.effect("keeps npm updates for binaries symlinked into npm's global node_modules tree", () =>
455 Effect.gen(function* () {
456 const tempDir = yield* makeTempDir("t3-npm-capabilities");
457 const binDir = NodePath.join(tempDir, "bin");
458 const packageBinDir = NodePath.join(
459 tempDir,
460 "lib",
461 "node_modules",
462 "@example",
463 "package-tool",
464 "bin",
465 );
466 NodeFS.mkdirSync(binDir, { recursive: true });
467 NodeFS.mkdirSync(packageBinDir, { recursive: true });
468 const packageBinPath = NodePath.join(packageBinDir, "package-tool.js");
469 const symlinkPath = NodePath.join(binDir, "package-tool");
470 NodeFS.writeFileSync(packageBinPath, "#!/usr/bin/env node\n");
471 NodeFS.chmodSync(packageBinPath, 0o755);
472 NodeFS.symlinkSync(packageBinPath, symlinkPath);
473​
474 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(packageToolUpdate, {
475 binaryPath: symlinkPath,
476 env: {
477 PATH: "",
478 },
479 });
480​
481 expect(capabilities).toEqual({
482 provider: driver("packageTool"),
483 packageName: "@example/package-tool",
484 update: {
485 command: "npm install -g @example/package-tool@latest",
486​
487 executable: "npm",
488​
489 args: ["install", "-g", "@example/package-tool@latest"],
490​
491 lockKey: "npm-global",
492 },
493 });
494 }),
495 );
496​
497 it.effect("uses Effect FileSystem realPath when detecting pnpm global symlinks", () =>
498 Effect.gen(function* () {
499 const tempDir = yield* makeTempDir("t3-pnpm-realpath-capabilities");
500 const binDir = NodePath.join(tempDir, "bin");
501 const packageBinDir = NodePath.join(
502 tempDir,
503 ".local",
504 "share",
505 "pnpm",
506 "global",
507 "5",
508 "node_modules",
509 "@example",
510 "package-tool",
511 "bin",
512 );
513 NodeFS.mkdirSync(binDir, { recursive: true });
514 NodeFS.mkdirSync(packageBinDir, { recursive: true });
515 const packageBinPath = NodePath.join(packageBinDir, "package-tool.js");
516 const symlinkPath = NodePath.join(binDir, "package-tool");
517 NodeFS.writeFileSync(packageBinPath, "#!/usr/bin/env node\n");
518 NodeFS.chmodSync(packageBinPath, 0o755);
519 NodeFS.symlinkSync(packageBinPath, symlinkPath);
520​
521 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(packageToolUpdate, {
522 binaryPath: symlinkPath,
523 env: {
524 PATH: "",
525 },
526 });
527​
528 expect(capabilities).toEqual({
529 provider: driver("packageTool"),
530 packageName: "@example/package-tool",
531 update: {
532 command: "pnpm add -g @example/package-tool@latest",
533​
534 executable: "pnpm",
535​
536 args: ["add", "-g", "@example/package-tool@latest"],
537​
538 lockKey: "pnpm-global",
539 },
540 });
541 }),
542 );
543​
544 it("disables one-click updates for explicit custom binary paths it cannot safely map", () => {
545 expect(
546 packageToolUpdate.resolve({
547 binaryPath: "C:\\Tools\\package-tool\\package-tool.exe",
548 env: {
549 PATH: "",
550 PATHEXT: ".COM;.EXE;.BAT;.CMD",
551 },
552 }),
553 ).toEqual({
554 provider: driver("packageTool"),
555 packageName: "@example/package-tool",
556 update: null,
557 });
558 });
559});