import hashlib from types import SimpleNamespace import pytest from k1link.simulation.ai_polygon.originals import retain_legacy_original from k1link.simulation.ai_polygon.worlds import WorldStore, write_json from k1link.simulation.gaussian_pipeline_gateway import GaussianPipelineIntegrityError def setup(tmp_path): worlds = WorldStore(tmp_path) wid = "aiworld-" + "a" * 32 root = worlds.root / wid root.mkdir() raw = b"original" (root / "source.ply").write_bytes(raw) sha = hashlib.sha256(raw).hexdigest() write_json( root / "world.json", dict(world_id=wid, status="available", byte_length=len(raw), sha256=sha), ) output = root / "render.sog" output.write_bytes(b"optimized") editor = SimpleNamespace( request=lambda _: dict(key="cache"), get=lambda _: dict(key="cache", status="ready", files={"scene.sog": {}}), asset=lambda *_: (output, hashlib.sha256(b"optimized").hexdigest()), ) source = dict( format="ply", entrypoint="source.ply", total_byte_length=8, bundle_sha256="", members=[dict(upload_id="x", logical_path="source.ply", byte_length=8, sha256=sha)], ) import json # Real connector computes this exact canonical manifest; test uses its serializer. from k1link.simulation.gaussian_pipeline_gateway import _source_bundle_upload # key ordering is format/entrypoint/members, each member logical_path/sha256/byte_length. identity = dict( format="ply", entrypoint="source.ply", members=[dict(logical_path="source.ply", sha256=sha, byte_length=8)], ) source["bundle_sha256"] = hashlib.sha256( json.dumps(identity, separators=(",", ":")).encode() ).hexdigest() bundle = _source_bundle_upload(source) receipt = dict( schema_version="gaussian-pipeline.source-receipt/v1", retention="independent-of-jobs", source_id="src-" + "b" * 64, verified_at_utc="2026-11-16T00:00:00Z", input=dict(kind="folder", source=source), ) provider = SimpleNamespace( endpoint="http://worker", require_source_retention=lambda: None, upload_source_bundle=lambda *_a, **_k: bundle, retain_source=lambda _: receipt, retained_source=lambda r, **_k: r, ) return worlds, wid, root, editor, provider def test_cleanup_requires_verified_original_and_keeps_result_identity(tmp_path): worlds, wid, root, editor, provider = setup(tmp_path) result = retain_legacy_original(worlds, wid, provider, editor) assert result["removed_bytes"] == 8 and not (root / "source.ply").exists() assert (root / "render.sog").read_bytes() == b"optimized" assert worlds.get(wid)["original_storage"]["local_state"] == "remote" assert retain_legacy_original(worlds, wid, provider, editor)["removed_bytes"] == 0 def test_worker_failure_never_removes_only_local_original(tmp_path): worlds, wid, root, editor, provider = setup(tmp_path) def unavailable(*_a, **_k): raise RuntimeError("offline") provider.retained_source = unavailable with pytest.raises(RuntimeError, match="offline"): retain_legacy_original(worlds, wid, provider, editor) assert (root / "source.ply").read_bytes() == b"original" def test_render_corruption_blocks_source_cleanup(tmp_path): worlds, wid, root, editor, provider = setup(tmp_path) (root / "render.sog").write_bytes(b"corrupt") with pytest.raises(GaussianPipelineIntegrityError): retain_legacy_original(worlds, wid, provider, editor) assert (root / "source.ply").exists() def test_original_download_streams_worker_bytes_without_local_source(tmp_path): from fastapi import FastAPI from fastapi.testclient import TestClient from k1link.web.simulation_originals_api import build_simulation_originals_router worlds, wid, root, editor, provider = setup(tmp_path) retain_legacy_original(worlds, wid, provider, editor) closed = [] provider.close = lambda: closed.append(True) provider.stream_retained_file = lambda *_: (chunk for chunk in [b"ori", b"ginal"]) app = FastAPI() app.include_router(build_simulation_originals_router(worlds, lambda: provider)) client = TestClient(app) response = client.get(f"/worlds/{wid}/originals") assert response.status_code == 200 assert response.json()["files"][0]["sha256"] == worlds.get(wid)["sha256"] response = client.get(f"/worlds/{wid}/original", params={"path": "source.ply"}) assert response.content == b"original" assert response.headers["content-disposition"].startswith("attachment;") assert not (root / "source.ply").exists() and closed assert ( client.get(f"/worlds/{wid}/original", params={"path": "../world.json"}).status_code == 404 ) offline = FastAPI() offline.include_router(build_simulation_originals_router(worlds, lambda: None)) assert TestClient(offline).get(f"/worlds/{wid}/originals").status_code == 503 def test_source_attribution_does_not_expand_closed_project_v1_response(tmp_path): from k1link.simulation.projects import SimulationProjectStore from k1link.web.simulation_projects_api import SimulationProjectDocument, SimulationProjectPage store = SimulationProjectStore(tmp_path) row = store.create( name="Test", scene_type="outdoor", source_kind="folder", files=[{"logical_path": "scene.ply", "byte_length": 8}], attribution={"author": "Owner", "license": "Private", "source_url": None}, ) assert store.get(row["project_id"])["attribution"]["author"] == "Owner" assert "attribution" not in SimulationProjectDocument.model_validate(row).model_dump() assert "attribution" not in SimulationProjectPage(projects=[row]).model_dump()["projects"][0]