"""A later approved paragraph stays progressive after the earlier one catches up.""" from __future__ import annotations import os from time import monotonic os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") import pytest from PySide6.QtCore import QCoreApplication, QEvent from PySide6.QtTest import QTest from PySide6.QtWidgets import QApplication, QScrollArea from doctor_workstation.ui.dialogs.ai_consult import _ChatBubble def wait_until(predicate, timeout=5): end = monotonic() + timeout while not predicate() and monotonic() < end: QTest.qWait(5) assert predicate() @pytest.fixture(scope="module") def application(): return QApplication.instance() or QApplication([]) @pytest.mark.parametrize("first_length,later_length", [(11000, 214), (7500, 120)]) def test_new_burst_has_multiple_document_updates_after_idle(application, first_length, later_length): host = QScrollArea() host.resize(720, 480) host.setWidgetResizable(True) bubble = _ChatBubble(role="ai", text="waiting") host.setWidget(bubble) host.show() application.processEvents() first = "A" * first_length later = "\n\nLATER_PARAGRAPH_START_" + "B" * (later_length - 34) + "_LATER_END" assert len(later) == later_length and len(first + later) < 12000 try: bubble.queue_approved_stream_text(first) wait_until(lambda: not bubble._presentation_timer.isActive()) updates = [] bubble.stream_frame_presented.connect(lambda: updates.append(bubble.body.toPlainText())) bubble.queue_approved_stream_text(first + later) wait_until(lambda: not bubble._presentation_timer.isActive()) assert len(updates) >= 2 assert not updates[0].endswith("_LATER_END") assert updates[-1].endswith("_LATER_END") assert bubble._raw_payload == first + later finally: bubble.stop_stream_presentation() assert not bubble._presentation_timer.isActive() host.close() host.deleteLater() QCoreApplication.sendPostedEvents(None, QEvent.Type.DeferredDelete) application.processEvents()