"""GET/PUT /settings/llm — 요약 모델 런타임 전환과 .env 영속화.""" from __future__ import annotations import pytest from fastapi.testclient import TestClient from config.settings import settings @pytest.fixture() def client(tmp_path, monkeypatch): import query.api as api monkeypatch.setattr(settings, "database_url", f"sqlite:///{tmp_path / 'index.db'}") monkeypatch.setattr(settings, "llm_model", "minimax/minimax-m2.7:free") env = tmp_path / ".env" env.write_text("LLM_BASE_URL=https://x\nLLM_MODEL=minimax/minimax-m2.7:free\n", encoding="utf-8") monkeypatch.setattr(api, "ENV_PATH", env) return TestClient(api.app), env def test_get_and_put_model(client): c, env = client assert c.get("/settings/llm").json()["model"] == "minimax/minimax-m2.7:free" r = c.put("/settings/llm", json={"model": "z-ai/glm-5.2"}) assert r.status_code == 200 and r.json() == {"model": "z-ai/glm-5.2"} assert settings.llm_model == "z-ai/glm-5.2" # 즉시 반영 text = env.read_text(encoding="utf-8") assert "LLM_MODEL=z-ai/glm-5.2" in text assert "LLM_BASE_URL=https://x" in text # 다른 줄은 보존 assert text.count("LLM_MODEL=") == 1 def test_put_model_rejects_bad_input(client): c, env = client for bad in ("", " ", "a b", "x\nLLM_API_KEY=evil", "한글모델"): assert c.put("/settings/llm", json={"model": bad}).status_code == 400 assert settings.llm_model == "minimax/minimax-m2.7:free" assert "evil" not in env.read_text(encoding="utf-8")