Zum Inhalt

pipeline/tests/test_viewer.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# SPDX-License-Identifier: AGPL-3.0-or-later

"""Tests for viewer URL builder, open_in_viewer behaviour, and config roundtrip."""

from __future__ import annotations

import http.client
from pathlib import Path
from unittest.mock import patch

import pytest

from autosplat.config import ViewerConfig, load_config
from autosplat.viewer import (
    PLAYCANVAS_VIEWER_URL,
    SUPERSPLAT_URL,
    _build_viewer_url,
    open_in_viewer,
    serve_directory,
)

# ─── Helpers ─────────────────────────────────────────────────────────────────


def _pick_free_port() -> int:
    """Bind to 0 → kernel picks a free high port; close, return it.

    Slightly race-y in theory (the port could be claimed between close and
    re-bind), but `_ReuseAddrTCPServer.allow_reuse_address` handles the case
    in practice — and ViewerConfig.local_http_port forbids 0 directly so we
    can't pass through.
    """
    import socket

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(("127.0.0.1", 0))
        return s.getsockname()[1]


def _make_cfg(**overrides) -> ViewerConfig:
    """Return a ViewerConfig with sensible defaults, accepting field overrides."""
    defaults = dict(
        auto_open=True,
        local_http_port=8765,
        target="supersplat",
        supersplat_local_port=3000,
        supersplat_dist_path=Path("target/supersplat/dist"),
    )
    defaults.update(overrides)
    return ViewerConfig(**defaults)


# ─── _build_viewer_url ────────────────────────────────────────────────────────


def test_build_url_supersplat_local() -> None:
    cfg = _make_cfg(target="supersplat-local", supersplat_local_port=3000, local_http_port=8765)
    ply = Path("/some/dir/scene.ply")
    url = _build_viewer_url(cfg, ply)
    assert url == "http://localhost:3000?load=http://127.0.0.1:8765/scene.ply"


def test_build_url_supersplat_local_custom_ports() -> None:
    cfg = _make_cfg(target="supersplat-local", supersplat_local_port=4000, local_http_port=9000)
    ply = Path("/run/output.ply")
    url = _build_viewer_url(cfg, ply)
    assert url == "http://localhost:4000?load=http://127.0.0.1:9000/output.ply"


def test_build_url_supersplat_remote() -> None:
    cfg = _make_cfg(target="supersplat")
    ply = Path("/some/dir/scene.ply")
    url = _build_viewer_url(cfg, ply)
    assert "playcanvas.com" in url
    assert SUPERSPLAT_URL in url or url.startswith(SUPERSPLAT_URL)
    # The remote PLY URL must be encoded in the query param
    assert "scene.ply" in url


def test_build_url_supersplat_remote_has_load_param() -> None:
    cfg = _make_cfg(target="supersplat")
    ply = Path("/data/capture/splat.ply")
    url = _build_viewer_url(cfg, ply)
    assert url.startswith(f"{SUPERSPLAT_URL}?load=")


def test_build_url_playcanvas() -> None:
    cfg = _make_cfg(target="playcanvas")
    ply = Path("/data/capture/splat.ply")
    url = _build_viewer_url(cfg, ply)
    assert "playcanvas.com/viewer" in url
    assert url.startswith(f"{PLAYCANVAS_VIEWER_URL}?load=")


# ─── open_in_viewer behaviour ─────────────────────────────────────────────────


def test_open_in_viewer_remote_supersplat_logs_deprecation_warning(
    tmp_path: Path,
) -> None:
    """v1.4.5 — target='supersplat' (remote, HTTPS) emits a deprecation
    warning pointing the user at the supersplat-local default. Hooked into
    the existing block-with-server path so the warning fires only when the
    code path is actually reached (not on auto_open=False / target=none)."""
    import threading

    ply = tmp_path / "scene.ply"
    ply.write_bytes(b"ply")
    cfg = _make_cfg(target="supersplat", local_http_port=_pick_free_port())

    stop = threading.Event()
    stop.set()

    with (
        patch("autosplat.viewer.webbrowser.open"),
        patch("autosplat.viewer.logger") as mock_logger,
    ):
        open_in_viewer(ply, cfg, stop_event=stop)

    # `logger.warning("viewer.remote_supersplat_deprecated", ...)` should fire
    warning_calls = [
        c
        for c in mock_logger.warning.call_args_list
        if c.args and c.args[0] == "viewer.remote_supersplat_deprecated"
    ]
    assert len(warning_calls) == 1, mock_logger.warning.call_args_list


def test_open_in_viewer_supersplat_local_no_deprecation_warning(
    tmp_path: Path,
) -> None:
    """Sanity: supersplat-local path must NOT fire the deprecation warning."""
    import threading

    ply = tmp_path / "scene.ply"
    ply.write_bytes(b"ply")
    dist = tmp_path / "dist"
    dist.mkdir()
    (dist / "index.html").write_text("ok", encoding="utf-8")
    cfg = _make_cfg(
        target="supersplat-local",
        supersplat_dist_path=dist,
        local_http_port=_pick_free_port(),
        supersplat_local_port=_pick_free_port(),
    )
    stop = threading.Event()
    stop.set()

    with (
        patch("autosplat.viewer.webbrowser.open"),
        patch("autosplat.viewer.logger") as mock_logger,
    ):
        open_in_viewer(ply, cfg, stop_event=stop)

    warning_calls = [
        c
        for c in mock_logger.warning.call_args_list
        if c.args and c.args[0] == "viewer.remote_supersplat_deprecated"
    ]
    assert warning_calls == []


def test_open_in_viewer_supersplat_local_no_browser_when_dist_missing(
    tmp_path: Path,
) -> None:
    """v1.4.4 — supersplat-local without a built dist falls back to a
    console hint and does NOT open a browser."""
    ply = tmp_path / "scene.ply"
    ply.write_bytes(b"ply")
    # Point dist_path at a tmp dir that doesn't have index.html
    cfg = _make_cfg(target="supersplat-local", supersplat_dist_path=tmp_path / "no_dist")

    with patch("autosplat.viewer.webbrowser.open") as mock_open:
        open_in_viewer(ply, cfg)

    mock_open.assert_not_called()


def test_open_in_viewer_supersplat_local_opens_local_servers_when_dist_present(
    tmp_path: Path,
) -> None:
    """v1.4.4 — supersplat-local WITH a built dist starts both local
    servers, opens the browser at the local SuperSplat URL with ?load=
    pointing at the local PLY server, and blocks until stop_event is set."""
    import threading

    ply = tmp_path / "scene.ply"
    ply.write_bytes(b"\x00ply")

    # Fake "built" dist
    dist = tmp_path / "dist"
    dist.mkdir()
    (dist / "index.html").write_text("<html>fake supersplat</html>", encoding="utf-8")

    cfg = _make_cfg(
        target="supersplat-local",
        supersplat_dist_path=dist,
        local_http_port=_pick_free_port(),
        supersplat_local_port=_pick_free_port(),
    )

    stop = threading.Event()
    stop.set()  # don't actually block

    with patch("autosplat.viewer.webbrowser.open") as mock_open:
        open_in_viewer(ply, cfg, stop_event=stop)

    mock_open.assert_called_once()
    called = mock_open.call_args[0][0]
    # Local SuperSplat URL with ?load=<local PLY URL>
    assert called.startswith("http://127.0.0.1:")
    assert "?load=http://127.0.0.1:" in called
    assert "/scene.ply" in called


def test_open_in_viewer_auto_open_false_no_browser(tmp_path: Path) -> None:
    ply = tmp_path / "scene.ply"
    ply.write_bytes(b"ply")
    cfg = _make_cfg(auto_open=False, target="supersplat")

    with patch("autosplat.viewer.webbrowser.open") as mock_open:
        open_in_viewer(ply, cfg)

    mock_open.assert_not_called()


def test_open_in_viewer_target_none_no_browser(tmp_path: Path) -> None:
    ply = tmp_path / "scene.ply"
    ply.write_bytes(b"ply")
    cfg = _make_cfg(target="none")

    with patch("autosplat.viewer.webbrowser.open") as mock_open:
        open_in_viewer(ply, cfg)

    mock_open.assert_not_called()


def test_open_in_viewer_supersplat_opens_browser_and_serves_ply(tmp_path: Path) -> None:
    """v1.4.2: open_in_viewer must (a) open browser at viewer URL,
    (b) actually serve the PLY on local_http_port so SuperSplat's
    ?load= fetch resolves, (c) block until stop_event is set."""
    import threading

    ply = tmp_path / "scene.ply"
    ply.write_bytes(b"ply")
    cfg = _make_cfg(target="supersplat", local_http_port=_pick_free_port())

    stop = threading.Event()
    stop.set()  # don't actually block

    from contextlib import contextmanager as _ctx

    served_paths: list[str] = []
    real_serve_directory = serve_directory

    @_ctx
    def tracking_serve(directory, port):
        with real_serve_directory(directory, port) as base:
            served_paths.append(f"{base}/{ply.name}")
            yield base

    with (
        patch("autosplat.viewer.webbrowser.open") as mock_open,
        patch("autosplat.viewer.serve_directory", tracking_serve),
    ):
        open_in_viewer(ply, cfg, stop_event=stop)

    mock_open.assert_called_once()
    called_url: str = mock_open.call_args[0][0]
    assert "playcanvas.com" in called_url
    # The ply server was actually started — its base-url appears in the
    # tracking list. Without the v1.4.2 fix this list would be empty.
    assert len(served_paths) == 1


def test_open_in_viewer_serves_ply_reachable_via_http(tmp_path: Path) -> None:
    """End-to-end: the PLY is actually fetchable from the served URL during
    the block (otherwise SuperSplat's ?load= would silently fail like
    pre-v1.4.2)."""
    import threading
    import urllib.parse
    from urllib.request import urlopen

    ply = tmp_path / "scene.ply"
    ply.write_bytes(b"\x00\x01\x02ply-content")
    cfg = _make_cfg(target="supersplat", local_http_port=_pick_free_port())

    stop = threading.Event()
    fetched: dict[str, bytes] = {}

    def consumer() -> None:
        # Sleep briefly so the server is up before we hit it
        import time as _t

        _t.sleep(0.2)
        # Recover the actual ply URL from mock_open call
        url = mock_open.call_args[0][0]
        parsed = urllib.parse.parse_qs(urllib.parse.urlsplit(url).query)
        ply_url = urllib.parse.unquote(parsed["load"][0])
        fetched["body"] = urlopen(ply_url).read()
        stop.set()

    with patch("autosplat.viewer.webbrowser.open") as mock_open:
        consumer_thread = threading.Thread(target=consumer, daemon=True)
        consumer_thread.start()
        open_in_viewer(ply, cfg, stop_event=stop)
        consumer_thread.join(timeout=5)

    assert fetched["body"] == b"\x00\x01\x02ply-content"


def test_open_in_viewer_missing_ply_no_browser(tmp_path: Path) -> None:
    ply = tmp_path / "nonexistent.ply"
    cfg = _make_cfg(target="supersplat")

    with patch("autosplat.viewer.webbrowser.open") as mock_open:
        open_in_viewer(ply, cfg)

    mock_open.assert_not_called()


# ─── Config roundtrip ─────────────────────────────────────────────────────────


def test_viewer_config_supersplat_local_port_default() -> None:
    cfg = load_config(include_xdg=False)
    assert cfg.viewer.supersplat_local_port == 3000


def test_viewer_config_supersplat_dist_path_default() -> None:
    from autosplat import resources

    cfg = load_config(include_xdg=False)
    assert cfg.viewer.supersplat_dist_path == resources.bundled_path("supersplat")


def test_viewer_config_supersplat_local_port_override(tmp_path: Path) -> None:
    user = tmp_path / "u.toml"
    user.write_text(
        """
[viewer]
supersplat_local_port = 4321
""",
        encoding="utf-8",
    )
    cfg = load_config(user_config_path=user, include_xdg=False)
    assert cfg.viewer.supersplat_local_port == 4321
    # Other viewer fields remain at defaults
    assert cfg.viewer.local_http_port == 8765


def test_viewer_config_supersplat_dist_path_override(tmp_path: Path) -> None:
    user = tmp_path / "u.toml"
    user.write_text(
        """
[viewer]
supersplat_dist_path = "/opt/supersplat/dist"
""",
        encoding="utf-8",
    )
    cfg = load_config(user_config_path=user, include_xdg=False)
    assert cfg.viewer.supersplat_dist_path == Path("/opt/supersplat/dist")


def test_viewer_config_target_accepts_supersplat_local(tmp_path: Path) -> None:
    user = tmp_path / "u.toml"
    user.write_text(
        """
[viewer]
target = "supersplat-local"
""",
        encoding="utf-8",
    )
    cfg = load_config(user_config_path=user, include_xdg=False)
    assert cfg.viewer.target == "supersplat-local"


def test_viewer_config_invalid_port_rejected() -> None:
    with pytest.raises(Exception):
        _make_cfg(supersplat_local_port=80)  # below ge=1024


# ─── serve_supersplat_local + _find_ply ──────────────────────────────────────


def test_serve_directory_sends_cors_header(tmp_path: Path) -> None:
    """serve_directory must include Access-Control-Allow-Origin: * so SuperSplat
    on :3000 can fetch the PLY from :8765 without CORS block."""
    (tmp_path / "test.ply").write_bytes(b"ply\nformat ascii 1.0\nend_header\n")
    with serve_directory(tmp_path, port=0) as base_url:
        port = int(base_url.rsplit(":", 1)[-1])
        conn = http.client.HTTPConnection("127.0.0.1", port)
        conn.request("GET", "/test.ply")
        res = conn.getresponse()
        assert res.status == 200
        assert res.getheader("Access-Control-Allow-Origin") == "*"


def test_serve_supersplat_local_yields_both_urls(tmp_path: Path) -> None:
    """Both servers start and URLs are returned."""
    dist = tmp_path / "dist"
    dist.mkdir()
    (dist / "index.html").write_text("<html/>")
    ply_dir = tmp_path / "capture"
    ply_dir.mkdir()
    (ply_dir / "scene.ply").write_bytes(b"ply\n")

    from autosplat.viewer import serve_supersplat_local

    with serve_supersplat_local(dist, 0, ply_dir, 0) as urls:
        # port 0 = OS-assigned; servers are running
        assert "supersplat" in urls
        assert "ply" in urls
        assert urls["supersplat"].startswith("http://127.0.0.1:")
        assert urls["ply"].startswith("http://127.0.0.1:")


def test_find_ply_direct(tmp_path: Path) -> None:
    (tmp_path / "scene.ply").write_bytes(b"ply\n")
    from autosplat.cli import _find_ply

    assert _find_ply(tmp_path) == tmp_path / "scene.ply"


def test_find_ply_in_output_subdir(tmp_path: Path) -> None:
    (tmp_path / "output").mkdir()
    (tmp_path / "output" / "scene.ply").write_bytes(b"ply\n")
    from autosplat.cli import _find_ply

    assert _find_ply(tmp_path) == tmp_path / "output" / "scene.ply"


def test_find_ply_not_found(tmp_path: Path) -> None:
    from autosplat.cli import _find_ply

    assert _find_ply(tmp_path) is None