File size: 2,403 Bytes
e9dac18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""The same sessions, run by the orchestrator itself instead of in memory.

The in-memory pipeline is the fast path; what the user is served with is a session of eight separate
processes that exchange envelopes. This test asks the orchestrator to run two of them, and checks
that every envelope came back with a claim the referee of the session confirmed, that the assurance
shown is the one the decision rule allows, and that a node which misbehaves is found out there too.
"""

import importlib.util
import sys
from pathlib import Path

import pytest

from lgstratum import corpus, lexicon

ROOT = Path(__file__).resolve().parent.parent
ITEMS = {item["id"]: item for item in corpus.load()}


def _tool():
    path = ROOT / "tools" / "ask.py"
    spec = importlib.util.spec_from_file_location("stratum_ask_tool", path)
    module = importlib.util.module_from_spec(spec)
    sys.modules[spec.name] = module
    spec.loader.exec_module(module)
    return module


def test_the_eight_nodes_of_the_topology_exchange_envelopes():
    pytest.importorskip("lingologos_orchestrator")
    tool = _tool()
    item = ITEMS["it-01"]
    result = tool.ask(str(item["question"]), str(item["lang"]))
    assert len(result["records"]) == 8, result["records"]
    for record in result["records"]:
        assert record["claimed"] == record["referee"], record
    final = result["final"]
    assert final["state"] == "AGREED", final
    assert 0 < final["assurance"] <= 2, final
    assert final["assurance"] <= int(final.get("math_assurance", 0) or 0), final
    assert str(final["answer"]).strip(), final
    labels = lexicon.labels(str(item["lang"]))
    text = str(result["reply"].get("text", ""))
    assert labels["math_layer"] in text, text
    assert labels["understood"] in text, text


def test_the_orchestrator_reports_a_verdict_that_is_not_the_one_the_arbiter_wrote():
    pytest.importorskip("lingologos_orchestrator")
    tool = _tool()
    item = ITEMS["it-01"]
    honest = tool.ask(str(item["question"]), str(item["lang"]))
    forced = tool.ask(str(item["question"]), str(item["lang"]), faults_spec="arbiter:force_assurance")
    assert any(record["claimed"] != record["referee"] for record in forced["records"]), forced["records"]
    assert forced["final"]["assurance"] <= honest["final"]["assurance"], forced["final"]
    assert "arbiter" in (forced["final"].get("detected") or []), forced["final"]