|
| 1 | +"""Byte limits apply to the UTF-8 text delivered by an optional epoch prefetch.""" |
| 2 | +import sys |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from loopx.control_plane.turn_driver.epoch_prefetch_io import _utf8_prefix, run_bounded_prefetch_subprocess |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.parametrize( |
| 10 | + ("raw", "cap", "expected", "was_truncated"), |
| 11 | + [ |
| 12 | + ("界".encode(), 3, "界", False), |
| 13 | + ("界x".encode(), 3, "界", True), |
| 14 | + ("ab界z".encode(), 4, "ab", True), |
| 15 | + ("a🎯z".encode(), 2, "a", True), |
| 16 | + ("a🎯z".encode(), 3, "a", True), |
| 17 | + ("a🎯z".encode(), 4, "a", True), |
| 18 | + ("a🎯z".encode(), 5, "a🎯", True), |
| 19 | + (b"ab\xff", 3, "ab", True), |
| 20 | + (b"\xff", 3, "\ufffd", False), |
| 21 | + (b"ab\xe7", 6, "ab\ufffd", False), |
| 22 | + (b"", 0, "", False), |
| 23 | + ], |
| 24 | +) |
| 25 | +def test_decode_prefetch_output(raw, cap, expected, was_truncated): |
| 26 | + kept = bytearray(raw[:cap]) |
| 27 | + text, truncated = _utf8_prefix(kept, cap, truncated=len(raw) > cap) |
| 28 | + assert (text, truncated) == (expected, was_truncated) |
| 29 | + assert len(text.encode("utf-8")) <= cap |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.skipif(sys.platform == "win32", reason="The prefetch pipe selector requires POSIX file descriptors") |
| 33 | +def test_runner_reports_raw_count_when_a_utf8_character_crosses_the_cap(tmp_path): |
| 34 | + path = tmp_path / "source.txt" |
| 35 | + path.write_bytes("ab界z".encode("utf-8")) |
| 36 | + probe = run_bounded_prefetch_subprocess(["cat", str(path)], timeout_ms=2000, max_output_bytes=4) |
| 37 | + assert probe["exit_code"] == 0 |
| 38 | + assert probe["prefetched_output"] == "ab" |
| 39 | + assert probe["truncated"] is True |
| 40 | + assert probe["raw_bytes_read"] == 6 |
0 commit comments