def test_get_settings_defaults(client): resp = client.get("/api/settings") assert resp.status_code == 200 data = resp.json() assert data["active_provider"] == "lmstudio" assert "system_prompt" in data assert "providers" in data # API keys should be masked or empty for prov in ("anthropic", "openai"): key = data["providers"][prov].get("api_key", "") assert "****" not in key or len(key) <= 8 # masked or empty def test_patch_system_prompt(client): new_prompt = "Custom system prompt for testing." resp = client.patch("/api/settings", json={"system_prompt": new_prompt}) assert resp.status_code == 200 resp2 = client.get("/api/settings") assert resp2.json()["system_prompt"] == new_prompt def test_patch_active_provider(client): resp = client.patch("/api/settings", json={"active_provider": "ollama"}) assert resp.status_code == 200 assert resp.json()["active_provider"] == "ollama" def test_patch_invalid_provider(client): resp = client.patch("/api/settings", json={"active_provider": "unknown"}) assert resp.status_code == 400 def test_patch_provider_config(client): resp = client.patch("/api/settings", json={ "providers": { "ollama": {"model": "mistral", "base_url": "http://host.docker.internal:11434"} } }) assert resp.status_code == 200 assert resp.json()["providers"]["ollama"]["model"] == "mistral" def test_masked_api_key_not_overwritten(client): """Patching with a masked key should not overwrite the real stored key.""" # First set a real key client.patch("/api/settings", json={"providers": {"anthropic": {"api_key": "sk-ant-realkey"}}}) # Then patch with masked key (simulating frontend re-submitting) client.patch("/api/settings", json={"providers": {"anthropic": {"api_key": "****key"}}}) # The stored key should still be the real one import services.storage as st settings = st.load_settings() assert settings["providers"]["anthropic"]["api_key"] == "sk-ant-realkey" def test_get_default_prompt(client): resp = client.get("/api/settings/default-prompt") assert resp.status_code == 200 assert "system_prompt" in resp.json() assert len(resp.json()["system_prompt"]) > 0