File size: 4,258 Bytes
9c890d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import base64
import gzip
import json
from pathlib import Path

import numpy as np
import pytest

from demo.scale4d_view_count.bundle import (
    build_viewer,
    bundle_path,
    file_sha256,
    validate_scene,
    verify_bundle,
)


def scene(dataset):
    positions = [[[0.0, 0.0, 2.0], [0.01, 0.0, 2.0]]]
    return {
        "id": dataset,
        "dataset": dataset,
        "frames": 2,
        "query_count": 1,
        "position_source": "uv_depth",
        "states": [
            {"views": count, "positions": positions, "valid": [[True, True]]}
            for count in range(1, 11)
        ],
        "gt": {"positions": positions, "valid": [[True, True]]},
        "camera_ids": [1],
        "intrinsics": np.eye(3)[None, None].repeat(2, axis=1).tolist(),
        "relative_cameras": np.eye(4)[None, None].repeat(2, axis=1).tolist(),
        "video": dataset + ".webm",
    }


def test_standalone_roundtrip_preserves_metric_tracks(tmp_path):
    rows = []
    for dataset in ("pstudio", "show3d"):
        data = scene(dataset)
        name = dataset + ".json.gz"
        (tmp_path / name).write_bytes(gzip.compress(json.dumps(data).encode()))
        (tmp_path / data["video"]).write_bytes(b"video bytes")
        rows.append({"id": dataset, "dataset": dataset, "data": name})
    (tmp_path / "scenes.json").write_text(json.dumps(rows))
    output = build_viewer(tmp_path, tmp_path / "index.html")
    text = output.read_text()
    opening = '<script id="inline-assets" type="application/json">'
    assets = json.loads(text.split(opening, 1)[1].split("</script>", 1)[0])
    restored = json.loads(
        gzip.decompress(base64.b64decode(assets["pstudio.json.gz"]["data"]))
    )
    assert restored == scene("pstudio")
    assert "{{VIEWER_JS}}" not in text
    with pytest.raises(FileExistsError):
        build_viewer(tmp_path, output)


def test_scene_rejects_missing_views_and_wrong_track_order():
    data = scene("pstudio")
    data["states"].pop()
    with pytest.raises(ValueError, match="1–10"):
        validate_scene(data)
    data = scene("pstudio")
    data["states"][0]["positions"] = [[[0, 0, 2]], [[0, 0, 2]]]
    with pytest.raises(ValueError, match="dimensions"):
        validate_scene(data)


def test_matched_camera_modes_can_share_the_same_view_count():
    import copy

    data = scene("pstudio")
    state = data["states"][2]
    on, off = copy.deepcopy(state), copy.deepcopy(state)
    on["camera"], off["camera"] = "on", "off"
    data["states"] = [on, off]
    data["available_view_counts"] = [3]
    validate_scene(data)
    data["states"].append(copy.deepcopy(off))
    with pytest.raises(ValueError, match="declared"):
        validate_scene(data)


def test_distinct_ema_teachers_support_native_xyz_at_the_same_input_setting():
    import copy

    data = scene("adt")
    data["position_source"] = "xyz"
    data["available_view_counts"] = [3]
    original = data["states"][2]
    data["states"] = []
    for model in ("b2_ema", "b3_ema"):
        for camera in ("on", "off"):
            state = copy.deepcopy(original)
            state.update(model=model, camera=camera)
            data["states"].append(state)
    validate_scene(data)
    data["states"].append(copy.deepcopy(data["states"][0]))
    with pytest.raises(ValueError, match="declared"):
        validate_scene(data)


def test_manifest_detects_tampering_and_path_escape(tmp_path):
    asset = tmp_path / "result.npz"
    asset.write_bytes(b"original result")
    manifest = {
        "format_version": 1,
        "files": [
            {
                "path": asset.name,
                "bytes": asset.stat().st_size,
                "sha256": file_sha256(asset),
            }
        ],
    }
    (tmp_path / "manifest.json").write_text(json.dumps(manifest))
    verify_bundle(tmp_path)
    asset.write_bytes(b"modified result")
    with pytest.raises(ValueError, match="checksum"):
        verify_bundle(tmp_path)
    for name in ("../token", "/tmp/token"):
        with pytest.raises(ValueError, match="path"):
            bundle_path(tmp_path, name)
    (tmp_path / "escape").symlink_to(Path("/tmp"), target_is_directory=True)
    with pytest.raises(ValueError, match="escapes"):
        bundle_path(tmp_path, "escape/token")