import json from types import SimpleNamespace import pytest from k1link.simulation.gaussian_pipeline_gateway import GaussianPipelineGatewayError from k1link.simulation.projects import ( SimulationProjectNotFoundError, SimulationProjectService, SimulationProjectStore, ) def fixture(tmp_path): store = SimulationProjectStore(tmp_path) row = store.create( name="Disposable", scene_type="outdoor", source_kind="folder", files=[dict(logical_path="a.ply", byte_length=8)], ) receipt = dict(source_id="src-" + "a" * 64) store.update_source_storage( row["project_id"], storage=dict(provider_endpoint="http://worker", receipt=receipt, local_state="remote"), ) store.update_processing( row["project_id"], status="processing", provider_job_id="gsp-20261116000000-1234abcd" ) calls = [] provider = SimpleNamespace( endpoint="http://worker", close=lambda: None, delete_job=lambda id: calls.append(("job", id)), delete_retained_source=lambda r: calls.append(("source", r["source_id"])), ) service = SimulationProjectService(store, lambda: provider) service.deletions.start = lambda: None return store, row["project_id"], provider, service, calls def test_source_failure_keeps_row_and_recovery_completes_after_restart(tmp_path): store, pid, provider, service, calls = fixture(tmp_path) good = provider.delete_retained_source def offline(_): raise GaussianPipelineGatewayError("Worker offline") provider.delete_retained_source = offline service.deletions.request(pid) service.deletions.attempt(service.deletions.list()[0]) assert store.get(pid) and service.deletions.list()[0]["state"] == "waiting" assert calls[0][0] == "job" provider.delete_retained_source = good recovered = SimulationProjectService(store, lambda: provider) recovered.deletions.attempt(recovered.deletions.list()[0]) assert not recovered.deletions.list() with pytest.raises(SimulationProjectNotFoundError): store.get(pid) assert calls[-1][0] == "source" def test_shared_source_and_job_survive_until_last_project_owner(tmp_path): store, pid, provider, service, calls = fixture(tmp_path) second = store.create( name="Second", scene_type="outdoor", source_kind="folder", files=[dict(logical_path="a.ply", byte_length=8)], ) first = store.get(pid) store.update_source_storage(second["project_id"], storage=first["source"]["storage"]) store.update_processing( second["project_id"], status="processing", provider_job_id=first["provider"]["job_id"] ) service.delete(pid) assert calls == [] and store.get(second["project_id"]) service.delete(second["project_id"]) assert [call[0] for call in calls] == ["job", "source"] def test_dependency_blocks_deletion_instead_of_orphaning_run(tmp_path): store, pid, provider, service, calls = fixture(tmp_path) run = tmp_path / "ai-polygon/runs/run" run.mkdir(parents=True) (run / "run.json").write_text(json.dumps({"world_id": pid.replace("sim-", "aiworld-")})) with pytest.raises(RuntimeError, match="сохранённых заданиях"): service.deletions.request(pid) assert not calls and store.get(pid) def test_ui_delete_endpoint_persists_intent_without_waiting_for_worker(tmp_path): from fastapi import FastAPI from fastapi.testclient import TestClient from k1link.web.simulation_projects_api import build_simulation_projects_router store, pid, provider, service, calls = fixture(tmp_path) app = FastAPI() app.include_router(build_simulation_projects_router(store=store, service=service)) client = TestClient(app) assert client.delete("/api/v1/simulation-worlds/projects/" + pid).status_code == 202 assert ( client.get("/api/v1/simulation-worlds/deletions").json()["deletions"][0]["project_id"] == pid ) assert store.get(pid) and not calls def test_prepared_archive_members_and_original_are_both_removed(tmp_path): store, pid, provider, service, calls = fixture(tmp_path) store.update_source_storage(pid, prepared_receipt=dict(source_id="src-" + "b" * 64)) service.delete(pid) assert [call[0] for call in calls] == ["job", "source", "source"] assert calls[1][1] == "src-" + "b" * 64 def test_large_unrelated_evidence_does_not_block_deletion(tmp_path): store, pid, provider, service, calls = fixture(tmp_path) evidence = tmp_path / "ai-polygon/urban-maps/other.json" evidence.parent.mkdir(parents=True) evidence.write_text('{"geometry":"' + 'x' * (9 * 1024**2) + '"}') service.delete(pid) assert [call[0] for call in calls] == ["job", "source"] def test_dependency_scan_finds_identity_across_chunk_boundary(tmp_path): from k1link.simulation.project_deletions import contains_identity identity = "aiworld-" + "f" * 32 evidence = tmp_path / "large.json" evidence.write_bytes(b"x" * (64 * 1024 - 10) + identity.encode() + b"x") assert contains_identity(evidence, identity, "sim-" + "f" * 32) def test_deleting_project_cannot_be_requeued(tmp_path): store, pid, provider, service, calls = fixture(tmp_path) service.deletions.request(pid) with pytest.raises(RuntimeError, match="Удаление локации уже начато"): service.enqueue(pid) assert not calls