from mdhelper.document_session import DocumentStore from mdhelper.file_watch import FileWatchService, WatchContext from mdhelper.project_catalog import ProjectCatalog def test_catalog_updates_titles_search_and_removed_documents(tmp_path): note = tmp_path / "note.md" note.write_text("# First\nold text", encoding="utf-8") catalog = ProjectCatalog() assert catalog.scan(tmp_path, ("markdown",)) assert catalog.search_index.search("old text") assert not catalog.scan(tmp_path, ("markdown",)) note.write_text("# New title\nnew text", encoding="utf-8") assert catalog.scan(tmp_path, ("markdown",)) assert catalog.entries[0].title == "New title" assert catalog.search_index.search("new text") note.unlink() assert catalog.scan(tmp_path, ("markdown",)) assert not catalog.entries and not catalog.search_index.search("new text") def test_monitor_distinguishes_internal_external_and_resource_changes(qt_application, tmp_path): note, image = tmp_path / "note.md", tmp_path / "picture.png" note.write_text("# Note\n![image](picture.png)", encoding="utf-8") image.write_bytes(b"image") mode = ["resources"] refreshed, trees = [], [] monitor = FileWatchService(None, WatchContext( lambda: tmp_path, lambda: mode[0], lambda: {note}, lambda p: p.suffix == ".md", lambda: False, refreshed.append, lambda: trees.append(True)), DocumentStore.signature) monitor.file_watcher.blockSignals(True) # Deliver deterministic events below. try: monitor.update_paths() assert image in monitor.watched_file_signatures monitor.internal_save_paths.add(note) note.write_text("# Note saved\n![image](picture.png)", encoding="utf-8") monitor.on_file_changed(str(note)) monitor.refresh_changed_documents() assert not refreshed and not monitor.pending_changed_files note.write_text("# Externally changed\n![image](picture.png)", encoding="utf-8") monitor.on_file_changed(str(note)) assert monitor.refresh_timer.isActive() monitor.refresh_timer.stop() monitor.refresh_changed_documents() assert refreshed == [{note}] and not trees image.write_bytes(b"changed image data") monitor.on_file_changed(str(image)) monitor.refresh_timer.stop() monitor.refresh_changed_documents() assert refreshed == [{note}, {note}] mode[0] = "current" monitor.update_paths() assert image not in monitor.watched_file_signatures assert not monitor.affected_documents({image}) monitor.refresh_timer.start() monitor.tree_refresh_timer.start() monitor.reset() assert not monitor.refresh_timer.isActive() and not monitor.tree_refresh_timer.isActive() assert not monitor.file_watcher.files() and not monitor.file_watcher.directories() finally: monitor.reset()