"""last_wifi_target 持久化 helper 单元测试。 覆盖: - set_last_wifi_target 写入;get 读到正确字段 - set_last_wifi_target("") 等价于 clear - clear_last_wifi_target 删除已有键不影响其他键 - 读不到键时 get 返回空 dict - 损坏的键值(非 dict / 缺 target)返回空 dict """ from __future__ import annotations import json from pathlib import Path from unittest.mock import patch import pytest from gui.gui_core_pkg import config as cfg_mod @pytest.fixture def temp_config(tmp_path, monkeypatch): """把 CONFIG_PATH 临时指向 tmp_path/config.json。""" p = tmp_path / "config.json" p.write_text("{}", encoding="utf-8") monkeypatch.setattr(cfg_mod, "CONFIG_PATH", str(p)) return p class TestSetGetClearLastWifiTarget: def test_initial_empty(self, temp_config): assert cfg_mod.get_last_wifi_target() == {} def test_set_and_get(self, temp_config): cfg_mod.set_last_wifi_target("192.168.5.2:5555", "PEPM00") result = cfg_mod.get_last_wifi_target() assert result["target"] == "192.168.5.2:5555" assert result["model"] == "PEPM00" assert isinstance(result.get("saved_at"), int) assert result["saved_at"] > 0 def test_set_overwrites(self, temp_config): cfg_mod.set_last_wifi_target("10.0.0.1:5555", "old") cfg_mod.set_last_wifi_target("192.168.5.2:5555", "new") result = cfg_mod.get_last_wifi_target() assert result["target"] == "192.168.5.2:5555" assert result["model"] == "new" def test_set_empty_clears(self, temp_config): cfg_mod.set_last_wifi_target("192.168.5.2:5555", "PEPM00") cfg_mod.set_last_wifi_target("", "") assert cfg_mod.get_last_wifi_target() == {} def test_clear_removes_key(self, temp_config): cfg_mod.set_last_wifi_target("192.168.5.2:5555", "PEPM00") cfg_mod.clear_last_wifi_target() assert cfg_mod.get_last_wifi_target() == {} # 文件里不应再有 last_wifi_target 键 data = json.loads(temp_config.read_text(encoding="utf-8")) assert "last_wifi_target" not in data def test_clear_preserves_other_keys(self, temp_config): # 先写入其它配置 temp_config.write_text( json.dumps({"theme": "dark", "y_pianyi": 100}), encoding="utf-8" ) cfg_mod.set_last_wifi_target("192.168.5.2:5555", "PEPM00") cfg_mod.clear_last_wifi_target() # 其他键应保留 data = json.loads(temp_config.read_text(encoding="utf-8")) assert data.get("theme") == "dark" assert data.get("y_pianyi") == 100 assert "last_wifi_target" not in data def test_set_preserves_other_keys(self, temp_config): temp_config.write_text( json.dumps({"theme": "dark", "y_pianyi": 100}), encoding="utf-8" ) cfg_mod.set_last_wifi_target("192.168.5.2:5555", "PEPM00") data = json.loads(temp_config.read_text(encoding="utf-8")) assert data.get("theme") == "dark" assert data.get("y_pianyi") == 100 assert data["last_wifi_target"]["target"] == "192.168.5.2:5555" class TestGetLastWifiTargetRobustness: def test_corrupted_value_returns_empty(self, temp_config): """value 不是 dict 时返回空 dict。""" temp_config.write_text( json.dumps({"last_wifi_target": "not a dict"}), encoding="utf-8" ) assert cfg_mod.get_last_wifi_target() == {} def test_dict_without_target_returns_empty(self, temp_config): """dict 但缺 target 字段 → 视为无效。""" temp_config.write_text( json.dumps({"last_wifi_target": {"model": "X", "saved_at": 0}}), encoding="utf-8", ) assert cfg_mod.get_last_wifi_target() == {} def test_missing_config_file(self, tmp_path, monkeypatch): """配置文件不存在时 get 返回空 dict 不抛异常。""" non_existent = tmp_path / "no_such_config.json" monkeypatch.setattr(cfg_mod, "CONFIG_PATH", str(non_existent)) assert cfg_mod.get_last_wifi_target() == {} def test_set_with_whitespace_target(self, temp_config): """target 周围空白应被 strip 后再处理。""" cfg_mod.set_last_wifi_target(" 192.168.5.2:5555 ", "PEPM00") result = cfg_mod.get_last_wifi_target() assert result["target"] == "192.168.5.2:5555" def test_set_whitespace_only_clears(self, temp_config): """target 全是空白等价于清除。""" cfg_mod.set_last_wifi_target("192.168.5.2:5555", "X") cfg_mod.set_last_wifi_target(" ", "") assert cfg_mod.get_last_wifi_target() == {}