File size: 7,303 Bytes
13a6b66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""The decision rule, and the ceiling that the stratum is not allowed to cross."""

import copy

from lgstratum import decide, pipeline

SECTION = "properties section rect 1/5 2/5"
OTHER = "properties section rect 1/5 3/5"
ANSWER = "A = 2/25 m^2, I = 2/1875 m^4"


def _reading(sentence, kind="properties", slots=None, notes=()):
    return {"status": "READ", "kind": kind, "sentence": sentence, "slots": slots or {}, "notes": list(notes)}


def _struc(status="VERIFIED", assurance=2, answer=ANSWER, sentence=SECTION, **extra):
    verdict = {"status": status, "assurance": assurance, "answer_text": answer, "problem_struc": sentence}
    verdict.update(extra)
    return verdict


def _dossier(readings, comparison, coverage, struc_verdict, direct=None, echo_ok=True):
    dossier = {"question": "che sezione ha un rettangolo di 1/5 per 2/5?", "lang": "it", "sent": SECTION,
               "reading_a": readings[0] if len(readings) > 0 else {},
               "reading_b": readings[1] if len(readings) > 1 else {},
               "compare": comparison, "coverage": coverage, "struc": struc_verdict,
               "echo_ok": echo_ok}
    if direct is not None:
        dossier["reading_direct"] = direct
    return dossier


def test_no_state_promises_more_than_the_structures_topology_can_give():
    # Two is the highest level the structures topology reaches: its two solution roads and its
    # referee agreeing. A question written in a human language adds one fallible step before that, so
    # no state of this stratum is capped above the partner: the minimum can only lower what is shown.
    assert set(decide.STATES) == set(decide.CEILING)
    assert max(decide.CEILING.values()) == 2
    for state in decide.STATES:
        assert decide.CEILING[state] <= 2, state


def test_two_agreeing_readers_take_the_minimum_with_the_structures_answer():
    readings = [_reading(SECTION), _reading(SECTION)]
    for status, assurance, expected in (("VERIFIED", 2, 2), ("SINGLE_PROVER", 1, 1), ("UNKNOWN", 0, 0)):
        final = decide.decide(_dossier(readings, {"same": True}, {"ok": True},
                                       _struc(status, assurance, "" if status == "UNKNOWN" else ANSWER)))
        assert final["state"] == "AGREED"
        assert final["ceiling"] == 2
        assert final["assurance"] == expected, (status, final)
        assert final["assurance"] == min(2, assurance), (status, final)
        assert final["struc_assurance"] == assurance, (status, final)


def test_a_missing_witness_caps_the_session_at_one():
    readings = [_reading(SECTION), {}]
    final = decide.decide(_dossier(readings, None, None, _struc()))
    assert final["state"] == "SINGLE"
    assert final["assurance"] == 1
    # The cap is not the only thing that holds with one reader: the count of numbers still does, so a
    # witness that stands alone does not thereby get to answer another structure.
    kept = decide.decide(_dossier(readings, None, {"ok": True}, _struc()))
    assert kept["state"] == "SINGLE"
    assert kept["assurance"] == 1


def test_a_lone_reading_that_lost_a_number_answers_a_different_structure():
    # What the ablation of the topology found: with the second reader switched out, a number shifted by
    # the first one used to reach the person as a single witness answer. The ceiling said one, but the
    # answer was of another structure, so the count of the numbers refuses it instead.
    readings = [_reading(OTHER), {}]
    final = decide.decide(_dossier(readings, None, {"ok": False}, _struc(sentence=OTHER)))
    assert final["state"] == "DISPUTED"
    assert final["assurance"] == 0
    assert final["answer"] == ""
    assert "number_count" in final["reasons"]


def test_a_question_that_announces_two_kinds_of_problem_is_left_to_the_user():
    readings = [{"status": "AMBIGUOUS", "kind": None, "sentence": "", "slots": {},
                 "notes": [{"reason": "multiple"}]}, {}]
    final = decide.decide(_dossier(readings, None, None, None))
    assert final["state"] == "AMBIGUOUS"
    assert final["ceiling"] == 0
    assert final["assurance"] == 0
    assert "multiple" in final["reasons"]


def test_a_question_this_language_does_not_write_is_not_covered():
    final = decide.decide(_dossier([{}, {}], None, None, None))
    assert final["state"] == "NOT COVERED"
    assert final["assurance"] == 0
    assert final["consulted_struc"] is False
    assert final["answer"] == ""


def test_a_disagreement_is_never_resolved_by_the_structures_topology():
    readings = [_reading(SECTION), _reading(OTHER)]
    final = decide.decide(_dossier(readings, {"same": False, "why": "text"}, {"ok": True}, _struc()))
    assert final["state"] == "DISPUTED"
    assert final["assurance"] == 0
    assert final["answer"] == ""
    assert "disagree" in final["reasons"]


def test_a_reading_that_lost_a_number_is_disputed_even_when_the_readers_agree():
    readings = [_reading(SECTION), _reading(SECTION)]
    final = decide.decide(_dossier(readings, {"same": True}, {"ok": False}, _struc()))
    assert final["state"] == "DISPUTED"
    assert "number_count" in final["reasons"]
    assert final["assurance"] == 0


def test_an_echo_that_names_another_sentence_is_reported_as_tampering():
    readings = [_reading(SECTION), _reading(SECTION)]
    final = decide.decide(_dossier(readings, {"same": True}, {"ok": True}, _struc(), echo_ok=False))
    assert final["state"] == "TAMPERED"
    assert final["assurance"] == 0
    assert "boundary" in final["reasons"]


def test_a_sentence_already_written_in_the_structures_language_passes_through():
    direct = {"kind": "properties", "sentence": SECTION, "slots": {"S": "rect 1/5 2/5"}}
    final = decide.decide(_dossier([{}, {}], None, {"ok": True}, _struc(), direct=direct))
    assert final["state"] == "PASSTHROUGH"
    assert final["ceiling"] == 2
    assert final["assurance"] == 2


def test_no_answer_is_shown_when_the_structures_topology_did_not_answer():
    readings = [_reading(SECTION), _reading(SECTION)]
    final = decide.decide(_dossier(readings, {"same": True}, {"ok": True}, _struc("UNKNOWN", 0, "")))
    assert final["assurance"] == 0
    assert final["answer"] == ""
    assert "struc_unknown" in final["reasons"]


def test_the_decision_is_pure_and_leaves_the_evidence_as_it_found_it():
    dossier = _dossier([_reading(SECTION), _reading(SECTION)], {"same": True}, {"ok": True}, _struc())
    before = copy.deepcopy(dossier)
    first = decide.decide(dossier)
    second = decide.decide(dossier)
    assert first == second
    assert dossier == before


def test_a_claimed_assurance_is_marked_and_the_tools_lower_it_again():
    honest = decide.decide(_dossier([_reading(SECTION), _reading(SECTION)],
                                    {"same": True}, {"ok": True}, _struc()))
    forced = decide.raise_assurance(honest, 3)
    assert forced["assurance"] == 3
    assert forced["claimed_above_evidence"] is True
    # The tools do not show what the arbiter wrote: they re-run the decision on the transcript, and
    # what stands is the more cautious of the two.
    checked = pipeline.referee_adjusted(forced, [{"sender": "arbiter", "claimed": "unsat", "referee": "sat"}])
    assert checked["assurance"] <= min(2, honest["assurance"])
    assert "arbiter" in (checked.get("detected") or [])