44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
import json
|
|
import sys
|
|
import os
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")))
|
|
from guardrail_space.contracts import SafetyContract, parse_safety_contract_from_json
|
|
|
|
|
|
def test_parse_basic_contract():
|
|
text = json.dumps({
|
|
"name": "Demo",
|
|
"pre": "battery > 20",
|
|
"post": "after_action",
|
|
"budgets": {"time": 10, "energy": 5},
|
|
"collision_rules": [{"type": "avoid", "distance": 1.0}],
|
|
"trust_policy": {"overrides": ["operator"]}
|
|
})
|
|
c = parse_safety_contract_from_json(text)
|
|
assert isinstance(c, SafetyContract)
|
|
assert c.name == "Demo"
|
|
assert c.pre == "battery > 20"
|
|
assert c.budgets["time"] == 10
|
|
assert c.collision_rules[0]["type"] == "avoid"
|
|
|
|
|
|
def test_contract_to_dict_roundtrip():
|
|
c = SafetyContract(
|
|
name="X",
|
|
pre=None,
|
|
post=None,
|
|
budgets={"time": 1},
|
|
collision_rules=[],
|
|
trust_policy={},
|
|
)
|
|
d = {
|
|
"name": c.name,
|
|
"pre": c.pre,
|
|
"post": c.post,
|
|
"budgets": c.budgets,
|
|
"collision_rules": c.collision_rules,
|
|
"trust_policy": c.trust_policy,
|
|
}
|
|
assert d["name"] == "X"
|
|
assert d["budgets"]["time"] == 1
|