File size: 3,200 Bytes
1f5647a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Direct-helper regressions: malformed requests never become char context."""
import unittest

from contracts import sha256
from inference import DiscourseModel


class RuntimeInputValidation(unittest.TestCase):
    def setUp(self):
        # A boundary-only engine has no tokenizer/session. Any accidental
        # inference during an invalid request therefore fails this test.
        self.engine = object.__new__(DiscourseModel)
        self.engine.audit = False
        self.engine.manifest = {"method": "doxa-contextual-discourse-v0.2.0", "model_id": "authored-boundary-test",
            "revision": "0" * 40, "publication_status": "qualified_release", "scope": "Source contract only"}

    def assert_invalid(self, **changes):
        arguments = {"target": "A source sentence.", "before": ["A preceding source sentence."], "after": [], **changes}
        result = self.engine.check(**arguments)
        self.assertEqual(result["prediction_status"], "invalid_input")
        self.assertTrue(all(row["model_score"] is None and row["suggested_decision"] is None for row in result["results"]))

    def test_neighbor_strings_none_and_nonstring_members_are_invalid(self):
        for value in ("a source string is not an array", None, [None], [12], {"text": "a source"}):
            with self.subTest(value=value):
                self.assert_invalid(before=value)
                self.assert_invalid(after=value)

    def test_invalid_or_duplicate_task_arrays_are_invalid(self):
        for value in ("BASIL_lexical_bias", None, [], [None], [""], ["BASIL_lexical_bias", "BASIL_lexical_bias"], ["BASIL_lexical_bias", {}]):
            with self.subTest(value=value):
                self.assert_invalid(tasks=value)

    def test_anchor_integer_types_are_strict(self):
        text = "A source sentence."
        anchor = {"document_id": "source", "start": 0, "end": len(text), "offset_unit": "unicode_codepoint", "sha256": sha256(text)}
        for value in (False, 0., "0", -1):
            self.assert_invalid(source_anchor={**anchor, "start": value})
        for value in (True, float(len(text)), str(len(text)), 0):
            self.assert_invalid(source_anchor={**anchor, "end": value})
        for value in ([], {}, "source", {**anchor, "document_id": 1}, {**anchor, "sha256": "not a hash"}):
            self.assert_invalid(source_anchor=value)

    def test_malformed_unicode_and_domain_metadata_are_invalid(self):
        self.assert_invalid(target="A malformed \ud800 source")
        self.assert_invalid(before=["A malformed \udc00 context"])
        self.assert_invalid(applied_rubric=7)
        self.assert_invalid(language=None)
        self.assert_invalid(genre=[])

    def test_whitespace_context_is_missing_and_unknown_tasks_stay_unsupported(self):
        result = self.engine.check("A source sentence.", [" \n"], [])
        self.assertTrue(all(row["prediction_status"] == "insufficient_context" for row in result["results"]))
        result = self.engine.check("A source sentence.", [], [], tasks=["comparative_double_standard"])
        self.assertEqual(result["results"][0]["prediction_status"], "unsupported_criterion")


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