"""Location settings preserve source files, geometry identity and run evidence.""" import pytest from fastapi import FastAPI from fastapi.testclient import TestClient from test_ai_polygon import make_world from test_ai_polygon_realtime import realtime as _realtime from test_simulation_location_library import project_fixture from k1link.simulation.ai_polygon.contracts import RunCreate, WorkerWorldCreate, WorldSettings from k1link.simulation.ai_polygon.world_library import WorldLibrary from k1link.simulation.ai_polygon.worlds import WorldStore from k1link.web.ai_polygon_api import build_ai_polygon_router realtime = _realtime API = "/api/v1/ai-polygon" def client_for(root): app = FastAPI() app.include_router(build_ai_polygon_router(root)) return TestClient(app) @pytest.mark.parametrize("kind", ["local", "worker", "project"]) def test_rename_remove_restore_survives_restart_without_rewriting_source(tmp_path, kind): worlds = WorldStore(tmp_path) if kind == "project": worlds, project = project_fixture(tmp_path) row = worlds.projects.world(project) elif kind == "worker": request = WorkerWorldCreate(name="Worker scan", filename="scan.ply", byte_length=1024, author="test", license="CC0", sha256="b" * 64, collider_sha256="c" * 64, splat_count=10, settings=WorldSettings(prepared=True)) row = worlds.register_worker_asset(request, "worker-006") else: row = make_world(worlds) identity = row["world_id"] original = worlds.get(identity) files = {p: p.read_bytes() for p in tmp_path.rglob("*") if p.is_file()} with client_for(tmp_path) as client: response = client.patch(f"{API}/worlds/{identity}", json={"display_name":" Новое имя "}) assert response.status_code == 200 assert response.json()["name"] == "Новое имя" assert response.json()["sha256"] == original["sha256"] assert client.patch(f"{API}/worlds/{identity}", json={"removed":True}).status_code == 200 with client_for(tmp_path) as client: catalog = client.get(f"{API}/catalog").json() assert not catalog["worlds"] assert catalog["removed_worlds"][0]["world_id"] == identity assert catalog["removed_worlds"][0]["name"] == "Новое имя" assert client.patch(f"{API}/worlds/{identity}", json={"removed":False}).status_code == 200 assert client.get(f"{API}/catalog").json()["worlds"][0]["name"] == "Новое имя" assert worlds.get(identity) == original for p, before in files.items(): assert p.read_bytes() == before, p if kind == "worker": assert worlds.register_worker_asset(request, "worker-006")["world_id"] == identity assert len(worlds.list()) == 1 def test_invalid_mutations_and_unknown_scan_have_no_effect(tmp_path): worlds = WorldStore(tmp_path) row = make_world(worlds) with client_for(tmp_path) as client: for changes in [{}, {"display_name":" "}, {"display_name":"x"*121}, {"display_name":"bad\x00name"}, {"removed":"yes"}, {"sha256":"bad"}]: assert client.patch(f"{API}/worlds/{row['world_id']}", json=changes).status_code == 422 assert client.patch(f"{API}/worlds/aiworld-{'f'*32}", json={"removed":True}).status_code == 404 assert WorldLibrary(worlds).get(row["world_id"]) == row def test_active_world_cannot_be_removed_and_removed_world_cannot_start(realtime, tmp_path): store, hello, run = realtime identity = run["world"]["world_id"] with client_for(tmp_path) as client: assert client.patch(f"{API}/worlds/{identity}", json={"removed":True}).status_code == 409 assert client.patch(f"{API}/worlds/{identity}", json={"display_name":"Renamed"}).status_code == 200 assert store.get(run["run_id"])["world"]["name"] == run["world"]["name"] store.finish(run["run_id"], hello.instance_id, "stopped", "") WorldLibrary(store.worlds).update(identity, {"removed":True}) with pytest.raises(RuntimeError, match="удалена"): store.start(RunCreate(world_id=identity, clock="realtime"), "removed-location") assert store.active is None def test_metadata_cannot_follow_symlink(tmp_path): worlds = WorldStore(tmp_path) row = make_world(worlds) library = WorldLibrary(worlds) outside = tmp_path / "untouched.json" outside.write_text('{"name":"original"}') library.metadata.path(row["world_id"]).symlink_to(outside) with pytest.raises(ValueError, match="regular"): library.update(row["world_id"], {"removed":True}) assert outside.read_text() == '{"name":"original"}'