File size: 4,945 Bytes
39cfcd1 c212805 39cfcd1 24a79a8 39cfcd1 c212805 39cfcd1 24a79a8 39cfcd1 24a79a8 39cfcd1 24a79a8 39cfcd1 24a79a8 c212805 24a79a8 c212805 24a79a8 c212805 24a79a8 39cfcd1 24a79a8 39cfcd1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | """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"
|