import copy import hashlib import json import pytest from k1link.simulation.ai_polygon.qualified_maps import qualified_document from k1link.simulation.ai_polygon.urban_maps import UrbanMapStore, validate_map def source(): return dict(schema_version="missioncore.refined-full-plan-research/v1", type="FeatureCollection", point_clouds=False, manual_geometry=False, features=[dict(type="Feature", id="front-1", properties=dict(**{"class": "building_facade"}, review_status="unreviewed"), geometry=dict(type="LineString", coordinates=[[1, 2, 3], [4, 5, 6]]))]) WORLD = {"sha256": "a" * 64, "collider_sha256": "b" * 64} def adapt(plan=None, **kwargs): raw = json.dumps(source() if plan is None else plan).encode() options = dict(artifact_sha256=hashlib.sha256(raw).hexdigest(), source_world_sha256=WORLD["sha256"], qualification="symbol-pass-20261014", source_snapshot="test", tiles_total=3) return qualified_document(raw, WORLD, **{**options, **kwargs}) def test_lossless_geometry_and_honest_confidence(tmp_path): plan = source() result = adapt(plan) assert result["features"][0]["geometry"] == plan["features"][0]["geometry"] p = result["features"][0]["properties"] assert p["confidence"] is None and p["confidence_status"] == "not-assessed" assert p["authority"] == "observation-only" and p["route_permission"] == "unassigned" assert "confidence" not in plan["features"][0]["properties"] store = UrbanMapStore(tmp_path) store.save(WORLD, result) assert store.get(WORLD) == result @pytest.mark.parametrize("change", [dict(artifact_sha256="0" * 64), dict(source_world_sha256="c" * 64), dict(tiles_total=-1)]) def test_changed_or_foreign_source_rejected(change): with pytest.raises(ValueError): adapt(**change) @pytest.mark.parametrize("property,value", [("route_permission", "allowed"), ("authority", "navigation")]) def test_import_never_promotes_authority(property, value): plan = source(); plan["features"][0]["properties"][property] = value with pytest.raises(ValueError): adapt(plan) def test_null_confidence_requires_v3_explicit_unknown(): result = adapt() bad = copy.deepcopy(result); bad["schema_version"] = "missioncore.urban-map/v2" with pytest.raises(ValueError): validate_map(bad, WORLD) bad = copy.deepcopy(result); del bad["features"][0]["properties"]["confidence_status"] with pytest.raises(ValueError): validate_map(bad, WORLD) bad = copy.deepcopy(result); bad["features"][0]["properties"]["confidence"] = float("nan") with pytest.raises(ValueError): validate_map(bad, WORLD) def test_polygons_holes_and_open_ends_stay_exact(): plan = source(); plan["features"].append(dict(type="Feature", id="bike-1", properties={"class":"road_marking","subtype":"bicycle"}, geometry=dict(type="Polygon",coordinates=[ [[0,0,0],[4,0,0],[4,4,0],[0,4,0],[0,0,0]],[[1,1,0],[1,2,0],[2,2,0],[2,1,0],[1,1,0]]]))) result = adapt(plan) assert [f["geometry"] for f in result["features"]] == [f["geometry"] for f in plan["features"]] def test_systematic_plan_is_lossless_and_keeps_observation_authority(): plan = source() plan["schema_version"] = "missioncore.systematic-plan/v1" result = adapt(plan) assert result["features"][0]["geometry"] == plan["features"][0]["geometry"] assert result["features"][0]["properties"]["route_permission"] == "unassigned" plan["features"][0]["properties"]["authority"] = "navigation" with pytest.raises(ValueError): adapt(plan) def test_unregistered_plan_schema_still_fails(): plan = source() plan["schema_version"] = "missioncore.systematic-plan/v999" with pytest.raises(ValueError, match="qualified automatic Gaussian plan"): adapt(plan)