Explorar el Código

refactor: remove dead code, add edge case tests for hasUsableLlm

Mochocyang hace 2 meses
padre
commit
f915cee267
Se han modificado 2 ficheros con 20 adiciones y 25 borrados
  1. 18 0
      src/lib/has-usable-llm.test.ts
  2. 2 25
      src/lib/has-usable-llm.ts

+ 18 - 0
src/lib/has-usable-llm.test.ts

@@ -19,6 +19,10 @@ describe("hasUsableLlm", () => {
     expect(hasUsableLlm(baseCfg, providers)).toBe(true)
   })
 
+  it("accepts hosted provider with default providerConfigs parameter (backward compatibility)", () => {
+    expect(hasUsableLlm(baseCfg)).toBe(true)
+  })
+
   it("rejects hosted provider without apiKey", () => {
     const providers: ProviderConfigs = {}
     expect(hasUsableLlm({ ...baseCfg, apiKey: "" }, providers)).toBe(false)
@@ -59,6 +63,14 @@ describe("hasUsableLlm", () => {
     expect(hasUsableLlm(cfg, providers)).toBe(false)
   })
 
+  it("accepts codex-cli when the preset is enabled", () => {
+    const providers: ProviderConfigs = {
+      "codex-cli": { enabled: true },
+    }
+    const cfg: LlmConfig = { ...baseCfg, provider: "codex-cli", apiKey: "", model: "gpt-5" }
+    expect(hasUsableLlm(cfg, providers)).toBe(true)
+  })
+
   it("accepts ollama without apiKey when enabled", () => {
     const providers: ProviderConfigs = {
       "ollama-local": { enabled: true, model: "qwen2.5" },
@@ -73,6 +85,12 @@ describe("hasUsableLlm", () => {
     expect(hasUsableLlm(cfg, providers)).toBe(true)
   })
 
+  it("rejects custom with apiKey but no model", () => {
+    const providers: ProviderConfigs = {}
+    const cfg: LlmConfig = { ...baseCfg, provider: "custom", apiKey: "sk-test", model: "", customEndpoint: "https://example.com/v1" }
+    expect(hasUsableLlm(cfg, providers)).toBe(false)
+  })
+
   it("rejects custom without apiKey when no custom-xxx preset is enabled", () => {
     const providers: ProviderConfigs = {}
     const cfg: LlmConfig = { ...baseCfg, provider: "custom", apiKey: "", model: "qwen-plus", customEndpoint: "http://localhost:1234/v1" }

+ 2 - 25
src/lib/has-usable-llm.ts

@@ -2,29 +2,6 @@ import type { LlmConfig, ProviderConfigs } from "@/stores/wiki-store"
 
 export type LlmProvider = LlmConfig["provider"]
 
-/**
- * Providers that don't need an API key to operate:
- *   - `ollama` runs on a local HTTP endpoint with no auth
- *   - `custom` is an OpenAI-compatible local-or-LAN endpoint that
- *     may or may not require auth (LM Studio, llama.cpp, vLLM
- *     defaults are all unauthenticated; users who deploy behind a
- *     proxy can still set apiKey to add Bearer auth)
- *   - `claude-code` spawns the Claude Code CLI subprocess, which
- *     authenticates via the user's existing ~/.claude OAuth — no
- *     API key is needed (or accepted) at this layer.
- *   - `codex-cli` spawns the Codex CLI subprocess, which authenticates
- *     via the user's existing Codex/ChatGPT login.
- *
- * Hosted providers (openai, anthropic, google, minimax) require a
- * key from the user.
- */
-export const PROVIDERS_WITHOUT_KEY: ReadonlySet<LlmProvider> = new Set<LlmProvider>([
-  "ollama",
-  "custom",
-  "claude-code",
-  "codex-cli",
-])
-
 /**
  * Maps LlmConfig.provider values to their corresponding LLM_PRESETS id,
  * for providers that are gated by a single well-known preset toggle.
@@ -70,8 +47,8 @@ export function hasUsableLlm(
   const hasModel = cfg.model.trim().length > 0
 
   if (cfg.provider === "claude-code" || cfg.provider === "codex-cli") {
-    const presetId = PRESET_ID_BY_PROVIDER[cfg.provider]
-    return presetId !== undefined && isPresetEnabled(providerConfigs, presetId)
+    const presetId = PRESET_ID_BY_PROVIDER[cfg.provider]!
+    return isPresetEnabled(providerConfigs, presetId)
   }
 
   if (cfg.provider === "ollama") {