import ast import os from pathlib import Path import pytest import torch from PIL import Image from src.demo import hf_runtime, hf_ui from src.demo.hf_runtime import ( LOCAL_RGB_CHECKPOINT, SPLAT_TRANSFORM_PACKAGE, BrowserViewerArtifacts, GaussianArtifact, ViewerTemplate, build_standalone_viewer, build_splat_transform_command, build_viewer_template_command, export_filtered_gaussian_ply, install_shared_viewer_assets, load_gaussian_artifact, prepare_viewer_template, resolve_rgb_checkpoint, save_gaussian_artifact, ) from src.demo.hf_ui import ( build_viewer_iframe, build_viewer_preloader, cleanup_request_directories, ) from src.demo.infer_single_image import patch_supersplat_html_viewer_bridge from src.utils.gaussians import Gaussians3D def _gaussians() -> Gaussians3D: return Gaussians3D( mean_vectors=torch.tensor([[[1.0, 2.0, 3.0]]]), singular_values=torch.tensor([[[0.1, 0.2, 0.3]]]), quaternions=torch.tensor([[[1.0, 0.0, 0.0, 0.0]]]), colors=torch.tensor([[[0.4, 0.5, 0.6]]]), opacities=torch.tensor([[0.9]]), covariances=torch.eye(3).reshape(1, 1, 3, 3), ) def test_checkpoint_override_has_highest_priority(tmp_path, monkeypatch) -> None: override = tmp_path / "override.ckpt" override.touch() monkeypatch.setenv("INFINISPLAT_CHECKPOINT", str(override)) assert resolve_rgb_checkpoint() == override def test_repository_checkpoint_is_reused_before_hub(tmp_path, monkeypatch) -> None: checkpoint = tmp_path / "infinisplat_rgb.ckpt" checkpoint.touch() monkeypatch.delenv("INFINISPLAT_CHECKPOINT", raising=False) monkeypatch.setattr(hf_runtime, "LOCAL_RGB_CHECKPOINT", checkpoint) monkeypatch.setattr( hf_runtime, "hf_hub_download", lambda **_: pytest.fail("Hub download should not be called"), ) assert resolve_rgb_checkpoint() == checkpoint def test_checkpoint_falls_back_to_hub(tmp_path, monkeypatch) -> None: downloaded = tmp_path / "downloaded.ckpt" downloaded.touch() monkeypatch.delenv("INFINISPLAT_CHECKPOINT", raising=False) monkeypatch.setattr(hf_runtime, "LOCAL_RGB_CHECKPOINT", tmp_path / "missing.ckpt") monkeypatch.setattr(hf_runtime, "hf_hub_download", lambda **_: str(downloaded)) assert resolve_rgb_checkpoint() == downloaded def test_local_checkpoint_path_matches_repository_layout() -> None: assert LOCAL_RGB_CHECKPOINT == Path("checkpoints/infinisplat_rgb.ckpt").resolve() def test_request_cleanup_removes_only_expired_uuid_directories(tmp_path) -> None: old_request = tmp_path / ("a" * 32) recent_request = tmp_path / ("b" * 32) shared_assets = tmp_path / "_viewer_assets" old_request.mkdir() recent_request.mkdir() shared_assets.mkdir() (old_request / "viewer.sog").write_bytes(b"old") (recent_request / "viewer.sog").write_bytes(b"recent") (shared_assets / "index.js").write_text("shared") os.utime(old_request, (100.0, 100.0)) os.utime(recent_request, (900.0, 900.0)) removed = cleanup_request_directories( output_root=tmp_path, max_age_seconds=500, now=1000.0, ) assert removed == [old_request] assert not old_request.exists() assert recent_request.is_dir() assert shared_assets.is_dir() def test_gaussian_artifact_round_trip(tmp_path) -> None: path = tmp_path / "artifact.pt" expected = GaussianArtifact( gaussians=_gaussians(), focal_length_px=1200.0, image_shape=(1152, 1536), ) save_gaussian_artifact(expected, path) actual = load_gaussian_artifact(path) assert actual.focal_length_px == expected.focal_length_px assert actual.image_shape == expected.image_shape for expected_tensor, actual_tensor in zip(expected.gaussians, actual.gaussians): assert torch.equal(expected_tensor, actual_tensor) def test_filtered_ply_export_removes_spatial_outlier(tmp_path, monkeypatch) -> None: points = torch.cat( [ torch.randn(100, 3) * 0.01, torch.tensor([[10.0, 10.0, 10.0]]), ], dim=0, ).unsqueeze(0) count = points.shape[1] gaussians = Gaussians3D( mean_vectors=points, singular_values=torch.ones(1, count, 3), quaternions=torch.ones(1, count, 4), colors=torch.ones(1, count, 3), opacities=torch.ones(1, count), ) artifact_path = tmp_path / "gaussians.pt" save_gaussian_artifact( GaussianArtifact( gaussians=gaussians, focal_length_px=1200.0, image_shape=(1152, 1536), ), artifact_path, ) captured = {} def fake_save_ply(*, gaussians, path, **_) -> None: captured["gaussians"] = gaussians path.write_bytes(b"ply") monkeypatch.setattr(hf_runtime, "save_ply", fake_save_ply) result = export_filtered_gaussian_ply(artifact_path, tmp_path / "output") assert result.read_bytes() == b"ply" assert captured["gaussians"].mean_vectors.shape[1] == count - 1 assert float(captured["gaussians"].mean_vectors.abs().max()) < 1.0 def test_build_splat_transform_command_is_fixed_and_non_interactive() -> None: command = build_splat_transform_command( scene_ply=Path("scene.ply"), output_sog=Path("viewer.sog"), ) assert command[:4] == [ "npx", "--yes", "--prefer-offline", SPLAT_TRANSFORM_PACKAGE, ] assert SPLAT_TRANSFORM_PACKAGE == "@playcanvas/splat-transform@3.1.6" assert command[4:] == [ "--quiet", "--overwrite", "scene.ply", "--filter-harmonics", "0", "viewer.sog", ] def test_build_viewer_template_command_is_unbundled() -> None: command = build_viewer_template_command( scene_ply=Path("template.ply"), output_html=Path("template.html"), viewer_settings=Path("viewer.json"), ) assert command[:4] == [ "npx", "--yes", "--prefer-offline", SPLAT_TRANSFORM_PACKAGE, ] assert command[4:] == [ "--quiet", "--overwrite", "--unbundled", "--viewer-settings", "viewer.json", "template.ply", "--filter-harmonics", "0", "template.html", ] def test_build_viewer_iframe_uses_gradio_file_route(tmp_path) -> None: viewer_path = tmp_path / "viewer output.html" iframe = build_viewer_iframe(viewer_path) assert '