|
1 | 1 | from collections.abc import AsyncIterator |
2 | | -from contextlib import asynccontextmanager |
| 2 | +from contextlib import asynccontextmanager, redirect_stdout |
3 | 3 | from crystal.server import get_request_url |
| 4 | +from crystal.tests.util.asserts import assertEqual |
4 | 5 | from crystal.tests.util.server import assert_does_open_webbrowser_to, extracted_project |
5 | 6 | from crystal.tests.util.windows import MainWindow, OpenOrCreateDialog |
6 | 7 | from crystal.ui.log_drawer import LogDrawer |
7 | 8 | from crystal.util.controls import click_button, TreeItem |
| 9 | +from io import StringIO |
8 | 10 | from unittest import skip |
| 11 | +from unittest.mock import patch |
9 | 12 | import wx |
10 | 13 | from wx.richtext import RichTextCtrl |
11 | 14 |
|
@@ -65,6 +68,36 @@ def test_always_displays_vertical_scrollbar_and_never_displays_horizontal_scroll |
65 | 68 | pass |
66 | 69 |
|
67 | 70 |
|
| 71 | +async def test_when_write_many_lines_to_writer_beyond_max_then_leading_lines_trimmed() -> None: |
| 72 | + MAX_LINE_COUNT = 50 |
| 73 | + with patch('crystal.ui.log_drawer._MAX_LINE_COUNT', MAX_LINE_COUNT): |
| 74 | + async with _log_drawer_visible() as (mw, log_drawer): |
| 75 | + textarea = log_drawer._textarea |
| 76 | + writer = log_drawer.writer |
| 77 | + |
| 78 | + # Count lines already present (from server startup banner) |
| 79 | + initial_line_count = textarea.GetNumberOfLines() |
| 80 | + |
| 81 | + # Write lines until we reach exactly MAX_LINE_COUNT |
| 82 | + lines_to_add = MAX_LINE_COUNT - initial_line_count |
| 83 | + with redirect_stdout(StringIO()): |
| 84 | + for i in range(lines_to_add): |
| 85 | + writer.write(f'line {i}\n') |
| 86 | + assertEqual(MAX_LINE_COUNT, textarea.GetNumberOfLines()) |
| 87 | + |
| 88 | + # Write one more line, which should cause the leading line to be trimmed |
| 89 | + final_line_text = 'this is the final line' |
| 90 | + with redirect_stdout(StringIO()): |
| 91 | + writer.write(f'{final_line_text}\n') |
| 92 | + assertEqual(MAX_LINE_COUNT, textarea.GetNumberOfLines(), |
| 93 | + 'Line count should still be MAX_LINE_COUNT after trimming') |
| 94 | + |
| 95 | + # Verify the last line of content is the one we just wrote |
| 96 | + last_content_line = textarea.GetLineText(MAX_LINE_COUNT - 2) |
| 97 | + assertEqual(final_line_text, last_content_line, |
| 98 | + 'Last content line should be the final line we wrote') |
| 99 | + |
| 100 | + |
68 | 101 | # === Test: Drag or Double-Click Sash === |
69 | 102 |
|
70 | 103 | @skip('not yet automated') |
|
0 commit comments