076914e5b7
- Split client, native, remote, serve, markdown, and SDK internals into focused packages with direct imports. - Move local and remote transport framing/protocol helpers behind clearer module boundaries. - Break up the extension injected DOM logic into a separate content dispatch bundle and dedicated content modules. - Add explicit client handling for passive remote discovery without noisy PQ warnings. - Keep behavior covered with updated unit, integration, and extension tests.
60 lines
1.5 KiB
Python
60 lines
1.5 KiB
Python
import asyncio
|
|
import struct
|
|
|
|
import pytest
|
|
|
|
from browser_cli.framing import (
|
|
async_recv_frame,
|
|
frame,
|
|
recv_frame,
|
|
send_frame,
|
|
)
|
|
from browser_cli.version_manager import MAX_MSG_BYTES
|
|
|
|
class FakeRecv:
|
|
def __init__(self, chunks):
|
|
self.chunks = list(chunks)
|
|
|
|
def recv(self, n):
|
|
if not self.chunks:
|
|
return b""
|
|
chunk = self.chunks.pop(0)
|
|
if len(chunk) > n:
|
|
self.chunks.insert(0, chunk[n:])
|
|
return chunk[:n]
|
|
return chunk
|
|
|
|
def test_frame_prefixes_payload_length():
|
|
assert frame(b"abc") == b"\x03\x00\x00\x00abc"
|
|
|
|
def test_recv_frame_reads_chunked_socket_payload():
|
|
sock = FakeRecv([b"\x05", b"\x00\x00\x00he", b"llo"])
|
|
assert recv_frame(sock) == b"hello"
|
|
|
|
def test_recv_frame_allow_eof_returns_none():
|
|
assert recv_frame(FakeRecv([]), allow_eof=True) is None
|
|
|
|
def test_recv_frame_rejects_oversized_payload():
|
|
sock = FakeRecv([struct.pack("<I", MAX_MSG_BYTES + 1)])
|
|
with pytest.raises(ConnectionError, match="too large"):
|
|
recv_frame(sock, label="message")
|
|
|
|
def test_send_frame_writes_prefixed_payload():
|
|
sent = []
|
|
|
|
class FakeSend:
|
|
def sendall(self, data):
|
|
sent.append(data)
|
|
|
|
send_frame(FakeSend(), b"ok")
|
|
assert sent == [b"\x02\x00\x00\x00ok"]
|
|
|
|
def test_async_recv_frame_reads_payload():
|
|
async def run():
|
|
reader = asyncio.StreamReader()
|
|
reader.feed_data(frame(b"async"))
|
|
reader.feed_eof()
|
|
return await async_recv_frame(reader)
|
|
|
|
assert asyncio.run(run()) == b"async"
|