|
| 1 | +"""Tests for brain.user_preferences — GUI-surfaceable cadence file.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from brain.user_preferences import ( |
| 8 | + DEFAULT_DREAM_EVERY_HOURS, |
| 9 | + UserPreferences, |
| 10 | + read_raw_keys, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +def test_load_missing_file_returns_defaults(tmp_path: Path) -> None: |
| 15 | + prefs = UserPreferences.load(tmp_path / "nope.json") |
| 16 | + assert prefs.dream_every_hours == DEFAULT_DREAM_EVERY_HOURS |
| 17 | + |
| 18 | + |
| 19 | +def test_load_well_formed_file(tmp_path: Path) -> None: |
| 20 | + path = tmp_path / "user_preferences.json" |
| 21 | + path.write_text('{"dream_every_hours": 12.0}\n', encoding="utf-8") |
| 22 | + prefs = UserPreferences.load(path) |
| 23 | + assert prefs.dream_every_hours == 12.0 |
| 24 | + |
| 25 | + |
| 26 | +def test_load_corrupt_json_returns_defaults(tmp_path: Path) -> None: |
| 27 | + path = tmp_path / "user_preferences.json" |
| 28 | + path.write_text("{not json", encoding="utf-8") |
| 29 | + prefs = UserPreferences.load(path) |
| 30 | + assert prefs.dream_every_hours == DEFAULT_DREAM_EVERY_HOURS |
| 31 | + |
| 32 | + |
| 33 | +def test_load_non_object_payload_returns_defaults(tmp_path: Path) -> None: |
| 34 | + path = tmp_path / "user_preferences.json" |
| 35 | + path.write_text("[1, 2, 3]", encoding="utf-8") |
| 36 | + prefs = UserPreferences.load(path) |
| 37 | + assert prefs.dream_every_hours == DEFAULT_DREAM_EVERY_HOURS |
| 38 | + |
| 39 | + |
| 40 | +def test_load_wrong_field_type_returns_defaults(tmp_path: Path) -> None: |
| 41 | + path = tmp_path / "user_preferences.json" |
| 42 | + path.write_text('{"dream_every_hours": "not-a-number"}', encoding="utf-8") |
| 43 | + prefs = UserPreferences.load(path) |
| 44 | + assert prefs.dream_every_hours == DEFAULT_DREAM_EVERY_HOURS |
| 45 | + |
| 46 | + |
| 47 | +def test_save_round_trip(tmp_path: Path) -> None: |
| 48 | + path = tmp_path / "user_preferences.json" |
| 49 | + UserPreferences(dream_every_hours=8.0).save(path) |
| 50 | + assert UserPreferences.load(path).dream_every_hours == 8.0 |
| 51 | + |
| 52 | + |
| 53 | +def test_save_is_atomic(tmp_path: Path) -> None: |
| 54 | + path = tmp_path / "user_preferences.json" |
| 55 | + UserPreferences(dream_every_hours=12.0).save(path) |
| 56 | + assert path.exists() |
| 57 | + assert not path.with_suffix(path.suffix + ".new").exists() |
| 58 | + |
| 59 | + |
| 60 | +# ---- read_raw_keys ---- |
| 61 | + |
| 62 | + |
| 63 | +def test_read_raw_keys_missing_file(tmp_path: Path) -> None: |
| 64 | + assert read_raw_keys(tmp_path / "missing.json") == set() |
| 65 | + |
| 66 | + |
| 67 | +def test_read_raw_keys_returns_present_keys(tmp_path: Path) -> None: |
| 68 | + path = tmp_path / "user_preferences.json" |
| 69 | + path.write_text('{"dream_every_hours": 8.0, "future_field": 1}', encoding="utf-8") |
| 70 | + assert read_raw_keys(path) == {"dream_every_hours", "future_field"} |
| 71 | + |
| 72 | + |
| 73 | +def test_read_raw_keys_corrupt_json_returns_empty(tmp_path: Path) -> None: |
| 74 | + path = tmp_path / "user_preferences.json" |
| 75 | + path.write_text("not json", encoding="utf-8") |
| 76 | + assert read_raw_keys(path) == set() |
| 77 | + |
| 78 | + |
| 79 | +def test_read_raw_keys_non_object_returns_empty(tmp_path: Path) -> None: |
| 80 | + path = tmp_path / "user_preferences.json" |
| 81 | + path.write_text("[1, 2, 3]", encoding="utf-8") |
| 82 | + assert read_raw_keys(path) == set() |
0 commit comments