56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
import json
|
|
from spacesafeml_certification_benchmark_and_ import (
|
|
AgentCapabilities,
|
|
Contract,
|
|
ResourceBudgets,
|
|
SafetyPreCondition,
|
|
SafetyPostCondition,
|
|
DataSharingPolicy,
|
|
TelemetrySchema,
|
|
Scenario,
|
|
SchemaRegistry,
|
|
manifest_fingerprint,
|
|
)
|
|
|
|
|
|
def test_contract_roundtrip_and_fingerprint():
|
|
agent = AgentCapabilities(
|
|
name="rvr-rover",
|
|
version="0.1",
|
|
compute_arch="armv7",
|
|
sensors=["camera", "lidar"],
|
|
actuators=["wheel_left", "wheel_right"],
|
|
)
|
|
|
|
contract = Contract(
|
|
agent=agent,
|
|
pre_conditions=[SafetyPreCondition(description="battery>20%", expression="battery>20")],
|
|
post_conditions=[SafetyPostCondition(description="no-collision", expression="distance_to_obstacle>0")],
|
|
resource_budgets=ResourceBudgets(cpu_ms=100, memory_mb=128, energy_joules=2000.0),
|
|
data_sharing=DataSharingPolicy(allow_telemetry=True, allowed_partners=["ground"], retention_seconds=3600),
|
|
telemetry=TelemetrySchema(version="0.1", signals={"camera": "image", "lidar": "pointcloud"}),
|
|
scenario=Scenario(id="sc-001", description="Perception dropout test", fault_model={"camera": "dropout"}, replay_hooks=["replay_v1"]),
|
|
)
|
|
|
|
# Round-trip
|
|
js = contract.to_json()
|
|
parsed = Contract.from_json(js)
|
|
assert parsed.agent.name == contract.agent.name
|
|
assert parsed.telemetry.signals["camera"] == "image"
|
|
|
|
# Fingerprint deterministic
|
|
fp1 = manifest_fingerprint(contract)
|
|
fp2 = manifest_fingerprint(parsed)
|
|
assert fp1 == fp2
|
|
|
|
|
|
def test_schema_registry():
|
|
registry = SchemaRegistry()
|
|
agent = AgentCapabilities(name="x", version="v")
|
|
contract = Contract(agent=agent)
|
|
key = registry.register("x", "v", contract)
|
|
assert key == "x:v"
|
|
entry = registry.get("x", "v")
|
|
assert entry is not None
|
|
assert "fingerprint" in entry
|