from threading import Event, Thread import time def test_native_queue_is_fifo_and_waiting_cancel_never_runs_action(): from cbb_refresh_verify.operation_queue import NativeQueue, QueueCancelled queue = NativeQueue() holding, release, waiting, cancelled = Event(), Event(), Event(), Event() order = [] first = Thread(target=lambda: queue.run(lambda: (holding.set(), release.wait(3), order.append('first')))) first.start(); assert holding.wait(1) def second(): try: queue.run(lambda: order.append('cancelled'), stop=cancelled, waiting=waiting.set) except QueueCancelled: pass second_thread = Thread(target=second); second_thread.start(); assert waiting.wait(1) cancelled.set(); second_thread.join(1); assert not second_thread.is_alive() third = Thread(target=lambda: queue.run(lambda: order.append('third'))) third.start(); release.set(); first.join(2); third.join(2) assert order == ['first', 'third'] def test_background_scan_keeps_ui_usable_and_yields_between_projects(qtbot, tmp_path, monkeypatch): from test_ui_filters import load_window from cbb_refresh_verify.verify_runner import VerifyResult started, release = Event(), Event() order = [] def verify(request): order.append(request.block_name) if request.block_name == 'Beta': started.set(); release.wait(3) return VerifyResult('CHECKED') window = load_window(qtbot, tmp_path, monkeypatch, verifier=verify) window._run(['Beta', 'Gamma'], maintenance=True) try: qtbot.waitUntil(started.is_set) assert not window.busy and window.search_edit.isEnabled() and window.load_button.isEnabled() window.search_edit.setText('Gamma') assert [r.name for r in window.visible_records()] == ['Gamma'] window._start(lambda: order.append('foreground'), lambda _: None) qtbot.wait(30) assert order == ['Beta'] release.set() qtbot.waitUntil(lambda: not window.icdb_task.active and not window.busy) assert order == ['Beta', 'foreground', 'Gamma'] assert window.search_edit.text() == 'Gamma' assert window.icdb_repair_button.isEnabled() finally: release.set() def test_unconfirmed_open_is_amber_and_does_not_claim_icdb_damage(qtbot, tmp_path, monkeypatch): from test_ui_filters import load_window from cbb_refresh_verify.verify_runner import VerifyResult window = load_window(qtbot, tmp_path, monkeypatch, verifier=lambda _: VerifyResult('FAIL', error_category='DATABASE_SAFETY_STOP', stderr='Continue to open read/write?')) window._run(['Beta'], maintenance=True) qtbot.waitUntil(lambda: not window.icdb_task.active) cell = window.cbb_table.item(window._row_by_name['Beta'], 3) assert '未确认' in cell.text() and 'ICDB 错误' not in cell.text() assert cell.background().color().name() == '#fff8e1' def test_completed_old_library_result_does_not_release_new_library(qtbot, tmp_path, monkeypatch): from test_ui_filters import load_window from cbb_refresh_verify.verify_runner import VerifyResult from pathlib import Path window = load_window(qtbot, tmp_path, monkeypatch, verifier=lambda _: VerifyResult('FAIL', safe_to_continue=False)) window._run(['Beta'], maintenance=True) qtbot.waitUntil(lambda: not window._icdb_active()) summary = window.icdb_summary window.lmc_edit.setText('other.lmc') next(button for button in summary.buttons() if '释放' in button.text()).click() assert getattr(window, 'release_dialog', None) is None assert '切' in window.status_label.text() def test_later_management_invalidates_old_recovery_offer(qtbot, tmp_path, monkeypatch): from test_ui_filters import load_window from test_reimport_recovery import failed_repair window = load_window(qtbot, tmp_path, monkeypatch, verifier=lambda _: failed_repair(tmp_path/'old/result.json', safe=True)) window._run(['Beta'], maintenance=True) qtbot.waitUntil(lambda: not window._icdb_active()) action = next(b for b in window.icdb_summary.buttons() if '副本' in b.text()) assert action.isEnabled() window._invalidate_check_targets('test.lmc', ['Beta']) assert not action.isEnabled() and not window._reimport_offers def test_check_release_action_tracks_busy_and_retained_queue(qtbot, tmp_path, monkeypatch): from test_ui_filters import load_window from test_review326_queue_guidance import retain from cbb_refresh_verify.verify_runner import VerifyResult window = load_window(qtbot, tmp_path, monkeypatch, verifier=lambda _: VerifyResult('FAIL', safe_to_continue=False)) window._run(['Beta'], maintenance=True) qtbot.waitUntil(lambda: not window._icdb_active()) action = next(b for b in window.icdb_summary.buttons() if '释放' in b.text()) window.busy = True window._update() assert not action.isEnabled(), 'A normal native call cannot admit release yet' retain(monkeypatch, window.mentor_lmc_path) window._queue_wait_library = window.mentor_lmc_path window._update() assert action.isEnabled(), 'Retained-queue release must stay reachable' opened = [] monkeypatch.setattr(window, 'check_library_release', lambda **kwargs: opened.append(kwargs)) action.click() assert opened == [{'queued': True}] window.busy = False window._update() assert action.isEnabled()