code: def test_set_state_data_truncate(echo):
      w = TruncateDataWidget()
      data = memoryview(b"x" * 30)
      w.set_state(
          dict(
              a=True,
              d={"data": data},
          )
      )
      # Get message for checking
      assert (
          len(w.comm.messages) == 2 if echo else 1
      )  # ensure we didn't get more than expected
      msg = w.comm.messages[-1]
      # Assert that the data update (truncation) sends an update
      buffers = msg[1].pop("buffers")
      assert msg == (
          (),
          dict(
              data=dict(
                  method="update",
                  state=dict(d={}),
                  buffer_paths=[["d", "data"]],
              )
          ),
      )
  
      # Sanity:
      assert len(buffers) == 1
      assert buffers[0] == data[:20].tobytes()
  
imports: import pytest
  from traitlets import Bool, Tuple, List, Instance, CFloat, CInt, Float, Int, TraitError, observe
  from .utils import setup, teardown
  import ipywidgets
  from ipywidgets import Widget
label_desc: Use image node_id 10 for calling a global function with example usage: test_set_state_data_truncate(echo) without return types