from pathlib import Path 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 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) def test_restore_tab_order_editor_mode_and_lazy_background(qt_application, tmp_path, monkeypatch): 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) 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 [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 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()