Download src/lib/__tests__/get-claude.test.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 6.67 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/__tests__/get-claude.test.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/lib/__tests__/get-claude.test.ts
-
curl -L -o get-claude.test.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/__tests__/get-claude.test.ts
6.67 kB
| /** | |
| * 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" }); | |
| }); | |
| }); | |