|
| 1 | +""" |
| 2 | +Snapshots must carry real pixels, never a dangling binary change-token. |
| 3 | +
|
| 4 | +Under the Electron binary transport (``APL_BINARY_TRANSPORT=1``) a panel's |
| 5 | +image bytes do not travel in the state at all: ``Plot2D._encode_pixels`` writes |
| 6 | +a ``"\\x00bin:<adler>"`` change-token and the real bytes ride a PLOTBIN frame |
| 7 | +emitted by ``_electron._route_change``. That split is right for a live wire and |
| 8 | +wrong for a snapshot — ``save_html`` / ``to_html`` / ``figure_state`` produce a |
| 9 | +document with no PLOTBIN behind it, so a token left in the state resolves to |
| 10 | +nothing and the pixels are simply lost. |
| 11 | +
|
| 12 | +``Figure._sync_for_export`` therefore re-pushes with ``resolve_pixels=True``. |
| 13 | +It has to say so explicitly because the transport gate is a process-global env |
| 14 | +var, which is on in a host app even while that push is serialising a snapshot. |
| 15 | +
|
| 16 | +Regression: the re-push (added for widget-position capture) rewrote |
| 17 | +``panel_<id>_json`` unconditionally with unresolved tokens, which both lost the |
| 18 | +pixels and undid any materialisation a caller had done beforehand. An overlay |
| 19 | +LAYER was the visible casualty — ``_layerBytes`` in figure_esm.js bails on a |
| 20 | +token (``b64.charCodeAt(0) === 0``), so the layer silently did not draw while |
| 21 | +the base image, which is plain base64, still did. |
| 22 | +""" |
| 23 | +from __future__ import annotations |
| 24 | + |
| 25 | +import json |
| 26 | + |
| 27 | +import numpy as np |
| 28 | +import pytest |
| 29 | + |
| 30 | +import anyplotlib as apl |
| 31 | +from anyplotlib.embed import figure_state |
| 32 | + |
| 33 | + |
| 34 | +TOKEN = "\x00bin:" |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def binary_wire(monkeypatch): |
| 39 | + """Turn on the same env gate the Electron host sets.""" |
| 40 | + monkeypatch.setenv("APL_BINARY_TRANSPORT", "1") |
| 41 | + |
| 42 | + |
| 43 | +def _fig_with_layer(): |
| 44 | + """A base image + one overlay layer, both on the token path. |
| 45 | +
|
| 46 | + ``imshow`` encodes the base BEFORE the plot is attached to its Figure, so |
| 47 | + ``_encode_pixels`` finds no ``_raw_pixels`` side-table and falls back to |
| 48 | + base64 for it; ``add_layer`` runs after the attach and does produce a token. |
| 49 | + One ``set_data`` puts the base on the token path too — the state a live host |
| 50 | + is actually in once anything has redrawn. (That asymmetry is why the |
| 51 | + regression looked so odd in the wild: the base image still rendered and only |
| 52 | + the overlay vanished.) |
| 53 | + """ |
| 54 | + img = np.linspace(0, 1, 32 * 32, dtype=np.float32).reshape(32, 32) |
| 55 | + fig, ax = apl.subplots(1, 1, figsize=(300, 300)) |
| 56 | + plot = ax.imshow(img, cmap="gray") |
| 57 | + plot.add_layer(img, cmap="magma", alpha=0.5) |
| 58 | + plot.set_data(img) |
| 59 | + return fig, plot |
| 60 | + |
| 61 | + |
| 62 | +def _panel(state, plot): |
| 63 | + return json.loads(state[f"panel_{plot._id}_json"]) |
| 64 | + |
| 65 | + |
| 66 | +def _geom(state, plot): |
| 67 | + return json.loads(state[f"panel_{plot._id}_geom"]) |
| 68 | + |
| 69 | + |
| 70 | +def _is_b64(value): |
| 71 | + return isinstance(value, str) and value != "" and not value.startswith(TOKEN) |
| 72 | + |
| 73 | + |
| 74 | +class TestSnapshotResolvesPixelTokens: |
| 75 | + def test_producer_really_emits_tokens(self, binary_wire): |
| 76 | + """Guard the premise: without this, the tests below prove nothing.""" |
| 77 | + fig, plot = _fig_with_layer() |
| 78 | + state = plot.to_state_dict() |
| 79 | + assert state["image_b64"].startswith(TOKEN) |
| 80 | + assert state["layers"][0]["image_b64"].startswith(TOKEN) |
| 81 | + |
| 82 | + def test_base_image_is_inline_base64(self, binary_wire): |
| 83 | + fig, plot = _fig_with_layer() |
| 84 | + assert _is_b64(_geom(figure_state(fig), plot)["image_b64"]) |
| 85 | + |
| 86 | + def test_layer_pixels_are_inline_base64(self, binary_wire): |
| 87 | + """The nested copy is the one figure_esm.js `_layerBytes` reads.""" |
| 88 | + fig, plot = _fig_with_layer() |
| 89 | + layer = _panel(figure_state(fig), plot)["layers"][0] |
| 90 | + assert _is_b64(layer["image_b64"]) |
| 91 | + |
| 92 | + def test_layer_geom_key_is_inline_base64(self, binary_wire): |
| 93 | + fig, plot = _fig_with_layer() |
| 94 | + geom = _geom(figure_state(fig), plot) |
| 95 | + assert _is_b64(geom[f"layer_{plot._state['layers'][0]['id']}_b64"]) |
| 96 | + |
| 97 | + def test_no_token_survives_anywhere_in_the_snapshot(self, binary_wire): |
| 98 | + fig, plot = _fig_with_layer() |
| 99 | + blob = json.dumps(figure_state(fig)) |
| 100 | + # json.dumps escapes NUL as \\u0000. |
| 101 | + assert "\\u0000bin:" not in blob |
| 102 | + |
| 103 | + def test_caller_materialisation_is_not_undone(self, binary_wire): |
| 104 | + """A host that resolves the traits itself before exporting keeps them. |
| 105 | +
|
| 106 | + ``_sync_for_export`` rewrites ``panel_<id>_json`` unconditionally, so an |
| 107 | + unresolved re-push would clobber the caller's work — the exact shape of |
| 108 | + the reported regression. |
| 109 | + """ |
| 110 | + fig, plot = _fig_with_layer() |
| 111 | + state = plot.to_state_dict() |
| 112 | + plot.resolve_pixel_tokens(state) |
| 113 | + resolved = state["layers"][0]["image_b64"] |
| 114 | + setattr(fig, f"panel_{plot._id}_json", json.dumps(state)) |
| 115 | + |
| 116 | + after = _panel(figure_state(fig), plot)["layers"][0]["image_b64"] |
| 117 | + assert after == resolved |
| 118 | + |
| 119 | + def test_multi_layer_all_resolved(self, binary_wire): |
| 120 | + img = np.linspace(0, 1, 16 * 16, dtype=np.float32).reshape(16, 16) |
| 121 | + fig, ax = apl.subplots(1, 1, figsize=(300, 300)) |
| 122 | + plot = ax.imshow(img, cmap="gray") |
| 123 | + for cmap in ("magma", "cividis", "plasma"): |
| 124 | + plot.add_layer(img, cmap=cmap, alpha=0.4) |
| 125 | + layers = _panel(figure_state(fig), plot)["layers"] |
| 126 | + assert len(layers) == 3 |
| 127 | + assert all(_is_b64(ly["image_b64"]) for ly in layers) |
| 128 | + |
| 129 | + def test_widget_positions_still_reconciled(self, binary_wire): |
| 130 | + """The pixel fix must not cost the reason _sync_for_export exists.""" |
| 131 | + fig, plot = _fig_with_layer() |
| 132 | + widget = plot.add_widget("rectangle", x=2, y=2, w=4, h=4) |
| 133 | + widget.set(x=21, y=23) |
| 134 | + got = _panel(figure_state(fig), plot)["overlay_widgets"][0] |
| 135 | + assert got["x"] == 21 and got["y"] == 23 |
| 136 | + |
| 137 | + |
| 138 | +class TestLiveWireStillUsesTokens: |
| 139 | + """The live path must keep its token/PLOTBIN split — that is the whole |
| 140 | + point of the binary transport, and resolving there would push megabytes of |
| 141 | + base64 through the comm on every scrub frame.""" |
| 142 | + |
| 143 | + def test_ordinary_push_keeps_the_token(self, binary_wire): |
| 144 | + fig, plot = _fig_with_layer() |
| 145 | + fig._push(plot._id) |
| 146 | + geom = json.loads(getattr(fig, f"panel_{plot._id}_geom")) |
| 147 | + assert geom["image_b64"].startswith(TOKEN) |
| 148 | + layer_key = f"layer_{plot._state['layers'][0]['id']}_b64" |
| 149 | + assert geom[layer_key].startswith(TOKEN) |
| 150 | + |
| 151 | + def test_set_data_keeps_the_token(self, binary_wire): |
| 152 | + fig, plot = _fig_with_layer() |
| 153 | + plot.set_data(np.zeros((32, 32), dtype=np.float32)) |
| 154 | + geom = json.loads(getattr(fig, f"panel_{plot._id}_geom")) |
| 155 | + assert geom["image_b64"].startswith(TOKEN) |
| 156 | + |
| 157 | + def test_push_after_export_returns_to_tokens(self, binary_wire): |
| 158 | + """An export must not leave the live wire on the base64 path.""" |
| 159 | + fig, plot = _fig_with_layer() |
| 160 | + figure_state(fig) |
| 161 | + plot.set_data(np.ones((32, 32), dtype=np.float32)) |
| 162 | + geom = json.loads(getattr(fig, f"panel_{plot._id}_geom")) |
| 163 | + assert geom["image_b64"].startswith(TOKEN) |
| 164 | + |
| 165 | + |
| 166 | +class TestNoBinaryTransport: |
| 167 | + """With no binary wire the state was always inline base64; keep it so.""" |
| 168 | + |
| 169 | + def test_snapshot_is_inline_base64(self, monkeypatch): |
| 170 | + monkeypatch.delenv("APL_BINARY_TRANSPORT", raising=False) |
| 171 | + fig, plot = _fig_with_layer() |
| 172 | + layer = _panel(figure_state(fig), plot)["layers"][0] |
| 173 | + assert _is_b64(layer["image_b64"]) |
| 174 | + assert _is_b64(_geom(figure_state(fig), plot)["image_b64"]) |
0 commit comments