import json import sys from pathlib import Path import pytest ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "simulation/ai-polygon")) import infrastructure_pipeline as pipeline # noqa: E402 - canonical worker modules def definition(): return { "groups": [ { "group": "base", "stages": ["ortho_semantic_survey"], "files": {"ortho_semantic_survey.py": "stage.py"}, "stage_outputs": {"ortho_semantic_survey": {"/layers": "full_plan"}}, }, { "group": "full_plan", "stages": ["assemble_layers"], "files": {"assemble_layers.py": "assemble.py"}, }, ], "aliases": {"full_plan": "fullplan"}, } def test_full_runner_passes_joint_outputs_to_original_consumer(tmp_path, monkeypatch): module = tmp_path / "module" (module / "pipeline").mkdir(parents=True) (module / "pipeline/definition.json").write_text(json.dumps(definition())) for name in ["config-ortho.json", "config-mv.json", "worker-profile.json"]: (module / "pipeline" / name).write_text(json.dumps({"models": {}})) (module / "stage.py").write_text("pass\n") (module / "assemble.py").write_text("pass\n") package = tmp_path / "input" package.mkdir() (package / "poses.json").write_text("{}") output = tmp_path / "output" output.mkdir() calls = [] def run(marker, phase, image, mounts, cmd, out, pulse, timeout, **kwargs): calls.append((phase, image, mounts, kwargs)) m = {alias: (path, ro) for path, alias, ro in mounts} if "semantic" in phase: assert image == "semantic-image" and kwargs["gpu"] assert m["/run"] == (output / "ortho", False) assert m["/layers"] == (output / "full_plan", False) assert m["/input"] == (package, True) (m["/layers"][0] / "evidence").mkdir() (m["/layers"][0] / "evidence/report.json").write_text('{"complete":true}') else: assert image == "mapper-image" and not kwargs["gpu"] assert json.loads((out / "evidence/report.json").read_text())["complete"] (out / "full-map.json").write_text('{"features":[]}') monkeypatch.setattr(pipeline, "run_container", run) result, snapshot = pipeline.run_pipeline( package, {"sha256": "a" * 64, "poses_sha256": "b" * 64}, output, tmp_path / "marker", { "maximum_seconds": 1000, "maximum_stage_seconds": 100, "semantic_model": "assets/model", "semantic_image": "semantic-image", "mapper_image": "mapper-image", }, tmp_path / "runtime", lambda: None, module=module, ) assert result == output / "full_plan/full-map.json" and len(snapshot) == 64 assert len(calls) == 2 and (output / "full_plan/group-inventory.json").is_file() @pytest.mark.parametrize("target", ["base", "missing", "../private", "/input"]) def test_invalid_target_refused(target): d = definition() d["groups"][0]["stage_outputs"]["ortho_semantic_survey"]["/layers"] = target with pytest.raises(ValueError): pipeline.validate_stage_outputs(d) @pytest.mark.parametrize("alias", ["/input", "/fullplan", "/layers/escape", "../layers", "/survey"]) def test_alias_cannot_shadow_inputs(alias): d = definition() d["groups"][0]["stage_outputs"]["ortho_semantic_survey"] = {alias: "full_plan"} with pytest.raises(ValueError): pipeline.validate_stage_outputs(d) def test_escape_through_existing_link_refused(tmp_path): output = tmp_path / "owned" outside = tmp_path / "outside" output.mkdir() outside.mkdir() (output / "full_plan").symlink_to(outside, target_is_directory=True) with pytest.raises(ValueError, match="escapes"): pipeline.stage_output_mounts(output, definition()["groups"][0], "ortho_semantic_survey") assert not list(outside.iterdir()) def test_unique_publisher_required(): d = definition() g = d["groups"][0] g["stages"].append("second") g["stage_outputs"]["second"] = {"/other": "full_plan"} with pytest.raises(ValueError, match="multiple"): pipeline.validate_stage_outputs(d) def test_shipped_graph_uses_one_semantic_producer_with_measured_duration(): from k1link.simulation.ai_polygon.infrastructure_progress import plan stages, _ = plan() assert len(stages) == 90 names = [r["stage"] for r in stages] assert names.count("ortho_semantic_survey") == 1 assert "ortho_segment" not in names and "semantic_layers" not in names row = next(r for r in stages if r["stage"] == "ortho_semantic_survey") assert row["measured"] and row["seconds"] == 251.484