from dataclasses import asdict from PySide6.QtCore import QSettings from mdhelper.preferences import ApplicationConfiguration, ConfigurationStore, ReaderPreferences from mdhelper.export_settings import PageSettings def test_local_settings_and_json_use_the_same_schema(tmp_path): settings = QSettings(str(tmp_path / "settings.ini"), QSettings.Format.IniFormat) store = ConfigurationStore(settings) config = ApplicationConfiguration.from_payload({ "reader": {"theme_color": "colorful", "theme_background": "cloud", "document_font_size": 22, "custom_document_styles": {"custom:demo": {"name": "Demo", "css": "/* kept */ p {color: red}"}}, "document_style_id": "custom:demo", "document_file_groups": ["markdown", "python"]}, "recent_files": [str(tmp_path / "note.md")], "bookmarks": [{"title": "Example"}], "scroll_positions": {"note.md": 123}, "external_applications": {".TXT": "viewer.exe"}, "page_settings": asdict(PageSettings(paper="Letter", header="header", toc_depth=4)), }) store.save(config) local = store.load() portable = ApplicationConfiguration.from_payload(config.to_payload("0.3.1")) assert local == portable == config assert "/* kept */" in local.reader.custom_document_styles["custom:demo"]["css"] assert "editor/drafts" not in config.to_payload("0.3.1") def test_legacy_settings_migrate_without_losing_pdf_margins(tmp_path): settings = QSettings(str(tmp_path / "legacy.ini"), QSettings.Format.IniFormat) settings.setValue("reader/theme", "dark") settings.setValue("reader/font_scale", "1.25") settings.setValue("reader/auto_refresh", "false") settings.setValue("pdf/top_margin_mm", 8) store = ConfigurationStore(settings) config = store.load() assert config.reader.theme_color == "black" and config.reader.document_font_size == 20 assert config.reader.auto_refresh_mode == "off" assert config.page_settings.top == 8 store.save(config) assert store.load() == config def test_invalid_configuration_values_have_safe_defaults(): preferences = ReaderPreferences.from_mapping({"theme_color": {}, "theme_background": [], "document_font_size": "NaN", "ui_font_point_size": "inf", "wheel_scroll_speed": "invalid", "reader_width_scale": None, "math_engine": [], "custom_document_styles": [], "auto_refresh_delay_ms": "not a number"}) assert preferences == ReaderPreferences() def test_importing_a_partial_configuration_preserves_other_settings(): base = ApplicationConfiguration(reader=ReaderPreferences(document_font_size=20, font_scale=1.25), bookmarks=[{"title": "keep"}], page_settings=PageSettings(header="keep")) changed = ApplicationConfiguration.from_payload({"reader": {"theme_color": "black"}}, base) assert changed.reader.document_font_size == 20 and changed.reader.theme == "dark" assert changed.bookmarks == base.bookmarks and changed.page_settings == base.page_settings