File size: 1,747 Bytes
7be0127 | 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 | """Gemini structured-output validation (no network)."""
from svarasetu.compose.polish import _validate_structured
from svarasetu.shield.outbound import unsupported_claims
def test_validate_structured_happy_path():
parsed = {
"answer": "The heart has four chambers.",
"language": "en-IN",
"grounded": True,
"confidence": 0.8,
"refused": False,
"citations": [
{"chunk_id": "c1", "claim": "four chambers", "supported": True},
{"chunk_id": "drop-me", "claim": "ignored", "supported": True},
],
}
out = _validate_structured(parsed, "en", ["c1", "c2"])
assert out is not None
assert out["language"] == "en"
assert out["grounded"] is True
assert out["citations"] == [
{"chunk_id": "c1", "claim": "four chambers", "supported": True}
]
def test_validate_structured_refuse_on_insufficient():
parsed = {
"answer": "I don't have enough grounded information to answer that.",
"language": "hi",
"grounded": True,
"confidence": 0.1,
"refused": False,
"citations": [],
}
out = _validate_structured(parsed, "hi", [])
assert out["refused"] is True
assert out["grounded"] is False
def test_validate_structured_rejects_empty():
assert _validate_structured({"answer": " "}, "en", []) is None
assert _validate_structured({}, "en", []) is None
def test_unsupported_claims_flags_alien_sentence():
bad = unsupported_claims(
"The heart has four chambers. Alien spacecraft landed on Mars in 1845.",
[{"text": "The human heart has four muscular chambers that pump blood."}],
min_overlap=0.25,
)
assert any("Alien" in s for s in bad)
|