"""语音客户端配置:系统级 + 用户级合并与保存。""" from __future__ import annotations import json import pytest from voice_confirmation_client.core.machine_config import ( http_base_url_from_config, load_voice_client_config, machine_config_file_path, save_user_voice_client_config, user_voice_client_config_path, voice_terminal_id_from_config, ) def test_fields_from_system_file_only(tmp_path, monkeypatch: pytest.MonkeyPatch) -> None: cfg = tmp_path / "voice_client.json" cfg.write_text( json.dumps( {"voice_terminal_id": "t-1", "http_base_url": "http://api.example:38080"}, ), encoding="utf-8", ) monkeypatch.setenv("VOICE_CLIENT_MACHINE_CONFIG_FILE", str(cfg)) monkeypatch.setenv("VOICE_CLIENT_USER_CONFIG_FILE", str(tmp_path / "none.json")) (tmp_path / "none.json").write_text("{}", encoding="utf-8") data = load_voice_client_config() assert voice_terminal_id_from_config(data) == "t-1" assert http_base_url_from_config(data) == "http://api.example:38080" def test_user_file_overrides_system(tmp_path, monkeypatch: pytest.MonkeyPatch) -> None: sys_f = tmp_path / "sys.json" sys_f.write_text( json.dumps({"voice_terminal_id": "sys", "http_base_url": "http://sys:1"}), encoding="utf-8", ) usr_f = tmp_path / "usr.json" usr_f.write_text( json.dumps({"voice_terminal_id": "usr", "http_base_url": "http://usr:2"}), encoding="utf-8", ) monkeypatch.setenv("VOICE_CLIENT_MACHINE_CONFIG_FILE", str(sys_f)) monkeypatch.setenv("VOICE_CLIENT_USER_CONFIG_FILE", str(usr_f)) data = load_voice_client_config() assert voice_terminal_id_from_config(data) == "usr" assert http_base_url_from_config(data) == "http://usr:2" def test_http_base_default_when_missing_key( tmp_path, monkeypatch: pytest.MonkeyPatch ) -> None: cfg = tmp_path / "voice_client.json" cfg.write_text(json.dumps({"voice_terminal_id": "x"}), encoding="utf-8") monkeypatch.setenv("VOICE_CLIENT_MACHINE_CONFIG_FILE", str(cfg)) monkeypatch.setenv("VOICE_CLIENT_USER_CONFIG_FILE", str(tmp_path / "empty.json")) (tmp_path / "empty.json").write_text("{}", encoding="utf-8") data = load_voice_client_config() assert http_base_url_from_config(data) == "http://127.0.0.1:38080" def test_machine_config_file_path_respects_override( tmp_path, monkeypatch: pytest.MonkeyPatch ) -> None: p = tmp_path / "custom.json" monkeypatch.setenv("VOICE_CLIENT_MACHINE_CONFIG_FILE", str(p)) assert machine_config_file_path() == p def test_user_config_file_path_respects_override( tmp_path, monkeypatch: pytest.MonkeyPatch ) -> None: p = tmp_path / "u.json" monkeypatch.setenv("VOICE_CLIENT_USER_CONFIG_FILE", str(p)) assert user_voice_client_config_path() == p def test_missing_files_return_empty_merge(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("VOICE_CLIENT_MACHINE_CONFIG_FILE", "/nonexistent/a.json") monkeypatch.setenv("VOICE_CLIENT_USER_CONFIG_FILE", "/nonexistent/b.json") assert load_voice_client_config() == {} def test_save_user_voice_client_config(tmp_path, monkeypatch: pytest.MonkeyPatch) -> None: out = tmp_path / "out.json" monkeypatch.setenv("VOICE_CLIENT_USER_CONFIG_FILE", str(out)) save_user_voice_client_config( voice_terminal_id=" t99 ", http_base_url="http://host:38080/", ) assert out.is_file() data = json.loads(out.read_text(encoding="utf-8")) assert data == {"voice_terminal_id": "t99", "http_base_url": "http://host:38080"}