import json from types import SimpleNamespace import pytest from PySide6.QtCore import Qt, Signal from PySide6.QtWidgets import QApplication from dx_productivity.trial_task_center.ui import dsh_automation as module from .test_dsh_native_menu import HarnessView class SlowHarness(HarnessView): diagnosis_event = Signal(object) def __init__(self, parent): super().__init__(parent) self.requests, self.cancellations = [], [] self.is_running = True def request_diagnosis(self, request): self.requests.append(request) def cancel_diagnosis(self, request_id): self.cancellations.append(request_id) def shutdown(self): self.is_running = False self.stopped.emit() @pytest.fixture def slow_diagnosis(ui, qtbot, monkeypatch): ui.settle() window = ui.window published = [] monkeypatch.setattr(QApplication, 'platformName', lambda: 'windows') monkeypatch.setattr(window, '_create_dsh_view', lambda: SlowHarness(window)) monkeypatch.setattr(module, 'GitIssuePublisher', lambda path: lambda *args: published.append(args) or {'status': 'unavailable'}) automation = module.DshAutomation(window) window._dsh_automation = automation def start(*, read_only=True, cleanup=False): automation.enqueue('', 'batch_resources_pending' if cleanup else 'operation_stalled', 'controlled slow diagnosis', read_only=read_only, cleanup_allowed=cleanup, snapshot_override={'task_id': '', 'status': 'unknown'}) automation._drain() qtbot.waitUntil(lambda: bool(window._dsh_view.requests), timeout=5000) return window._dsh_view yield window, automation, published, start if automation.worker is not None: if hasattr(automation, 'cancel'): automation.cancel() else: for request_id in list(automation.waiters): automation._diagnosis_event({'request_id': request_id, 'status': 'failed', 'error': 'test_cleanup'}) qtbot.waitUntil(lambda: automation.worker is None, timeout=5000) if not window._closed: ui.settle() def test_readonly_diagnosis_keeps_task_controls_usable_and_can_be_cancelled(slow_diagnosis, qtbot, ui): window, automation, published, start = slow_diagnosis view = start() assert automation.worker not in window._workers assert window.task_list.list.isEnabled() assert window.task_list.new_button.isEnabled() assert window.cancel_diagnosis_button.isVisible() qtbot.mouseClick(window.cancel_diagnosis_button, Qt.MouseButton.LeftButton) qtbot.waitUntil(lambda: automation.worker is None, timeout=5000) assert view.cancellations assert automation.latest['status'] == 'cancelled' assert not published assert not window.cancel_diagnosis_button.isVisible() ui.settle() def test_readonly_diagnosis_does_not_prevent_window_close(slow_diagnosis, qtbot): window, automation, published, start = slow_diagnosis start() window.close() qtbot.waitUntil(lambda: window._closed and automation.worker is None, timeout=5000) assert not window._close_pending assert not published def test_resource_diagnosis_has_no_cad_pause_or_force_controls(slow_diagnosis, qtbot, ui): window, automation, published, start = slow_diagnosis view = start(read_only=False, cleanup=True) assert automation.worker in window._workers assert window._batch_stop is None assert not window.run_panel.stop_queue_button.isVisible() assert not window.run_panel.force_stop_button.isVisible() qtbot.mouseClick(window.cancel_diagnosis_button, Qt.MouseButton.LeftButton) qtbot.waitUntil(lambda: automation.worker is None, timeout=5000) assert view.cancellations and not published ui.settle() def test_model_wait_is_limited_by_remaining_total_budget(slow_diagnosis, monkeypatch, ui): window, automation, _, _ = slow_diagnosis window._open_dsh() waits = [] class Complete: def wait(self, timeout): waits.append(timeout) return False monkeypatch.setattr(module, 'Event', Complete) monkeypatch.setattr(module, 'monotonic', lambda: 100.0) automation._analysis_deadline = 100.1 with pytest.raises(TimeoutError): automation._analyze('a' * 24, {'task_id': ''}, 'evidence', 'reflect') assert 0 < waits[0] <= 0.1 ui.settle() def test_native_cancel_removes_queued_request_and_sends_only_target_id(): from dx_productivity.trial_task_center.ui.dsh_native import NativeHarnessView sent = [] native = SimpleNamespace(_diagnosis_queue=[{'request_id': 'cancel'}, {'request_id': 'keep'}], _started=False, is_running=True, process=SimpleNamespace(write=sent.append)) NativeHarnessView.cancel_diagnosis(native, 'cancel') assert native._diagnosis_queue == [{'request_id': 'keep'}] assert not sent native._started = True NativeHarnessView.cancel_diagnosis(native, 'active') assert json.loads(sent[0]) == {'type': 'diagnose_cancel', 'request_id': 'active'} def test_completed_incident_removes_embedded_cancel_action(slow_diagnosis, qtbot, ui): from dx_productivity.trial_task_center.ui.workers import ServiceWorker window, automation, _, _ = slow_diagnosis window._open_dsh() automation._current_identity = ('', 'operation_stalled', 'blocker-completed') automation._owns_execution_lane = False automation._start_worker(ServiceWorker(lambda: {'status': 'observed_cleared', 'task_id': ''})) qtbot.waitUntil(lambda: automation.worker is None) assert window._dsh_view.automation['can_cancel'] is False assert not window.cancel_diagnosis_button.isVisible() ui.settle()