File size: 2,257 Bytes
d3d09d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Unit tests for Second-Pass Answer Verification."""

import unittest
from ingest import DocumentChunk
from verify import AnswerVerifier


class TestVerify(unittest.TestCase):
    def setUp(self):
        self.verifier = AnswerVerifier()
        self.chunk = DocumentChunk(
            chunk_id="c_sec",
            doc_id="d1",
            file_name="sec.txt",
            text="Master encryption keys must undergo automated cryptographic rotation every 90 days using AWS KMS.",
            page_number=2,
            section_title="Encryption Standards",
            chunk_index=0,
            token_count=16
        )

    def test_claim_extraction(self):
        text = (
            "Master encryption keys rotate every 90 days. "
            "AWS KMS is used for key management [Page 2, Section \"Encryption Standards\"]."
        )
        claims = self.verifier.extract_claims(text)
        self.assertEqual(len(claims), 2)
        # Verify citations were stripped from claims
        for c in claims:
            self.assertNotIn("[Page 2", c)

    def test_supported_claim_verification(self):
        answer = "Master encryption keys rotate every 90 days using AWS KMS."
        result = self.verifier.verify_answer(answer, [self.chunk])
        self.assertEqual(result.faithfulness_score, 1.0)
        self.assertFalse(result.has_hallucination)
        self.assertEqual(len(result.supported_claims), 1)

    def test_unsupported_claim_flagging(self):
        answer = (
            "Master encryption keys rotate every 90 days. "
            "The company also uses Azure Key Vault in London."
        )
        result = self.verifier.verify_answer(answer, [self.chunk])
        self.assertLess(result.faithfulness_score, 1.0)
        self.assertTrue(result.has_hallucination)
        self.assertIn("Azure Key Vault", result.verified_answer)
        # Check warning flag appended
        self.assertIn("⚠️", result.verified_answer)

    def test_not_found_handling(self):
        answer = "Not found in the document."
        result = self.verifier.verify_answer(answer, [self.chunk])
        self.assertEqual(result.faithfulness_score, 1.0)
        self.assertFalse(result.has_hallucination)


if __name__ == "__main__":
    unittest.main()