Open History to see the last commit
1import { afterEach, describe, expect, it } from "@effect/vitest";
2import { chmodSync, mkdirSync, symlinkSync, writeFileSync } from "node:fs";
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import os from "node:os";
5import path from "node:path";
6import { ProviderDriverKind } from "@t3tools/contracts";
7import { Effect, Random } from "effect";
8import {
9 clearLatestProviderVersionCacheForTests,
10 createProviderVersionAdvisory,
11 makePackageManagedProviderMaintenanceResolver,
12 makeProviderMaintenanceCapabilities,
13 makeStaticProviderMaintenanceResolver,
14 normalizeCommandPath,
15 resolveProviderMaintenanceCapabilitiesEffect,
16} from "./providerMaintenance.ts";
17
18const driver = (value: string) => ProviderDriverKind.make(value);
19const makeTempDir = Effect.fn("makeTempDir")(function* (name: string) {
20 const id = yield* Random.nextUUIDv4;
21 return path.join(os.tmpdir(), `${name}-${id}`);
22});
23const isNativeTestCommandPath =
24 (expectedPathSegment: string) =>
25 (commandPath: string): boolean =>
26 normalizeCommandPath(commandPath).includes(expectedPathSegment);
27const packageToolUpdate = makePackageManagedProviderMaintenanceResolver({
28 provider: driver("packageTool"),
29 npmPackageName: "@example/package-tool",
30 homebrewFormula: "package-tool",
31 nativeUpdate: null,
32});
33const nativePackageToolUpdate = makePackageManagedProviderMaintenanceResolver({
34 provider: driver("nativePackageTool"),
35 npmPackageName: "@example/native-package-tool",
36 homebrewFormula: "native-package-tool",
37 nativeUpdate: {
38 executable: "native-package-tool",
39 args: ["update"],
40 lockKey: "native-package-tool-native",
41 isCommandPath: isNativeTestCommandPath("/.local/bin/native-package-tool"),
42 },
43});
44const scopedPackageToolUpdate = makePackageManagedProviderMaintenanceResolver({
45 provider: driver("scopedPackageTool"),
46 npmPackageName: "@example/scoped-package-tool",
47 homebrewFormula: "example/tap/scoped-package-tool",
48 nativeUpdate: {
49 executable: "scoped-package-tool",
50 args: ["upgrade"],
51 lockKey: "scoped-package-tool-native",
52 isCommandPath: isNativeTestCommandPath("/.scoped-package-tool/bin/scoped-package-tool"),
53 },
54});
55const staticToolUpdate = makeStaticProviderMaintenanceResolver(
56 makeProviderMaintenanceCapabilities({
57 provider: driver("staticTool"),
58 packageName: null,
59 updateExecutable: "static-tool",
60 updateArgs: ["update"],
61 updateLockKey: "static-tool",
62 }),
63);
64
65afterEach(() => {
66 clearLatestProviderVersionCacheForTests();
67});
68
69describe("providerMaintenance", () => {
70 it("marks providers with unknown current versions as unknown", () => {
71 expect(
72 createProviderVersionAdvisory({
73 driver: driver("packageTool"),
74 currentVersion: null,
75 latestVersion: "9.9.9",
76 }),
77 ).toMatchObject({
78 status: "unknown",
79 currentVersion: null,
80 latestVersion: "9.9.9",
81 });
82 });
83
84 it("marks providers with unknown latest versions as unknown", () => {
85 expect(
86 createProviderVersionAdvisory({
87 driver: driver("packageTool"),
88 currentVersion: "1.0.0",
89 latestVersion: null,
90 }),
91 ).toMatchObject({
92 status: "unknown",
93 currentVersion: "1.0.0",
94 latestVersion: null,
95 message: null,
96 });
97 });
98
99 it("marks installed providers behind latest when a newer provider version is available", () => {
100 expect(
101 createProviderVersionAdvisory({
102 driver: driver("nativePackageTool"),
103 currentVersion: "2.1.110",
104 latestVersion: "2.1.117",
105 maintenanceCapabilities: nativePackageToolUpdate.resolve(),
106 }),
107 ).toMatchObject({
108 status: "behind_latest",
109 currentVersion: "2.1.110",
110 latestVersion: "2.1.117",
111 updateCommand: "npm install -g @example/native-package-tool@latest",
112 canUpdate: true,
113 message: "Install the update now or review provider settings.",
114 });
115 });
116
117 it("keeps update commands owned by provider maintenance capabilities", () => {
118 expect(staticToolUpdate.resolve()).toEqual({
119 provider: driver("staticTool"),
120 packageName: null,
121 update: {
122 command: "static-tool update",
123
124 executable: "static-tool",
125
126 args: ["update"],
127
128 lockKey: "static-tool",
129 },
130 });
131 });
132
133 it.effect(
134 "switches package-managed providers to vite-plus updates when the resolved binary lives in vite-plus global bin",
135 () =>
136 Effect.gen(function* () {
137 const tempDir = yield* makeTempDir("t3-vite-plus-capabilities");
138 const vitePlusBinDir = path.join(tempDir, ".vite-plus", "bin");
139 mkdirSync(vitePlusBinDir, { recursive: true });
140 const packageToolPath = path.join(vitePlusBinDir, "package-tool");
141 writeFileSync(packageToolPath, "#!/bin/sh\n");
142 chmodSync(packageToolPath, 0o755);
143
144 expect(
145 packageToolUpdate.resolve({
146 binaryPath: "package-tool",
147 platform: "darwin",
148 env: {
149 PATH: vitePlusBinDir,
150 },
151 }),
152 ).toEqual({
153 provider: driver("packageTool"),
154 packageName: "@example/package-tool",
155 update: {
156 command: "vp i -g @example/package-tool",
157
158 executable: "vp",
159
160 args: ["i", "-g", "@example/package-tool"],
161
162 lockKey: "vite-plus-global",
163 },
164 });
165 }),
166 );
167
168 it.effect(
169 "switches package-managed providers to bun updates when the resolved binary lives in bun's global bin",
170 () =>
171 Effect.gen(function* () {
172 const tempDir = yield* makeTempDir("t3-bun-capabilities");
173 const bunBinDir = path.join(tempDir, ".bun", "bin");
174 mkdirSync(bunBinDir, { recursive: true });
175 writeFileSync(path.join(bunBinDir, "native-package-tool.exe"), "MZ");
176
177 expect(
178 nativePackageToolUpdate.resolve({
179 binaryPath: "native-package-tool",
180 platform: "win32",
181 env: {
182 PATH: bunBinDir,
183 PATHEXT: ".COM;.EXE;.BAT;.CMD",
184 },
185 }),
186 ).toEqual({
187 provider: driver("nativePackageTool"),
188 packageName: "@example/native-package-tool",
189 update: {
190 command: "bun i -g @example/native-package-tool@latest",
191
192 executable: "bun",
193
194 args: ["i", "-g", "@example/native-package-tool@latest"],
195
196 lockKey: "bun-global",
197 },
198 });
199 }),
200 );
201
202 it.effect(
203 "switches package-managed providers to pnpm updates when the resolved binary lives in pnpm's global bin",
204 () =>
205 Effect.gen(function* () {
206 const tempDir = yield* makeTempDir("t3-pnpm-capabilities");
207 const pnpmHomeDir = path.join(tempDir, ".local", "share", "pnpm");
208 mkdirSync(pnpmHomeDir, { recursive: true });
209 const scopedPackageToolPath = path.join(pnpmHomeDir, "scoped-package-tool");
210 writeFileSync(scopedPackageToolPath, "#!/bin/sh\n");
211 chmodSync(scopedPackageToolPath, 0o755);
212
213 expect(
214 scopedPackageToolUpdate.resolve({
215 binaryPath: "scoped-package-tool",
216 platform: "darwin",
217 env: {
218 PATH: pnpmHomeDir,
219 },
220 }),
221 ).toEqual({
222 provider: driver("scopedPackageTool"),
223 packageName: "@example/scoped-package-tool",
224 update: {
225 command: "pnpm add -g @example/scoped-package-tool@latest",
226
227 executable: "pnpm",
228
229 args: ["add", "-g", "@example/scoped-package-tool@latest"],
230
231 lockKey: "pnpm-global",
232 },
233 });
234 }),
235 );
236
237 it("switches package-tool to Homebrew updates when the binary resolves through Homebrew", () => {
238 expect(
239 packageToolUpdate.resolve({
240 binaryPath: "/opt/homebrew/bin/package-tool",
241 platform: "darwin",
242 env: {
243 PATH: "",
244 },
245 }),
246 ).toEqual({
247 provider: driver("packageTool"),
248 packageName: "@example/package-tool",
249 update: {
250 command: "brew upgrade package-tool",
251
252 executable: "brew",
253
254 args: ["upgrade", "package-tool"],
255
256 lockKey: "homebrew",
257 },
258 });
259 });
260
261 it.effect(
262 "switches native-package-tool to native updates when the binary resolves through the native installer",
263 () =>
264 Effect.gen(function* () {
265 const tempDir = yield* makeTempDir("t3-native-package-tool-native-capabilities");
266 const nativeBinDir = path.join(tempDir, ".local", "bin");
267 mkdirSync(nativeBinDir, { recursive: true });
268 const nativePackageToolPath = path.join(nativeBinDir, "native-package-tool");
269 writeFileSync(nativePackageToolPath, "#!/bin/sh\n");
270 chmodSync(nativePackageToolPath, 0o755);
271
272 expect(
273 nativePackageToolUpdate.resolve({
274 binaryPath: "native-package-tool",
275 platform: "darwin",
276 env: {
277 PATH: nativeBinDir,
278 },
279 }),
280 ).toEqual({
281 provider: driver("nativePackageTool"),
282 packageName: "@example/native-package-tool",
283 update: {
284 command: "native-package-tool update",
285
286 executable: "native-package-tool",
287
288 args: ["update"],
289
290 lockKey: "native-package-tool-native",
291 },
292 });
293 }),
294 );
295
296 it.effect(
297 "switches scoped-package-tool to native upgrades when the binary resolves through the standalone installer",
298 () =>
299 Effect.gen(function* () {
300 const tempDir = yield* makeTempDir("t3-scoped-package-tool-native-capabilities");
301 const nativeBinDir = path.join(tempDir, ".scoped-package-tool", "bin");
302 mkdirSync(nativeBinDir, { recursive: true });
303 const scopedPackageToolPath = path.join(nativeBinDir, "scoped-package-tool");
304 writeFileSync(scopedPackageToolPath, "#!/bin/sh\n");
305 chmodSync(scopedPackageToolPath, 0o755);
306
307 expect(
308 scopedPackageToolUpdate.resolve({
309 binaryPath: "scoped-package-tool",
310 platform: "darwin",
311 env: {
312 PATH: nativeBinDir,
313 },
314 }),
315 ).toEqual({
316 provider: driver("scopedPackageTool"),
317 packageName: "@example/scoped-package-tool",
318 update: {
319 command: "scoped-package-tool upgrade",
320
321 executable: "scoped-package-tool",
322
323 args: ["upgrade"],
324
325 lockKey: "scoped-package-tool-native",
326 },
327 });
328 }),
329 );
330
331 it("switches native-package-tool to Homebrew updates when the binary resolves through Homebrew", () => {
332 expect(
333 nativePackageToolUpdate.resolve({
334 binaryPath: "/opt/homebrew/bin/native-package-tool",
335 platform: "darwin",
336 env: {
337 PATH: "",
338 },
339 }),
340 ).toEqual({
341 provider: driver("nativePackageTool"),
342 packageName: "@example/native-package-tool",
343 update: {
344 command: "brew upgrade native-package-tool",
345
346 executable: "brew",
347
348 args: ["upgrade", "native-package-tool"],
349
350 lockKey: "homebrew",
351 },
352 });
353 });
354
355 it("switches scoped-package-tool to Homebrew updates when the binary resolves through Homebrew", () => {
356 expect(
357 scopedPackageToolUpdate.resolve({
358 binaryPath: "/opt/homebrew/bin/scoped-package-tool",
359 platform: "darwin",
360 env: {
361 PATH: "",
362 },
363 }),
364 ).toEqual({
365 provider: driver("scopedPackageTool"),
366 packageName: "@example/scoped-package-tool",
367 update: {
368 command: "brew upgrade example/tap/scoped-package-tool",
369
370 executable: "brew",
371
372 args: ["upgrade", "example/tap/scoped-package-tool"],
373
374 lockKey: "homebrew",
375 },
376 });
377 });
378
379 it.effect("keeps npm updates for binaries symlinked into npm's global node_modules tree", () =>
380 Effect.gen(function* () {
381 const tempDir = yield* makeTempDir("t3-npm-capabilities");
382 const binDir = path.join(tempDir, "bin");
383 const packageBinDir = path.join(
384 tempDir,
385 "lib",
386 "node_modules",
387 "@example",
388 "package-tool",
389 "bin",
390 );
391 mkdirSync(binDir, { recursive: true });
392 mkdirSync(packageBinDir, { recursive: true });
393 const packageBinPath = path.join(packageBinDir, "package-tool.js");
394 const symlinkPath = path.join(binDir, "package-tool");
395 writeFileSync(packageBinPath, "#!/usr/bin/env node\n");
396 chmodSync(packageBinPath, 0o755);
397 symlinkSync(packageBinPath, symlinkPath);
398
399 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(packageToolUpdate, {
400 binaryPath: symlinkPath,
401 platform: "darwin",
402 env: {
403 PATH: "",
404 },
405 }).pipe(Effect.provide(NodeServices.layer));
406
407 expect(capabilities).toEqual({
408 provider: driver("packageTool"),
409 packageName: "@example/package-tool",
410 update: {
411 command: "npm install -g @example/package-tool@latest",
412
413 executable: "npm",
414
415 args: ["install", "-g", "@example/package-tool@latest"],
416
417 lockKey: "npm-global",
418 },
419 });
420 }),
421 );
422
423 it.effect("uses Effect FileSystem realPath when detecting pnpm global symlinks", () =>
424 Effect.gen(function* () {
425 const tempDir = yield* makeTempDir("t3-pnpm-realpath-capabilities");
426 const binDir = path.join(tempDir, "bin");
427 const packageBinDir = path.join(
428 tempDir,
429 ".local",
430 "share",
431 "pnpm",
432 "global",
433 "5",
434 "node_modules",
435 "@example",
436 "package-tool",
437 "bin",
438 );
439 mkdirSync(binDir, { recursive: true });
440 mkdirSync(packageBinDir, { recursive: true });
441 const packageBinPath = path.join(packageBinDir, "package-tool.js");
442 const symlinkPath = path.join(binDir, "package-tool");
443 writeFileSync(packageBinPath, "#!/usr/bin/env node\n");
444 chmodSync(packageBinPath, 0o755);
445 symlinkSync(packageBinPath, symlinkPath);
446
447 const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(packageToolUpdate, {
448 binaryPath: symlinkPath,
449 platform: "darwin",
450 env: {
451 PATH: "",
452 },
453 }).pipe(Effect.provide(NodeServices.layer));
454
455 expect(capabilities).toEqual({
456 provider: driver("packageTool"),
457 packageName: "@example/package-tool",
458 update: {
459 command: "pnpm add -g @example/package-tool@latest",
460
461 executable: "pnpm",
462
463 args: ["add", "-g", "@example/package-tool@latest"],
464
465 lockKey: "pnpm-global",
466 },
467 });
468 }),
469 );
470
471 it("disables one-click updates for explicit custom binary paths it cannot safely map", () => {
472 expect(
473 packageToolUpdate.resolve({
474 binaryPath: "C:\\Tools\\package-tool\\package-tool.exe",
475 platform: "win32",
476 env: {
477 PATH: "",
478 PATHEXT: ".COM;.EXE;.BAT;.CMD",
479 },
480 }),
481 ).toEqual({
482 provider: driver("packageTool"),
483 packageName: "@example/package-tool",
484 update: null,
485 });
486 });
487});