From d26bd9a2155977e3ff020bdd5c1bb862a4c1210f Mon Sep 17 00:00:00 2001 From: David Brochart Date: Thu, 30 May 2024 10:13:00 +0200 Subject: [PATCH] Add undo manager test (#48) --- tests/test_pycrdt_yjs.py | 15 +++++++++++++++ tests/yjs_client_2.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 tests/yjs_client_2.js diff --git a/tests/test_pycrdt_yjs.py b/tests/test_pycrdt_yjs.py index fe0fca2..a1bb72c 100644 --- a/tests/test_pycrdt_yjs.py +++ b/tests/test_pycrdt_yjs.py @@ -58,3 +58,18 @@ async def test_pycrdt_yjs_1(yws_server, yws_provider, yjs_client): await ystate_change.wait() assert ycells.to_py() == [{"metadata": {"foo": "bar"}, "source": "1 + 2"}] assert ystate.to_py() == {"state": {"dirty": False}} + + +@pytest.mark.parametrize("yjs_client", [2], indirect=True) +async def test_pycrdt_yjs_2(yws_server, yws_provider, yjs_client): + ydoc, _ = yws_provider + ydoc["map0"] = map0 = Map() + map0_change = watch(map0) + await map0_change.wait() + assert map0.get("key0") == "val0" + # client is synced, let's make a change + map0["key1"] = "val1" + # wait for client to undo the change + map0_change = watch(map0) + await map0_change.wait() + assert "key1" not in map0 diff --git a/tests/yjs_client_2.js b/tests/yjs_client_2.js new file mode 100644 index 0000000..54a0fbc --- /dev/null +++ b/tests/yjs_client_2.js @@ -0,0 +1,29 @@ +const Y = require('yjs') +const WebsocketProvider = require('y-websocket').WebsocketProvider +const ws = require('ws') + +const port = process.argv[2] +const ydoc = new Y.Doc() + +const wsProvider = new WebsocketProvider( + `ws://127.0.0.1:${port}`, 'my-roomname', + ydoc, + { WebSocketPolyfill: ws } +) + +wsProvider.on('sync', () => { + const map0 = ydoc.getMap('map0') + const undoManager = new Y.UndoManager(map0, { + trackedOrigins: new Set([wsProvider]), + }) + map0.set('key0', 'val0') + undoManager.clear() + + function undo() { + undoManager.undo() + } + + map0.observe((event, transaction) => { + setTimeout(undo, 100) + }) +})