fix: preserve catalog state and verify PCB write access

This commit is contained in:
阿白叔 committed 2026-11-20 23:24:47 +08:00
1 parent 3efeaed656
commit 86837155ea
11 files changed
+651 -92

No files matched your search

+99
View File
@@ -649,3 +649,102 @@ def test_recovery_retry_rejects_old_external_progress_and_choice(qtbot,tmp_path,
assert choice.decision=='cancel'
finally:
release.set(); qtbot.waitUntil(lambda:not dialog.busy)
@pytest.mark.parametrize('cancel', [True, False])
def test_uncommitted_catalog_refresh_preserves_last_full_result(qtbot, tmp_path, monkeypatch, cancel):
from cbb_refresh_verify.management import Catalog, CatalogEntry
from cbb_refresh_verify.ui import main_window as ui
reading, release = Event(), Event()
calls = []
snapshot = tmp_path/'status-refresh.jsonl'
def read_status(names, path, checkpoint_path=None):
calls.append(names)
if checkpoint_path is not None:
checkpoint_path.parent.mkdir(parents=True, exist_ok=True)
checkpoint_path.write_text('full scan' if len(calls) == 1 else 'partial scan', encoding='utf-8')
if len(calls) == 2:
reading.set()
release.wait(5)
return {name: 'READY' for name in names}
monkeypatch.setattr(ui, 'authoritative_status_map', read_status)
window = window_for(qtbot, tmp_path, monkeypatch,
reader=lambda _: LibraryReadResult((CbbRecord('A', 'LP'),), directory_count=1),
catalog_reader=lambda path, _: Catalog(path, 1, (CatalogEntry('A', 'Logical-Physical'),)))
try:
load(window, qtbot)
qtbot.waitUntil(lambda: window.ready_names == {'A'})
reports = []
monkeypatch.setattr(window, '_publish_check_statuses',
lambda library, statuses, lease: reports.append(dict(statuses)))
original_replace = Path.replace
def replace_file(path, target):
if target == snapshot:
raise PermissionError('snapshot is held')
return original_replace(path, target)
if not cancel:
monkeypatch.setattr(Path, 'replace', replace_file)
window.load_button.click()
qtbot.waitUntil(reading.is_set)
assert window.ready_names == {'A'}
assert not window.batch_button.isEnabled()
assert snapshot.read_text(encoding='utf-8') == 'full scan'
if cancel:
window.stop_button.click()
release.set()
qtbot.waitUntil(lambda: not any(job.kind == 'catalog' for job in window._jobs.values()))
assert ('已取消读取' if cancel else 'snapshot is held') in window.status_label.text()
assert reports == []
assert window.ready_names == {'A'}
assert snapshot.read_text(encoding='utf-8') == 'full scan'
window.status_refresher(('A',), Path('a.lmc'))
assert snapshot.read_text(encoding='utf-8') == 'full scan'
finally:
release.set()
window.pool.waitForDone(6000)
@pytest.mark.parametrize('cancelled', [False, True])
def test_external_catalog_owner_wait_retries_once_or_cancels(qtbot, tmp_path, monkeypatch, cancelled):
from cbb_refresh_verify.management import Catalog, CatalogEntry
from cbb_refresh_verify.native_management import NativeOperationError
probing, released = Event(), Event()
calls = []
def read_catalog(path, evidence):
calls.append(evidence)
if len(calls) == 1:
raise NativeOperationError('LIBRARY_MANAGER_BUSY: 已有 Library Manager 运行。', safe_to_continue=True)
return Catalog(path, 1, (CatalogEntry('A', 'Logical-Physical'),))
def owners(*args, **kwargs):
probing.set()
return [] if released.is_set() else [{'pid': 123}]
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', owners)
window = window_for(qtbot, tmp_path, monkeypatch,
reader=lambda _: LibraryReadResult((CbbRecord('A', 'LP'),), directory_count=1),
catalog_reader=read_catalog, status_refresher=lambda names, _: {name: 'READY' for name in names})
try:
load(window, qtbot)
qtbot.waitUntil(probing.is_set)
assert not window.native_queue.active
if cancelled:
window.stop_button.click()
released.set()
qtbot.waitUntil(lambda: not any(job.kind == 'catalog' for job in window._jobs.values()), timeout=4000)
assert len(calls) == (1 if cancelled else 2)
if cancelled:
assert '已取消读取' in window.status_label.text()
else:
assert calls[0] != calls[1]
assert window.ready_names == {'A'}
finally:
released.set()
window.pool.waitForDone(6000)