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 { |
| 4 | DEFAULT_SERVER_SETTINGS, |
| 5 | ProviderDriverKind, |
| 6 | ProviderInstanceId, |
| 7 | type ServerProvider, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 10 | import { |
| 11 | enrichProviderSnapshotWithVersionAdvisory, |
| 12 | invalidateLatestProviderVersion, |
| 13 | } from "@t3tools/provider-core/server/maintenanceResolver"; |
| 14 | import * as ProviderLatestVersions from "@t3tools/provider-core/server/ProviderLatestVersions"; |
| 15 | import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost"; |
| 16 | import { makeManagedServerProvider } from "@t3tools/provider-core/server/managedProvider"; |
| 17 | import * as Effect from "effect/Effect"; |
| 18 | import * as Fiber from "effect/Fiber"; |
| 19 | import * as FileSystem from "effect/FileSystem"; |
| 20 | import * as Layer from "effect/Layer"; |
| 21 | import * as Path from "effect/Path"; |
| 22 | import * as Queue from "effect/Queue"; |
| 23 | import * as Stream from "effect/Stream"; |
| 24 | import * as TestClock from "effect/testing/TestClock"; |
| 25 | import * as HttpClient from "effect/http/HttpClient"; |
| 26 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 27 | import * as Sink from "effect/Sink"; |
| 28 | import * as ChildProcess from "effect/process/ChildProcess"; |
| 29 | import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 30 | import { makeClaudeMaintenanceResolution } from "./ClaudeMaintenance.ts"; |
| 31 | |
| 32 | const snapshot: ServerProvider = { |
| 33 | instanceId: ProviderInstanceId.make("claude"), |
| 34 | driver: ProviderDriverKind.make("claudeAgent"), |
| 35 | enabled: true, |
| 36 | installed: true, |
| 37 | version: "2.1.314", |
| 38 | status: "ready", |
| 39 | auth: { status: "authenticated" }, |
| 40 | checkedAt: "2026-10-31T00:00:00.000Z", |
| 41 | models: [], |
| 42 | slashCommands: [], |
| 43 | skills: [], |
| 44 | }; |
| 45 | |
| 46 | const fixture = Effect.fn("ClaudeMaintenance.test.fixture")(function* ( |
| 47 | install: "native" | "xdgNative" | "npm" | "wrapper" = "native", |
| 48 | platform: NodeJS.Platform = "linux", |
| 49 | programFiles?: string, |
| 50 | ) { |
| 51 | const fs = yield* FileSystem.FileSystem; |
| 52 | const path = yield* Path.Path; |
| 53 | const directory = yield* fs.makeTempDirectoryScoped(); |
| 54 | const binaryPath = |
| 55 | install === "npm" |
| 56 | ? path.join(directory, "lib/node_modules/@anthropic-ai/claude-code/cli.js") |
| 57 | : path.join(directory, ".local/bin", platform === "win32" ? "claude.exe" : "claude"); |
| 58 | // Unix native installs link the launcher to a versioned binary; Windows places a real .exe. |
| 59 | const realBinaryPath = |
| 60 | install === "native" && platform !== "win32" |
| 61 | ? path.join(directory, ".local/share/claude/versions/2.1.314") |
| 62 | : install === "xdgNative" |
| 63 | ? path.join(directory, "xdg/data/claude/versions/2.1.314") |
| 64 | : binaryPath; |
| 65 | yield* fs.makeDirectory(path.dirname(realBinaryPath), { recursive: true }); |
| 66 | yield* fs.writeFileString(realBinaryPath, "#!/bin/sh\n"); |
| 67 | yield* fs.chmod(realBinaryPath, 0o755); |
| 68 | if (realBinaryPath !== binaryPath) { |
| 69 | yield* fs.makeDirectory(path.dirname(binaryPath), { recursive: true }); |
| 70 | yield* fs.symlink(realBinaryPath, binaryPath); |
| 71 | } |
| 72 | const configDirectory = path.join(directory, "config"); |
| 73 | yield* fs.makeDirectory(configDirectory); |
| 74 | const settingsPath = path.join(configDirectory, "settings.json"); |
| 75 | const managed = new Map<string, string>(); |
| 76 | const managedDirectory = |
| 77 | platform === "win32" |
| 78 | ? `${programFiles ?? "C:/Program Files"}/ClaudeCode` |
| 79 | : platform === "darwin" |
| 80 | ? "/Library/Application Support/ClaudeCode" |
| 81 | : "/etc/claude-code"; |
| 82 | const npmPath = path.join(directory, "bin/npm"); |
| 83 | if (install === "npm") { |
| 84 | yield* fs.makeDirectory(path.dirname(npmPath), { recursive: true }); |
| 85 | yield* fs.writeFileString(npmPath, "#!/bin/sh\n"); |
| 86 | yield* fs.chmod(npmPath, 0o755); |
| 87 | } |
| 88 | const npmProbes: Array<ReadonlyArray<string>> = []; |
| 89 | let installationReads = 0; |
| 90 | const testFs = FileSystem.makeNoop({ |
| 91 | ...fs, |
| 92 | realPath: (file) => { |
| 93 | if (file === binaryPath) installationReads++; |
| 94 | return fs.realPath(file); |
| 95 | }, |
| 96 | readFileString: (file, ...args) => |
| 97 | file.replaceAll("\\", "/").startsWith(`${managedDirectory}/`) |
| 98 | ? managed.has(file.replaceAll("\\", "/")) |
| 99 | ? Effect.succeed(managed.get(file.replaceAll("\\", "/"))!) |
| 100 | : fs.readFileString(path.join(directory, "missing")) |
| 101 | : fs.readFileString(file, ...args), |
| 102 | readDirectory: (file) => |
| 103 | file.replaceAll("\\", "/") === `${managedDirectory}/managed-settings.d` |
| 104 | ? Effect.succeed( |
| 105 | [...managed.keys()] |
| 106 | .filter((key) => key.includes("/managed-settings.d/")) |
| 107 | .map((key) => path.basename(key)), |
| 108 | ) |
| 109 | : fs.readDirectory(file), |
| 110 | }); |
| 111 | return { |
| 112 | directory, |
| 113 | fs, |
| 114 | managed, |
| 115 | managedDirectory, |
| 116 | settingsPath, |
| 117 | npmProbes, |
| 118 | installationReads: () => installationReads, |
| 119 | makeResolution: makeClaudeMaintenanceResolution({ |
| 120 | binaryPath, |
| 121 | env: { PATH: install === "npm" ? path.dirname(npmPath) : "" }, |
| 122 | }).pipe( |
| 123 | Effect.provideService( |
| 124 | ChildProcessSpawner.ChildProcessSpawner, |
| 125 | ChildProcessSpawner.make((command) => { |
| 126 | if (!ChildProcess.isStandardCommand(command) || command.command !== npmPath) { |
| 127 | return Effect.die("version checks may only ask npm"); |
| 128 | } |
| 129 | npmProbes.push(command.args.map(String)); |
| 130 | return Effect.succeed( |
| 131 | ChildProcessSpawner.makeHandle({ |
| 132 | pid: ChildProcessSpawner.ProcessId(1), |
| 133 | exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(1)), |
| 134 | isRunning: Effect.succeed(false), |
| 135 | kill: () => Effect.void, |
| 136 | unref: Effect.succeed(Effect.void), |
| 137 | stdin: Sink.drain, |
| 138 | stdout: Stream.encodeText( |
| 139 | Stream.make(JSON.stringify({ "@anthropic-ai/claude-code": { latest: "2.1.316" } })), |
| 140 | ), |
| 141 | stderr: Stream.empty, |
| 142 | all: Stream.empty, |
| 143 | getInputFd: () => Sink.drain, |
| 144 | getOutputFd: () => Stream.empty, |
| 145 | }), |
| 146 | ); |
| 147 | }), |
| 148 | ), |
| 149 | Effect.provideService(FileSystem.FileSystem, testFs), |
| 150 | Effect.provideService(HostProcess.Platform, platform), |
| 151 | Effect.provideService(HostProcess.Environment, { |
| 152 | PATH: "", |
| 153 | CLAUDE_CONFIG_DIR: configDirectory, |
| 154 | ...(programFiles ? { PROGRAMFILES: programFiles } : {}), |
| 155 | }), |
| 156 | ), |
| 157 | }; |
| 158 | }); |
| 159 | |
| 160 | it.layer(NodeServices.layer)("ClaudeMaintenance", (it) => { |
| 161 | it.effect.each([ |
| 162 | { |
| 163 | name: "default native channel", |
| 164 | settings: undefined, |
| 165 | channel: "latest", |
| 166 | body: "2.1.314\n", |
| 167 | status: 200, |
| 168 | expected: "2.1.314", |
| 169 | advisory: "current", |
| 170 | npm: false, |
| 171 | }, |
| 172 | { |
| 173 | name: "explicit latest channel", |
| 174 | settings: '{"autoUpdatesChannel":"latest"}', |
| 175 | channel: "latest", |
| 176 | body: "2.1.314", |
| 177 | status: 200, |
| 178 | expected: "2.1.314", |
| 179 | advisory: "current", |
| 180 | npm: false, |
| 181 | }, |
| 182 | { |
| 183 | name: "stable channel", |
| 184 | settings: '{"autoUpdatesChannel":"stable"}', |
| 185 | channel: "stable", |
| 186 | body: "2.1.308", |
| 187 | status: 200, |
| 188 | expected: "2.1.308", |
| 189 | advisory: "current", |
| 190 | npm: false, |
| 191 | }, |
| 192 | { |
| 193 | name: "new native release", |
| 194 | settings: undefined, |
| 195 | channel: "latest", |
| 196 | body: "2.1.315", |
| 197 | status: 200, |
| 198 | expected: "2.1.315", |
| 199 | advisory: "behind_latest", |
| 200 | npm: false, |
| 201 | }, |
| 202 | { |
| 203 | name: "unavailable feed", |
| 204 | settings: undefined, |
| 205 | channel: "latest", |
| 206 | body: "Unavailable", |
| 207 | status: 503, |
| 208 | expected: null, |
| 209 | advisory: "unknown", |
| 210 | npm: false, |
| 211 | }, |
| 212 | { |
| 213 | name: "malformed feed", |
| 214 | settings: undefined, |
| 215 | channel: "latest", |
| 216 | body: "<html>error</html>", |
| 217 | status: 200, |
| 218 | expected: null, |
| 219 | advisory: "unknown", |
| 220 | npm: false, |
| 221 | }, |
| 222 | { |
| 223 | name: "empty feed", |
| 224 | settings: undefined, |
| 225 | channel: "latest", |
| 226 | body: " ", |
| 227 | status: 200, |
| 228 | expected: null, |
| 229 | advisory: "unknown", |
| 230 | npm: false, |
| 231 | }, |
| 232 | { |
| 233 | name: "npm install", |
| 234 | settings: '{"autoUpdatesChannel":"stable"}', |
| 235 | channel: "latest", |
| 236 | body: "", |
| 237 | status: 200, |
| 238 | expected: "2.1.316", |
| 239 | advisory: "behind_latest", |
| 240 | npm: true, |
| 241 | }, |
| 242 | ])("advertises the installable version for $name", (row) => |
| 243 | Effect.gen(function* () { |
| 244 | const f = yield* fixture(row.npm ? "npm" : "native"); |
| 245 | if (row.settings) yield* f.fs.writeFileString(f.settingsPath, row.settings); |
| 246 | const requests: string[] = []; |
| 247 | const client = HttpClient.make((request) => { |
| 248 | requests.push(request.url); |
| 249 | return Effect.succeed( |
| 250 | HttpClientResponse.fromWeb( |
| 251 | request, |
| 252 | request.url.includes("claude-code-releases/") |
| 253 | ? new Response(row.body, { status: row.status }) |
| 254 | : Response.json({ version: "2.1.316" }), |
| 255 | ), |
| 256 | ); |
| 257 | }); |
| 258 | const resolve = yield* f.makeResolution.pipe( |
| 259 | Effect.provideService(HttpClient.HttpClient, client), |
| 260 | ); |
| 261 | const maintenance = yield* resolve(); |
| 262 | const versions = yield* ProviderLatestVersions.make(); |
| 263 | const enrich = enrichProviderSnapshotWithVersionAdvisory(snapshot, maintenance).pipe( |
| 264 | Effect.provideService(HttpClient.HttpClient, client), |
| 265 | Effect.provideService(ProviderLatestVersions.ProviderLatestVersions, versions), |
| 266 | ); |
| 267 | expect((yield* enrich).versionAdvisory).toMatchObject({ |
| 268 | status: row.advisory, |
| 269 | latestVersion: row.expected, |
| 270 | }); |
| 271 | // npm installs ask `npm outdated`, which applies npm's release-age settings. |
| 272 | const lookups = row.npm ? f.npmProbes : requests; |
| 273 | expect(lookups).toHaveLength(1); |
| 274 | if (row.npm) { |
| 275 | expect(requests).toEqual([]); |
| 276 | expect(f.npmProbes[0]?.[0]).toBe("outdated"); |
| 277 | } else { |
| 278 | expect(requests[0]).toMatch(new RegExp(`claude-code-releases/${row.channel}$`)); |
| 279 | } |
| 280 | yield* enrich; |
| 281 | expect(lookups).toHaveLength(1); |
| 282 | }), |
| 283 | ); |
| 284 | |
| 285 | it.effect.each([ |
| 286 | { name: "Linux", platform: "linux", programFiles: undefined }, |
| 287 | { name: "macOS", platform: "darwin", programFiles: undefined }, |
| 288 | { name: "Windows default path", platform: "win32", programFiles: undefined }, |
| 289 | { name: "Windows custom path", platform: "win32", programFiles: "D:/Programs" }, |
| 290 | ] as const)( |
| 291 | "separates channel caches and honors $name managed overrides and feed invalidation", |
| 292 | (row) => |
| 293 | Effect.gen(function* () { |
| 294 | const f = yield* fixture("native", row.platform, row.programFiles); |
| 295 | const requests: string[] = []; |
| 296 | const client = HttpClient.make((request) => { |
| 297 | requests.push(request.url); |
| 298 | return Effect.succeed( |
| 299 | HttpClientResponse.fromWeb( |
| 300 | request, |
| 301 | new Response(request.url.endsWith("/stable") ? "2.1.308" : "2.1.314"), |
| 302 | ), |
| 303 | ); |
| 304 | }); |
| 305 | const versions = yield* ProviderLatestVersions.make(); |
| 306 | const resolve = yield* f.makeResolution.pipe( |
| 307 | Effect.provideService(HttpClient.HttpClient, client), |
| 308 | ); |
| 309 | const check = Effect.gen(function* () { |
| 310 | const capabilities = yield* resolve(); |
| 311 | return yield* enrichProviderSnapshotWithVersionAdvisory(snapshot, capabilities); |
| 312 | }).pipe( |
| 313 | Effect.provideService(HttpClient.HttpClient, client), |
| 314 | Effect.provideService(ProviderLatestVersions.ProviderLatestVersions, versions), |
| 315 | ); |
| 316 | expect((yield* check).versionAdvisory?.latestVersion).toBe("2.1.314"); |
| 317 | yield* f.fs.writeFileString(f.settingsPath, '{"autoUpdatesChannel":"stable"}'); |
| 318 | expect((yield* check).versionAdvisory?.latestVersion).toBe("2.1.308"); |
| 319 | f.managed.set( |
| 320 | `${f.managedDirectory}/managed-settings.json`, |
| 321 | '{"autoUpdatesChannel":"latest"}', |
| 322 | ); |
| 323 | expect((yield* check).versionAdvisory?.latestVersion).toBe("2.1.314"); |
| 324 | f.managed.set( |
| 325 | `${f.managedDirectory}/managed-settings.d/20-channel.json`, |
| 326 | '{"autoUpdatesChannel":"stable"}', |
| 327 | ); |
| 328 | f.managed.set( |
| 329 | `${f.managedDirectory}/managed-settings.d/10-channel.json`, |
| 330 | '{"autoUpdatesChannel":"latest"}', |
| 331 | ); |
| 332 | expect((yield* check).versionAdvisory?.latestVersion).toBe("2.1.308"); |
| 333 | expect(requests).toHaveLength(2); |
| 334 | const capabilities = yield* resolve(); |
| 335 | yield* invalidateLatestProviderVersion(capabilities).pipe( |
| 336 | Effect.provideService(ProviderLatestVersions.ProviderLatestVersions, versions), |
| 337 | ); |
| 338 | yield* check; |
| 339 | expect(requests).toHaveLength(3); |
| 340 | }), |
| 341 | ); |
| 342 | |
| 343 | it.effect.each([ |
| 344 | { name: "a wrapper script", install: "wrapper", lockKey: undefined }, |
| 345 | { |
| 346 | name: "a native install under XDG_DATA_HOME", |
| 347 | install: "xdgNative", |
| 348 | lockKey: "claudeAgent-native", |
| 349 | }, |
| 350 | ] as const)("resolves the native launcher path for $name", (row) => |
| 351 | Effect.gen(function* () { |
| 352 | const f = yield* fixture(row.install); |
| 353 | const resolve = yield* f.makeResolution.pipe( |
| 354 | Effect.provideService( |
| 355 | HttpClient.HttpClient, |
| 356 | HttpClient.make(() => Effect.die("resolution must not look up a version")), |
| 357 | ), |
| 358 | ); |
| 359 | expect((yield* resolve()).update?.lockKey).toBe(row.lockKey); |
| 360 | }), |
| 361 | ); |
| 362 | |
| 363 | it.effect("automatic refresh rereads the native channel while preserving both caches", () => |
| 364 | Effect.gen(function* () { |
| 365 | const f = yield* fixture(); |
| 366 | const requests: string[] = []; |
| 367 | const client = HttpClient.make((request) => { |
| 368 | requests.push(request.url); |
| 369 | return Effect.succeed( |
| 370 | HttpClientResponse.fromWeb( |
| 371 | request, |
| 372 | new Response(request.url.endsWith("/stable") ? "2.1.308" : "2.1.315"), |
| 373 | ), |
| 374 | ); |
| 375 | }); |
| 376 | const resolveMaintenance = yield* f.makeResolution.pipe( |
| 377 | Effect.provideService(HttpClient.HttpClient, client), |
| 378 | ); |
| 379 | const versions = yield* ProviderLatestVersions.make(); |
| 380 | const publications = yield* Queue.unbounded<ServerProvider>(); |
| 381 | yield* makeManagedServerProvider({ |
| 382 | resolveMaintenance, |
| 383 | getSettings: Effect.succeed({ enabled: true }), |
| 384 | streamSettings: Stream.empty, |
| 385 | haveSettingsChanged: () => false, |
| 386 | initialSnapshot: () => Effect.succeed(snapshot), |
| 387 | checkProvider: Effect.succeed(snapshot), |
| 388 | refreshInterval: "1 minute", |
| 389 | enrichSnapshot: ({ snapshot, publishSnapshot }) => |
| 390 | resolveMaintenance().pipe( |
| 391 | Effect.flatMap((maintenance) => |
| 392 | enrichProviderSnapshotWithVersionAdvisory(snapshot, maintenance), |
| 393 | ), |
| 394 | Effect.provideService(HttpClient.HttpClient, client), |
| 395 | Effect.provideService(ProviderLatestVersions.ProviderLatestVersions, versions), |
| 396 | Effect.flatMap((enriched) => |
| 397 | publishSnapshot(enriched).pipe( |
| 398 | Effect.andThen(Queue.offer(publications, enriched)), |
| 399 | Effect.asVoid, |
| 400 | ), |
| 401 | ), |
| 402 | ), |
| 403 | }).pipe( |
| 404 | Effect.provide( |
| 405 | Layer.mock(ProviderHost.ProviderHost)({ |
| 406 | awaitServerReady: Effect.void, |
| 407 | paths: { |
| 408 | cwd: f.directory, |
| 409 | baseDir: f.directory, |
| 410 | stateDir: f.directory, |
| 411 | providerStatusCacheDir: f.directory, |
| 412 | attachmentsDir: f.directory, |
| 413 | }, |
| 414 | settings: { |
| 415 | get: Effect.succeed(DEFAULT_SERVER_SETTINGS), |
| 416 | withSnapshot: (use) => use(DEFAULT_SERVER_SETTINGS), |
| 417 | changes: Stream.empty, |
| 418 | subscribe: Effect.succeed(Stream.empty), |
| 419 | }, |
| 420 | shouldRunBackgroundWork: () => Effect.succeed(true), |
| 421 | resolveAttachmentPath: () => null, |
| 422 | }), |
| 423 | ), |
| 424 | ); |
| 425 | expect((yield* Queue.take(publications)).versionAdvisory).toMatchObject({ |
| 426 | latestVersion: "2.1.315", |
| 427 | status: "behind_latest", |
| 428 | }); |
| 429 | expect(requests).toHaveLength(1); |
| 430 | |
| 431 | for (const channel of ["stable", "stable", "latest"] as const) { |
| 432 | yield* f.fs.writeFileString( |
| 433 | f.settingsPath, |
| 434 | JSON.stringify({ autoUpdatesChannel: channel }), |
| 435 | ); |
| 436 | yield* TestClock.adjust("1 minute"); |
| 437 | expect((yield* Queue.take(publications)).versionAdvisory).toMatchObject({ |
| 438 | latestVersion: channel === "stable" ? "2.1.308" : "2.1.315", |
| 439 | status: channel === "stable" ? "current" : "behind_latest", |
| 440 | }); |
| 441 | expect(requests).toEqual([ |
| 442 | "https://downloads.claude.ai/claude-code-releases/latest", |
| 443 | "https://downloads.claude.ai/claude-code-releases/stable", |
| 444 | ]); |
| 445 | expect(f.installationReads()).toBe(1); |
| 446 | } |
| 447 | }), |
| 448 | ); |
| 449 | |
| 450 | it.effect("bounds stalled feed bodies and skips disabled update checks", () => |
| 451 | Effect.gen(function* () { |
| 452 | const f = yield* fixture(); |
| 453 | let requests = 0; |
| 454 | const client = HttpClient.make((request) => { |
| 455 | requests++; |
| 456 | return Effect.succeed( |
| 457 | HttpClientResponse.fromWeb(request, new Response(new ReadableStream())), |
| 458 | ); |
| 459 | }); |
| 460 | const resolve = yield* f.makeResolution.pipe( |
| 461 | Effect.provideService(HttpClient.HttpClient, client), |
| 462 | ); |
| 463 | const capabilities = yield* resolve(); |
| 464 | const enrich = (enabled: boolean) => |
| 465 | enrichProviderSnapshotWithVersionAdvisory(snapshot, capabilities, { |
| 466 | enableProviderUpdateChecks: enabled, |
| 467 | }).pipe( |
| 468 | Effect.provide(ProviderLatestVersions.layer), |
| 469 | Effect.provideService(HttpClient.HttpClient, client), |
| 470 | ); |
| 471 | expect((yield* enrich(false)).versionAdvisory?.status).toBe("unknown"); |
| 472 | expect(requests).toBe(0); |
| 473 | const pending = yield* enrich(true).pipe(Effect.forkChild({ startImmediately: true })); |
| 474 | yield* TestClock.adjust("4 seconds"); |
| 475 | expect((yield* Fiber.join(pending)).versionAdvisory?.status).toBe("unknown"); |
| 476 | expect(requests).toBe(1); |
| 477 | }), |
| 478 | ); |
| 479 | }); |