Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Jun 6, 2024
1 parent d8ac45c commit 06565d5
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 2 deletions.
3 changes: 3 additions & 0 deletions javascript/src/ycell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,9 @@ export class YCodeCell
set outputs(v: Array<nbformat.IOutput>) {
this.setOutputs(v);
}
get youtputs(): Y.Array<any> {
return this._youtputs;
}

/**
* Execution, display, or stream outputs.
Expand Down
8 changes: 6 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ async def yws_server(request):
@pytest.fixture
def yjs_client(request):
client_id = request.param
p = subprocess.Popen(f"yarn node {here / 'yjs_client_'}{client_id}.js", shell=True)
p = subprocess.Popen(["node", f"{here / 'yjs_client_'}{client_id}.js"])
yield p
p.kill()
p.terminate()
try:
p.wait(timeout=10)
except Exception:
p.kill()
43 changes: 43 additions & 0 deletions tests/files/nb1.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
25 changes: 25 additions & 0 deletions tests/test_pycrdt_yjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
nb = stringify_source(json.loads((files_dir / "nb1.ipynb").read_text()))
ynotebook.source = nb
async with connect("ws://localhost:1234/my-roomname") as websocket, WebsocketProvider(
ydoc, websocket
):
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()
Expand Down
23 changes: 23 additions & 0 deletions tests/yjs_client_1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 wsProvider = new WebsocketProvider(
'ws://localhost:1234', 'my-roomname',
notebook.ydoc,
{ WebSocketPolyfill: ws }
)

wsProvider.on('sync', (isSynced) => {
const cell = notebook.getCell(0)
const youtput = cell.youtputs.get(0)
const text = youtput.get('text')
text.insert(1, [' World!'])
})

0 comments on commit 06565d5

Please sign in to comment.