Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ClaudeCapabilitiesProbe.test.ts

89 lines3.8 KB

1// @effect-diagnostics nodeBuiltinImport:off - cleanup uses Node's retrying rm, which the FileSystem service does not expose.
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { ClaudeSettings, type ServerProvider } from "@t3tools/contracts";
4import { vi } from "vite-plus/test";
5import * as ClaudeSdk from "@anthropic-ai/claude-agent-sdk";
6import { assert, it } from "@effect/vitest";
7import * as Effect from "effect/Effect";
8import * as FileSystem from "effect/FileSystem";
9import * as Schema from "effect/Schema";
10import * as TestClock from "effect/testing/TestClock";
11​
12import {
13 buildClaudeCapabilitiesProbeQueryOptions,
14 probeClaudeCapabilities,
15 probeClaudeWorkspaceSnapshot,
16} from "./ClaudeProvider.ts";
17​
18vi.mock("@anthropic-ai/claude-agent-sdk", { spy: true });
19​
20const decodeClaudeSettings = Schema.decodeEffect(ClaudeSettings);
21​
22it("drops strict MCP mode, and only that, next to an administrator's managed-mcp.json", () => {
23 const options = buildClaudeCapabilitiesProbeQueryOptions({
24 executablePath: "/usr/bin/claude",
25 abortController: new AbortController(),
26 environment: { HOME: "/home/user" },
27 cwd: undefined,
28 managedMcpConfig: true,
29 });
30​
31 // Claude exits before initializing when handed --strict-mcp-config here.
32 assert.equal(options.strictMcpConfig, undefined);
33 assert.deepEqual(options.mcpServers, {});
34 assert.equal(options.env?.ENABLE_CLAUDEAI_MCP_SERVERS, "false");
35});
36​
37it.effect("reports a deleted worktree's scan as finished, not pending", () =>
38 Effect.gen(function* () {
39 const snapshot = yield* probeClaudeWorkspaceSnapshot(
40 yield* decodeClaudeSettings({}),
41 { skills: [], slashCommands: [] } as unknown as ServerProvider,
42 "/nonexistent/t3-deleted-worktree",
43 { HOME: "/nonexistent/t3-home" },
44 );
45 assert.strictEqual(snapshot.slashCommandsPending, false);
46 }).pipe(Effect.provide(NodeServices.layer)),
47);
48​
49it.effect("keeps the probe subprocess alive while gateway model discovery can finish", () =>
50 Effect.gen(function* () {
51 let abortSignal: AbortSignal | undefined;
52 const query = vi.spyOn(ClaudeSdk, "query").mockImplementation(({ options }) => {
53 abortSignal = options?.abortController?.signal;
54 return {
55 initializationResult: async () => ({
56 account: { email: "dev@example.com", subscriptionType: "pro", tokenSource: "oauth" },
57 commands: [{ name: "review", description: "Review changes", argumentHint: "[path]" }],
58 models: [{ value: "gateway/claude-relay", displayName: "Relay", description: "" }],
59 }),
60 usage_EXPERIMENTAL_MAY_CHANGE_DO_NOT_RELY_ON_THIS_API_YET: () =>
61 new Promise((_, reject) => reject(new Error("usage unavailable"))),
62 } as ReturnType<typeof ClaudeSdk.query>;
63 });
64 yield* Effect.addFinalizer(() => Effect.sync(() => query.mockRestore()));
65 const fileSystem = yield* FileSystem.FileSystem;
66 const configDir = yield* fileSystem.makeTempDirectoryScoped();
67 // Claude Code also reads these from settings.json, which is where gateway users set them.
68 yield* fileSystem.writeFileString(
69 `${configDir}/settings.json`,
70 JSON.stringify({
71 env: {
72 ANTHROPIC_BASE_URL: "https://gateway.example",
73 CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY: "1",
74 },
75 }),
76 );
77 const capabilities = yield* probeClaudeCapabilities(
78 yield* decodeClaudeSettings({ binaryPath: "claude" }),
79 { PATH: process.env.PATH, CLAUDE_CONFIG_DIR: configDir },
80 );
81 assert.deepEqual(
82 capabilities.models?.map((model) => model.value),
83 ["gateway/claude-relay"],
84 );
85 assert.equal(abortSignal?.aborted, false);
86 yield* TestClock.adjust("3 seconds");
87 assert.equal(abortSignal?.aborted, true);
88 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
89);