commit 662eb8d801cf1de103d79e8dacac54235dda67a5
parent 8d3064173a2bfd22b608bd40712bb9f195ea61b1
Author: triesap <tyson@radroots.org>
Date: Mon, 21 Sep 2026 03:44:26 +0000
spec: test assertion projection semantics
Diffstat:
2 files changed, 194 insertions(+), 0 deletions(-)
diff --git a/tests/projection_assertions.mojo b/tests/projection_assertions.mojo
@@ -0,0 +1,135 @@
+from std.collections import List
+
+from json import Value, dumps, loads
+
+
+def registered_projection_operators() -> List[String]:
+ var operators = List[String]()
+ operators.append("equals")
+ operators.append("absent")
+ operators.append("present")
+ operators.append("contains")
+ operators.append("not_equals")
+ operators.append("tolerance")
+ return operators^
+
+
+def _has_key(value: Value, key: String) -> Bool:
+ for candidate in value.object_keys():
+ if candidate == key:
+ return True
+ return False
+
+
+def _lookup(actual: Value, pointer: String) raises -> Value:
+ var current = actual.copy()
+ if pointer == "" or pointer == "/":
+ return current^
+ for raw_token in pointer.split("/"):
+ var token = String(raw_token)
+ if token == "":
+ continue
+ if current.is_array():
+ current = current.array_items()[Int(token)].copy()
+ elif current.is_object():
+ current = loads(current.get(token))
+ else:
+ raise Error("projection path does not resolve: " + pointer)
+ return current^
+
+
+def _path_exists(actual: Value, pointer: String) -> Bool:
+ try:
+ _ = _lookup(actual, pointer)
+ return True
+ except:
+ return False
+
+
+def _deep_equal(lhs: Value, rhs: Value) -> Bool:
+ return dumps(lhs) == dumps(rhs)
+
+
+def _is_number(value: Value) -> Bool:
+ return value.is_int() or value.is_float()
+
+
+def _as_float(value: Value) -> Float64:
+ if value.is_float():
+ return value.float_value()
+ return Float64(value.int_value())
+
+
+def assert_projection(actual: Value, assertions: List[Value]) raises:
+ for assertion in assertions:
+ if not _has_key(assertion, "operator"):
+ raise Error("projection assertion requires 'operator'")
+ var operator = assertion["operator"].string_value()
+ var registered = registered_projection_operators()
+ var known = False
+ for candidate in registered:
+ if candidate == operator:
+ known = True
+ if not known:
+ raise Error("unknown projection operator: " + operator)
+
+ if operator == "present":
+ if not _path_exists(actual, assertion["path"].string_value()):
+ raise Error(
+ "expected present path: "
+ + assertion["path"].string_value()
+ )
+ continue
+ if operator == "absent":
+ if _path_exists(actual, assertion["path"].string_value()):
+ raise Error(
+ "expected absent path: "
+ + assertion["path"].string_value()
+ )
+ continue
+
+ var observed = _lookup(actual, assertion["path"].string_value())
+ if operator == "equals":
+ if not _deep_equal(observed, assertion["value"]):
+ raise Error(
+ "expected equality at "
+ + assertion["path"].string_value()
+ )
+ elif operator == "not_equals":
+ if _deep_equal(observed, assertion["value"]):
+ raise Error(
+ "expected inequality at "
+ + assertion["path"].string_value()
+ )
+ elif operator == "contains":
+ if not observed.is_array():
+ raise Error(
+ "contains requires an array at "
+ + assertion["path"].string_value()
+ )
+ var found = False
+ for item in observed.array_items():
+ if _deep_equal(item, assertion["value"]):
+ found = True
+ if not found:
+ raise Error(
+ "expected contained value at "
+ + assertion["path"].string_value()
+ )
+ elif operator == "tolerance":
+ if not _is_number(observed) or not _is_number(
+ assertion["value"]
+ ):
+ raise Error(
+ "tolerance requires numeric values at "
+ + assertion["path"].string_value()
+ )
+ var delta = _as_float(observed) - _as_float(assertion["value"])
+ if delta < 0.0:
+ delta = -delta
+ var tolerance = _as_float(assertion["tolerance"])
+ if delta > tolerance:
+ raise Error(
+ "value out of tolerance at "
+ + assertion["path"].string_value()
+ )
diff --git a/tests/test_hyf.mojo b/tests/test_hyf.mojo
@@ -27,6 +27,7 @@ from fixture_loader import (
load_fixture_top_level_field_from_path,
)
from fixture_validator import validate_fixture_corpus
+from projection_assertions import assert_projection
from hyf_core.backends.selector import (
execute_capability as execute_core_capability,
resolve_backend,
@@ -1270,5 +1271,63 @@ def test_fixture_validator_accepts_corpus_and_rejects_corruptions() raises:
)
+def test_projection_assertions_enforce_exactness_and_reject_unknown_operators() raises:
+ var actual = loads(
+ '{"assessment":{"eligibility":"eligible"},'
+ '"plans":[{"plan_id":"p1","allocations":[{"lot_id":"lot-1",'
+ '"revision":"l1","quantity":30}]}],'
+ '"execution":{"hyf_business_writes":0}}'
+ )
+ var passing = List[Value]()
+ passing.append(
+ loads('{"operator":"equals","path":"/assessment/eligibility","value":"eligible"}')
+ )
+ passing.append(
+ loads('{"operator":"absent","path":"/assessment/failed_checks"}')
+ )
+ passing.append(
+ loads('{"operator":"contains","path":"/plans/0/allocations","value":{"lot_id":"lot-1","revision":"l1","quantity":30}}')
+ )
+ passing.append(
+ loads('{"operator":"tolerance","path":"/plans/0/allocations/0/quantity","value":30,"tolerance":0}')
+ )
+ assert_projection(actual, passing)
+
+ var wrong_value = List[Value]()
+ wrong_value.append(
+ loads('{"operator":"equals","path":"/assessment/eligibility","value":"ineligible"}')
+ )
+ with assert_raises():
+ assert_projection(actual, wrong_value)
+
+ var missing_revision = List[Value]()
+ missing_revision.append(
+ loads('{"operator":"present","path":"/plans/0/allocations/0/expected_revision"}')
+ )
+ with assert_raises():
+ assert_projection(actual, missing_revision)
+
+ var wrong_order = List[Value]()
+ wrong_order.append(
+ loads('{"operator":"equals","path":"/plans/0/allocations/0","value":{"lot_id":"lot-2","revision":"l1","quantity":30}}')
+ )
+ with assert_raises():
+ assert_projection(actual, wrong_order)
+
+ var out_of_tolerance = List[Value]()
+ out_of_tolerance.append(
+ loads('{"operator":"tolerance","path":"/plans/0/allocations/0/quantity","value":31,"tolerance":0}')
+ )
+ with assert_raises():
+ assert_projection(actual, out_of_tolerance)
+
+ var unknown_operator = List[Value]()
+ unknown_operator.append(
+ loads('{"operator":"approximately","path":"/assessment/eligibility","value":"eligible"}')
+ )
+ with assert_raises():
+ assert_projection(actual, unknown_operator)
+
+
def main() raises:
TestSuite.discover_tests[__functions_in_module()]().run()