qsvaps / tests /test_models.py
vmore2
Initial release: QSVAPS v0.1.0 - Quantum Superposition Verification for Agent Plan Safety
ce8c08a
"""Tests for QSVAPS data models."""
import pytest
from qsvaps.models import (
Plan,
PlanAction,
PlanConstraint,
ResourceConstraint,
ConstraintType,
FailureWitness,
VerificationResult,
VariableMapping,
)
class TestPlanAction:
def test_basic_creation(self):
action = PlanAction(name="fetch", description="Fetch data")
assert action.name == "fetch"
assert action.description == "Fetch data"
assert action.can_fail is True
assert action.fallback is None
assert action.resources == []
def test_with_resources(self):
action = PlanAction(
name="process",
resources=["gpu", "memory"],
can_fail=False,
)
assert action.resources == ["gpu", "memory"]
assert action.can_fail is False
def test_with_fallback(self):
action = PlanAction(name="main", fallback="backup")
assert action.fallback == "backup"
class TestPlan:
def test_basic_plan(self):
plan = Plan(
name="Test Plan",
actions=[
PlanAction(name="a"),
PlanAction(name="b"),
],
dependencies=[("a", "b")],
)
assert len(plan.actions) == 2
assert plan.action_names == ["a", "b"]
def test_get_action(self):
plan = Plan(
name="Test",
actions=[PlanAction(name="x"), PlanAction(name="y")],
)
assert plan.get_action("x").name == "x"
assert plan.get_action("z") is None
def test_get_action_index(self):
plan = Plan(
name="Test",
actions=[PlanAction(name="a"), PlanAction(name="b")],
)
assert plan.get_action_index("a") == 0
assert plan.get_action_index("b") == 1
assert plan.get_action_index("c") == -1
class TestVariableMapping:
def test_num_variables(self):
vm = VariableMapping(
variables={"s_a": 0, "s_b": 1},
descriptions={"s_a": "A succeeds", "s_b": "B succeeds"},
)
assert vm.num_variables == 2
def test_get_var_name(self):
vm = VariableMapping(variables={"s_a": 0, "s_b": 1})
assert vm.get_var_name(0) == "s_a"
assert vm.get_var_name(1) == "s_b"
assert vm.get_var_name(99) == "x99"
def test_get_description(self):
vm = VariableMapping(
variables={"s_a": 0},
descriptions={"s_a": "Action A succeeds"},
)
assert vm.get_description(0) == "Action A succeeds"
class TestFailureWitness:
def test_creation(self):
constraint = PlanConstraint(
expression="x0",
description="A must succeed",
constraint_type=ConstraintType.COMPLETION,
)
witness = FailureWitness(
assignment={"s_a": False},
violated_constraints=[constraint],
bitstring="0",
measurement_count=42,
explanation="A failed",
)
assert witness.measurement_count == 42
assert len(witness.violated_constraints) == 1
class TestVerificationResult:
def test_safe_result(self):
plan = Plan(name="Safe", actions=[])
result = VerificationResult(
plan=plan,
is_safe=True,
witnesses=[],
num_variables=3,
num_violations=0,
total_states=8,
grover_iterations=0,
)
assert result.is_safe
assert result.num_violations == 0