|
| 1 | +/** |
| 2 | + * Tests for findClaudePath() resolver (B-009). |
| 3 | + * |
| 4 | + * The resolver has 5 steps: env override, SDK env var, `which claude`, |
| 5 | + * standard install locations, nvm glob. We test the env-based steps |
| 6 | + * (1, 2) directly and verify cache + reset behavior. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, beforeEach, afterEach } from "node:test"; |
| 10 | +import assert from "node:assert/strict"; |
| 11 | +import { writeFileSync, mkdirSync, unlinkSync, chmodSync } from "node:fs"; |
| 12 | +import { join } from "node:path"; |
| 13 | +import { tmpdir } from "node:os"; |
| 14 | +import { findClaudePath, _resetFindClaudePath } from "../src/utils/agent-options.ts"; |
| 15 | + |
| 16 | +// Save original env |
| 17 | +let savedAxme: string | undefined; |
| 18 | +let savedEntrypoint: string | undefined; |
| 19 | + |
| 20 | +function createTmpExecutable(name: string): string { |
| 21 | + const dir = join(tmpdir(), `axme-test-${Date.now()}-${Math.random().toString(36).slice(2)}`); |
| 22 | + mkdirSync(dir, { recursive: true }); |
| 23 | + const file = join(dir, name); |
| 24 | + writeFileSync(file, "#!/bin/sh\necho mock", "utf-8"); |
| 25 | + try { chmodSync(file, 0o755); } catch {} |
| 26 | + return file; |
| 27 | +} |
| 28 | + |
| 29 | +function removeTmp(path: string): void { |
| 30 | + try { unlinkSync(path); } catch {} |
| 31 | +} |
| 32 | + |
| 33 | +describe("findClaudePath — resolution order (B-009)", () => { |
| 34 | + beforeEach(() => { |
| 35 | + _resetFindClaudePath(); |
| 36 | + savedAxme = process.env.AXME_CLAUDE_EXECUTABLE; |
| 37 | + savedEntrypoint = process.env.CLAUDE_CODE_ENTRYPOINT; |
| 38 | + delete process.env.AXME_CLAUDE_EXECUTABLE; |
| 39 | + delete process.env.CLAUDE_CODE_ENTRYPOINT; |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + _resetFindClaudePath(); |
| 44 | + if (savedAxme !== undefined) process.env.AXME_CLAUDE_EXECUTABLE = savedAxme; |
| 45 | + else delete process.env.AXME_CLAUDE_EXECUTABLE; |
| 46 | + if (savedEntrypoint !== undefined) process.env.CLAUDE_CODE_ENTRYPOINT = savedEntrypoint; |
| 47 | + else delete process.env.CLAUDE_CODE_ENTRYPOINT; |
| 48 | + }); |
| 49 | + |
| 50 | + it("step 1: AXME_CLAUDE_EXECUTABLE env var takes priority over everything", () => { |
| 51 | + const tmp = createTmpExecutable("claude"); |
| 52 | + try { |
| 53 | + process.env.AXME_CLAUDE_EXECUTABLE = tmp; |
| 54 | + const result = findClaudePath(); |
| 55 | + assert.equal(result, tmp); |
| 56 | + } finally { |
| 57 | + removeTmp(tmp); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + it("step 1: AXME_CLAUDE_EXECUTABLE with non-existent path is skipped", () => { |
| 62 | + process.env.AXME_CLAUDE_EXECUTABLE = "/nonexistent/path/to/claude"; |
| 63 | + const result = findClaudePath(); |
| 64 | + // Should fall through to step 2+ (not crash, not return the bogus path) |
| 65 | + assert.notEqual(result, "/nonexistent/path/to/claude"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("step 2: CLAUDE_CODE_ENTRYPOINT used when AXME_CLAUDE_EXECUTABLE absent", () => { |
| 69 | + const tmp = createTmpExecutable("claude"); |
| 70 | + try { |
| 71 | + process.env.CLAUDE_CODE_ENTRYPOINT = tmp; |
| 72 | + const result = findClaudePath(); |
| 73 | + assert.equal(result, tmp); |
| 74 | + } finally { |
| 75 | + removeTmp(tmp); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + it("step 2: CLAUDE_CODE_ENTRYPOINT with non-existent path is skipped", () => { |
| 80 | + process.env.CLAUDE_CODE_ENTRYPOINT = "/nonexistent/entrypoint"; |
| 81 | + const result = findClaudePath(); |
| 82 | + assert.notEqual(result, "/nonexistent/entrypoint"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("step 1 beats step 2 when both are set", () => { |
| 86 | + const tmp1 = createTmpExecutable("claude-axme"); |
| 87 | + const tmp2 = createTmpExecutable("claude-sdk"); |
| 88 | + try { |
| 89 | + process.env.AXME_CLAUDE_EXECUTABLE = tmp1; |
| 90 | + process.env.CLAUDE_CODE_ENTRYPOINT = tmp2; |
| 91 | + const result = findClaudePath(); |
| 92 | + assert.equal(result, tmp1, "AXME_CLAUDE_EXECUTABLE should win over CLAUDE_CODE_ENTRYPOINT"); |
| 93 | + } finally { |
| 94 | + removeTmp(tmp1); |
| 95 | + removeTmp(tmp2); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + it("result is cached after first successful lookup", () => { |
| 100 | + const tmp = createTmpExecutable("claude"); |
| 101 | + try { |
| 102 | + process.env.AXME_CLAUDE_EXECUTABLE = tmp; |
| 103 | + const first = findClaudePath(); |
| 104 | + assert.equal(first, tmp); |
| 105 | + |
| 106 | + // Change env — cached result should still be the first one |
| 107 | + delete process.env.AXME_CLAUDE_EXECUTABLE; |
| 108 | + const second = findClaudePath(); |
| 109 | + assert.equal(second, tmp, "Should return cached value, not re-resolve"); |
| 110 | + } finally { |
| 111 | + removeTmp(tmp); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + it("_resetFindClaudePath clears the cache", () => { |
| 116 | + const tmp1 = createTmpExecutable("claude-a"); |
| 117 | + const tmp2 = createTmpExecutable("claude-b"); |
| 118 | + try { |
| 119 | + process.env.AXME_CLAUDE_EXECUTABLE = tmp1; |
| 120 | + assert.equal(findClaudePath(), tmp1); |
| 121 | + |
| 122 | + _resetFindClaudePath(); |
| 123 | + process.env.AXME_CLAUDE_EXECUTABLE = tmp2; |
| 124 | + assert.equal(findClaudePath(), tmp2, "After reset, should re-resolve"); |
| 125 | + } finally { |
| 126 | + removeTmp(tmp1); |
| 127 | + removeTmp(tmp2); |
| 128 | + } |
| 129 | + }); |
| 130 | + |
| 131 | + it("returns a string (not undefined) on a dev machine with claude installed", () => { |
| 132 | + // This test runs on our dev machine where `claude` IS in PATH via nvm. |
| 133 | + // On CI without claude installed, this test will find it via nvm glob |
| 134 | + // or standard paths — if neither exist, we just skip the assertion. |
| 135 | + const result = findClaudePath(); |
| 136 | + if (result) { |
| 137 | + assert.equal(typeof result, "string"); |
| 138 | + assert.ok(result.length > 0); |
| 139 | + assert.ok(result.includes("claude"), `Expected path containing 'claude', got: ${result}`); |
| 140 | + } |
| 141 | + // If result is undefined (bare CI runner), that's OK — the important |
| 142 | + // tests are the env-var ones above which we control. |
| 143 | + }); |
| 144 | +}); |
0 commit comments