147 lines
5.3 KiB
Python
147 lines
5.3 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
|
|
|
import threading
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from PySide6.QtCore import QSettings, QThreadPool, Signal
|
|
from PySide6.QtWidgets import QApplication, QTextBrowser
|
|
|
|
from mdhelper import main_window, startup_scan
|
|
|
|
|
|
@pytest.fixture
|
|
def gui(tmp_path, monkeypatch):
|
|
app = QApplication.instance() or QApplication([])
|
|
monkeypatch.setattr(main_window, "QSettings", lambda *_: QSettings(str(tmp_path / "settings.ini"), QSettings.Format.IniFormat))
|
|
yield app
|
|
app.processEvents()
|
|
|
|
|
|
def wait_until(app, condition, seconds=8):
|
|
deadline = time.monotonic() + seconds
|
|
while not condition() and time.monotonic() < deadline:
|
|
app.processEvents()
|
|
time.sleep(0.005)
|
|
assert condition(), "Timed out waiting for the background directory scan"
|
|
|
|
|
|
def test_welcome_does_not_create_browser_process(gui, tmp_path, monkeypatch):
|
|
def unexpected_browser(_):
|
|
pytest.fail("Welcome page must not initialize WebEngine")
|
|
monkeypatch.setattr(main_window.MainWindow, "_create_reader_view", unexpected_browser)
|
|
window = main_window.MainWindow(None, app_root=tmp_path)
|
|
try:
|
|
assert isinstance(window.tabs.currentWidget(), QTextBrowser)
|
|
assert window.web_view is None
|
|
monkeypatch.setattr(window, "_welcome_html", lambda: "<h1>Updated welcome</h1>")
|
|
window.refresh_all_tab_appearances()
|
|
assert "Updated welcome" in window.tabs.currentWidget().toPlainText()
|
|
finally:
|
|
window.close()
|
|
|
|
|
|
def test_prewarmed_reader_replaces_welcome_without_empty_tab(gui, tmp_path, monkeypatch):
|
|
class StandbyReader(QTextBrowser):
|
|
loadFinished = Signal(bool)
|
|
|
|
def setHtml(self, html, _base_url=None):
|
|
super().setHtml(html)
|
|
self.loadFinished.emit(True)
|
|
|
|
window = main_window.MainWindow(None, app_root=tmp_path)
|
|
standby = StandbyReader()
|
|
standby.navigation_history = None
|
|
tab_counts: list[int] = []
|
|
try:
|
|
monkeypatch.setattr(window, "_create_reader_view", lambda: standby)
|
|
window._prewarm_reader_view()
|
|
assert window._standby_reader_view is standby
|
|
|
|
window.tabs.currentChanged.connect(lambda _index: tab_counts.append(window.tabs.count()))
|
|
target = window._prepare_target_tab(False, "Guide")
|
|
|
|
assert target is standby
|
|
assert window._standby_reader_view is None
|
|
assert window._welcome_tab_index() == -1
|
|
assert window.tabs.count() == 1
|
|
assert window.tabs.currentWidget() is standby
|
|
assert all(count > 0 for count in tab_counts)
|
|
finally:
|
|
window.close()
|
|
|
|
|
|
def test_directory_scan_does_not_block_first_window(gui, tmp_path, monkeypatch):
|
|
(tmp_path / "guide.md").write_text("# Guide\n", encoding="utf-8")
|
|
entered, release = threading.Event(), threading.Event()
|
|
original = startup_scan.discover_documents
|
|
def slow_scan(*args):
|
|
entered.set()
|
|
if not release.wait(8):
|
|
raise TimeoutError("Test did not release scanner")
|
|
return original(*args)
|
|
monkeypatch.setattr(startup_scan, "discover_documents", slow_scan)
|
|
window = main_window.MainWindow(tmp_path, defer_startup=True)
|
|
try:
|
|
assert not entered.is_set()
|
|
assert window.entries == []
|
|
window.show()
|
|
wait_until(gui, entered.is_set)
|
|
assert window.isVisible()
|
|
assert window._startup_scan_pending
|
|
window.search_box.setText("Guide")
|
|
gui.processEvents()
|
|
release.set()
|
|
wait_until(gui, lambda: not window._startup_scan_pending)
|
|
assert [entry.title for entry in window.entries] == ["Guide"]
|
|
assert window.search_index.search("Guide")
|
|
finally:
|
|
release.set()
|
|
QThreadPool.globalInstance().waitForDone(10000)
|
|
window.close()
|
|
|
|
|
|
def test_switching_projects_discards_old_scan(gui, tmp_path, monkeypatch):
|
|
old_root, new_root = tmp_path / "old", tmp_path / "new"
|
|
old_root.mkdir()
|
|
new_root.mkdir()
|
|
(old_root / "old.md").write_text("# Old\n", encoding="utf-8")
|
|
(new_root / "new.md").write_text("# New\n", encoding="utf-8")
|
|
entered, release = threading.Event(), threading.Event()
|
|
original = startup_scan.discover_documents
|
|
def slow_scan(*args):
|
|
entered.set()
|
|
release.wait(8)
|
|
return original(*args)
|
|
monkeypatch.setattr(startup_scan, "discover_documents", slow_scan)
|
|
window = main_window.MainWindow(old_root, defer_startup=True)
|
|
try:
|
|
wait_until(gui, entered.is_set)
|
|
window.set_project_root(new_root, save=False)
|
|
release.set()
|
|
QThreadPool.globalInstance().waitForDone(10000)
|
|
gui.processEvents()
|
|
assert window.project_root == new_root.resolve()
|
|
assert [entry.title for entry in window.entries] == ["New"]
|
|
finally:
|
|
release.set()
|
|
QThreadPool.globalInstance().waitForDone(10000)
|
|
window.close()
|
|
|
|
|
|
def test_closing_project_cancels_queued_startup(gui, tmp_path):
|
|
initial = tmp_path / "guide.md"
|
|
initial.write_text("# Guide\n", encoding="utf-8")
|
|
window = main_window.MainWindow(tmp_path, initial_path=initial, defer_startup=True)
|
|
try:
|
|
window.close_project_folder()
|
|
gui.processEvents()
|
|
assert not window.has_project
|
|
assert window.current_file is None
|
|
finally:
|
|
window.close()
|