Something went wrong. Try again.
personal memory agent
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354# SPDX-License-Identifier: AGPL-3.0-only# Copyright (c) 2026 sol pbc
from __future__ import annotations
import pytestfrom websockets.exceptions import ConnectionClosed
from solstone.think.link.ws_buffer import BufferedWsReader, WsBufferClosed
class FakeWs: def __init__(self, frames: list[bytes | str]) -> None: self.frames = list(frames)
async def recv(self) -> bytes | str: if not self.frames: raise ConnectionClosed(None, None) return self.frames.pop(0)
@pytest.mark.asyncioasync def test_peek_does_not_consume() -> None: reader = BufferedWsReader(FakeWs([b"abcdef"]))
assert await reader.peek(4) == b"abcd" assert await reader.read_exactly(4) == b"abcd" assert await reader.read_exactly(2) == b"ef"
@pytest.mark.asyncioasync def test_read_exactly_spans_frames_and_normalizes_text() -> None: reader = BufferedWsReader(FakeWs([b"ab", "cd", b"ef"]))
assert await reader.read_exactly(5) == b"abcde" assert await reader.read_exactly(1) == b"f"
@pytest.mark.asyncioasync def test_drain_returns_surplus() -> None: reader = BufferedWsReader(FakeWs([b"abcdef"]))
assert await reader.peek(4) == b"abcd" assert reader.drain_buffer() == b"abcdef" assert reader.drain_buffer() == b""
@pytest.mark.asyncioasync def test_short_close_raises_clear_error() -> None: reader = BufferedWsReader(FakeWs([b"ab"]))
with pytest.raises(WsBufferClosed, match="before 4 bytes"): await reader.read_exactly(4)