"""Background check and foreground queue interaction regressions.""" from threading import Event import pytest from test_ui_filters import load_window from cbb_refresh_verify.verify_runner import VerifyResult def background(window, qtbot): started, release = Event(), Event() calls=[] def verify(request): calls.append(request.block_name) if request.block_name=='Beta': started.set() assert release.wait(5) return VerifyResult('CHECKED') window.verifier=verify window._run(['Beta','Gamma'],maintenance=True) qtbot.waitUntil(started.is_set) return release,calls def finish(window, qtbot, release): release.set() window.icdb_task.resume() qtbot.waitUntil(lambda:not window._icdb_active() and not window.busy) qtbot.waitUntil(lambda:window.pool.activeThreadCount()==0) def test_stop_whole_queue_does_not_resume_background_checks(qtbot,tmp_path,monkeypatch): window=load_window(qtbot,tmp_path,monkeypatch) release,calls=background(window,qtbot) try: window._start(lambda:None,lambda _:None) qtbot.waitUntil(lambda:bool(window.native_queue.pending)) window.stop_button.click() release.set() qtbot.waitUntil(lambda:not window._icdb_active() and not window.busy) assert calls==['Beta'],f'Cancelled queue still ran: {calls}' finally:finish(window,qtbot,release) def test_check_progress_survives_queued_verify(qtbot,tmp_path,monkeypatch): window=load_window(qtbot,tmp_path,monkeypatch, status_refresher=lambda names,path:{name:'PENDING' for name in names}) release,calls=background(window,qtbot) try: assert not window.icdb_progress.isHidden() window._run(['Gamma']) assert not window.icdb_progress.isHidden(),'Queued verify hid the active check progress' finally:finish(window,qtbot,release) def test_release_failure_is_not_labelled_as_read_write_prompt(qtbot,tmp_path,monkeypatch): window=load_window(qtbot,tmp_path,monkeypatch,verifier=lambda request:VerifyResult( 'FAIL',error_category='DATABASE_SAFETY_STOP',safe_to_continue=False, stderr='PCB_RELEASE_UNCONFIRMED: launcher still running')) window._run(['Beta'],maintenance=True) qtbot.waitUntil(lambda:not window._icdb_active()) cell=window.cbb_table.item(window._row_by_name['Beta'],3) assert '读写确认' not in cell.text(),cell.text() def test_pending_check_skips_a_target_that_has_become_ready(qtbot,tmp_path,monkeypatch): window=load_window(qtbot,tmp_path,monkeypatch) release,calls=background(window,qtbot) try: window.icdb_task.pause() release.set() qtbot.waitUntil(lambda:len(window.icdb_task.results)==1 and not window.icdb_task.running) window._apply_refreshed_records((('Gamma',),{'Gamma':'READY'},{},'')) assert 'Gamma' in window.ready_names window.icdb_task.resume() qtbot.waitUntil(lambda:not window._icdb_active()) assert calls==['Beta'],f'Already READY target was opened again: {calls}' finally:finish(window,qtbot,release) def test_worker_refresh_excludes_ready_before_any_gui_result_callback(qtbot,tmp_path,monkeypatch): window=load_window(qtbot,tmp_path,monkeypatch, status_refresher=lambda names,path:{name:'READY' for name in names}) release,calls=background(window,qtbot) try: library=window.mentor_lmc_path or window.loaded_path window._start(lambda:window._read_affected_records(('Gamma',),library=library),lambda payload:None) qtbot.waitUntil(lambda:bool(window.native_queue.pending)) release.set() qtbot.waitUntil(lambda:not window._icdb_active() and not window.busy) assert window._records_by_name['Gamma'].status=='UNKNOWN' assert calls==['Beta'] assert window.icdb_task.results[1].status=='SKIPPED' assert 'READY' in window.icdb_task.results[1].stderr finally:finish(window,qtbot,release) def test_another_library_ready_status_cannot_skip_this_library_target(qtbot,tmp_path,monkeypatch): window=load_window(qtbot,tmp_path,monkeypatch) release,calls=background(window,qtbot) try: window._publish_check_statuses('other.lmc',{'Gamma':'READY'}) release.set() qtbot.waitUntil(lambda:not window._icdb_active()) assert calls==['Beta','Gamma'] finally:finish(window,qtbot,release) @pytest.mark.parametrize('detail,safe,expected',[ ('Continue to open the design with read/write access permission?',True,'读写确认'), ('PCB_RELEASE_UNCONFIRMED: launcher still running',False,'释放未确认'), ('ICDB_RELEASE_UNCONFIRMED: database still occupied',False,'释放未确认'), ('PCB_SESSION_ACTIVE: target already open',True,'会话或数据库状态待确认'), ]) def test_safety_stop_label_tracks_actual_reason(qtbot,tmp_path,monkeypatch,detail,safe,expected): window=load_window(qtbot,tmp_path,monkeypatch,verifier=lambda request:VerifyResult( 'FAIL',error_category='DATABASE_SAFETY_STOP',safe_to_continue=safe,stderr=detail)) window._run(['Beta'],maintenance=True) qtbot.waitUntil(lambda:not window._icdb_active()) cell=window.cbb_table.item(window._row_by_name['Beta'],3) assert expected in cell.text() assert cell.toolTip()==detail assert cell.background().color().name()=='#fff8e1'