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

ClaudeCapabilitiesProbe.test.ts

172 lines7.3 KB

1// @effect-diagnostics nodeBuiltinImport:off - cleanup uses Node's retrying rm, which the FileSystem service does not expose.
2import * as ClaudeSdk from "@anthropic-ai/claude-agent-sdk";
3import { vi } from "vite-plus/test";
4import * as Deferred from "effect/Deferred";
5import * as Fiber from "effect/Fiber";
6import * as TestClock from "effect/testing/TestClock";
7import { ClaudeSettings } from "@t3tools/contracts";
8import * as NodeFSP from "node:fs/promises";
9import * as NodeServices from "@effect/platform-node/NodeServices";
10import { assert, it } from "@effect/vitest";
11import * as Effect from "effect/Effect";
12import * as FileSystem from "effect/FileSystem";
13import * as Path from "effect/Path";
14import * as Schema from "effect/Schema";
15​
16import {
17 buildClaudeCapabilitiesProbeQueryOptions,
18 CLAUDE_CAPABILITIES_PROBE_SETTING_SOURCES,
19 probeClaudeCapabilities,
20} from "./ClaudeProvider.ts";
21​
22vi.mock("@anthropic-ai/claude-agent-sdk", { spy: true });
23​
24const decodeClaudeSettings = Schema.decodeSync(ClaudeSettings);
25​
26it("isolates Claude capability probes without dropping workspace setting sources", () => {
27 const abortController = new AbortController();
28 const options = buildClaudeCapabilitiesProbeQueryOptions({
29 executablePath: "/usr/bin/claude",
30 abortController,
31 environment: {
32 HOME: "/home/user",
33 ENABLE_CLAUDEAI_MCP_SERVERS: "true",
34 FORCE_CODE_TERMINAL: "1",
35 },
36 cwd: "/workspace/project",
37 });
38​
39 assert.deepEqual(options.mcpServers, {});
40 assert.equal(options.strictMcpConfig, true);
41 assert.equal(options.cwd, "/workspace/project");
42 assert.deepEqual(options.settingSources, [...CLAUDE_CAPABILITIES_PROBE_SETTING_SOURCES]);
43 assert.deepEqual(options.settings, { disableAllHooks: true });
44 assert.deepEqual(options.allowedTools, []);
45 assert.equal(options.persistSession, false);
46 assert.equal(options.pathToClaudeCodeExecutable, "/usr/bin/claude");
47 assert.equal(options.abortController, abortController);
48 assert.equal(options.env?.HOME, "/home/user");
49 assert.equal(options.env?.ENABLE_CLAUDEAI_MCP_SERVERS, "false");
50 assert.equal(options.env?.FORCE_CODE_TERMINAL, undefined);
51 assert.equal(options.env?.CLAUDE_CODE_AUTO_CONNECT_IDE, "0");
52 assert.equal(options.env?.CLAUDE_CODE_IDE_SKIP_AUTO_INSTALL, "1");
53});
54​
55it.layer(NodeServices.layer)("Claude capability probe SDK boundary", (it) => {
56 it.effect("serializes strict no-MCP options and still resolves account capabilities", () =>
57 Effect.gen(function* () {
58 const fs = yield* FileSystem.FileSystem;
59 const path = yield* Path.Path;
60 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-probe-sdk-" });
61 const executablePath = yield* path.fromFileUrl(
62 new URL("./testing/ClaudeCapabilitiesProbe.fixture.mjs", import.meta.url),
63 );
64 const invocationPath = path.join(tempDir, "invocation.json");
65 // The probe aborts the SDK without awaiting the child's exit, and on
66 // Windows a directory that is still some process's cwd cannot be
67 // removed. Keep the workspace outside the scoped directory and let it
68 // go with a retrying removal once the child has gone.
69 const workspaceCwd = yield* fs.makeTempDirectory({ prefix: "t3-claude-probe-cwd-" });
70 // Node's own retry rather than an Effect schedule: it.effect runs on a
71 // TestClock, so a scheduled retry would wait for time nobody advances.
72 // If the child still holds the directory after that, an empty temp
73 // directory is left behind rather than failing the test for it.
74 yield* Effect.addFinalizer(() =>
75 Effect.promise(() =>
76 NodeFSP.rm(workspaceCwd, {
77 recursive: true,
78 force: true,
79 maxRetries: 20,
80 retryDelay: 250,
81 }).catch(() => undefined),
82 ),
83 );
84​
85 const capabilities = yield* probeClaudeCapabilities(
86 decodeClaudeSettings({ binaryPath: executablePath }),
87 {
88 ...process.env,
89 T3_PROBE_INVOCATION_PATH: invocationPath,
90 ENABLE_CLAUDEAI_MCP_SERVERS: "true",
91 },
92 workspaceCwd,
93 );
94​
95 assert.deepEqual(capabilities, {
96 email: "dev@example.com",
97 subscriptionType: "pro",
98 tokenSource: "oauth",
99 apiProvider: undefined,
100 slashCommands: [
101 {
102 name: "review",
103 description: "Review changes",
104 input: { hint: "[path]" },
105 },
106 ],
107 usage: {
108 rate_limits_available: true,
109 rate_limits: { five_hour: { utilization: 12, resets_at: "2026-07-18T14:39:00Z" } },
110 },
111 });
112​
113 // @effect-diagnostics-next-line preferSchemaOverJson:off
114 const invocation = JSON.parse(yield* fs.readFileString(invocationPath)) as {
115 readonly args: ReadonlyArray<string>;
116 readonly cwd: string;
117 readonly connectorEnv: string;
118 readonly mcpConfig: unknown;
119 };
120 assert.equal(invocation.cwd, yield* fs.realPath(workspaceCwd));
121 assert.equal(invocation.connectorEnv, "false");
122 assert.equal(invocation.args.includes("--strict-mcp-config"), true);
123 assert.equal(invocation.args.includes("--mcp-config"), false);
124 assert.equal(invocation.mcpConfig, undefined);
125​
126 assert.equal(invocation.args.includes("--setting-sources=user,project,local"), true);
127​
128 const settingsFlagIndex = invocation.args.indexOf("--settings");
129 assert.notEqual(settingsFlagIndex, -1);
130 // @effect-diagnostics-next-line preferSchemaOverJson:off
131 const flagSettings = JSON.parse(invocation.args[settingsFlagIndex + 1] ?? "{}") as {
132 readonly disableAllHooks?: boolean;
133 };
134 assert.equal(flagSettings.disableAllHooks, true);
135 }).pipe(Effect.scoped),
136 );
137});
138​
139it.effect("preserves initialized capabilities when optional usage times out", () =>
140 Effect.gen(function* () {
141 const usageStarted = yield* Deferred.make<void>();
142 let abortSignal: AbortSignal | undefined;
143 const query = vi.spyOn(ClaudeSdk, "query").mockImplementation(({ options }) => {
144 abortSignal = options?.abortController?.signal;
145 return {
146 initializationResult: async () => ({
147 account: { email: "dev@example.com", subscriptionType: "pro", tokenSource: "oauth" },
148 commands: [{ name: "review", description: "Review changes", argumentHint: "[path]" }],
149 }),
150 usage_EXPERIMENTAL_MAY_CHANGE_DO_NOT_RELY_ON_THIS_API_YET: () => {
151 Deferred.doneUnsafe(usageStarted, Effect.void);
152 return new Promise(() => {});
153 },
154 } as ReturnType<typeof ClaudeSdk.query>;
155 });
156 yield* Effect.addFinalizer(() => Effect.sync(() => query.mockRestore()));
157 const probe = yield* probeClaudeCapabilities(
158 decodeClaudeSettings({ binaryPath: "claude" }),
159 ).pipe(Effect.forkChild);
160 yield* Deferred.await(usageStarted);
161 yield* TestClock.adjust("4 seconds");
162 const capabilities = yield* Fiber.join(probe);
163 assert.equal(capabilities?.email, "dev@example.com");
164 assert.equal(capabilities?.subscriptionType, "pro");
165 assert.equal(capabilities?.tokenSource, "oauth");
166 assert.deepEqual(capabilities?.slashCommands, [
167 { name: "review", description: "Review changes", input: { hint: "[path]" } },
168 ]);
169 assert.equal(capabilities?.usage, undefined);
170 assert.equal(abortSignal?.aborted, true);
171 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
172);