|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Unit tests for bp_utils_readable_compress. |
| 4 | +""" |
| 5 | + |
| 6 | +import os |
| 7 | +import sys |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src')) |
| 12 | + |
| 13 | +from smolagents.bp_utils_readable_compress import ( |
| 14 | + readable_compress, |
| 15 | + _strip_ansi, |
| 16 | + _normalize_whitespace, |
| 17 | + _collapse_repeated_lines, |
| 18 | + _collapse_progress_lines, |
| 19 | + _strip_timestamps, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +# ── ANSI stripping ────────────────────────────────────────────────────── |
| 24 | + |
| 25 | +class TestStripAnsi: |
| 26 | + def test_removes_colour_codes(self): |
| 27 | + assert _strip_ansi("\x1b[31mERROR\x1b[0m") == "ERROR" |
| 28 | + |
| 29 | + def test_removes_cursor_move(self): |
| 30 | + assert _strip_ansi("\x1b[2Ahello") == "hello" |
| 31 | + |
| 32 | + def test_plain_text_unchanged(self): |
| 33 | + assert _strip_ansi("hello world") == "hello world" |
| 34 | + |
| 35 | + def test_osc_sequence(self): |
| 36 | + assert _strip_ansi("\x1b]0;title\x07rest") == "rest" |
| 37 | + |
| 38 | + |
| 39 | +# ── Whitespace normalization ──────────────────────────────────────────── |
| 40 | + |
| 41 | +class TestNormalizeWhitespace: |
| 42 | + def test_strips_trailing_spaces(self): |
| 43 | + assert _normalize_whitespace(["hello ", "world "]) == ["hello", "world"] |
| 44 | + |
| 45 | + def test_collapses_blank_runs(self): |
| 46 | + lines = ["a", "", "", "", "b"] |
| 47 | + assert _normalize_whitespace(lines) == ["a", "", "b"] |
| 48 | + |
| 49 | + def test_single_blank_preserved(self): |
| 50 | + lines = ["a", "", "b"] |
| 51 | + assert _normalize_whitespace(lines) == ["a", "", "b"] |
| 52 | + |
| 53 | + |
| 54 | +# ── Duplicate collapsing ──────────────────────────────────────────────── |
| 55 | + |
| 56 | +class TestCollapseRepeatedLines: |
| 57 | + def test_no_repeats(self): |
| 58 | + lines = ["a", "b", "c"] |
| 59 | + assert _collapse_repeated_lines(lines) == ["a", "b", "c"] |
| 60 | + |
| 61 | + def test_two_repeats_kept(self): |
| 62 | + lines = ["a", "a", "b"] |
| 63 | + assert _collapse_repeated_lines(lines) == ["a", "a", "b"] |
| 64 | + |
| 65 | + def test_many_repeats_collapsed(self): |
| 66 | + lines = ["x"] * 50 |
| 67 | + result = _collapse_repeated_lines(lines) |
| 68 | + assert len(result) == 2 |
| 69 | + assert result[0] == "x" |
| 70 | + assert "49 more" in result[1] |
| 71 | + |
| 72 | + def test_empty_input(self): |
| 73 | + assert _collapse_repeated_lines([]) == [] |
| 74 | + |
| 75 | + def test_mixed_runs(self): |
| 76 | + lines = ["a", "a", "a", "b", "b", "c"] |
| 77 | + result = _collapse_repeated_lines(lines) |
| 78 | + assert result == ["a", " ... (repeated 2 more times)", "b", "b", "c"] |
| 79 | + |
| 80 | + |
| 81 | +# ── Progress line removal ────────────────────────────────────────────── |
| 82 | + |
| 83 | +class TestCollapseProgressLines: |
| 84 | + def test_percentage_with_bar(self): |
| 85 | + lines = [ |
| 86 | + "Downloading [## ] 20%", |
| 87 | + "Downloading [##### ] 50%", |
| 88 | + "Downloading [##########] 100%", |
| 89 | + "Done.", |
| 90 | + ] |
| 91 | + result = _collapse_progress_lines(lines) |
| 92 | + assert result == ["Downloading [##########] 100%", "Done."] |
| 93 | + |
| 94 | + def test_spinner_lines(self): |
| 95 | + lines = ["|", "/", "-", "\\", "finished"] |
| 96 | + result = _collapse_progress_lines(lines) |
| 97 | + assert result == ["\\", "finished"] |
| 98 | + |
| 99 | + def test_normal_lines_unchanged(self): |
| 100 | + lines = ["compiling foo.c", "compiling bar.c"] |
| 101 | + assert _collapse_progress_lines(lines) == lines |
| 102 | + |
| 103 | + def test_percentage_without_bar_not_matched(self): |
| 104 | + lines = ["Test passed: 100% coverage"] |
| 105 | + assert _collapse_progress_lines(lines) == lines |
| 106 | + |
| 107 | + def test_empty(self): |
| 108 | + assert _collapse_progress_lines([]) == [] |
| 109 | + |
| 110 | + |
| 111 | +# ── Timestamp stripping ───────────────────────────────────────────────── |
| 112 | + |
| 113 | +class TestStripTimestamps: |
| 114 | + def test_iso_timestamp(self): |
| 115 | + lines = ["2026-04-10T12:00:01Z hello", "2026-04-10T12:00:02Z world"] |
| 116 | + result = _strip_timestamps(lines) |
| 117 | + assert result[0].startswith("[timestamps stripped") |
| 118 | + assert "hello" in result[1] |
| 119 | + assert "world" in result[2] |
| 120 | + |
| 121 | + def test_bracketed_time(self): |
| 122 | + lines = ["[12:00:01] info message"] |
| 123 | + result = _strip_timestamps(lines) |
| 124 | + assert len(result) == 2 |
| 125 | + assert "info message" in result[1] |
| 126 | + |
| 127 | + def test_syslog_timestamp(self): |
| 128 | + lines = ["Apr 10 12:00:01 server msg"] |
| 129 | + result = _strip_timestamps(lines) |
| 130 | + assert "server msg" in result[1] |
| 131 | + |
| 132 | + def test_no_timestamps(self): |
| 133 | + lines = ["hello", "world"] |
| 134 | + result = _strip_timestamps(lines) |
| 135 | + assert result == ["hello", "world"] |
| 136 | + |
| 137 | + |
| 138 | +# ── Full pipeline ─────────────────────────────────────────────────────── |
| 139 | + |
| 140 | +class TestReadableCompress: |
| 141 | + def test_full_pipeline(self): |
| 142 | + text = ( |
| 143 | + "\x1b[32m2026-04-10T12:00:01Z Starting build\x1b[0m\n" |
| 144 | + "2026-04-10T12:00:02Z Compiling\n" |
| 145 | + "Downloading [## ] 20%\n" |
| 146 | + "Downloading [##### ] 50%\n" |
| 147 | + "Downloading [##########] 100%\n" |
| 148 | + "line\n" |
| 149 | + "line\n" |
| 150 | + "line\n" |
| 151 | + "line\n" |
| 152 | + "line\n" |
| 153 | + "\n" |
| 154 | + "\n" |
| 155 | + "\n" |
| 156 | + "done" |
| 157 | + ) |
| 158 | + result = readable_compress(text) |
| 159 | + # ANSI stripped |
| 160 | + assert "\x1b" not in result |
| 161 | + # Timestamps stripped |
| 162 | + assert "[timestamps stripped" in result |
| 163 | + # Progress collapsed — only 100% kept |
| 164 | + assert "20%" not in result |
| 165 | + assert "100%" in result |
| 166 | + # Duplicate lines collapsed |
| 167 | + assert "repeated" in result |
| 168 | + # Blank line run collapsed |
| 169 | + assert "\n\n\n" not in result |
| 170 | + # Content preserved |
| 171 | + assert "done" in result |
| 172 | + |
| 173 | + def test_plain_text_passthrough(self): |
| 174 | + text = "hello\nworld" |
| 175 | + assert readable_compress(text) == "hello\nworld" |
0 commit comments