sahilfarib/Legal-Document-Intelligence
0
1"""2tests/test_validator.py — Tests for post-generation citation validator3"""4import sys5from pathlib import Path6import pytest7 8sys.path.insert(0, str(Path(__file__).parent.parent))9 10from models import EvidenceChunk11from generation.validator import CitationValidator, ValidationReport12 13 14@pytest.fixture15def validator():16 return CitationValidator()17 18 19@pytest.fixture20def evidence():21 return [22 EvidenceChunk(chunk_id="aabb001100000000", text="Contract text.", source_file="f.pdf", score=0.9),23 EvidenceChunk(chunk_id="ccdd002200000000", text="Payment terms.", source_file="f.pdf", score=0.8),24 ]25 26 27class TestValidator:28 def test_fully_cited_draft(self, validator, evidence):29 draft = (30 "## 1. Document Overview\n"31 "The contract was signed in January. [CHUNK-aabb0011]\n"32 "## 2. Parties and Roles\n"33 "Payment is $25,000 monthly. [CHUNK-ccdd0022]\n"34 "## 3. Key Facts and Dates\n"35 "The term is 12 months. [CHUNK-aabb0011]\n"36 "## 4. Obligations and Terms\n"37 "Late fees apply at 1.5%. [CHUNK-ccdd0022]\n"38 "## 5. Red Flags and Issues\n"39 "Limitation clause may be unenforceable. [CHUNK-aabb0011]\n"40 "## 6. Analyst Notes\n"41 "Recommend further review of liability cap. [CHUNK-ccdd0022]\n"42 )43 report = validator.validate(draft, evidence)44 assert report.uncited_ratio < 0.1045 assert report.is_clean()46 assert report.severity() == "green"47 48 def test_uncited_sentences_detected(self, validator, evidence):49 draft = (50 "## 1. Document Overview\n"51 "The contract was signed in January. [CHUNK-aabb0011]\n"52 "This sentence has no citation and is long enough to count.\n"53 "Another uncited claim that should be flagged by the validator.\n"54 )55 report = validator.validate(draft, evidence)56 assert len(report.uncited_sentences) == 257 assert report.uncited_ratio > 058 59 def test_invalid_citation_detected(self, validator, evidence):60 draft = "The contract mentions something. [CHUNK-deadbeef]\n"61 report = validator.validate(draft, evidence)62 assert len(report.invalid_citations) == 163 64 def test_not_evidenced_marker(self, validator, evidence):65 draft = "The jurisdiction is not stated. [NOT EVIDENCED]\n"66 report = validator.validate(draft, evidence)67 assert report.not_evidenced_count == 168 assert len(report.uncited_sentences) == 069 70 def test_section_completeness(self, validator, evidence):71 draft = "## 1. Document Overview\nSome text. [CHUNK-aabb0011]\n"72 report = validator.validate(draft, evidence)73 assert len(report.missing_sections) > 074 assert report.section_completeness < 1.075 76 def test_empty_draft(self, validator, evidence):77 report = validator.validate("", evidence)78 assert report.total_sentences == 079 80 81class TestValidationReport:82 def test_severity_green(self):83 r = ValidationReport(uncited_ratio=0.05)84 assert r.severity() == "green"85 86 def test_severity_yellow(self):87 r = ValidationReport(uncited_ratio=0.15)88 assert r.severity() == "yellow"89 90 def test_severity_red(self):91 r = ValidationReport(uncited_ratio=0.30)92 assert r.severity() == "red"93 94 def test_to_dict(self):95 r = ValidationReport(total_sentences=10, cited_sentences=8)96 d = r.to_dict()97 assert "total_sentences" in d98 assert "severity" in d99 