|
| 1 | +from pycrdt import Map |
| 2 | + |
| 3 | +from jupyter_ydoc import YNotebook |
| 4 | + |
| 5 | + |
| 6 | +def make_code_cell(source: str): |
| 7 | + return { |
| 8 | + "cell_type": "code", |
| 9 | + "source": source, |
| 10 | + "metadata": {}, |
| 11 | + "outputs": [], |
| 12 | + "execution_count": None, |
| 13 | + } |
| 14 | + |
| 15 | + |
| 16 | +class AnyInstanceOf: |
| 17 | + def __init__(self, cls): |
| 18 | + self.cls = cls |
| 19 | + |
| 20 | + def __eq__(self, other): |
| 21 | + return isinstance(other, self.cls) |
| 22 | + |
| 23 | + |
| 24 | +def test_set_preserves_cells_when_unchanged(): |
| 25 | + nb = YNotebook() |
| 26 | + nb.append_cell(make_code_cell("print('a')\n")) |
| 27 | + nb.append_cell(make_code_cell("print('b')\n")) |
| 28 | + |
| 29 | + changes = [] |
| 30 | + |
| 31 | + def record_changes(topic, event): |
| 32 | + changes.append((topic, event)) |
| 33 | + |
| 34 | + nb.observe(record_changes) |
| 35 | + |
| 36 | + model = nb.get() |
| 37 | + |
| 38 | + # Call set with identical structure |
| 39 | + nb.set(model) |
| 40 | + |
| 41 | + # No changes should be observed at all |
| 42 | + assert changes == [] |
| 43 | + |
| 44 | + |
| 45 | +def test_set_preserves_cells_with_insert_and_remove(): |
| 46 | + nb = YNotebook() |
| 47 | + nb.append_cell(make_code_cell("print('a')\n")) # original 0 |
| 48 | + nb.append_cell(make_code_cell("print('b')\n")) # original 1 (will remove) |
| 49 | + nb.append_cell(make_code_cell("print('c')\n")) # original 2 |
| 50 | + |
| 51 | + # Capture textual content for sanity check |
| 52 | + cell0_source_text = str(nb.ycells[0]["source"]) |
| 53 | + cell2_source_text = str(nb.ycells[2]["source"]) |
| 54 | + |
| 55 | + # Get the model as Python object |
| 56 | + model = nb.get() |
| 57 | + |
| 58 | + # Remove the middle cell and insert a new one between the retained cells |
| 59 | + cells = model["cells"] |
| 60 | + assert len(cells) == 3 |
| 61 | + |
| 62 | + # The cell ids are needed for retention logic; keep first and last |
| 63 | + first = cells[0] |
| 64 | + last = cells[2] |
| 65 | + |
| 66 | + # New inserted cell |
| 67 | + inserted = make_code_cell("print('x')\n") |
| 68 | + model["cells"] = [first, inserted, last] |
| 69 | + |
| 70 | + changes = [] |
| 71 | + |
| 72 | + def record_changes(topic, event): |
| 73 | + changes.append((topic, event)) |
| 74 | + |
| 75 | + nb.observe(record_changes) |
| 76 | + nb.set(model) |
| 77 | + |
| 78 | + assert nb.cell_number == 3 |
| 79 | + |
| 80 | + # Content of the first and last cells should remain the same |
| 81 | + assert str(nb.ycells[0]["source"]) == cell0_source_text |
| 82 | + assert str(nb.ycells[2]["source"]) == cell2_source_text |
| 83 | + |
| 84 | + # The middle cell should have a different source now |
| 85 | + assert str(nb.ycells[1]["source"]) == "print('x')\n" |
| 86 | + |
| 87 | + # We should have one cell event |
| 88 | + cell_events = [e for t, e in changes if t == "cells"] |
| 89 | + assert len(cell_events) == 1 |
| 90 | + event_transactions = cell_events[0] |
| 91 | + assert len(event_transactions) == 1 |
| 92 | + assert event_transactions[0].delta == [ |
| 93 | + {"retain": 1}, |
| 94 | + {"delete": 1}, |
| 95 | + {"insert": [AnyInstanceOf(Map)]}, |
| 96 | + ] |
0 commit comments