import json import os import stat import pytest from client.config import ( Config, ConfigMissingError, config_path, load_config, save_config, ) def test_config_path_under_assistant_state(tmp_path, monkeypatch): monkeypatch.setenv("HOME", str(tmp_path)) assert config_path() == tmp_path / "assistant" / "state" / "config.json" def test_load_missing_raises(tmp_path, monkeypatch): monkeypatch.setenv("HOME", str(tmp_path)) with pytest.raises(ConfigMissingError): load_config() def test_save_then_load_round_trip(tmp_path, monkeypatch): monkeypatch.setenv("HOME", str(tmp_path)) cfg = Config( device_id="11111111-1111-1111-1111-111111111111", device_token="t0k3n", backend_ws="wss://example/device", ) save_config(cfg) loaded = load_config() assert loaded == cfg def test_save_creates_parent_dirs(tmp_path, monkeypatch): monkeypatch.setenv("HOME", str(tmp_path)) cfg = Config(device_id="x", device_token="y", backend_ws="wss://z") save_config(cfg) assert (tmp_path / "assistant" / "state").is_dir() def test_save_sets_mode_0600(tmp_path, monkeypatch): monkeypatch.setenv("HOME", str(tmp_path)) cfg = Config(device_id="x", device_token="y", backend_ws="wss://z") save_config(cfg) mode = stat.S_IMODE(os.stat(config_path()).st_mode) assert mode == 0o600 def test_load_rejects_missing_fields(tmp_path, monkeypatch): monkeypatch.setenv("HOME", str(tmp_path)) config_path().parent.mkdir(parents=True, exist_ok=True) config_path().write_text(json.dumps({"device_id": "x"})) with pytest.raises(ConfigMissingError): load_config()