Skip to content

Commit

Permalink
Test client ID in update (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart authored Oct 17, 2023
1 parent 33a9968 commit 84385af
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion python/pycrdt/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_state(self) -> bytes:

def get_update(self, state: bytes | None = None) -> bytes:
if state is None:
state = self.get_state()
state = b"\x00"
return self._doc.get_update(state)

def apply_update(self, update: bytes) -> None:
Expand Down
24 changes: 22 additions & 2 deletions tests/test_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ def callback(events, event):
events.append(event)


def encode_client_id(client_id_bytes):
client_id_len = len(client_id_bytes)
b = []
for i, n in enumerate(client_id_bytes[::-1]):
j = n << i
if i != client_id_len - 1:
j |= 0x80
b.append(j)
return bytes(b)


def test_subdoc():
doc0 = Doc()
state0 = doc0.get_state()
Expand Down Expand Up @@ -80,5 +91,14 @@ def test_client_id():
doc1 = Doc()
assert doc0.client_id != doc1.client_id

doc2 = Doc(client_id=123)
assert doc2.client_id == 123
client_id_bytes = b"\x01\x02\x03\x04"
client_id = int.from_bytes(client_id_bytes, byteorder="big")
doc2 = Doc(client_id=client_id)
assert doc2.client_id == client_id
text = Text("Hello, World!")
doc2["text"] = text
update = doc2.get_update()

b = encode_client_id(client_id_bytes)

assert update[2 : 2 + len(b)] == b

0 comments on commit 84385af

Please sign in to comment.