import hashlib import json from types import SimpleNamespace import pytest from fastapi import FastAPI from fastapi.testclient import TestClient from k1link.laboratory.local_presentations import LocalPresentationError, LocalPresentations from k1link.web.local_storage_api import build_local_storage_router def fixture(tmp_path): now = [1000.0] store = LocalPresentations(tmp_path, clock=lambda: now[0]) store.root.mkdir() path = store.root / ("a" * 64 + ".replay.rrd") path.write_bytes(b"RRF2-complete-video-and-spatial-recording") artifact = SimpleNamespace( path=path, sha256=hashlib.sha256(path.read_bytes()).hexdigest(), byte_length=path.stat().st_size, ) sidecar = store.root / ("b" * 64 + ".json") sidecar.write_text(json.dumps({"sha256": artifact.sha256})) store.register("run", artifact, sidecar) return store, now, artifact, sidecar def test_persistence_unpin_starts_full_retention_and_catalog_survives(tmp_path): store, now, artifact, sidecar = fixture(tmp_path) store.policy(7) store.pin("run", True) now[0] += 40 * 86400 store.prune() assert artifact.path.exists() reloaded = LocalPresentations(tmp_path, clock=lambda: now[0]) assert reloaded.snapshot()["entries"]["run"]["pinned"] reloaded.pin("run", False) reloaded.prune() assert artifact.path.exists() now[0] += 7 * 86400 reloaded.prune() assert not artifact.path.exists() and not sidecar.exists() assert reloaded.snapshot()["entries"]["run"]["local"] is False def test_no_purge_of_unknown_files_and_changed_bytes(tmp_path): store, now, artifact, _ = fixture(tmp_path) unknown = store.root / "original-unregistered.rrd" unknown.write_bytes(b"valuable") artifact.path.write_bytes(b"changed") now[0] += 100 * 86400 store.prune() assert unknown.read_bytes() == b"valuable" assert artifact.path.read_bytes() == b"changed" with pytest.raises(LocalPresentationError): store.pin("run", True) def test_playback_lease_and_pin_protect_delete_and_ttl(tmp_path): store, now, artifact, _ = fixture(tmp_path) with store.use("run"): with pytest.raises(LocalPresentationError): store.remove("run") now[0] += 100 * 86400 store.prune() assert artifact.path.exists() store.pin("run", True) with pytest.raises(LocalPresentationError): store.remove("run") def test_symlink_is_preserved_without_traversal(tmp_path): store, _, artifact, _ = fixture(tmp_path) victim = tmp_path / "source" victim.write_bytes(artifact.path.read_bytes()) artifact.path.unlink() artifact.path.symlink_to(victim) with pytest.raises(LocalPresentationError): store.remove("run") assert victim.exists() def test_shared_file_is_retained_for_other_result(tmp_path): store, _, artifact, sidecar = fixture(tmp_path) store.register("other", artifact, sidecar) store.pin("other", True) store.remove("run") assert artifact.path.exists() assert store.snapshot()["entries"]["other"]["local"] def test_invalid_policy_and_pin_missing_copy_fail_without_mutation(tmp_path): store, _, _, _ = fixture(tmp_path) app = FastAPI() app.include_router(build_local_storage_router(store)) with TestClient(app) as client: root = "/api/v1/storage/laboratory" for value in (0, 366, "7", True): assert client.put(root + "/policy", json={"retention_days": value}).status_code == 422 assert ( client.put(root + "/policy", json={"retention_days": 7}).json()["retention_days"] == 7 ) assert client.put(root + "/copies/missing/pin", json={"pinned": True}).status_code == 409 assert client.delete(root + "/copies/run").status_code == 200 assert not client.get(root).json()["entries"]["run"]["local"] def test_explicit_lab_deletion_removes_pin_but_not_active_playback(tmp_path): store, _, artifact, _ = fixture(tmp_path) store.pin("run", True) with store.use("run"), pytest.raises(LocalPresentationError): store.remove("run", delete_lab=True) store.remove("run", delete_lab=True) assert not artifact.path.exists() assert "run" not in store.snapshot()["entries"]