"""서버 스모크 테스트 — 기동, OpenAI 호환 응답 형식, 프로필 선택, 오류 코드 (가짜 LLM).""" import pytest from fastapi.testclient import TestClient from router.router import app from services.generation.llm_client import LLMError from tests.test_generation import FakeClient, make_generator client = TestClient(app) @pytest.fixture def fake_generator(tmp_path): original = app.state.generator gen, clients = make_generator(tmp_path, reply="다른 공급사 견적이 4.1% 낮게 와 있습니다. 67,000원으로 올려 보겠습니다.") app.state.generator = gen yield gen, clients app.state.generator = original def test_health(): res = client.get("/health") assert res.status_code == 200 assert res.json()["status"] == "OK" def test_tests_do_not_write_evidence_folder(): """테스트는 임시 설정으로 기동한다 — 측정 증빙 폴더(../logs/server)에 쓰지 않는다.""" from config.config_loader import CONFIG_PATH assert "grounding_agent_test_" in str(CONFIG_PATH) assert "grounding_agent_test_" in client.get("/v1/config").json()["instrument"]["generation_log"] def test_models_and_config(): ids = [m["id"] for m in client.get("/v1/models").json()["data"]] assert {"baseline", "developed"} <= set(ids) body = client.get("/v1/config").json() assert body["profiles"]["developed"]["grounding"] is True assert "api_key" not in str(body["profiles"]).replace("api_key_env", "") def test_chat_completions_openai_format(fake_generator, turn5_messages): """Grounding_Eval generate 가 읽는 필드: choices[0].message.content, model, usage, finish_reason.""" res = client.post("/v1/chat/completions", json={ "model": "developed", "messages": turn5_messages, "temperature": 0, "top_p": 1.0, "seed": 0, "max_tokens": 512, }) assert res.status_code == 200 body = res.json() assert body["object"] == "chat.completion" assert body["model"] == "developed" assert body["choices"][0]["message"] == {"role": "assistant", "content": "다른 공급사 견적이 4.1% 낮게 와 있습니다. 67,000원으로 올려 보겠습니다."} assert body["choices"][0]["finish_reason"] == "stop" assert body["usage"]["total_tokens"] == 15 assert body["grounding"]["rule"] == "card:경쟁사 비교 전략" assert body["system_fingerprint"] == "fp_test" assert "X-Response-Time-Ms" in res.headers def test_baseline_profile(fake_generator, turn5_messages): _, clients = fake_generator res = client.post("/v1/chat/completions", json={"model": "baseline", "messages": turn5_messages}) assert res.status_code == 200 assert res.json()["grounding"]["grounding"] is False assert clients["baseline"].calls[0]["messages"][1:] == turn5_messages[1:] def test_unknown_profile_404(fake_generator, turn5_messages): res = client.post("/v1/chat/completions", json={"model": "gpt-4o", "messages": turn5_messages}) assert res.status_code == 404 def test_llm_failure_502(fake_generator, turn5_messages): gen, clients = fake_generator class Broken(FakeClient): def chat(self, *args, **kwargs): raise LLMError("연결 실패") gen.clients["developed"] = Broken("") res = client.post("/v1/chat/completions", json={"model": "developed", "messages": turn5_messages}) assert res.status_code == 502