# -*- coding: utf-8 -*- """Сквозной smoke API: /api/health, /api/check → job → report (+docx) без внешних служб.""" import time from fastapi.testclient import TestClient import app TXT = "Дежурный сообщил что выезд состоялся вовремя и без задержек.\n" def _run(client, name, content, use_llm="false"): r = client.post("/api/check", files={"file": (name, content, "text/plain")}, data={"use_llm": use_llm}) assert r.status_code == 200 job = r.json()["job_id"] for _ in range(300): st = client.get(f"/api/job/{job}").json() if st.get("status") in ("done", "error"): break time.sleep(0.05) assert st.get("status") == "done", st return job, client.get(f"/api/report/{job}").json() def test_health_with_mocks(monkeypatch, lt_mock): monkeypatch.setattr(app, "LT_URL", lt_mock["url"]) client = TestClient(app.app) h = client.get("/api/health").json() assert h["lt"]["ok"] is True assert "exceptions" in h and "exceptions_files" in h def test_pipeline_txt_with_lt_mock(monkeypatch, lt_mock): monkeypatch.setattr(app, "LT_URL", lt_mock["url"]) client = TestClient(app.app) job, rep = _run(client, "doc.txt", TXT.encode("utf-8")) assert rep["paragraphs"] == 1 assert rep["findings"], "мок должен дать находку на «сообщил»" f = rep["findings"][0] assert f["fragment"] == "сообщил" assert f["source"] == "LanguageTool" assert rep["lt_seconds"] >= 0 assert rep["llm"] == {"enabled": False} assert rep["exceptions_dropped"] == 0 def test_pipeline_exceptions_applied(monkeypatch, lt_mock, tmp_path): monkeypatch.setattr(app, "LT_URL", lt_mock["url"]) import tempfile dictfile = tmp_path / "exceptions.txt" dictfile.write_text("сообщил\n", encoding="utf-8") monkeypatch.setattr(app, "EXCEPTIONS_FILES", [str(dictfile)]) client = TestClient(app.app) _, rep = _run(client, "doc.txt", TXT.encode("utf-8")) assert rep["findings"] == [] assert rep["exceptions_dropped"] == 1 def test_report_docx_endpoint(monkeypatch, lt_mock): """Docx-выгрузка не падает и даёт валидный docx (урок про control-символы — через валидатор).""" monkeypatch.setattr(app, "LT_URL", lt_mock["url"]) client = TestClient(app.app) job, rep = _run(client, "doc.txt", TXT.encode("utf-8")) r = client.get(f"/api/report/{job}/docx") assert r.status_code == 200 assert "wordprocessing" in r.headers["content-type"] from docx import Document import io doc = Document(io.BytesIO(r.content)) texts = [p.text for p in doc.paragraphs] assert any("Отчёт" in t for t in texts) def test_empty_and_bogus_files(monkeypatch, lt_mock): monkeypatch.setattr(app, "LT_URL", lt_mock["url"]) client = TestClient(app.app) r = client.post("/api/check", files={"file": ("e.txt", b"", "text/plain")}) assert r.status_code == 400 # пустой файл отбраковывается сразу # неподдержанный формат: job завершается статусом error (не HTTP 400) r = client.post("/api/check", files={"file": ("doc.rtf", b"{\\rtf1}", "text/plain")}) assert r.status_code == 200 job = r.json()["job_id"] st = client.get(f"/api/job/{job}").json() for _ in range(200): if st.get("status") != "running": break time.sleep(0.05) st = client.get(f"/api/job/{job}").json() assert st.get("status") == "error" assert "Формат" in (st.get("error") or "") or "не поддержан" in (st.get("error") or "")