from __future__ import annotations from storage.device_config import DeviceConfigStore def test_device_config_store_add_remove_list_and_get(tmp_path) -> None: store = DeviceConfigStore(tmp_path / "device_config.sqlite3") store.add( device_id="iphone-1", name="Desk iPhone", driver_type="wda", connection_info={"server_url": "http://127.0.0.1:4723", "udid": "abc123"}, ) store.add( device_id="iphone-2", name=None, driver_type="wda", connection_info={"wda_local_port": 8101}, ) assert store.get("iphone-1") == { "device_id": "iphone-1", "name": "Desk iPhone", "driver_type": "wda", "connection_info": { "server_url": "http://127.0.0.1:4723", "udid": "abc123", }, "cloud_device_id": None, } assert [config["device_id"] for config in store.list()] == ["iphone-1", "iphone-2"] store.remove("iphone-1") assert store.get("iphone-1") is None assert [config["device_id"] for config in store.list()] == ["iphone-2"] def test_device_config_store_settings_get_set_and_defaults(tmp_path) -> None: store = DeviceConfigStore(tmp_path / "device_config.sqlite3") # assert store.get_setting("max_steps") == "999999" assert store.get_setting("missing") is None store.set_setting("max_steps", 30) store.set_setting("feature_flag", "enabled") reopened = DeviceConfigStore(tmp_path / "device_config.sqlite3") assert reopened.get_setting("max_steps") == "30" assert reopened.get_setting("feature_flag") == "enabled" def test_device_config_store_unknown_device_remove_is_noop(tmp_path) -> None: store = DeviceConfigStore(tmp_path / "device_config.sqlite3") store.remove("missing") assert store.get("missing") is None def test_device_config_store_persists_cloud_mapping_without_losing_it_on_update( tmp_path, ) -> None: store = DeviceConfigStore(tmp_path / "device_config.sqlite3") store.add( device_id="iphone-local", name="iPhone", driver_type="wda", connection_info={"udid": "abc"}, ) assert store.set_cloud_device_id("iphone-local", "device-cloud-a") is True store.add( device_id="iphone-local", name="Renamed iPhone", driver_type="wda", connection_info={"udid": "abc", "wda_local_port": 8101}, ) config = store.get("iphone-local") assert config is not None assert config["cloud_device_id"] == "device-cloud-a" assert config["name"] == "Renamed iPhone" assert store.set_cloud_device_id("missing", "device-cloud-b") is False