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

ClaudeExecutable.test.ts

153 lines5.4 KB

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 );
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("returns the configured path when a shim has no known package entry", () =>
106 Effect.gen(function* () {
107 expect(
108 yield* resolveClaudeSdkExecutablePath("claude", {}).pipe(
109 withWindowsResolution({ resolvedCommand: NPM_SHIM }),
110 ),
111 ).toBe("claude");
112 }),
113 );
114});
115​
116describe("claudeProgramReader", () => {
117 /** An install as the Claude installer lays it out: one file per version and a link. */
118 const makeInstall = Effect.gen(function* () {
119 const fileSystem = yield* FileSystem.FileSystem;
120 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "claude-program-" });
121 const bin = NodePath.join(root, "bin");
122 const link = NodePath.join(bin, "claude");
123 const version = (name: string) => {
124 const file = NodePath.join(root, "versions", name);
125 NodeFS.mkdirSync(NodePath.dirname(file), { recursive: true });
126 NodeFS.writeFileSync(file, "#!/bin/sh\n", { mode: 0o755 });
127 return file;
128 };
129 NodeFS.mkdirSync(bin);
130 NodeFS.symlinkSync(version("1.0.0"), link);
131 return {
132 bin,
133 update: () => {
134 NodeFS.unlinkSync(link);
135 NodeFS.symlinkSync(version("1.0.1"), link);
136 },
137 };
138 });
139​
140 it.effect("follows a bare command name through PATH", () =>
141 Effect.scoped(
142 Effect.gen(function* () {
143 const { bin, update } = yield* makeInstall;
144 const readProgram = yield* claudeProgramReader("claude", { PATH: bin });
145 const startedFrom = yield* readProgram;
146 expect(startedFrom).toBeDefined();
147 expect(claudeProgramWasReplaced(startedFrom, yield* readProgram)).toBe(false);
148 update();
149 expect(claudeProgramWasReplaced(startedFrom, yield* readProgram)).toBe(true);
150 }),
151 ).pipe(Effect.provide(NodeServices.layer)),
152 );
153});