|
| 1 | +import path from "node:path"; |
| 2 | +import { mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { describe, expect, test, afterEach } from "bun:test"; |
| 5 | +import { detectOpencodeSupervisorCreds } from "../lib/opencode-auth.js"; |
| 6 | +import { loadSupervisorLlmConfig } from "../lib/supervisor-config.js"; |
| 7 | + |
| 8 | +const ENV_KEYS = [ |
| 9 | + "RALPH_OPENCODE_AUTH_PATH", |
| 10 | + "RALPH_SUPERVISOR_API_KEY", |
| 11 | + "RALPH_SUPERVISOR_MODEL", |
| 12 | + "RALPH_SUPERVISOR_BASE_URL", |
| 13 | +] as const; |
| 14 | + |
| 15 | +async function writeAuth(contents: unknown): Promise<{ dir: string; authPath: string }> { |
| 16 | + const dir = await mkdtemp(path.join(tmpdir(), "ralph-auth-")); |
| 17 | + const authPath = path.join(dir, "auth.json"); |
| 18 | + await writeFile(authPath, JSON.stringify(contents), "utf8"); |
| 19 | + return { dir, authPath }; |
| 20 | +} |
| 21 | + |
| 22 | +describe("detectOpencodeSupervisorCreds", () => { |
| 23 | + const saved = Object.fromEntries(ENV_KEYS.map((k) => [k, process.env[k]])); |
| 24 | + afterEach(() => { |
| 25 | + for (const k of ENV_KEYS) { |
| 26 | + if (saved[k] === undefined) delete process.env[k]; |
| 27 | + else process.env[k] = saved[k]; |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + test("uses a keyed provider (opencode-go) and maps the Zen endpoint", async () => { |
| 32 | + const { dir, authPath } = await writeAuth({ |
| 33 | + anthropic: { type: "oauth", access: "x" }, |
| 34 | + "opencode-go": { type: "api", key: "zen-key-123" }, |
| 35 | + }); |
| 36 | + try { |
| 37 | + process.env.RALPH_OPENCODE_AUTH_PATH = authPath; |
| 38 | + const creds = await detectOpencodeSupervisorCreds(); |
| 39 | + expect(creds?.apiKey).toBe("zen-key-123"); |
| 40 | + expect(creds?.baseUrl).toBe("https://opencode.ai/zen/v1"); |
| 41 | + expect(creds?.source).toBe("opencode-auth:opencode-go"); |
| 42 | + } finally { |
| 43 | + await rm(dir, { recursive: true, force: true }); |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + test("skips OAuth-only providers", async () => { |
| 48 | + const { dir, authPath } = await writeAuth({ |
| 49 | + openai: { type: "oauth", access: "x" }, |
| 50 | + anthropic: { type: "oauth", access: "y" }, |
| 51 | + }); |
| 52 | + try { |
| 53 | + process.env.RALPH_OPENCODE_AUTH_PATH = authPath; |
| 54 | + expect(await detectOpencodeSupervisorCreds()).toBeNull(); |
| 55 | + } finally { |
| 56 | + await rm(dir, { recursive: true, force: true }); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + test("returns null when no auth file exists", async () => { |
| 61 | + process.env.RALPH_OPENCODE_AUTH_PATH = path.join(tmpdir(), "does-not-exist-xyz", "auth.json"); |
| 62 | + expect(await detectOpencodeSupervisorCreds()).toBeNull(); |
| 63 | + }); |
| 64 | + |
| 65 | + test("loadSupervisorLlmConfig falls back to OpenCode auth when no key is set", async () => { |
| 66 | + const { dir, authPath } = await writeAuth({ |
| 67 | + google: { type: "api", key: "g-key" }, |
| 68 | + }); |
| 69 | + const worktree = await mkdtemp(path.join(tmpdir(), "ralph-wt-")); |
| 70 | + try { |
| 71 | + delete process.env.RALPH_SUPERVISOR_API_KEY; |
| 72 | + delete process.env.RALPH_SUPERVISOR_MODEL; |
| 73 | + delete process.env.RALPH_SUPERVISOR_BASE_URL; |
| 74 | + process.env.RALPH_OPENCODE_AUTH_PATH = authPath; |
| 75 | + |
| 76 | + const cfg = await loadSupervisorLlmConfig(worktree); |
| 77 | + expect(cfg.apiKey).toBe("g-key"); |
| 78 | + expect(cfg.source).toBe("opencode-auth:google"); |
| 79 | + expect(cfg.model).toBe("gemini-2.5-flash"); |
| 80 | + } finally { |
| 81 | + await rm(dir, { recursive: true, force: true }); |
| 82 | + await rm(worktree, { recursive: true, force: true }); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + test("env key wins over auto-detect", async () => { |
| 87 | + const { dir, authPath } = await writeAuth({ google: { type: "api", key: "g-key" } }); |
| 88 | + const worktree = await mkdtemp(path.join(tmpdir(), "ralph-wt-")); |
| 89 | + try { |
| 90 | + process.env.RALPH_OPENCODE_AUTH_PATH = authPath; |
| 91 | + process.env.RALPH_SUPERVISOR_API_KEY = "my-explicit-key"; |
| 92 | + const cfg = await loadSupervisorLlmConfig(worktree); |
| 93 | + expect(cfg.apiKey).toBe("my-explicit-key"); |
| 94 | + expect(cfg.source).toBe("env"); |
| 95 | + } finally { |
| 96 | + await rm(dir, { recursive: true, force: true }); |
| 97 | + await rm(worktree, { recursive: true, force: true }); |
| 98 | + } |
| 99 | + }); |
| 100 | +}); |
0 commit comments