import assert from "node:assert/strict"; import { describe, it } from "node:test"; import { resolveApiKey } from "../src/auth.ts"; describe("resolveApiKey", () => { it("prefers provided key", () => { assert.equal(resolveApiKey("provided"), "provided"); }); it("reads CLINE_API_KEY from env", () => { assert.equal( resolveApiKey(undefined, { env: { CLINE_API_KEY: "env-key" } }), "env-key", ); }); it("reads clinepass OAuth from pi auth.json", () => { const key = resolveApiKey(undefined, { authPaths: ["/virtual/auth.json"], fileExists: (p) => p === "/virtual/auth.json", readFile: () => JSON.stringify({ clinepass: { type: "oauth", access: "workos:token", refresh: "refresh", }, }), }); assert.equal(key, undefined); }); it("reads static API key from clinefree auth field", () => { const key = resolveApiKey(undefined, { authPaths: ["/virtual/auth.json"], fileExists: (p) => p === "/virtual/auth.json", readFile: () => JSON.stringify({ clinefree: "static-api-key-from-pi-login", }), }); assert.equal(key, "static-api-key-from-pi-login"); }); it("reads static API key from Cline CLI providers.json", () => { const key = resolveApiKey(undefined, { authPaths: ["/virtual/providers.json"], fileExists: (p) => p === "/virtual/providers.json", readFile: () => JSON.stringify({ providers: { "cline-pass": { settings: { apiKey: "cli-static-key", }, }, }, }), }); assert.equal(key, "cli-static-key"); }); });