Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeFS from "node:fs";
3import * as NodePath from "node:path";
4
5import * as NodeServices from "@effect/platform-node/NodeServices";
6import { describe, expect, it } from "@effect/vitest";
7import * as FileSystem from "effect/FileSystem";
8import * as HostProcess from "@t3tools/shared/HostProcess";
9import { SpawnExecutableResolution } from "@t3tools/shared/shell";
10import * as Effect from "effect/Effect";
11
12import {
13 ClaudeExecutableFileCheck,
14 claudeProgramReader,
15 claudeProgramWasReplaced,
16 resolveClaudeSdkExecutablePath,
17} from "./ClaudeExecutable.ts";
18
19const NPM_DIR = "C:\\Users\\dev\\AppData\\Roaming\\npm";
20const NPM_SHIM = `${NPM_DIR}\\claude.cmd`;
21const NPM_PACKAGE_EXE = `${NPM_DIR}\\node_modules\\@anthropic-ai\\claude-code\\bin\\claude.exe`;
22const NPM_PACKAGE_CLI = `${NPM_DIR}\\node_modules\\@anthropic-ai\\claude-code\\cli.js`;
23const WINGET_DIR = "C:\\Users\\dev\\AppData\\Local\\Microsoft\\WinGet";
24const WINGET_SHIM = `${WINGET_DIR}\\Links\\claude.cmd`;
25const WINGET_EXE = `${WINGET_DIR}\\Packages\\Anthropic.ClaudeCode_Microsoft.Winget.Source_8wekyb3d8bbwe\\claude.exe`;
26
27function withWindowsResolution(input: {
28 readonly resolvedCommand: string | undefined;
29 readonly existingFiles?: ReadonlyArray<string>;
30}) {
31 const existing = new Set(input.existingFiles ?? []);
32 return <A, E, R>(effect: Effect.Effect<A, E, R>) =>
33 effect.pipe(
34 Effect.provideService(HostProcess.Platform, "win32"),
35 Effect.provideService(SpawnExecutableResolution, async () => input.resolvedCommand),
36 Effect.provideService(ClaudeExecutableFileCheck, (filePath) => existing.has(filePath)),
37 );
38}
39
40describe("resolveClaudeSdkExecutablePath", () => {
41 it.effect("follows an npm launcher shim to the packaged native binary", () =>
42 Effect.gen(function* () {
43 expect(
44 yield* resolveClaudeSdkExecutablePath("claude", {}).pipe(
45 withWindowsResolution({
46 resolvedCommand: NPM_SHIM,
47 existingFiles: [NPM_PACKAGE_EXE, NPM_PACKAGE_CLI],
48 }),
49 ),
50 ).toBe(NPM_PACKAGE_EXE);
51 }),
52 );
53
54 it.effect("follows the WinGet launcher to the package's native binary", () =>
55 Effect.gen(function* () {
56 expect(
57 yield* resolveClaudeSdkExecutablePath("claude", {}).pipe(
58 withWindowsResolution({ resolvedCommand: WINGET_SHIM, existingFiles: [WINGET_EXE] }),
59 ),
60 ).toBe(WINGET_EXE);
61 }),
62 );
63
64 it.effect("follows .bat and .ps1 launcher shims the same way", () =>
65 Effect.gen(function* () {
66 for (const shim of [`${NPM_DIR}\\claude.bat`, `${NPM_DIR}\\claude.ps1`]) {
67 expect(
68 yield* resolveClaudeSdkExecutablePath("claude", {}).pipe(
69 withWindowsResolution({
70 resolvedCommand: shim,
71 existingFiles: [NPM_PACKAGE_EXE],
72 }),
73 ),
74 ).toBe(NPM_PACKAGE_EXE);
75 }
76 }),
77 );
78
79 it.effect("normalizes mixed-case shim extensions before matching", () =>
80 Effect.gen(function* () {
81 expect(
82 yield* resolveClaudeSdkExecutablePath("claude", {}).pipe(
83 withWindowsResolution({
84 resolvedCommand: `${NPM_DIR}\\claude.CMD`,
85 existingFiles: [NPM_PACKAGE_EXE],
86 }),
87 ),
88 ).toBe(NPM_PACKAGE_EXE);
89 }),
90 );
91
92 it.effect("falls back to cli.js when the package ships no native binary", () =>
93 Effect.gen(function* () {
94 expect(
95 yield* resolveClaudeSdkExecutablePath("claude", {}).pipe(
96 withWindowsResolution({
97 resolvedCommand: NPM_SHIM,
98 existingFiles: [NPM_PACKAGE_CLI],
99 }),
100 ),
101 ).toBe(NPM_PACKAGE_CLI);
102 }),
103 );
104
105 it.effect("follows a pnpm global launcher to the hashed package's native binary", () =>
106 Effect.scoped(
107 Effect.gen(function* () {
108 const fileSystem = yield* FileSystem.FileSystem;
109 const pnpmHome = yield* fileSystem.makeTempDirectoryScoped({ prefix: "pnpm-home-" });
110 const binDir = NodePath.join(pnpmHome, "bin");
111 const shim = NodePath.join(binDir, "claude.CMD");
112 const target =
113 "global\\v11\\7674-1a16f3bff9d-6efed413fb25d243\\node_modules\\@anthropic-ai\\claude-code\\bin\\claude.exe";
114 NodeFS.mkdirSync(binDir);
115 NodeFS.writeFileSync(
116 shim,
117 [
118 "@SETLOCAL",
119 "@IF NOT DEFINED NODE_PATH (",
120 ' @SET "NODE_PATH=C:\\pnpm\\global\\v11\\hash\\node_modules\\.pnpm\\node_modules"',
121 ")",
122 `@"%~dp0\\..\\${target}" %*`,
123 "",
124 ].join("\r\n"),
125 );
126 const packageExe = NodePath.win32.join(binDir, "..", target);
127
128 expect(
129 yield* resolveClaudeSdkExecutablePath("claude", {}).pipe(
130 withWindowsResolution({ resolvedCommand: shim, existingFiles: [packageExe] }),
131 ),
132 ).toBe(packageExe);
133 }),
134 ).pipe(Effect.provide(NodeServices.layer)),
135 );
136
137 it.effect("returns the configured path when a shim has no known package entry", () =>
138 Effect.gen(function* () {
139 expect(
140 yield* resolveClaudeSdkExecutablePath("claude", {}).pipe(
141 withWindowsResolution({ resolvedCommand: NPM_SHIM }),
142 ),
143 ).toBe("claude");
144 }),
145 );
146});
147
148describe("claudeProgramReader", () => {
149 /** An install as the Claude installer lays it out: one file per version and a link. */
150 const makeInstall = Effect.gen(function* () {
151 const fileSystem = yield* FileSystem.FileSystem;
152 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "claude-program-" });
153 const bin = NodePath.join(root, "bin");
154 const link = NodePath.join(bin, "claude");
155 const version = (name: string) => {
156 const file = NodePath.join(root, "versions", name);
157 NodeFS.mkdirSync(NodePath.dirname(file), { recursive: true });
158 NodeFS.writeFileSync(file, "#!/bin/sh\n", { mode: 0o755 });
159 return file;
160 };
161 NodeFS.mkdirSync(bin);
162 NodeFS.symlinkSync(version("1.0.0"), link);
163 return {
164 bin,
165 update: () => {
166 NodeFS.unlinkSync(link);
167 NodeFS.symlinkSync(version("1.0.1"), link);
168 },
169 };
170 });
171
172 it.effect("follows a bare command name through PATH", () =>
173 Effect.scoped(
174 Effect.gen(function* () {
175 const { bin, update } = yield* makeInstall;
176 const readProgram = yield* claudeProgramReader("claude", { PATH: bin });
177 const startedFrom = yield* readProgram;
178 expect(startedFrom).toBeDefined();
179 expect(claudeProgramWasReplaced(startedFrom, yield* readProgram)).toBe(false);
180 update();
181 expect(claudeProgramWasReplaced(startedFrom, yield* readProgram)).toBe(true);
182 }),
183 ).pipe(Effect.provide(NodeServices.layer)),
184 );
185});