idea176-goc-synth-automated/tests/test_basic.py

61 lines
2.4 KiB
Python

import importlib.util
import os
from idea176_goc_synth_automated.dsl import GraphOfContractsSeed
from idea176_goc_synth_automated.dsl import LocalProblem
from idea176_goc_synth_automated.dsl import SharedVariables
from idea176_goc_synth_automated.generator import generate_mvp_adapters
from idea176_goc_synth_automated.registry import load_registry
def _load_module(path, name):
spec = importlib.util.spec_from_file_location(name, path)
assert spec is not None
module = importlib.util.module_from_spec(spec)
assert spec.loader is not None
spec.loader.exec_module(module)
return module
def test_generate_mvp_adapters_creates_skeletons(tmp_path, monkeypatch):
lp = LocalProblem(domain="Energy", objective="minimize_cost", constraints={"power": 100})
seed = GraphOfContractsSeed(
name="test_goc",
seeds={
"LocalProblem": lp,
"SharedVariables": SharedVariables().variables,
"PlanDelta": {},
"DualVariables": {},
"PrivacyBudget": {},
"AuditLog": [],
"PolicyBlock": {},
},
)
monkeypatch.setenv("GOC_REGISTRY_PATH", str(tmp_path / "registry.json"))
result = generate_mvp_adapters(seed, out_dir=str(tmp_path))
assert os.path.exists(os.path.join(tmp_path, "energy_adapter.py"))
assert os.path.exists(os.path.join(tmp_path, "robotics_adapter.py"))
assert os.path.exists(os.path.join(tmp_path, "energy_openapi.json"))
assert os.path.exists(os.path.join(tmp_path, "robotics_adapter.proto"))
assert os.path.exists(os.path.join(tmp_path, "adapter_manifest.json"))
registry = load_registry()
assert registry["name"] == "test_goc"
assert registry["did_binding"]["controller"].startswith("did:example:")
assert result["registry_entry"]["transport"] == "tls"
def test_generated_simulator_is_deterministic(tmp_path, monkeypatch):
seed = GraphOfContractsSeed(name="sim-test", seeds={"LocalProblem": {"domain": "Energy"}})
monkeypatch.setenv("GOC_REGISTRY_PATH", str(tmp_path / "registry.json"))
result = generate_mvp_adapters(seed, out_dir=str(tmp_path))
module = _load_module(result["simulation"], "generated_sim")
sim = module.DeltaSyncSimulator({"LocalProblem": {"domain": "Energy"}})
first = sim.run(rounds=2)
second = sim.run(rounds=2)
assert first == second
assert sim.replay(first)["accepted"] is True