|
| 1 | +import pytest |
| 2 | +from qwed_new.core.fact_verifier import FactVerifier |
| 3 | +from qwed_new.core.image_verifier import ImageVerifier, ImageAnalysisResult |
| 4 | + |
| 5 | +def test_fact_verifier_bounded_regex(): |
| 6 | + """ |
| 7 | + Test that FactVerifier's new bounded regexes correctly extract entities |
| 8 | + and handle long inputs without crashing. |
| 9 | + """ |
| 10 | + verifier = FactVerifier(use_llm_fallback=False) |
| 11 | + |
| 12 | + # 1. Normal Input - Should extract numbers correctly |
| 13 | + claim = "The profit was 10.5 million in 2024." |
| 14 | + context = "In 2024, the company reported 10.5 million profit." |
| 15 | + |
| 16 | + # Analyze - this triggers _match_entities which uses the new \d{1,20} regex |
| 17 | + result = verifier.verify_fact(claim, context) |
| 18 | + |
| 19 | + # Verify entity matching logic still works |
| 20 | + assert "2024" in result["reasoning"] or result["scores"]["entity_match"] > 0.5 |
| 21 | + |
| 22 | + # 2. Long Input - Should be truncated/handled safely |
| 23 | + # Create valid sentence structure but very long |
| 24 | + base_sentence = "This is a sentence. " |
| 25 | + long_context = base_sentence * 5000 # ~100k chars |
| 26 | + |
| 27 | + # This triggers _segment_sentences with the new safe_pattern |
| 28 | + result = verifier.verify_fact("test", long_context) |
| 29 | + # Should run without timeout/error |
| 30 | + assert result["verdict"] is not None |
| 31 | + |
| 32 | +def test_image_verifier_bounded_regex(): |
| 33 | + """ |
| 34 | + Test that ImageVerifier's bounded regexes match valid dimensions |
| 35 | + and reject excessively long claims. |
| 36 | + """ |
| 37 | + verifier = ImageVerifier(use_llm_fallback=False) |
| 38 | + dummy_meta = ImageAnalysisResult( |
| 39 | + description="test", objects=[], text=[], valid=True, |
| 40 | + width=800, height=600, format="PNG", mode="RGB" |
| 41 | + ) |
| 42 | + |
| 43 | + # 1. Valid Dimensions - Should match using the new \d{1,10} regex |
| 44 | + claim_valid = "The image is 800x600 pixels." |
| 45 | + result_valid = verifier._verify_size_claim(claim_valid, dummy_meta) |
| 46 | + |
| 47 | + assert result_valid.verdict == "SUPPORTED" |
| 48 | + assert "Dimensions match" in result_valid.reasoning |
| 49 | + |
| 50 | + # 2. ReDoS Attempt - Should be blocked by length check |
| 51 | + long_claim = "a" * 600 |
| 52 | + result_blocked = verifier._verify_size_claim(long_claim, dummy_meta) |
| 53 | + |
| 54 | + assert result_blocked.verdict == "INCONCLUSIVE" |
| 55 | + assert "Claim too long" in result_blocked.reasoning |
| 56 | + |
| 57 | + # 3. Mixed spaces - Should match with bounded \s{0,5} |
| 58 | + claim_spaces = "800 x 600" |
| 59 | + result_spaces = verifier._verify_size_claim(claim_spaces, dummy_meta) |
| 60 | + assert result_spaces.verdict == "SUPPORTED" |
0 commit comments