From fb5298c95cf230b2b2c87744aeb17c0f96cfaf79 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Wed, 5 Jun 2024 23:21:24 +0200 Subject: [PATCH] Add test --- javascript/src/ycell.ts | 3 +++ tests/files/nb1.ipynb | 43 ++++++++++++++++++++++++++++++++ tests/test_pycrdt_yjs.py | 25 +++++++++++++++++++ tests/yjs_client_1.js | 53 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 tests/files/nb1.ipynb create mode 100644 tests/yjs_client_1.js diff --git a/javascript/src/ycell.ts b/javascript/src/ycell.ts index 4d9076e..bcf0936 100644 --- a/javascript/src/ycell.ts +++ b/javascript/src/ycell.ts @@ -753,6 +753,9 @@ export class YCodeCell set outputs(v: Array) { this.setOutputs(v); } + get youtputs(): Y.Array { + return this._youtputs; + } /** * Execution, display, or stream outputs. diff --git a/tests/files/nb1.ipynb b/tests/files/nb1.ipynb new file mode 100644 index 0000000..4ed7180 --- /dev/null +++ b/tests/files/nb1.ipynb @@ -0,0 +1,43 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "4166c837-41c7-4ada-b86e-fd9a7720a409", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello," + ] + } + ], + "source": [ + "print(\"Hello,\", end=\"\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/tests/test_pycrdt_yjs.py b/tests/test_pycrdt_yjs.py index f928c81..c6176ca 100644 --- a/tests/test_pycrdt_yjs.py +++ b/tests/test_pycrdt_yjs.py @@ -74,6 +74,31 @@ async def test_ypy_yjs_0(yws_server, yjs_client): assert ytest.source == nb +@pytest.mark.asyncio +@pytest.mark.parametrize("yjs_client", "1", indirect=True) +async def test_ypy_yjs_1(yws_server, yjs_client): + ydoc = Doc() + ynotebook = YNotebook(ydoc) + async with connect("ws://localhost:1234/my-roomname") as websocket, WebsocketProvider( + ydoc, websocket + ): + nb = stringify_source(json.loads((files_dir / "nb1.ipynb").read_text())) + ynotebook.source = nb + output_text = ynotebook.ycells[0]["outputs"][0]["text"] + assert output_text.to_py() == ["Hello,"] + event = Event() + + def callback(_event): + event.set() + + output_text.observe(callback) + + with move_on_after(10): + await event.wait() + + assert output_text.to_py() == ["Hello,", " World!"] + + def test_plotly_renderer(): """This test checks in particular that the type cast is not breaking the data.""" ydoc = Doc() diff --git a/tests/yjs_client_1.js b/tests/yjs_client_1.js new file mode 100644 index 0000000..c1f1055 --- /dev/null +++ b/tests/yjs_client_1.js @@ -0,0 +1,53 @@ +/* + * Copyright (c) Jupyter Development Team. + * Distributed under the terms of the Modified BSD License. + */ + +import { YNotebook } from '@jupyter/ydoc' +import { WebsocketProvider } from 'y-websocket' +import ws from 'ws' + +const notebook = new YNotebook() +const cells = notebook.ydoc.getArray('cells') + +function append_text() { + const cell = notebook.getCell(0) + const youtput = cell.youtputs.get(0) + const text = youtput.get('text') + text.insert(1, [' World!']) +} + +function callback2(event) { + if (event.transaction.local) { + return + } + + const cell = notebook.getCell(0) + if (cell.youtputs.length > 0) { + setTimeout(append_text, 100) + } +} + +function callback1(event) { + if (event.transaction.local) { + return + } + + if (cells.length > 0) { + const cell = notebook.getCell(0) + if (cell.youtputs.length > 0) { + setTimeout(append_text, 100) + } + else { + cell.youtputs.observe(callback2) + } + } +} + +cells.observe(callback1) + +const wsProvider = new WebsocketProvider( + 'ws://localhost:1234', 'my-roomname', + notebook.ydoc, + { WebSocketPolyfill: ws } +)