"""Testy FastAPI vrstvy — health, admin auth, runtime settings API.""" from fastapi.testclient import TestClient def client_for(app_module): return TestClient(app_module.app) def test_health_no_auth(app_module): c = client_for(app_module) r = c.get("/health") assert r.status_code == 200 data = r.json() assert data["status"] == "ok" assert data["engine"]["state"] == "stopped" assert data["settings_revision"] == 0 assert "disk_free_gb" in data def test_admin_requires_auth(app_module): c = client_for(app_module) assert c.get("/admin/settings").status_code == 401 assert c.get("/admin/settings", headers={"Authorization": "Bearer spatny"}).status_code == 401 def test_admin_get_settings(app_module, auth): c = client_for(app_module) r = c.get("/admin/settings", headers=auth) assert r.status_code == 200 data = r.json() assert data["settings"]["model"] == "Qwen/Qwen3-Coder-Next-FP8" assert "model" in data["engine_fields"] assert "max_steps" not in data["engine_fields"] def test_admin_post_instant_setting(app_module, auth): c = client_for(app_module) r = c.post("/admin/settings", json={"max_steps": 44, "temperature": 0.5}, headers=auth) assert r.status_code == 200 data = r.json() assert data["applied"] == {"max_steps": 44, "temperature": 0.5} assert data["engine_reload_needed"] is False assert data["engine_reload_started"] is False assert data["revision"] == 1 # nastavení se opravdu projevilo assert app_module.SETTINGS.get().max_steps == 44 def test_admin_post_engine_setting_without_reload(app_module, auth): c = client_for(app_module) r = c.post("/admin/settings?reload=false", json={"model": "org/deferred"}, headers=auth) data = r.json() assert data["engine_reload_needed"] is True assert data["engine_reload_started"] is False assert app_module.ENGINE.status_dict()["state"] == "stopped" def test_admin_post_validation_errors(app_module, auth): c = client_for(app_module) r = c.post("/admin/settings", json={"agent_mode": "nesmysl"}, headers=auth) data = r.json() assert data["applied"] == {} assert "agent_mode" in data["errors"] def test_admin_presets(app_module, auth): c = client_for(app_module) r = c.get("/admin/presets", headers=auth) assert r.status_code == 200 presets = r.json()["presets"] assert "qwen3-coder-30b" in presets assert presets["qwen3-235b-fp8"]["model"] == "Qwen/Qwen3-235B-A22B-Instruct-2507-FP8" def test_v1_models(app_module, auth): c = client_for(app_module) assert c.get("/v1/models").status_code == 401 r = c.get("/v1/models", headers=auth) ids = {m["id"] for m in r.json()["data"]} assert ids == {"code-agent", "code-agent-llm"} def test_v1_passthrough_engine_down(app_module, auth): c = client_for(app_module) r = c.post("/v1/chat/completions", headers=auth, json={"model": "code-agent-llm", "messages": [{"role": "user", "content": "ahoj"}]}) assert r.status_code == 503 def test_agent_chat_needs_runner(app_module): out = list(app_module.agent_chat("test", [])) assert any("runner_url" in chunk for chunk in out) def test_history_normalization(app_module): # Gradio 6 content-bloky i prosté stringy text = app_module._history_text([{"type": "text", "text": "a"}, {"type": "text", "text": "b"}]) assert text == "a\nb" assert app_module._history_text("plain") == "plain" assert app_module._history_text(None) == ""