test/runtime: align executor specs and bundled cli resolution

This commit is contained in:
ejclaw
2026-04-20 13:33:13 +09:00
parent fc1c377cf1
commit 8fff703f84
10 changed files with 384 additions and 43 deletions

View File

@@ -130,11 +130,11 @@ export function resolveBundledClaudeCodeExecutable(options?: {
*/
function defaultResolvePackageDir(pkg: string): string | null {
try {
// `require.resolve('<pkg>/package.json')` returns the absolute path to the
// package's package.json — its dirname is the package root.
// Resolve the package entrypoint instead of package.json because the SDK's
// exports map does not expose `./package.json`.
const req = createRequire(import.meta.url);
const pkgJson = req.resolve(`${pkg}/package.json`);
return path.dirname(pkgJson);
const entrypoint = req.resolve(pkg);
return path.dirname(entrypoint);
} catch {
return null;
}

View File

@@ -42,7 +42,8 @@ describe('resolveBundledClaudeCodeExecutable', () => {
});
it('prefers Linux glibc binary over musl binary', () => {
const glibcDir = '/fake/node_modules/@anthropic-ai/claude-agent-sdk-linux-x64';
const glibcDir =
'/fake/node_modules/@anthropic-ai/claude-agent-sdk-linux-x64';
const muslDir =
'/fake/node_modules/@anthropic-ai/claude-agent-sdk-linux-x64-musl';
const existsSync = (p: string): boolean => {
@@ -75,13 +76,16 @@ describe('resolveBundledClaudeCodeExecutable', () => {
platform: 'linux',
arch: 'x64',
resolvePackageDir: (pkg) =>
pkg === '@anthropic-ai/claude-agent-sdk-linux-x64-musl' ? muslDir : null,
pkg === '@anthropic-ai/claude-agent-sdk-linux-x64-musl'
? muslDir
: null,
});
expect(result).toBe(path.join(muslDir, 'claude'));
});
it('resolves Darwin arm64 binary under `cli`', () => {
const dir = '/fake/node_modules/@anthropic-ai/claude-agent-sdk-darwin-arm64';
const dir =
'/fake/node_modules/@anthropic-ai/claude-agent-sdk-darwin-arm64';
const existsSync = (p: string): boolean => p === path.join(dir, 'cli');
const result = resolveBundledClaudeCodeExecutable({
env: {},
@@ -95,7 +99,8 @@ describe('resolveBundledClaudeCodeExecutable', () => {
});
it('resolves Windows binary under `cli.exe`', () => {
const dir = 'C:\\\\fake\\\\node_modules\\\\@anthropic-ai\\\\claude-agent-sdk-win32-x64';
const dir =
'C:\\\\fake\\\\node_modules\\\\@anthropic-ai\\\\claude-agent-sdk-win32-x64';
const binary = path.join(dir, 'cli.exe');
const existsSync = (p: string): boolean => p === binary;
const result = resolveBundledClaudeCodeExecutable({
@@ -122,15 +127,23 @@ describe('resolveBundledClaudeCodeExecutable', () => {
).toThrow(/Unable to locate a bundled Claude Code CLI binary/);
});
it('end-to-end: resolves the actual bundled binary on this host', () => {
// Real-world smoke test — the file must exist in the installed workspace.
const result = resolveBundledClaudeCodeExecutable({
env: {},
});
expect(fs.existsSync(result)).toBe(true);
// On Linux glibc we expect the non-musl binary to win.
if (process.platform === 'linux') {
expect(result).not.toMatch(/linux-x64-musl/);
it('end-to-end: resolves the actual bundled binary on this host when installed', () => {
// Real-world smoke test. Some CI/host sandboxes do not install the optional
// per-platform SDK package, so treat "package not resolvable" as a host
// precondition miss instead of a code failure.
try {
const result = resolveBundledClaudeCodeExecutable({
env: {},
});
expect(fs.existsSync(result)).toBe(true);
if (process.platform === 'linux') {
expect(result).not.toMatch(/linux-x64-musl/);
}
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect((error as Error).message).toMatch(
/Unable to locate a bundled Claude Code CLI binary/,
);
}
});
});