113 lines
4.1 KiB
Python
113 lines
4.1 KiB
Python
"""MAA tests: Gate blocks manufacturing tools without MPC; allows with valid MPC."""
|
|
|
|
import pytest
|
|
|
|
from fusionagi.maa import MAAGate
|
|
from fusionagi.maa.layers import MPCAuthority
|
|
from fusionagi.maa.gap_detection import check_gaps, GapClass
|
|
from fusionagi.governance import Guardrails
|
|
from fusionagi.agents import ExecutorAgent
|
|
from fusionagi.tools import ToolRegistry
|
|
from fusionagi.maa.tools import cnc_emit_tool
|
|
from fusionagi.core import StateManager
|
|
|
|
|
|
def test_maa_gate_blocks_manufacturing_without_mpc() -> None:
|
|
mpc = MPCAuthority()
|
|
gate = MAAGate(mpc_authority=mpc)
|
|
allowed, result = gate.check("cnc_emit", {"machine_id": "m1", "toolpath_ref": "t1"})
|
|
assert allowed is False
|
|
assert "mpc_id" in str(result)
|
|
|
|
|
|
def test_maa_gate_allows_manufacturing_with_valid_mpc() -> None:
|
|
mpc = MPCAuthority()
|
|
cert = mpc.issue("design-001", metadata={})
|
|
gate = MAAGate(mpc_authority=mpc)
|
|
allowed, result = gate.check("cnc_emit", {"mpc_id": cert.mpc_id.value, "machine_id": "m1", "toolpath_ref": "t1"})
|
|
assert allowed is True
|
|
assert isinstance(result, dict)
|
|
assert result.get("mpc_id") == cert.mpc_id.value
|
|
|
|
|
|
def test_maa_gate_non_manufacturing_passes() -> None:
|
|
mpc = MPCAuthority()
|
|
gate = MAAGate(mpc_authority=mpc)
|
|
allowed, result = gate.check("file_read", {"path": "/tmp/foo"})
|
|
assert allowed is True
|
|
assert result == {"path": "/tmp/foo"}
|
|
|
|
|
|
def test_gap_detection_returns_gaps() -> None:
|
|
gaps = check_gaps({"require_numeric_bounds": True})
|
|
assert len(gaps) >= 1
|
|
assert gaps[0].gap_class == GapClass.MISSING_NUMERIC_BOUNDS
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"context,expected_gap_class",
|
|
[
|
|
({"require_numeric_bounds": True}, GapClass.MISSING_NUMERIC_BOUNDS),
|
|
({"require_explicit_tolerances": True}, GapClass.IMPLICIT_TOLERANCES),
|
|
({"require_datums": True}, GapClass.UNDEFINED_DATUMS),
|
|
({"require_process_type": True}, GapClass.ASSUMED_PROCESSES),
|
|
],
|
|
)
|
|
def test_gap_detection_parametrized(context: dict, expected_gap_class: GapClass) -> None:
|
|
"""Parametrized gap detection tests."""
|
|
gaps = check_gaps(context)
|
|
assert len(gaps) >= 1
|
|
assert gaps[0].gap_class == expected_gap_class
|
|
|
|
|
|
def test_gap_detection_no_gaps() -> None:
|
|
gaps = check_gaps({"numeric_bounds": {"x": [0, 1]}})
|
|
assert len(gaps) == 0
|
|
|
|
|
|
def test_gap_detection_no_gaps_empty_context() -> None:
|
|
gaps = check_gaps({})
|
|
assert len(gaps) == 0
|
|
|
|
|
|
def test_executor_with_guardrails_blocks_manufacturing_without_mpc() -> None:
|
|
guardrails = Guardrails()
|
|
mpc = MPCAuthority()
|
|
gate = MAAGate(mpc_authority=mpc)
|
|
guardrails.add_check(gate.check)
|
|
reg = ToolRegistry()
|
|
reg.register(cnc_emit_tool())
|
|
state = StateManager()
|
|
executor = ExecutorAgent(registry=reg, state_manager=state, guardrails=guardrails)
|
|
from fusionagi.schemas.messages import AgentMessage, AgentMessageEnvelope
|
|
env = AgentMessageEnvelope(
|
|
message=AgentMessage(
|
|
sender="orch",
|
|
recipient="executor",
|
|
intent="execute_step",
|
|
payload={
|
|
"step_id": "s1",
|
|
"plan": {"steps": [{"id": "s1", "description": "CNC", "dependencies": [], "tool_name": "cnc_emit", "tool_args": {"machine_id": "m1", "toolpath_ref": "t1"}}], "fallback_paths": []},
|
|
"tool_name": "cnc_emit",
|
|
"tool_args": {"machine_id": "m1", "toolpath_ref": "t1"},
|
|
},
|
|
),
|
|
task_id="t1",
|
|
)
|
|
out = executor.handle_message(env)
|
|
assert out is not None
|
|
assert out.message.intent == "step_failed"
|
|
assert "mpc_id" in out.message.payload.get("error", "")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test_maa_gate_blocks_manufacturing_without_mpc()
|
|
test_maa_gate_allows_manufacturing_with_valid_mpc()
|
|
test_maa_gate_non_manufacturing_passes()
|
|
test_gap_detection_returns_gaps()
|
|
test_gap_detection_parametrized({"require_numeric_bounds": True}, GapClass.MISSING_NUMERIC_BOUNDS)
|
|
test_gap_detection_no_gaps()
|
|
test_gap_detection_no_gaps_empty_context()
|
|
test_executor_with_guardrails_blocks_manufacturing_without_mpc()
|
|
print("MAA tests OK")
|