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