Files
MDHelper/tests/test_workspace_state.py
T

138 lines
7.0 KiB
Python

from pathlib import Path
import time
import pytest
from PySide6.QtCore import QSettings
from mdhelper import main_window
from mdhelper.markdown_editor import MarkdownEditorDialog
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()
good, outside = root / 'note.md', tmp_path / 'outside.md'
good.write_text('# Good'); outside.write_text('# Outside')
store = WorkspaceStore(settings)
store.save(root, [TabState(str(root / 'missing.md')), TabState(str(outside)), TabState(str(good), True)], 2)
states, current = store.load(root)
assert [s.path for s in states] == [str(good)] and current == 0 and states[0].editing
settings.setValue(store.key(root), '{"tabs":{}}')
assert store.load(root) == ([], 0)
@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]))], active)
window = main_window.MainWindow(tmp_path, defer_startup=True)
try:
seed = window.tabs.currentWidget()
window.prepare_reader_before_show()
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
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)
assert editor.view_mode_box.currentData() == 'source'
assert not editor.sync_scroll_checkbox.isChecked() and editor.editor.textCursor().position() == 4
assert window.tabs.count() == 3
window.close()
states, active = WorkspaceStore(settings).load(tmp_path)
assert active == 0 and states[0].editing and states[0].view == 'source'
finally:
window.close()
def test_explicit_launch_file_takes_precedence_over_workspace(qt_application, tmp_path, monkeypatch):
settings = QSettings(str(tmp_path / 'settings.ini'), QSettings.Format.IniFormat)
monkeypatch.setattr(main_window, 'QSettings', lambda *_: settings)
first, second = tmp_path / 'first.md', tmp_path / 'second.md'
first.write_text('# First'); second.write_text('# Second')
WorkspaceStore(settings).save(tmp_path, [TabState(str(first))], 0)
window = main_window.MainWindow(tmp_path, initial_path=second, defer_startup=True)
try:
window.prepare_reader_before_show()
window._finish_startup()
assert window.current_file == second and window.tabs.count() == 1
finally:
window.close()