fix: restore only the directory at startup and load tabs on request
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
from pathlib import Path
|
||||
import time
|
||||
import pytest
|
||||
from PySide6.QtCore import QSettings
|
||||
from mdhelper import main_window
|
||||
from mdhelper.markdown_editor import MarkdownEditorDialog
|
||||
@@ -6,6 +8,70 @@ from mdhelper.tab_controller import document_path
|
||||
from mdhelper.workspace_state import TabState, WorkspaceStore
|
||||
|
||||
|
||||
def wait_until(app, condition):
|
||||
deadline=time.monotonic()+10
|
||||
while not condition() and time.monotonic()<deadline:
|
||||
app.processEvents();time.sleep(.01)
|
||||
assert condition()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('legacy_restore', [None, True, False])
|
||||
@pytest.mark.parametrize('deferred', [False, True])
|
||||
def test_startup_restores_directory_without_any_document_tabs(qt_application,tmp_path,monkeypatch,legacy_restore,deferred):
|
||||
settings=QSettings(str(tmp_path/'settings.ini'),QSettings.Format.IniFormat)
|
||||
monkeypatch.setattr(main_window,'QSettings',lambda *_:settings)
|
||||
note=tmp_path/'history.md';note.write_text('# History\n\nSaved document text',encoding='utf8')
|
||||
settings.setValue('project/last_root',str(tmp_path))
|
||||
if legacy_restore is not None:settings.setValue('workspace/restore',legacy_restore)
|
||||
store=WorkspaceStore(settings)
|
||||
store.save(tmp_path,[TabState(str(note),editing=True)],0)
|
||||
window=main_window.MainWindow(tmp_path,defer_startup=deferred)
|
||||
try:
|
||||
window.prepare_reader_before_show()
|
||||
host=window.tabs.currentWidget()
|
||||
reader=host.reader_view
|
||||
window.show()
|
||||
wait_until(qt_application,lambda:not window._startup_scan_pending)
|
||||
assert window.has_project and window.project_root==tmp_path.resolve()
|
||||
assert [entry.path for entry in window.entries]==[note]
|
||||
assert window.current_file is None and window.windowTitle()=='MDHelper'
|
||||
assert window.tabs.count()==1 and window.tabs.currentWidget() is host
|
||||
assert host.property('is_welcome') and document_path(host) is None
|
||||
assert getattr(host,'workspace_state',None) is None
|
||||
assert host.stack.currentWidget() is host.welcome_view
|
||||
assert not window.restore_workspace_action.isCheckable()
|
||||
assert settings.value('project/last_root')==str(tmp_path)
|
||||
# A manual open uses the prepared host and displays content immediately
|
||||
# after loading; it does not need the user to close a placeholder tab.
|
||||
loaded=[];reader.loadFinished.connect(loaded.append)
|
||||
window.open_document(note)
|
||||
wait_until(qt_application,lambda:bool(loaded) and loaded[-1])
|
||||
assert window.tabs.count()==1 and window.tabs.currentWidget() is host
|
||||
assert host.reader_view is reader and host.stack.currentWidget() is reader
|
||||
text=[];reader.page().toPlainText(text.append)
|
||||
wait_until(qt_application,lambda:bool(text))
|
||||
assert 'Saved document text' in text[0]
|
||||
assert not window.restore_workspace_action.isEnabled()
|
||||
finally:window.close()
|
||||
|
||||
|
||||
def test_directory_only_session_preserves_manual_restore_history(qt_application,tmp_path,monkeypatch):
|
||||
settings=QSettings(str(tmp_path/'settings.ini'),QSettings.Format.IniFormat)
|
||||
monkeypatch.setattr(main_window,'QSettings',lambda *_:settings)
|
||||
note=tmp_path/'history.md';note.write_text('# History',encoding='utf8')
|
||||
store=WorkspaceStore(settings);store.save(tmp_path,[TabState(str(note))],0)
|
||||
saved=settings.value(store.key(tmp_path))
|
||||
window=main_window.MainWindow(tmp_path)
|
||||
window.prepare_reader_before_show()
|
||||
window.close()
|
||||
assert settings.value(store.key(tmp_path))==saved
|
||||
window=main_window.MainWindow(tmp_path)
|
||||
try:
|
||||
window.close_project_folder()
|
||||
assert settings.value(store.key(tmp_path))==saved
|
||||
finally:window.close()
|
||||
|
||||
|
||||
def test_workspace_validation_filters_deleted_and_outside_files(tmp_path):
|
||||
settings = QSettings(str(tmp_path / 'settings.ini'), QSettings.Format.IniFormat)
|
||||
root = tmp_path / 'project'; root.mkdir()
|
||||
@@ -19,20 +85,30 @@ def test_workspace_validation_filters_deleted_and_outside_files(tmp_path):
|
||||
assert store.load(root) == ([], 0)
|
||||
|
||||
|
||||
def test_restore_tab_order_editor_mode_and_lazy_background(qt_application, tmp_path, monkeypatch):
|
||||
@pytest.mark.parametrize('active', [0, 1, 2])
|
||||
def test_restore_tab_order_editor_mode_and_lazy_background(qt_application, tmp_path, monkeypatch, active):
|
||||
settings = QSettings(str(tmp_path / 'settings.ini'), QSettings.Format.IniFormat)
|
||||
monkeypatch.setattr(main_window, 'QSettings', lambda *_: settings)
|
||||
paths = [tmp_path / f'{n}.md' for n in range(3)]
|
||||
for path in paths: path.write_text('# Example\n\nText', encoding='utf-8')
|
||||
WorkspaceStore(settings).save(tmp_path, [TabState(str(paths[0]), True, 'source', False, 4),
|
||||
TabState(str(paths[1])), TabState(str(paths[2]))], 1)
|
||||
TabState(str(paths[1])), TabState(str(paths[2]))], active)
|
||||
window = main_window.MainWindow(tmp_path, defer_startup=True)
|
||||
try:
|
||||
seed = window.tabs.currentWidget()
|
||||
window.prepare_reader_before_show()
|
||||
assert window.tabs.currentIndex() == 1 and window.tabs.widget(1) is seed
|
||||
assert window.tabs.count()==1 and window.current_file is None
|
||||
window.restore_workspace_action.trigger()
|
||||
assert window.tabs.currentIndex() == active
|
||||
assert [document_path(window.tabs.widget(i)) for i in range(3)] == paths
|
||||
assert window.tabs.widget(0).reader_view is None and window.tabs.widget(2).reader_view is None
|
||||
for index in range(3):
|
||||
if index != active:assert window.tabs.widget(index).reader_view is None
|
||||
if active != 0:
|
||||
assert window.tabs.widget(active) is seed
|
||||
wait_until(qt_application,lambda:seed.stack.currentWidget() is seed.reader_view)
|
||||
text=[];seed.reader_view.page().toPlainText(text.append)
|
||||
wait_until(qt_application,lambda:bool(text))
|
||||
assert 'Example' in text[0]
|
||||
window.tabs.setCurrentIndex(0)
|
||||
editor = window.tabs.currentWidget()
|
||||
assert isinstance(editor, MarkdownEditorDialog)
|
||||
|
||||
Reference in New Issue
Block a user