|
| 1 | +from subsearch.runtime.logging.spinner_console import SpinnerConsole |
| 2 | + |
| 3 | +# A representative run: a banner with two transient lines, a plain line before |
| 4 | +# any banner, a second banner, and a final is_last close. The terminal and GUI |
| 5 | +# consoles must end up showing the same visible lines. |
| 6 | +RECORDS = [ |
| 7 | + {"message": "early line"}, |
| 8 | + {"message": "--- [Searching] ---", "is_banner": True, "title": "Searching", "done_title": "Searched"}, |
| 9 | + {"message": "opensubtitles: searching"}, |
| 10 | + {"message": "opensubtitles: found 3"}, |
| 11 | + {"message": "--- [Cleaning up] ---", "is_banner": True, "title": "Cleaning up", "done_title": "Cleaned up"}, |
| 12 | + {"message": "", "is_last": True}, |
| 13 | +] |
| 14 | + |
| 15 | + |
| 16 | +def terminal_visible_lines() -> list[str]: |
| 17 | + printed: list[str] = [] |
| 18 | + transient: list[str] = [] |
| 19 | + |
| 20 | + class StubTransient: |
| 21 | + def update(self, spinner, message: str) -> None: |
| 22 | + if transient: |
| 23 | + transient[-1] = message |
| 24 | + else: |
| 25 | + transient.append(message) |
| 26 | + |
| 27 | + def clear(self, spinner) -> None: |
| 28 | + transient.clear() |
| 29 | + |
| 30 | + class StubSpinner: |
| 31 | + def __init__(self, text: str) -> None: |
| 32 | + self.text = text |
| 33 | + |
| 34 | + def start(self) -> None: |
| 35 | + pass |
| 36 | + |
| 37 | + def stop(self) -> None: |
| 38 | + pass |
| 39 | + |
| 40 | + console = SpinnerConsole( |
| 41 | + spinner_factory=StubSpinner, |
| 42 | + printer=printed.append, |
| 43 | + transient_line=StubTransient(), |
| 44 | + ) |
| 45 | + for record in RECORDS: |
| 46 | + console.write(record["message"], **{key: value for key, value in record.items() if key != "message"}) |
| 47 | + return printed |
| 48 | + |
| 49 | + |
| 50 | +def gui_visible_lines(qtbot) -> list[str]: |
| 51 | + from subsearch.runtime.logging.logger import log |
| 52 | + from subsearch.ui.services.console_view import ConsoleViewSink |
| 53 | + from subsearch.ui.widgets.console_view import ConsoleView |
| 54 | + |
| 55 | + # The logger is a process-wide singleton; other tests leave records in its |
| 56 | + # history that the sink would replay into the view. Isolate to this sequence. |
| 57 | + log._console_history.clear() |
| 58 | + sink = ConsoleViewSink() |
| 59 | + view = ConsoleView(sink) |
| 60 | + qtbot.addWidget(view) |
| 61 | + dispatch = { |
| 62 | + "line": view._append, |
| 63 | + "banner": view._start_banner, |
| 64 | + "finish": view._finish_banner, |
| 65 | + "status": view._update_status, |
| 66 | + } |
| 67 | + try: |
| 68 | + for record in RECORDS: |
| 69 | + sink._on_message( |
| 70 | + record["message"], |
| 71 | + **{key: value for key, value in record.items() if key != "message"}, |
| 72 | + ) |
| 73 | + # Drive the view directly from the sink's recorded protocol; the live |
| 74 | + # widget uses queued connections that need an event loop to deliver. |
| 75 | + for kind, payload in sink.history(): |
| 76 | + dispatch[kind](payload) |
| 77 | + return [view._list.item(row).text() for row in range(view._list.count())] |
| 78 | + finally: |
| 79 | + sink.detach() |
| 80 | + |
| 81 | + |
| 82 | +def test_terminal_and_gui_show_the_same_lines(qtbot) -> None: |
| 83 | + # No normalization: the GUI must render finished banners as the plain |
| 84 | + # done-title the terminal prints, with no checkmark or other decoration. |
| 85 | + terminal = terminal_visible_lines() |
| 86 | + gui = gui_visible_lines(qtbot) |
| 87 | + assert terminal == gui |
0 commit comments