Keep snap helper safe without context (#1828)

This commit is contained in:
red person
2026-06-29 10:54:44 -07:00
committed by GitHub
parent d2a6d73aa5
commit 00dfd2d47a
2 changed files with 26 additions and 2 deletions
@@ -36,6 +36,28 @@ def test_compute_snap_tolerates_non_array_other_layers():
assert r["x"] == 10 and r["y"] == 10 and r["guides"] == []
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_compute_snap_tolerates_missing_layer_or_context():
js = f"""
import {{ computeSnap }} from '{_HELPER.as_posix()}';
console.log(JSON.stringify([
computeSnap(null, 10, 20, {{ zoom: 1, canvasW: 800, canvasH: 600 }}),
computeSnap({{ id: 'L1' }}, 11, 21, {{ zoom: 1, canvasW: 800, canvasH: 600 }}),
computeSnap({{ id: 'L1', canvas: {{ width: 100, height: 50 }} }}, 12, 22, null)
]));
"""
proc = subprocess.run(
["node", "--input-type=module"],
input=js, capture_output=True, text=True, cwd=str(_REPO), timeout=30,
)
assert proc.returncode == 0, proc.stderr
assert json.loads(proc.stdout.strip()) == [
{"x": 10, "y": 20, "guides": []},
{"x": 11, "y": 21, "guides": []},
{"x": 12, "y": 22, "guides": []},
]
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_compute_snap_still_snaps_to_a_layer_edge():
other = [{"id": "L2", "visible": True, "offset": {"x": 12, "y": 300},