File size: 6,667 Bytes
9580dce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | /**
* Unit tests for the request-scoped LLM provider selection in src/lib/claude.ts.
*
* Covers the pure provider resolver and the per-request context plumbing that
* lets each visitor run the app's AI features on their own Hugging Face token:
*
* resolveLlmProvider(config)
* - explicit hfToken β HF provider (DEFAULT_HF_MODEL when no model given)
* - explicit hfToken + hfModel β HF provider with that model
* - env HF_TOKEN / HF_API_KEY β HF provider
* - explicit hfToken overrides env HF_TOKEN
* - no HF anywhere but ANTHROPIC_API_KEY set β anthropic provider
* - nothing configured β "none"
* - whitespace-only token is ignored
*
* isLlmAvailable(config)
* - true when a provider resolves, false for "none"
*
* llmConfigFromHeaders(headers)
* - reads x-hf-token / x-hf-model, trims, empty β undefined
*
* runWithLlmConfig / getLlmConfig
* - getLlmConfig() returns the active config inside runWithLlmConfig
* - getLlmConfig() is undefined outside any run
* - resolveLlmProvider() with no arg reads the active request context
*/
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { resolveLlmProvider, isLlmAvailable } from "../claude";
import {
llmConfigFromHeaders,
runWithLlmConfig,
getLlmConfig,
} from "../llm-context";
import { DEFAULT_HF_MODEL } from "../llm-hf";
// βββ Env isolation ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const ENV_KEYS = ["HF_TOKEN", "HF_API_KEY", "HF_MODEL", "HF_BASE_URL", "ANTHROPIC_API_KEY"] as const;
let savedEnv: Record<string, string | undefined>;
beforeEach(() => {
savedEnv = {};
for (const k of ENV_KEYS) {
savedEnv[k] = process.env[k];
delete process.env[k];
}
});
afterEach(() => {
for (const k of ENV_KEYS) {
if (savedEnv[k] === undefined) delete process.env[k];
else process.env[k] = savedEnv[k];
}
});
// βββ resolveLlmProvider βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe("resolveLlmProvider", () => {
it("uses an explicit hfToken with the default model", () => {
const r = resolveLlmProvider({ hfToken: "hf_abc" });
expect(r).toEqual({ provider: "hf", apiKey: "hf_abc", model: DEFAULT_HF_MODEL, baseUrl: undefined });
});
it("uses an explicit hfToken and hfModel", () => {
const r = resolveLlmProvider({ hfToken: "hf_abc", hfModel: "meta-llama/Llama-3.3-70B-Instruct" });
expect(r).toMatchObject({ provider: "hf", apiKey: "hf_abc", model: "meta-llama/Llama-3.3-70B-Instruct" });
});
it("falls back to env HF_TOKEN", () => {
process.env.HF_TOKEN = "hf_env";
expect(resolveLlmProvider({})).toMatchObject({ provider: "hf", apiKey: "hf_env" });
});
it("falls back to env HF_API_KEY", () => {
process.env.HF_API_KEY = "hf_apikey";
expect(resolveLlmProvider({})).toMatchObject({ provider: "hf", apiKey: "hf_apikey" });
});
it("prefers an explicit hfToken over env HF_TOKEN", () => {
process.env.HF_TOKEN = "hf_env";
expect(resolveLlmProvider({ hfToken: "hf_req" })).toMatchObject({ provider: "hf", apiKey: "hf_req" });
});
it("uses env HF_MODEL when no model is passed", () => {
process.env.HF_TOKEN = "hf_env";
process.env.HF_MODEL = "some/model";
expect(resolveLlmProvider({})).toMatchObject({ provider: "hf", model: "some/model" });
});
it("falls back to anthropic when only ANTHROPIC_API_KEY is set", () => {
process.env.ANTHROPIC_API_KEY = "sk-ant-xxx";
expect(resolveLlmProvider({})).toEqual({ provider: "anthropic" });
});
it("returns 'none' when nothing is configured", () => {
expect(resolveLlmProvider({})).toEqual({ provider: "none" });
});
it("ignores a whitespace-only hfToken", () => {
process.env.ANTHROPIC_API_KEY = "sk-ant-xxx";
expect(resolveLlmProvider({ hfToken: " " })).toEqual({ provider: "anthropic" });
});
});
// βββ isLlmAvailable ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe("isLlmAvailable", () => {
it("is true with an hf token", () => {
expect(isLlmAvailable({ hfToken: "hf_x" })).toBe(true);
});
it("is true with an anthropic key", () => {
process.env.ANTHROPIC_API_KEY = "sk-ant-xxx";
expect(isLlmAvailable({})).toBe(true);
});
it("is false with nothing configured", () => {
expect(isLlmAvailable({})).toBe(false);
});
});
// βββ llmConfigFromHeaders ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe("llmConfigFromHeaders", () => {
it("reads x-hf-token and x-hf-model", () => {
const h = new Headers({ "x-hf-token": "hf_h", "x-hf-model": "m/n" });
expect(llmConfigFromHeaders(h)).toEqual({ hfToken: "hf_h", hfModel: "m/n" });
});
it("trims values and maps empty to undefined", () => {
const h = new Headers({ "x-hf-token": " hf_h ", "x-hf-model": " " });
expect(llmConfigFromHeaders(h)).toEqual({ hfToken: "hf_h", hfModel: undefined });
});
it("returns undefined fields when headers are absent", () => {
expect(llmConfigFromHeaders(new Headers())).toEqual({ hfToken: undefined, hfModel: undefined });
});
});
// βββ runWithLlmConfig / getLlmConfig βββββββββββββββββββββββββββββββββββββββββββ
describe("runWithLlmConfig / getLlmConfig", () => {
it("exposes the active config inside the run", () => {
const seen = runWithLlmConfig({ hfToken: "hf_ctx", hfModel: "ctx/model" }, () => getLlmConfig());
expect(seen).toEqual({ hfToken: "hf_ctx", hfModel: "ctx/model" });
});
it("returns undefined outside any run", () => {
expect(getLlmConfig()).toBeUndefined();
});
it("makes resolveLlmProvider() read the active request context with no arg", () => {
const r = runWithLlmConfig({ hfToken: "hf_ctx" }, () => resolveLlmProvider());
expect(r).toMatchObject({ provider: "hf", apiKey: "hf_ctx" });
});
it("survives async awaits inside the run", async () => {
const seen = await runWithLlmConfig({ hfToken: "hf_async" }, async () => {
await Promise.resolve();
return getLlmConfig();
});
expect(seen).toEqual({ hfToken: "hf_async" });
});
});
|