|
| 1 | +# tests/gui/main_window/test_user_config.py |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.gui |
| 10 | +class TestUserConfigPersistence: |
| 11 | + def test_valid_config_file_path_accepts_existing_file(self, window, tmp_path: Path): |
| 12 | + w = window |
| 13 | + |
| 14 | + config_path = tmp_path / "dlclive_config.json" |
| 15 | + config_path.write_text("{}", encoding="utf-8") |
| 16 | + |
| 17 | + assert w._valid_config_file_path(str(config_path)) == config_path.resolve() |
| 18 | + |
| 19 | + def test_valid_config_file_path_rejects_missing_file(self, window, tmp_path: Path): |
| 20 | + w = window |
| 21 | + |
| 22 | + missing = tmp_path / "missing_config.json" |
| 23 | + |
| 24 | + assert w._valid_config_file_path(str(missing)) is None |
| 25 | + assert w._valid_config_file_path(None) is None |
| 26 | + assert w._valid_config_file_path("") is None |
| 27 | + |
| 28 | + def test_suggest_config_dialog_path_prefers_current_config_path(self, window, tmp_path: Path): |
| 29 | + w = window |
| 30 | + |
| 31 | + config_path = tmp_path / "current_config.json" |
| 32 | + config_path.write_text("{}", encoding="utf-8") |
| 33 | + |
| 34 | + w._config_path = config_path |
| 35 | + |
| 36 | + assert w._suggest_config_dialog_path() == str(config_path) |
| 37 | + |
| 38 | + def test_suggest_config_dialog_path_uses_last_config_path_when_current_path_missing( |
| 39 | + self, |
| 40 | + monkeypatch, |
| 41 | + window, |
| 42 | + tmp_path: Path, |
| 43 | + ): |
| 44 | + w = window |
| 45 | + |
| 46 | + config_path = tmp_path / "last_config.json" |
| 47 | + config_path.write_text("{}", encoding="utf-8") |
| 48 | + |
| 49 | + w._config_path = None |
| 50 | + monkeypatch.setattr(w._settings_store, "get_last_config_path", lambda: str(config_path)) |
| 51 | + |
| 52 | + assert w._suggest_config_dialog_path() == str(config_path.resolve()) |
| 53 | + |
| 54 | + def test_suggest_config_dialog_path_uses_parent_of_missing_last_config( |
| 55 | + self, |
| 56 | + monkeypatch, |
| 57 | + window, |
| 58 | + tmp_path: Path, |
| 59 | + ): |
| 60 | + w = window |
| 61 | + |
| 62 | + missing_config_path = tmp_path / "missing_config.json" |
| 63 | + |
| 64 | + w._config_path = None |
| 65 | + monkeypatch.setattr(w._settings_store, "get_last_config_path", lambda: str(missing_config_path)) |
| 66 | + |
| 67 | + assert w._suggest_config_dialog_path() == str(missing_config_path) |
| 68 | + |
| 69 | + def test_save_config_to_path_persists_last_path_snapshot_and_syncs( |
| 70 | + self, |
| 71 | + monkeypatch, |
| 72 | + window, |
| 73 | + tmp_path: Path, |
| 74 | + ): |
| 75 | + w = window |
| 76 | + |
| 77 | + calls: list[tuple[str, object]] = [] |
| 78 | + |
| 79 | + class FakeConfig: |
| 80 | + def save(self, path: Path | str) -> None: |
| 81 | + Path(path).write_text("{}", encoding="utf-8") |
| 82 | + |
| 83 | + class FakeSettings: |
| 84 | + def sync(self) -> None: |
| 85 | + calls.append(("sync", True)) |
| 86 | + |
| 87 | + config_path = tmp_path / "saved_config.json" |
| 88 | + fake_config = FakeConfig() |
| 89 | + |
| 90 | + monkeypatch.setattr(w, "_current_config", lambda allow_empty_model_path=False: fake_config) |
| 91 | + monkeypatch.setattr( |
| 92 | + w._settings_store, |
| 93 | + "set_last_config_path", |
| 94 | + lambda path: calls.append(("last_path", path)), |
| 95 | + ) |
| 96 | + monkeypatch.setattr( |
| 97 | + w._settings_store, |
| 98 | + "save_full_config_snapshot", |
| 99 | + lambda cfg: calls.append(("snapshot", cfg)), |
| 100 | + ) |
| 101 | + monkeypatch.setattr(w, "settings", FakeSettings()) |
| 102 | + |
| 103 | + assert w._save_config_to_path(config_path) is True |
| 104 | + assert config_path.exists() |
| 105 | + assert ("last_path", str(config_path.resolve())) in calls |
| 106 | + assert ("snapshot", fake_config) in calls |
| 107 | + assert ("sync", True) in calls |
| 108 | + |
| 109 | + def test_save_config_to_path_returns_false_without_persisting_after_failure( |
| 110 | + self, |
| 111 | + monkeypatch, |
| 112 | + window, |
| 113 | + tmp_path: Path, |
| 114 | + ): |
| 115 | + w = window |
| 116 | + |
| 117 | + calls: list[tuple[str, object]] = [] |
| 118 | + errors: list[str] = [] |
| 119 | + |
| 120 | + class FakeConfig: |
| 121 | + def save(self, path: Path | str) -> None: |
| 122 | + raise OSError("cannot save") |
| 123 | + |
| 124 | + config_path = tmp_path / "failed_config.json" |
| 125 | + |
| 126 | + monkeypatch.setattr(w, "_current_config", lambda allow_empty_model_path=False: FakeConfig()) |
| 127 | + monkeypatch.setattr( |
| 128 | + w._settings_store, |
| 129 | + "set_last_config_path", |
| 130 | + lambda path: calls.append(("last_path", path)), |
| 131 | + ) |
| 132 | + monkeypatch.setattr( |
| 133 | + w._settings_store, |
| 134 | + "save_full_config_snapshot", |
| 135 | + lambda cfg: calls.append(("snapshot", cfg)), |
| 136 | + ) |
| 137 | + monkeypatch.setattr(w, "_show_error", errors.append) |
| 138 | + |
| 139 | + assert w._save_config_to_path(config_path) is False |
| 140 | + assert not config_path.exists() |
| 141 | + assert calls == [] |
| 142 | + assert errors |
0 commit comments