from PySide6.QtCore import Signal, Qt from PySide6.QtWidgets import QWidget from types import SimpleNamespace from dx_productivity.trial_task_center.errors import TaskCenterError class HarnessView(QWidget): action_requested = Signal(object) task_creation_requested = Signal(object) task_creation_cancelled = Signal(object) lifecycle_requested = Signal(object) lifecycle_cancelled = Signal(object) stopped = Signal() def __init__(self, parent): super().__init__(parent) self.starts = 0 self.binds = [] self.creation = {} self.lifecycle = {} self.operations = {} self.lifecycle_results = [] self.results = [] def start(self): self.starts += 1 def bind(self, snapshot, **kwargs): self.operations = kwargs.get('operations', {}) self.binds.append(snapshot) self.context = {'task_id': snapshot.task_id, 'revision': 'rev'} def shutdown(self): pass def complete_task_creation(self, value): self.results.append(value) def complete_lifecycle(self, value): self.lifecycle_results.append(value) def test_direct_navigation_opens_harness_and_returns_without_losing_task(ui, monkeypatch, qtbot): window = ui.window monkeypatch.setattr(type(window), '_create_dsh_view', lambda self: HarnessView(self), raising=False) task_id = window._task_id qtbot.mouseClick(window.dsh_navigation, Qt.MouseButton.LeftButton) assert window.content_stack.currentWidget() is window._dsh_view assert window._dsh_view.binds[-1].task_id == task_id assert window._dsh_view.starts == 1 assert window.dsh_navigation.isChecked() assert window.task_center_navigation.geometry().right() < window.dsh_navigation.geometry().left() qtbot.mouseClick(window.task_center_navigation, Qt.MouseButton.LeftButton) assert window.content_stack.currentIndex() == 0 assert window._task_id == task_id assert window.task_center_navigation.isChecked() qtbot.mouseClick(window.dsh_navigation, Qt.MouseButton.LeftButton) assert window._dsh_view.starts == 1 def test_dsh_navigation_surfaces_lazy_startup_failure(ui, monkeypatch, qtbot): window = ui.window def fail(_self): raise RuntimeError('controlled missing WebEngine') monkeypatch.setattr(type(window), '_create_dsh_view', fail) qtbot.mouseClick(window.dsh_navigation, Qt.MouseButton.LeftButton) assert 'DSH助手启动失败' in window.run_panel.operator_band.text() def test_cross_task_harness_action_never_reaches_task_service(ui, monkeypatch): window = ui.window monkeypatch.setattr(type(window), '_create_dsh_view', lambda self: HarnessView(self), raising=False) window.dsh_action.trigger() called = [] monkeypatch.setattr(window, '_retry_failed', lambda: called.append('retry')) window._handle_dsh_action({'action': 'retry_failed', 'task_id': 'another', 'revision': 'rev'}) assert not called def test_error_entry_queues_automatic_ai_without_running_synchronous_retry(ui, monkeypatch): window = ui.window events, retried = [], [] window._dsh_automation = SimpleNamespace(enqueue=lambda *args, **kwargs: events.append((args, kwargs)) or True) window._dsh.action_handler = lambda proposal: retried.append(proposal) window._show_error(TaskCenterError('worker_error', 'controlled error')) assert len(events) == 1 and events[0][0][:2] == (window._task_id, 'worker_error') assert not retried def test_failed_result_queues_ai_even_when_no_exception_is_raised(ui, monkeypatch): window = ui.window events = [] window._dsh_automation = SimpleNamespace(enqueue=lambda *args, **kwargs: events.append((args, kwargs)) or True) monkeypatch.setattr(window, '_metadata_changed', lambda _: None) window._active_quick_run_task_id = window._task_id window._show_batch_result(SimpleNamespace(paused=False, progress=SimpleNamespace(failed_items=1, saved_items=0, no_match_items=0))) assert events[0][0][1] == 'execution_failed' def test_cancelled_operation_does_not_start_ai(ui): window = ui.window events = [] window._dsh_automation = SimpleNamespace(enqueue=lambda *args, **kwargs: events.append(args)) window._show_error(TaskCenterError('operation_cancelled', 'cancelled')) assert not events def test_interrupted_incident_restores_as_pending_without_replay(ui, monkeypatch): import json from dx_productivity.trial_task_center.ui.dsh_automation import DshAutomation window = ui.window incident_id = '0' * 24 directory = ui.data / 'dsh' / 'incidents' / incident_id directory.mkdir(parents=True) (directory / 'incident.json').write_text(json.dumps({'incident_id': incident_id, 'task_id': window._task_id, 'issue_code': 'worker_error', 'status': 'retrying', 'attempts': 1, 'analyses': []}), encoding='utf8') automation = DshAutomation(window) assert automation.latest['status'] == 'pending_issue' assert not automation.pending and automation.worker is None window._dsh_automation = automation monkeypatch.setattr(type(window), '_create_dsh_view', lambda self: HarnessView(self)) window.dsh_action.trigger() assert window._dsh_view.automation['status'] == 'pending_issue'