test(scripts): resolveClaudeAuth — subscription/api-key/missing modes
This commit is contained in:
@@ -6,6 +6,7 @@ import {
|
||||
findNextTask,
|
||||
findFirstUncheckedBullet,
|
||||
buildTaskSpec,
|
||||
resolveClaudeAuth,
|
||||
} from "./dispatch.mjs";
|
||||
|
||||
function makeWorkTree({ epics }) {
|
||||
@@ -137,3 +138,63 @@ describe("buildTaskSpec", () => {
|
||||
expect(spec).toContain("## Goal");
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveClaudeAuth", () => {
|
||||
it("returns subscription mode when ~/.claude exists on host", () => {
|
||||
const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "auth-sub-"));
|
||||
fs.mkdirSync(path.join(tmpHome, ".claude"));
|
||||
const result = resolveClaudeAuth({ env: {}, home: tmpHome });
|
||||
expect(result.mode).toBe("subscription");
|
||||
expect(result.hostPath).toBe(path.join(tmpHome, ".claude"));
|
||||
expect(result.sandboxPath).toBe("~/.claude");
|
||||
});
|
||||
|
||||
it("honours SANDCASTLE_CLAUDE_CREDS_DIR override", () => {
|
||||
const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), "auth-override-"));
|
||||
const overrideDir = path.join(tmpRoot, "custom-claude");
|
||||
fs.mkdirSync(overrideDir);
|
||||
const result = resolveClaudeAuth({
|
||||
env: { SANDCASTLE_CLAUDE_CREDS_DIR: overrideDir },
|
||||
home: "/nonexistent",
|
||||
});
|
||||
expect(result.mode).toBe("subscription");
|
||||
expect(result.hostPath).toBe(overrideDir);
|
||||
});
|
||||
|
||||
it("falls back to ANTHROPIC_API_KEY when ~/.claude does not exist", () => {
|
||||
const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "auth-key-"));
|
||||
// No .claude directory created
|
||||
const result = resolveClaudeAuth({
|
||||
env: { ANTHROPIC_API_KEY: "sk-test" },
|
||||
home: tmpHome,
|
||||
});
|
||||
expect(result.mode).toBe("api-key");
|
||||
expect(result.env).toEqual({ ANTHROPIC_API_KEY: "sk-test" });
|
||||
});
|
||||
|
||||
it("falls back to OPENAI_API_KEY when only that is set", () => {
|
||||
const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "auth-openai-"));
|
||||
const result = resolveClaudeAuth({
|
||||
env: { OPENAI_API_KEY: "sk-openai" },
|
||||
home: tmpHome,
|
||||
});
|
||||
expect(result.mode).toBe("api-key");
|
||||
expect(result.env).toEqual({ OPENAI_API_KEY: "sk-openai" });
|
||||
});
|
||||
|
||||
it("returns missing when neither subscription nor API key available", () => {
|
||||
const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "auth-missing-"));
|
||||
const result = resolveClaudeAuth({ env: {}, home: tmpHome });
|
||||
expect(result.mode).toBe("missing");
|
||||
});
|
||||
|
||||
it("prefers subscription over API key when both available", () => {
|
||||
const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "auth-both-"));
|
||||
fs.mkdirSync(path.join(tmpHome, ".claude"));
|
||||
const result = resolveClaudeAuth({
|
||||
env: { ANTHROPIC_API_KEY: "sk-test" },
|
||||
home: tmpHome,
|
||||
});
|
||||
expect(result.mode).toBe("subscription");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user