File size: 2,492 Bytes
a964978
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path

from app.pipeline.chunker import chunk_blocks
from app.pipeline.parsers.pdf import parse_pdf
from app.pipeline.refmap import (
    build_reference_map,
    extract_references,
    is_false_positive_reference,
)


def test_false_positive_rejection():
    """Verify that false positive strings are properly rejected."""
    false_positives = [
        "Software release v1.2.3 installed on machine",
        "Connected to server at 192.168.1.1 port 8000",
        "Effective starting on 2026.10.24 without notice",
        "As a general rule of thumb, always be punctual",
        "The item cost exactly $63.74 yesterday",
        "The mathematical ratio was approximately 3.14159",
    ]

    rejected_count = sum(1 for fp in false_positives if is_false_positive_reference(fp))
    # Gate requires rejecting >= 3 false-positive strings
    assert rejected_count >= 3, f"Expected >= 3 false positives rejected, got {rejected_count}"


def test_reference_extraction_valid():
    """Verify extraction of real reference patterns."""
    text = (
        "According to rule 63.7.4, all leave is settled in accordance with clause 4.2. "
        "Employees must submit Form HR-702 as defined in standard ISO-27001."
    )
    refs = extract_references(text)
    ref_ids = [r.ref_id for r in refs]

    assert "rule:63.7.4" in ref_ids
    assert "clause:4.2" in ref_ids
    assert "form:hr-702" in ref_ids
    assert "standard:iso-27001" in ref_ids


def test_handbook_fixture_reference_mapping():
    """
    Gate check: On the fixture, >=4 real cross-references are mapped.
    """
    fixture_path = Path(__file__).resolve().parent.parent.parent / "eval" / "fixtures" / "handbook.pdf"
    assert fixture_path.exists()

    blocks = parse_pdf(str(fixture_path), source_name="handbook.pdf")
    chunks = chunk_blocks(blocks, doc_id="handbook", target_tokens=300, overlap_ratio=0.15)
    ref_map = build_reference_map(chunks)

    # Check mapped references across definitions and mentions
    all_mapped_keys = set(ref_map.definitions.keys()).union(set(ref_map.mentions.keys()))
    assert len(all_mapped_keys) >= 4, f"Expected >= 4 mapped refs, got {len(all_mapped_keys)}: {all_mapped_keys}"

    # Specifically check critical rule:63.7.4
    assert "rule:63.7.4" in ref_map.definitions or "rule:63.7.4" in ref_map.mentions
    # Check clause:4.2 or standard:iso-27001 or form:hr-702
    assert any(k in all_mapped_keys for k in ["clause:4.2", "form:hr-702", "rule:2.1.4"])