The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics nodeBuiltinImport:off - cleanup uses Node's retrying rm, which the FileSystem service does not expose. |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | import { ClaudeSettings, type ServerProvider } from "@t3tools/contracts"; |
| 4 | import { vi } from "vite-plus/test"; |
| 5 | import * as ClaudeSdk from "@anthropic-ai/claude-agent-sdk"; |
| 6 | import { assert, it } from "@effect/vitest"; |
| 7 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as FileSystem from "effect/FileSystem"; |
| 10 | import * as Schema from "effect/Schema"; |
| 11 | import * as TestClock from "effect/testing/TestClock"; |
| 12 | |
| 13 | import { |
| 14 | buildClaudeCapabilitiesProbeQueryOptions, |
| 15 | probeClaudeCapabilities, |
| 16 | probeClaudeWorkspaceSnapshot, |
| 17 | spawnClaudeProbeThroughEnv, |
| 18 | } from "./ClaudeProvider.ts"; |
| 19 | |
| 20 | vi.mock("@anthropic-ai/claude-agent-sdk", { spy: true }); |
| 21 | |
| 22 | const hostPath = process.env.PATH; |
| 23 | |
| 24 | const decodeClaudeSettings = Schema.decodeEffect(ClaudeSettings); |
| 25 | |
| 26 | it("drops strict MCP mode, and only that, next to an administrator's managed-mcp.json", () => { |
| 27 | const options = buildClaudeCapabilitiesProbeQueryOptions({ |
| 28 | executablePath: "/usr/bin/claude", |
| 29 | abortController: new AbortController(), |
| 30 | environment: { HOME: "/home/user" }, |
| 31 | cwd: undefined, |
| 32 | managedMcpConfig: true, |
| 33 | spawnVia: undefined, |
| 34 | }); |
| 35 | |
| 36 | // Claude exits before initializing when handed --strict-mcp-config here. |
| 37 | assert.equal(options.strictMcpConfig, undefined); |
| 38 | assert.deepEqual(options.mcpServers, {}); |
| 39 | assert.equal(options.env?.ENABLE_CLAUDEAI_MCP_SERVERS, "false"); |
| 40 | }); |
| 41 | |
| 42 | // A shell trampoline would not do: dash drops the exported function, and the launcher exits 127. |
| 43 | it.live.skipIf(HostProcess.Platform.defaultValue() !== "linux")( |
| 44 | "starts a Bash launcher that calls an exported function in the probe's workspace", |
| 45 | () => |
| 46 | Effect.gen(function* () { |
| 47 | const fileSystem = yield* FileSystem.FileSystem; |
| 48 | const workspace = yield* fileSystem.realPath(yield* fileSystem.makeTempDirectoryScoped()); |
| 49 | yield* fileSystem.writeFileString(`${workspace}/claude`, '#!/bin/bash\nt3_launch "$@"\n'); |
| 50 | yield* fileSystem.chmod(`${workspace}/claude`, 0o755); |
| 51 | const child = spawnClaudeProbeThroughEnv({ |
| 52 | command: "claude", |
| 53 | args: ["--probe"], |
| 54 | cwd: workspace, |
| 55 | env: { PATH: `.:${hostPath}`, "BASH_FUNC_t3_launch%%": '() { echo "$PWD $@"; }' }, |
| 56 | // @effect-diagnostics-next-line abortControllerInEffect:off - The launcher exits on its own. |
| 57 | signal: new AbortController().signal, |
| 58 | }); |
| 59 | assert.deepEqual(child.spawnargs.slice(0, 3), ["/usr/bin/env", "-C", workspace]); |
| 60 | const output = yield* Effect.promise(() => child.stdout.setEncoding("utf8").toArray()); |
| 61 | assert.equal(output.join(""), `${workspace} --probe\n`); |
| 62 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 63 | ); |
| 64 | |
| 65 | it.effect("reports a deleted worktree's scan as finished, not pending", () => |
| 66 | Effect.gen(function* () { |
| 67 | const snapshot = yield* probeClaudeWorkspaceSnapshot( |
| 68 | yield* decodeClaudeSettings({}), |
| 69 | { skills: [], slashCommands: [] } as unknown as ServerProvider, |
| 70 | "/nonexistent/t3-deleted-worktree", |
| 71 | { HOME: "/nonexistent/t3-home" }, |
| 72 | ); |
| 73 | assert.strictEqual(snapshot.slashCommandsPending, false); |
| 74 | }).pipe(Effect.provide(NodeServices.layer)), |
| 75 | ); |
| 76 | |
| 77 | it.effect("keeps the probe subprocess alive while gateway model discovery can finish", () => |
| 78 | Effect.gen(function* () { |
| 79 | let abortSignal: AbortSignal | undefined; |
| 80 | const query = vi.spyOn(ClaudeSdk, "query").mockImplementation(({ options }) => { |
| 81 | abortSignal = options?.abortController?.signal; |
| 82 | return { |
| 83 | initializationResult: async () => ({ |
| 84 | account: { email: "dev@example.com", subscriptionType: "pro", tokenSource: "oauth" }, |
| 85 | commands: [{ name: "review", description: "Review changes", argumentHint: "[path]" }], |
| 86 | models: [{ value: "gateway/claude-relay", displayName: "Relay", description: "" }], |
| 87 | }), |
| 88 | usage_EXPERIMENTAL_MAY_CHANGE_DO_NOT_RELY_ON_THIS_API_YET: () => |
| 89 | new Promise((_, reject) => reject(new Error("usage unavailable"))), |
| 90 | } as ReturnType<typeof ClaudeSdk.query>; |
| 91 | }); |
| 92 | yield* Effect.addFinalizer(() => Effect.sync(() => query.mockRestore())); |
| 93 | const fileSystem = yield* FileSystem.FileSystem; |
| 94 | const configDir = yield* fileSystem.makeTempDirectoryScoped(); |
| 95 | // Claude Code also reads these from settings.json, which is where gateway users set them. |
| 96 | yield* fileSystem.writeFileString( |
| 97 | `${configDir}/settings.json`, |
| 98 | JSON.stringify({ |
| 99 | env: { |
| 100 | ANTHROPIC_BASE_URL: "https://gateway.example", |
| 101 | CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY: "1", |
| 102 | }, |
| 103 | }), |
| 104 | ); |
| 105 | const capabilities = yield* probeClaudeCapabilities( |
| 106 | yield* decodeClaudeSettings({ binaryPath: "claude" }), |
| 107 | { PATH: (yield* HostProcess.Environment).PATH, CLAUDE_CONFIG_DIR: configDir }, |
| 108 | ); |
| 109 | assert.deepEqual( |
| 110 | capabilities.models?.map((model) => model.value), |
| 111 | ["gateway/claude-relay"], |
| 112 | ); |
| 113 | assert.equal(abortSignal?.aborted, false); |
| 114 | yield* TestClock.adjust("3 seconds"); |
| 115 | assert.equal(abortSignal?.aborted, true); |
| 116 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 117 | ); |