From de58209fd092a7606e70e2f27e27162d531bb1c4 Mon Sep 17 00:00:00 2001 From: Jared Miller Date: Mon, 9 Feb 2026 17:07:43 -0500 Subject: [PATCH] Add test for chunked IF response reading --- tests/test_if_session.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/test_if_session.py b/tests/test_if_session.py index 887ca22..c14a6b1 100644 --- a/tests/test_if_session.py +++ b/tests/test_if_session.py @@ -782,3 +782,36 @@ async def test_do_restore_handles_stdout_read_failure(tmp_path): # Should return empty string on failure (player starts fresh) assert result == "" + + +@pytest.mark.asyncio +async def test_read_response_handles_chunked_data(): + """_read_response() correctly handles data arriving in chunks.""" + player = MagicMock() + session = IFSession(player, "/path/to/story.z5") + + mock_process = AsyncMock() + mock_process.stdout = AsyncMock() + session.process = mock_process + + # Data arrives in multi-byte chunks, last chunk includes prompt + chunks = [ + b"Welcome to Zork.\n", + b"West of House\nYou are standing ", + b"in an open field.\n>", + ] + chunk_iter = iter(chunks) + + async def read_chunked(n): + try: + return next(chunk_iter) + except StopIteration: + return b"" + + mock_process.stdout.read = AsyncMock(side_effect=read_chunked) + + result = await session._read_response() + + assert "Welcome to Zork" in result + assert "open field" in result + assert not result.strip().endswith(">")