File size: 3,099 Bytes
7e2a2f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import json
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
    sys.path.insert(0, str(ROOT))

from decision_runtime import NormativeDecisionRuntime


def main() -> int:
    registry = json.loads((ROOT / "data" / "registry" / "normative_decision_contracts.json").read_text(encoding="utf-8"))
    runtime = NormativeDecisionRuntime(registry["contracts"], "test-build")

    cases = [
        (
            "decided_terminate",
            runtime.evaluate("open_education_enrollment_status", {"education_mode": "OPEN_EDUCATION", "consecutive_terms_conditions_not_met": 4}),
            "DECIDED",
        ),
        (
            "decided_continue",
            runtime.evaluate("open_education_enrollment_status", {"education_mode": "OPEN_EDUCATION", "consecutive_terms_conditions_not_met": 3}),
            "DECIDED",
        ),
        (
            "missing_fact",
            runtime.evaluate("open_education_enrollment_status", {"education_mode": "OPEN_EDUCATION"}),
            "UNKNOWN",
        ),
        (
            "legal_judgment",
            runtime.evaluate("remote_education_rector_assistant_limit", {"central_open_education_university": True}),
            "REQUIRES_JUDGMENT",
        ),
        ("unknown_scope", runtime.evaluate("not_published", {}), "OUT_OF_SCOPE"),
    ]

    conflict_contract = {
        "contract_id": "TEST_CONFLICT",
        "decision_type": "conflict_test",
        "source_refs": [{"document_id": "TEST", "article_id": "Madde 1"}],
        "input_schema": {"fields": {"x": {"required": True}}},
        "rules": [
            {"rule_id": "a", "priority": 10, "when": {"all": [{"fact": "x", "operator": "eq", "value": 1}]}, "outcome": "YES"},
            {"rule_id": "b", "priority": 10, "when": {"all": [{"fact": "x", "operator": "eq", "value": 1}]}, "outcome": "NO"},
        ],
    }
    conflict_runtime = NormativeDecisionRuntime([conflict_contract], "test-conflict")
    cases.append(("conflict", conflict_runtime.evaluate("conflict_test", {"x": 1}), "CONFLICT"))

    failures = []
    for name, result, expected in cases:
        if result.get("status") != expected:
            failures.append(f"{name}: {result.get('status')} != {expected}")

    first = runtime.evaluate("open_education_enrollment_status", {"education_mode": "OPEN_EDUCATION", "consecutive_terms_conditions_not_met": 4})
    second = runtime.evaluate("open_education_enrollment_status", {"consecutive_terms_conditions_not_met": 4, "education_mode": "OPEN_EDUCATION"})
    if first != second:
        failures.append("determinism: identical facts produced different results")
    if runtime.governance_report().get("warning_count", 0) < 1:
        failures.append("governance: expected at least one legal judgment warning")

    if failures:
        print("FAIL")
        print("\n".join(failures))
        return 1
    print(f"PASS decision runtime: {len(cases)} status cases + determinism + governance")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())