Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { expect, it } from "@effect/vitest";
3import { ProviderDriverKind, ProviderInstanceId, type ServerProvider } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Layer from "effect/Layer";
7import * as Path from "effect/Path";
8import * as Sink from "effect/Sink";
9import * as Stream from "effect/Stream";
10import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
11import * as HostProcess from "@t3tools/shared/HostProcess";
12import * as HttpClient from "effect/http/HttpClient";
13import * as HttpClientResponse from "effect/http/HttpClientResponse";
14
15import * as ProviderLatestVersions from "@t3tools/provider-core/server/ProviderLatestVersions";
16import { enrichProviderSnapshotWithVersionAdvisory } from "@t3tools/provider-core/server/maintenanceResolver";
17import { fetchLatestFxVersion, makeFxMaintenance } from "./maintenance.ts";
18
19const snapshot: ServerProvider = {
20 instanceId: ProviderInstanceId.make("fx"),
21 driver: ProviderDriverKind.make("fx"),
22 enabled: true,
23 installed: true,
24 version: "0.0.13",
25 status: "ready",
26 auth: { status: "authenticated" },
27 checkedAt: "2026-10-29T00:00:00.000Z",
28 models: [],
29 skills: [],
30 slashCommands: [],
31};
32
33const serving = (response: Response) =>
34 HttpClient.make((request) => Effect.succeed(HttpClientResponse.fromWeb(request, response)));
35
36const maintenanceLayer = Layer.merge(
37 NodeServices.layer,
38 Layer.succeed(HostProcess.KernelRelease, "24.0.0"),
39);
40
41it.layer(maintenanceLayer)("fx maintenance", (it) => {
42 it.effect("uses the owning Homebrew installation and otherwise stays manual-only", () =>
43 Effect.gen(function* () {
44 const fs = yield* FileSystem.FileSystem;
45 const path = yield* Path.Path;
46 const prefix = yield* fs
47 .makeTempDirectoryScoped({ prefix: "t3-fx-brew-" })
48 .pipe(Effect.flatMap((directory) => fs.realPath(directory)));
49 const brewPath = path.join(prefix, "bin", "brew");
50 yield* fs.makeDirectory(path.dirname(brewPath), { recursive: true });
51 yield* fs.writeFileString(brewPath, "");
52 let reportedPrefix = prefix;
53 const spawner = ChildProcessSpawner.make((command) => {
54 if (command._tag !== "StandardCommand") return Effect.die("Expected a brew probe");
55 const output =
56 command.args[0] === "--prefix"
57 ? reportedPrefix
58 : JSON.stringify({ formulae: [{ versions: { stable: "0.0.14" } }] });
59 return Effect.succeed(
60 ChildProcessSpawner.makeHandle({
61 pid: ChildProcessSpawner.ProcessId(1),
62 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
63 exitStatus: Effect.succeed({ _tag: "Exited", code: ChildProcessSpawner.ExitCode(0) }),
64 isRunning: Effect.succeed(false),
65 kill: () => Effect.void,
66 unref: Effect.succeed(Effect.void),
67 stdin: Sink.drain,
68 stdout: Stream.encodeText(Stream.make(output)),
69 stderr: Stream.empty,
70 all: Stream.empty,
71 getInputFd: () => Sink.drain,
72 getOutputFd: () => Stream.empty,
73 }),
74 );
75 });
76 const context = {
77 binaryPath: path.join(prefix, "bin", "fx"),
78 resolvedCommandPath: path.join(prefix, "bin", "fx"),
79 realCommandPath: path.join(prefix, "Cellar", "fx-agent", "0.0.13", "bin", "fx"),
80 platform: "darwin" as const,
81 env: { PATH: "" },
82 };
83 const resolver = makeFxMaintenance(Effect.die("Homebrew must not use fx's release channel"));
84 const capabilities = yield* resolver
85 .resolve(context)
86 .pipe(
87 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
88 Effect.provideService(HostProcess.Platform, "darwin"),
89 );
90 expect(capabilities.update).toMatchObject({
91 executable: brewPath,
92 args: ["upgrade", "fx-agent"],
93 lockKey: "homebrew",
94 });
95 expect(yield* capabilities.lookupLatestVersion!).toBe("0.0.14");
96
97 reportedPrefix = path.join(prefix, "another-installation");
98 expect(
99 (yield* resolver
100 .resolve(context)
101 .pipe(
102 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
103 Effect.provideService(HostProcess.Platform, "darwin"),
104 )).update,
105 ).toBeNull();
106 yield* fs.remove(brewPath);
107 expect((yield* resolver.resolve(context)).update).toBeNull();
108 expect(
109 (yield* resolver.resolve({
110 ...context,
111 realCommandPath: path.join(prefix, "Cellar", "mise", "1.0.0", "bin", "fx"),
112 })).update,
113 ).toBeNull();
114 }).pipe(Effect.scoped),
115 );
116
117 it.effect("offers `fx upgrade` on the instance's own binary once a newer release is out", () =>
118 Effect.gen(function* () {
119 const binaryPath = "/opt/tools/custom fx";
120 const capabilities = yield* makeFxMaintenance(
121 fetchLatestFxVersion().pipe(
122 Effect.provideService(HttpClient.HttpClient, serving(new Response("v0.0.15\n"))),
123 ),
124 ).resolve({
125 binaryPath,
126 resolvedCommandPath: binaryPath,
127 realCommandPath: "/opt/tools/fx-real",
128 platform: "darwin",
129 env: { HOME: "/home/fx" },
130 });
131 expect(capabilities.update).toMatchObject({
132 executable: binaryPath,
133 args: ["upgrade"],
134 command: `'${binaryPath}' upgrade`,
135 lockKey: "fx:/opt/tools/fx-real",
136 env: { HOME: "/home/fx" },
137 });
138
139 const enriched = yield* enrichProviderSnapshotWithVersionAdvisory(snapshot, capabilities, {
140 enableProviderUpdateChecks: true,
141 }).pipe(
142 Effect.provideService(HttpClient.HttpClient, serving(new Response("unused"))),
143 Effect.provide(ProviderLatestVersions.layer),
144 );
145 expect(enriched.versionAdvisory).toMatchObject({
146 status: "behind_latest",
147 currentVersion: "0.0.13",
148 latestVersion: "0.0.15",
149 canUpdate: true,
150 updateCommand: `'${binaryPath}' upgrade`,
151 });
152
153 const resolver = makeFxMaintenance(Effect.succeed("0.0.15"));
154 const fs = yield* FileSystem.FileSystem;
155 const path = yield* Path.Path;
156 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-fx-mise-" });
157 const wrapperPath = path.join(directory, "fx");
158 for (const { managedPath, env } of [
159 { managedPath: "/home/fx/.local/share/mise/installs/fx/0.0.15/bin/fx", env: {} },
160 { managedPath: "/home/fx/.local/share/mise/shims/fx", env: {} },
161 { managedPath: "/home/fx/.local/share/mise/command-wrappers/fx", env: {} },
162 {
163 managedPath: "/opt/tools/installs/fx/0.0.15/bin/fx",
164 env: { MISE_DATA_DIR: "/opt/tools/" },
165 },
166 {
167 managedPath: "/opt/versions/fx/0.0.15/bin/fx",
168 env: { MISE_INSTALLS_DIR: "/opt/versions" },
169 },
170 {
171 managedPath: "/opt/store/fx-abc/bin/fx",
172 env: { MISE_INSTALL_STORE_DIR: "/opt/store" },
173 },
174 {
175 managedPath: "/opt/system/installs/fx/0.0.15/bin/fx",
176 env: { MISE_SYSTEM_DATA_DIR: "/opt/system" },
177 },
178 {
179 managedPath: "/opt/system-installs/fx/0.0.15/bin/fx",
180 env: { MISE_SYSTEM_INSTALLS_DIR: "/opt/system-installs" },
181 },
182 ]) {
183 for (const paths of [
184 { resolvedCommandPath: managedPath, realCommandPath: binaryPath },
185 { resolvedCommandPath: binaryPath, realCommandPath: managedPath },
186 ]) {
187 const managed = yield* resolver.resolve({
188 binaryPath,
189 ...paths,
190 platform: "linux",
191 env,
192 });
193 expect(managed.update).toBeNull();
194 const managedSnapshot = yield* enrichProviderSnapshotWithVersionAdvisory(
195 snapshot,
196 managed,
197 {
198 enableProviderUpdateChecks: true,
199 },
200 ).pipe(
201 Effect.provideService(HttpClient.HttpClient, serving(new Response("unused"))),
202 Effect.provide(ProviderLatestVersions.layer),
203 );
204 expect(managedSnapshot.versionAdvisory).toMatchObject({
205 status: "behind_latest",
206 latestVersion: "0.0.15",
207 canUpdate: false,
208 });
209 expect(managed.installPath).toBe(paths.realCommandPath);
210 }
211 }
212 for (const command of ["x", "exec"]) {
213 yield* fs.writeFileString(wrapperPath, `#!/bin/sh\nexec mise ${command} -- fx "$@"\n`);
214 const managed = yield* resolver.resolve({
215 binaryPath: wrapperPath,
216 resolvedCommandPath: wrapperPath,
217 realCommandPath: wrapperPath,
218 platform: "linux",
219 env: { MISE_SHIMS_DIR: directory, MISE_SYSTEM_SHIMS_DIR: directory },
220 });
221 expect(managed.update).toBeNull();
222 expect(managed.installPath).toBe(wrapperPath);
223 expect(yield* managed.lookupLatestVersion!).toBe("0.0.15");
224 }
225 yield* fs.writeFileString(wrapperPath, "#!/bin/sh\nexit 0\n");
226 expect(
227 (yield* resolver.resolve({
228 binaryPath: wrapperPath,
229 resolvedCommandPath: wrapperPath,
230 realCommandPath: wrapperPath,
231 platform: "linux",
232 env: { MISE_SHIMS_DIR: directory, MISE_SYSTEM_SHIMS_DIR: directory },
233 })).update,
234 ).not.toBeNull();
235 const outsideMise = "/opt/tools-extra/installs/fx/0.0.15/bin/fx";
236 expect(
237 (yield* resolver.resolve({
238 binaryPath: outsideMise,
239 resolvedCommandPath: outsideMise,
240 realCommandPath: outsideMise,
241 platform: "linux",
242 env: { MISE_DATA_DIR: "/opt/tools" },
243 })).update,
244 ).not.toBeNull();
245 }).pipe(Effect.scoped),
246 );
247
248 it.effect("leaves the latest version unknown when the release file is unusable", () =>
249 Effect.gen(function* () {
250 for (const response of [
251 new Response("<html>not found</html>"),
252 new Response("v0.0.15", { status: 503 }),
253 ]) {
254 const latest = yield* fetchLatestFxVersion().pipe(
255 Effect.provideService(HttpClient.HttpClient, serving(response)),
256 );
257 expect(latest).toBeNull();
258 }
259 expect((yield* makeFxMaintenance(Effect.succeed("0.0.15")).resolve(null)).update).toBeNull();
260 }),
261 );
262
263 it.effect("offers no update or latest version on macOS 14, which newer fx cannot run on", () =>
264 Effect.gen(function* () {
265 const binaryPath = "/opt/tools/fx";
266 const capabilities = yield* makeFxMaintenance(Effect.succeed("0.0.15"))
267 .resolve({
268 binaryPath,
269 resolvedCommandPath: binaryPath,
270 realCommandPath: binaryPath,
271 platform: "darwin",
272 env: {},
273 })
274 .pipe(Effect.provideService(HostProcess.KernelRelease, "23.6.0"));
275 expect(capabilities.update).toBeNull();
276 expect(capabilities.lookupLatestVersion).toBeUndefined();
277 }),
278 );
279});