"""Whole-scene publications never rebase human work or admit raw edits.""" import copy import uuid from concurrent.futures import ThreadPoolExecutor import pytest from test_semantic_edits import change, make_setup from k1link.simulation.ai_polygon.semantic_edits import SemanticEdits def pair(model): raw, automatic = copy.deepcopy(model), copy.deepcopy(model) raw["semantic_stage"] = "raw" automatic["semantic_stage"] = "algorithmic" return raw, automatic def test_publication_keeps_manual_base_export_and_legacy_receipt(tmp_path): world, model, maps, edits, sha = make_setup(tmp_path) legacy = edits.save(world, change(sha)) before = edits.export(world, legacy["revision"]) raw, automatic = pair(model) automatic["features"][0]["geometry"]["coordinates"][1][1] = -1 published = maps.publish_pair(world, raw, automatic) assert maps.get(world) == automatic assert edits.get(world)["edited"] == legacy["edited"] assert edits.export(world, legacy["revision"]) == before assert edits.get(world, "raw")["model"] == raw assert edits.get(world, "algorithmic")["revision"] is None assert edits.versions(world)["manual"][0]["branch_id"] is None assert published["raw_sha256"] != published["sha256"] receipt = dict( state="received", package_sha256=before["sha256"], dataset_sha256=None, sample_count=0 ) assert edits.receipt(world, legacy["revision"], receipt) == receipt assert edits.export(world, legacy["revision"]) == before def test_new_branch_does_not_inherit_manual_and_pins_stale_display(tmp_path): world, model, maps, edits, sha = make_setup(tmp_path) legacy = edits.save(world, change(sha)) raw, auto = pair(model) maps.publish_pair(world, raw, auto) branch = str(uuid.uuid4()) body = {**change(sha), "branch_id": branch} saved = edits.save(world, body) assert saved["branch_id"] == branch assert saved["model_sha256"] == sha assert len(saved["history"]) == 1 assert edits.get(world)["revision"] == legacy["revision"] assert edits.get(world, "manual", branch)["edited"] == saved["edited"] reopened = SemanticEdits(tmp_path, maps) assert reopened.save(world, body) == saved assert len(reopened.versions(world)["manual"]) == 2 exported = reopened.export(world, saved["revision"]) assert exported["package"]["revisions"][0]["branch_id"] == branch assert any( row.get("branch_id") == branch for row in edits.pending([{**world, "world_id": "world"}]) ) def test_raw_cannot_be_saved_or_used_as_a_draft_base(tmp_path): world, model, maps, edits, _ = make_setup(tmp_path) raw, auto = pair(model) pointer = maps.publish_pair(world, raw, auto) view = edits.get(world, "raw") assert view["read_only"] is True and "edited" not in view for branch in [None, str(uuid.uuid4())]: body = change(pointer["raw_sha256"]) if branch: body["branch_id"] = branch with pytest.raises(ValueError, match="только для просмотра"): edits.save(world, body) assert not edits._path(world, branch).exists() with pytest.raises(ValueError, match="Raw"): maps.save(world, raw) assert maps.versions(world) == pointer def test_unknown_branch_and_corrupt_latest_do_not_fallback(tmp_path): world, _, maps, edits, sha = make_setup(tmp_path) branch = str(uuid.uuid4()) with pytest.raises(FileNotFoundError): edits.get(world, "manual", branch) assert not edits._path(world, branch).exists() saved = edits.save(world, {**change(sha), "branch_id": branch}) export = edits.export(world, saved["revision"]) (maps.directory(world) / "latest.json").write_text("broken") assert edits.get(world, "manual", branch)["edited"] == saved["edited"] assert edits.export(world, saved["revision"]) == export assert edits.versions(world)["algorithmic_sha256"] is None (maps.directory(world) / f"{sha}.json").write_text("broken") with pytest.raises(ValueError): edits.get(world, "manual", branch) def test_pair_failure_preserves_pointer_and_ordinary_save_clears_raw(tmp_path, monkeypatch): world, model, maps, _, _ = make_setup(tmp_path) raw, auto = pair(model) previous = maps.versions(world) original = maps._write_immutable def fail_second(world, document, sha): if document.get("semantic_stage") == "algorithmic": raise OSError("interrupted") return original(world, document, sha) monkeypatch.setattr(maps, "_write_immutable", fail_second) with pytest.raises(OSError): maps.publish_pair(world, raw, auto) assert maps.versions(world) == previous monkeypatch.setattr(maps, "_write_immutable", original) maps.publish_pair(world, raw, auto) maps.save(world, model) assert "raw_sha256" not in maps.versions(world) def test_branch_concurrency_and_path_validation(tmp_path): world, _, _, edits, sha = make_setup(tmp_path) branch = str(uuid.uuid4()) def save(i): try: return edits.save( world, {**change(sha, request=f"parallel-branch-{i:04}"), "branch_id": branch} ) except RuntimeError: return None with ThreadPoolExecutor(max_workers=2) as pool: results = list(pool.map(save, range(2))) assert sum(result is not None for result in results) == 1 for invalid in ["../../other", "", "legacy", 4]: with pytest.raises(ValueError): edits.get(world, "manual", invalid) def test_dense_raw_has_bounded_display_budget_and_does_not_expand_editable_maps(tmp_path): from k1link.simulation.ai_polygon.urban_maps import validate_map world, model, _, _, _ = make_setup(tmp_path) raw, auto = pair(model) raw["coverage"]["cell_m"] = 60 / 4096 raw["features"] = [{**model["features"][0], "id": f"raw-{i}"} for i in range(20001)] assert validate_map(raw, world) is raw auto["features"] = raw["features"] with pytest.raises(ValueError, match="feature count"): validate_map(auto, world) raw["features"] = raw["features"] * 3 with pytest.raises(ValueError, match="feature count"): validate_map(raw, world) raw["features"] = [{**model["features"][0], "geometry": { "type": "LineString", "coordinates": [[0, 0, 1]] * 300001}}] assert validate_map(raw, world) is raw auto["features"] = raw["features"] with pytest.raises(ValueError, match="vertex limit"): validate_map(auto, world) raw["features"][0]["geometry"]["coordinates"] = [[0, 0, 1]] * 400001 with pytest.raises(ValueError, match="vertex limit"): validate_map(raw, world)