67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
import json
|
|
import pytest
|
|
|
|
from nova_plan.dsl import LocalProblemDSL
|
|
from nova_plan.catopt_bridge import Object, Morphism, to_object, to_morphism, bridge_example, validate_contracts
|
|
|
|
|
|
def test_local_problem_to_object_and_morphism_basic():
|
|
# Create a minimal LocalProblem via DSL
|
|
dsl = LocalProblemDSL(
|
|
agent_id="rover1",
|
|
objective_expr='vars["x"]',
|
|
variables={"x": 1.5},
|
|
constraints={},
|
|
)
|
|
lp = dsl.to_local_problem()
|
|
|
|
# Convert to canonical object and verify structure
|
|
obj = to_object(lp)
|
|
od = obj.to_dict()
|
|
assert od["agent_id"] == "rover1"
|
|
assert od["variables"] == {"x": 1.5}
|
|
|
|
# Create a morphism (delta) and serialize
|
|
delta = {"x": 0.25}
|
|
morph = to_morphism(delta, source="rover1", target="habitat", version=1)
|
|
j = morph.to_json()
|
|
data = json.loads(j)
|
|
assert data["source"] == "rover1"
|
|
assert data["target"] == "habitat"
|
|
assert data["delta"] == delta
|
|
|
|
|
|
def test_bridge_example_integration():
|
|
dsl = LocalProblemDSL(
|
|
agent_id="habitat1",
|
|
objective_expr='vars["a"] + shared["b"]',
|
|
variables={"a": 2.0},
|
|
constraints={}
|
|
)
|
|
lp = dsl.to_local_problem()
|
|
res = bridge_example(lp, source="habitat1", target="rover1")
|
|
# Basic structure checks
|
|
assert "object" in res and isinstance(res["object"], dict)
|
|
assert "morphism" in res and isinstance(res["morphism"], str)
|
|
# Validate object payload keys
|
|
obj_payload = res["object"]
|
|
assert obj_payload["agent_id"] == "habitat1"
|
|
assert obj_payload["variables"] == {"a": 2.0}
|
|
|
|
|
|
def test_validate_contracts_basic_and_negative():
|
|
dsl = LocalProblemDSL(
|
|
agent_id="rover1",
|
|
objective_expr='vars["x"]',
|
|
variables={"x": 1.0},
|
|
constraints={},
|
|
)
|
|
lp = dsl.to_local_problem()
|
|
obj = Object(lp)
|
|
morph = to_morphism(delta={"x": 0.5}, source="rover1", target="habitat")
|
|
assert validate_contracts(obj, morph) is True
|
|
|
|
# Mismatched delta key should fail
|
|
bad_morph = Morphism(delta={"y": 0.2}, source="rover1", target="habitat")
|
|
assert validate_contracts(obj, bad_morph) is False
|