Spaces:
Sleeping
Sleeping
File size: 7,900 Bytes
06671aa | 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | import os
import shutil
import sys
import tempfile
import zipfile
from pathlib import Path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from docx_comments import apply_docx_comments
from models import ReviewFinding
def _finding(
*,
finding_id: str = "test_finding_1",
paragraph_id: str = "P_0008",
target_text: str = "",
anchor_mode: str = "paragraph",
) -> ReviewFinding:
return ReviewFinding(
id=finding_id,
risk_level="high",
clause_type="Indemnification",
issue="Test issue",
comment="This is a test comment from the Mike system.",
review_reasoning={"clause_section": "test", "violated_standard": "test", "zamil_exposure": "Manual test run"},
target_text=target_text,
anchor_mode=anchor_mode,
start_paragraph_id=paragraph_id,
end_paragraph_id=paragraph_id,
start_anchor=target_text[:220],
end_anchor=target_text[-220:] if target_text else "",
before_anchor="",
after_anchor="",
confidence=1.0,
)
def _write_minimal_table_docx(path: Path) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
with zipfile.ZipFile(path, "w") as zf:
zf.writestr(
"[Content_Types].xml",
"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
<Default Extension="xml" ContentType="application/xml"/>
<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
</Types>""",
)
zf.writestr(
"_rels/.rels",
"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
</Relationships>""",
)
zf.writestr(
"word/document.xml",
"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p><w:r><w:t>Body paragraph.</w:t></w:r></w:p><w:tbl><w:tr><w:tc><w:p><w:r><w:t>Table cell clause.</w:t></w:r></w:p></w:tc></w:tr></w:tbl><w:sectPr/></w:body></w:document>""",
)
def test_apply_paragraph_comment_without_target_text():
if shutil.which("dotnet") is None:
import pytest
pytest.skip("dotnet is required for the DOCX commenter integration test")
root = Path(__file__).resolve().parents[1]
sample_docx = root / "data" / "uploads" / "run_20260508_120049_e4b2552b55" / "test_contract.docx"
output_docx = root / "data" / "artifacts" / "test_comments_output.docx"
work_dir = root / "data" / "artifacts" / "test_comments_work"
findings = [_finding(target_text="")]
assert sample_docx.exists(), f"Missing test DOCX fixture: {sample_docx}"
import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
work_dir = Path(tmpdir)
results = apply_docx_comments(
input_docx=sample_docx,
output_docx=output_docx,
findings=findings,
work_dir=work_dir,
)
assert results
assert results[0].id == "test_finding_1"
assert results[0].status == "applied"
assert results[0].failure_code == "editable"
assert results[0].resolve_strategy == "paragraph_anchor"
def test_apply_paragraph_comment_ignores_non_matching_quote_hint():
if shutil.which("dotnet") is None:
import pytest
pytest.skip("dotnet is required for the DOCX commenter integration test")
root = Path(__file__).resolve().parents[1]
sample_docx = root / "data" / "uploads" / "run_20260508_120049_e4b2552b55" / "test_contract.docx"
output_docx = root / "data" / "artifacts" / "test_comments_output.docx"
with tempfile.TemporaryDirectory() as tmpdir:
results = apply_docx_comments(
input_docx=sample_docx,
output_docx=output_docx,
findings=[_finding(target_text="this exact quote does not exist")],
work_dir=Path(tmpdir),
)
assert results[0].status == "applied"
def test_apply_exact_comment_still_uses_target_text():
if shutil.which("dotnet") is None:
import pytest
pytest.skip("dotnet is required for the DOCX commenter integration test")
root = Path(__file__).resolve().parents[1]
sample_docx = root / "data" / "uploads" / "run_20260508_120049_e4b2552b55" / "test_contract.docx"
output_docx = root / "data" / "artifacts" / "test_comments_output.docx"
with tempfile.TemporaryDirectory() as tmpdir:
results = apply_docx_comments(
input_docx=sample_docx,
output_docx=output_docx,
findings=[_finding(paragraph_id="P_0007", target_text="Article 3: Indemnification", anchor_mode="exact")],
work_dir=Path(tmpdir),
)
assert results[0].status == "applied"
assert results[0].resolve_strategy == "exact"
def test_apply_paragraph_comment_invalid_block_id():
if shutil.which("dotnet") is None:
import pytest
pytest.skip("dotnet is required for the DOCX commenter integration test")
root = Path(__file__).resolve().parents[1]
sample_docx = root / "data" / "uploads" / "run_20260508_120049_e4b2552b55" / "test_contract.docx"
output_docx = root / "data" / "artifacts" / "test_comments_output.docx"
with tempfile.TemporaryDirectory() as tmpdir:
results = apply_docx_comments(
input_docx=sample_docx,
output_docx=output_docx,
findings=[_finding(paragraph_id="P_9999")],
work_dir=Path(tmpdir),
)
assert results[0].status == "not_applied"
assert results[0].failure_code == "paragraph_not_found"
def test_apply_paragraph_comment_hash_mismatch_reports_expected_and_actual():
if shutil.which("dotnet") is None:
import pytest
pytest.skip("dotnet is required for the DOCX commenter integration test")
root = Path(__file__).resolve().parents[1]
sample_docx = root / "data" / "uploads" / "run_20260508_120049_e4b2552b55" / "test_contract.docx"
output_docx = root / "data" / "artifacts" / "test_comments_output.docx"
finding = _finding().model_copy(update={"text_hash": "definitely_wrong"})
with tempfile.TemporaryDirectory() as tmpdir:
results = apply_docx_comments(
input_docx=sample_docx,
output_docx=output_docx,
findings=[finding],
work_dir=Path(tmpdir),
)
assert results[0].status == "not_applied"
assert results[0].failure_code == "anchor_stale"
assert results[0].expected_text_hash == "definitely_wrong"
assert results[0].actual_text_hash
assert results[0].actual_text_hash != results[0].expected_text_hash
def test_apply_paragraph_comment_inside_table_cell():
if shutil.which("dotnet") is None:
import pytest
pytest.skip("dotnet is required for the DOCX commenter integration test")
with tempfile.TemporaryDirectory() as tmpdir:
tmp = Path(tmpdir)
input_docx = tmp / "table.docx"
output_docx = tmp / "table_commented.docx"
_write_minimal_table_docx(input_docx)
results = apply_docx_comments(
input_docx=input_docx,
output_docx=output_docx,
findings=[_finding(paragraph_id="P_0002")],
work_dir=tmp / "work",
)
assert results[0].status == "applied"
assert results[0].paragraph_id == "P_0002"
if __name__ == "__main__":
test_apply_paragraph_comment_without_target_text()
|