import threading import time import pytest from PySide6.QtCore import QSettings from mdhelper.doc_check import check_documents from mdhelper.export_jobs import ExportCancelled from mdhelper import main_window def test_nested_documents_anchors_and_buffer_snapshot(tmp_path): folder = tmp_path / 'notes'; folder.mkdir() source = folder / 'source.md' target = folder / 'target.md' target.write_text('# 中文\n\n# 中文\n\nSetext\n------\n', encoding='utf-8') source.write_text('[a](target.md#%E4%B8%AD%E6%96%87-2)\n[b](target.md#setext)\n[c](#absent)\n![d](lost.png)\n`[ignored](lost.md)`', encoding='utf-8') page = folder / 'page.html' page.write_text('okbad', encoding='utf-8') excluded = tmp_path / 'build'; excluded.mkdir() (excluded / 'ignore.md').write_text('[bad](gone.md)') issues = check_documents(tmp_path) assert [(i.source.name, i.line_number, i.message) for i in issues] == [ ('page.html', 1, '章节锚点不存在'), ('source.md', 3, '章节锚点不存在'), ('source.md', 4, '本地目标不存在')] assert not check_documents(tmp_path, {source: '# absent', page: 'ok'}) cancel = threading.Event(); cancel.set() with pytest.raises(ExportCancelled): check_documents(tmp_path, cancel=cancel) def test_panel_scans_and_opens_source_line(qt_application, tmp_path, monkeypatch): settings = QSettings(str(tmp_path / 'settings.ini'), QSettings.Format.IniFormat) monkeypatch.setattr(main_window, 'QSettings', lambda *_: settings) source = tmp_path / 'note.md'; source.write_text('# Note\n\n[broken](gone.md)') window = main_window.MainWindow(tmp_path) try: window.show_document_check() panel = window._document_check_dialog deadline = time.monotonic() + 10 while panel.job is not None and time.monotonic() < deadline: qt_application.processEvents(); time.sleep(.01) assert panel.job is None and panel.results.topLevelItemCount() == 1 panel.open_issue(panel.results.topLevelItem(0)) editor = window.tabs.currentWidget() assert editor.session.path == source and editor.editor.textCursor().blockNumber() == 2 editor.replace_source_undoable('# Buffered\n\n[other](other.md)') assert window._editor_sources()[source].startswith('# Buffered') editor.mark_saved(source, editor.source_text(), window._file_signature(source)) finally: window.close()