any2human / tests /test_api.py
idnameraj's picture
Re-enable primary T5 paraphrase for stronger out1→out2 divergence.
24a79a8
Raw
History Blame Contribute Delete
4.95 kB
"""Backend-only rewrite API contract."""
from __future__ import annotations
import os
import importlib
os.environ.setdefault("LANGUAGE_TOOL_ENABLED", "false")
os.environ.setdefault("LANGUAGE_TOOL_URL", "")
os.environ.setdefault("GRAMMAR_FIX_OUTPUT", "false")
os.environ.setdefault("ENGINE_USE_MINILM_SAFETY", "false")
os.environ.setdefault("ENGINE_LEXICAL_REFINEMENT", "false")
os.environ.setdefault("ENGINE_FORCE_REWRITE", "false")
from fastapi.testclient import TestClient
from app.main import app
main_module = importlib.import_module("app.main")
client = TestClient(app)
def test_rewrite_accepts_text_only(monkeypatch):
monkeypatch.setattr(main_module, "ENGINE_LEXICAL_REFINEMENT", False)
monkeypatch.setattr(main_module, "ENGINE_FORCE_REWRITE", False)
monkeypatch.setattr(main_module, "ENGINE_PARAPHRASE", False)
response = client.post(
"/v1/rewrite",
json={"text": "Ram went to school yesterday happily."},
)
assert response.status_code == 200
body = response.json()
assert body["rewrite"] == "Yesterday, Ram happily went to school."
assert body["sentences"][0]["status"] == "rewritten"
assert body["sentences"][0]["template_id"]
assert isinstance(body["sentences"][0]["confidence"], float)
assert body["mapping"]
assert body["stats"]["batches"] == 1
assert "tone" not in body["meta"]
assert "strength" not in body["meta"]
assert "ml_polish" not in body["meta"]
def test_legacy_ui_controls_are_ignored(monkeypatch):
monkeypatch.setattr(main_module, "ENGINE_LEXICAL_REFINEMENT", False)
monkeypatch.setattr(main_module, "ENGINE_FORCE_REWRITE", False)
monkeypatch.setattr(main_module, "ENGINE_PARAPHRASE", False)
response = client.post(
"/v1/rewrite",
json={
"text": "Ram went to school yesterday happily.",
"tone": "Formal",
"strength": 2,
"ml_polish": True,
},
)
assert response.status_code == 200
# ml_polish still enables lexical when the env stage is off; force stays off.
assert "Yesterday" in response.json()["rewrite"]
assert "Ram" in response.json()["rewrite"]
def test_ml_polish_checkbox_boosts_lexical_changes(monkeypatch):
captured: dict[str, object] = {}
original = main_module.rewrite_document
def capture(text: str, **kwargs):
captured["enabled"] = kwargs["use_lexical_refinement"]
captured["max_changes"] = kwargs.get("lexical_max_changes")
captured["polish"] = kwargs.get("lexical_polish")
return original(text, use_lexical_refinement=False, force_rewrite=False)
monkeypatch.setattr(main_module, "ENGINE_LEXICAL_REFINEMENT", True)
monkeypatch.setattr(main_module, "rewrite_document", capture)
response = client.post(
"/v1/rewrite",
json={
"text": "Ram went to school yesterday happily.",
"ml_polish": True,
},
)
assert response.status_code == 200
assert captured["enabled"] is True
assert captured["max_changes"] is None
assert captured["polish"] is True
assert response.json()["meta"]["ml_polish_requested"] is True
def test_ml_polish_changes_output_more_aggressively():
text = (
"The manager subsequently assisted several diligent students during "
"the unusually difficult afternoon workshop."
)
plain = client.post("/v1/rewrite", json={"text": text, "ml_polish": False})
polished = client.post("/v1/rewrite", json={"text": text, "ml_polish": True})
assert plain.status_code == 200
assert polished.status_code == 200
plain_body = plain.json()
polished_body = polished.json()
assert plain_body["rewrite"] != polished_body["rewrite"]
plain_changes = [
change
for sentence in plain_body["sentences"]
for change in sentence["lexical_changes"]
]
polished_changes = [
change
for sentence in polished_body["sentences"]
for change in sentence["lexical_changes"]
]
assert len(polished_changes) > len(plain_changes)
def test_health_reports_rewrite_service():
response = client.get("/health")
assert response.status_code == 200
body = response.json()
assert body["service"] == "rewrite-api"
assert body["rewrite_engine"]["mode"] == (
"structural+paraphrase-primary+phrase+ensure"
)
assert body["rewrite_engine"]["paraphrase"]["primary"] is True
assert body["ui"] == "react"
assert "ml_polish" not in body
def test_frontend_root_is_served():
response = client.get("/")
assert response.status_code == 200
assert "text/html" in response.headers["content-type"]
logo = client.get("/zuzu-logo.png")
assert logo.status_code == 200
assert logo.headers["content-type"] == "image/png"