Skip to content

Commit fb5298c

Browse files
committed
Add test
1 parent d8ac45c commit fb5298c

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

javascript/src/ycell.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,9 @@ export class YCodeCell
753753
set outputs(v: Array<nbformat.IOutput>) {
754754
this.setOutputs(v);
755755
}
756+
get youtputs(): Y.Array<any> {
757+
return this._youtputs;
758+
}
756759

757760
/**
758761
* Execution, display, or stream outputs.

tests/files/nb1.ipynb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "4166c837-41c7-4ada-b86e-fd9a7720a409",
7+
"metadata": {},
8+
"outputs": [
9+
{
10+
"name": "stdout",
11+
"output_type": "stream",
12+
"text": [
13+
"Hello,"
14+
]
15+
}
16+
],
17+
"source": [
18+
"print(\"Hello,\", end=\"\")"
19+
]
20+
}
21+
],
22+
"metadata": {
23+
"kernelspec": {
24+
"display_name": "Python 3 (ipykernel)",
25+
"language": "python",
26+
"name": "python3"
27+
},
28+
"language_info": {
29+
"codemirror_mode": {
30+
"name": "ipython",
31+
"version": 3
32+
},
33+
"file_extension": ".py",
34+
"mimetype": "text/x-python",
35+
"name": "python",
36+
"nbconvert_exporter": "python",
37+
"pygments_lexer": "ipython3",
38+
"version": "3.12.3"
39+
}
40+
},
41+
"nbformat": 4,
42+
"nbformat_minor": 5
43+
}

tests/test_pycrdt_yjs.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,31 @@ async def test_ypy_yjs_0(yws_server, yjs_client):
7474
assert ytest.source == nb
7575

7676

77+
@pytest.mark.asyncio
78+
@pytest.mark.parametrize("yjs_client", "1", indirect=True)
79+
async def test_ypy_yjs_1(yws_server, yjs_client):
80+
ydoc = Doc()
81+
ynotebook = YNotebook(ydoc)
82+
async with connect("ws://localhost:1234/my-roomname") as websocket, WebsocketProvider(
83+
ydoc, websocket
84+
):
85+
nb = stringify_source(json.loads((files_dir / "nb1.ipynb").read_text()))
86+
ynotebook.source = nb
87+
output_text = ynotebook.ycells[0]["outputs"][0]["text"]
88+
assert output_text.to_py() == ["Hello,"]
89+
event = Event()
90+
91+
def callback(_event):
92+
event.set()
93+
94+
output_text.observe(callback)
95+
96+
with move_on_after(10):
97+
await event.wait()
98+
99+
assert output_text.to_py() == ["Hello,", " World!"]
100+
101+
77102
def test_plotly_renderer():
78103
"""This test checks in particular that the type cast is not breaking the data."""
79104
ydoc = Doc()

tests/yjs_client_1.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) Jupyter Development Team.
3+
* Distributed under the terms of the Modified BSD License.
4+
*/
5+
6+
import { YNotebook } from '@jupyter/ydoc'
7+
import { WebsocketProvider } from 'y-websocket'
8+
import ws from 'ws'
9+
10+
const notebook = new YNotebook()
11+
const cells = notebook.ydoc.getArray('cells')
12+
13+
function append_text() {
14+
const cell = notebook.getCell(0)
15+
const youtput = cell.youtputs.get(0)
16+
const text = youtput.get('text')
17+
text.insert(1, [' World!'])
18+
}
19+
20+
function callback2(event) {
21+
if (event.transaction.local) {
22+
return
23+
}
24+
25+
const cell = notebook.getCell(0)
26+
if (cell.youtputs.length > 0) {
27+
setTimeout(append_text, 100)
28+
}
29+
}
30+
31+
function callback1(event) {
32+
if (event.transaction.local) {
33+
return
34+
}
35+
36+
if (cells.length > 0) {
37+
const cell = notebook.getCell(0)
38+
if (cell.youtputs.length > 0) {
39+
setTimeout(append_text, 100)
40+
}
41+
else {
42+
cell.youtputs.observe(callback2)
43+
}
44+
}
45+
}
46+
47+
cells.observe(callback1)
48+
49+
const wsProvider = new WebsocketProvider(
50+
'ws://localhost:1234', 'my-roomname',
51+
notebook.ydoc,
52+
{ WebSocketPolyfill: ws }
53+
)

0 commit comments

Comments
 (0)