|
| 1 | +from pathlib import Path |
| 2 | +from types import SimpleNamespace |
| 3 | +import sys |
| 4 | + |
| 5 | +from src.config.legacy_migration import migrate_legacy_bind_env_to_bot_config_dict |
| 6 | +from src.config.startup_bindings import ( |
| 7 | + BindAddress, |
| 8 | + get_startup_main_bind_address, |
| 9 | + get_startup_webui_bind_address, |
| 10 | + resolve_main_bind_address, |
| 11 | + resolve_webui_bind_address, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def test_startup_bindings_use_defaults_when_config_file_missing(tmp_path: Path): |
| 16 | + missing_path = tmp_path / "missing_bot_config.toml" |
| 17 | + |
| 18 | + assert get_startup_main_bind_address(missing_path) == BindAddress(host="127.0.0.1", port=8080) |
| 19 | + assert get_startup_webui_bind_address(missing_path) == BindAddress(host="127.0.0.1", port=8001) |
| 20 | + |
| 21 | + |
| 22 | +def test_startup_bindings_can_read_addresses_from_bot_config(tmp_path: Path): |
| 23 | + config_path = tmp_path / "bot_config.toml" |
| 24 | + config_path.write_text( |
| 25 | + """ |
| 26 | +[inner] |
| 27 | +version = "8.3.1" |
| 28 | +
|
| 29 | +[maim_message] |
| 30 | +ws_server_host = "0.0.0.0" |
| 31 | +ws_server_port = 22345 |
| 32 | +
|
| 33 | +[webui] |
| 34 | +host = "192.168.1.9" |
| 35 | +port = 18001 |
| 36 | +""".strip(), |
| 37 | + encoding="utf-8", |
| 38 | + ) |
| 39 | + |
| 40 | + assert get_startup_main_bind_address(config_path) == BindAddress(host="0.0.0.0", port=22345) |
| 41 | + assert get_startup_webui_bind_address(config_path) == BindAddress(host="192.168.1.9", port=18001) |
| 42 | + |
| 43 | + |
| 44 | +def test_resolve_bindings_prefer_initialized_global_config(monkeypatch): |
| 45 | + fake_config_module = SimpleNamespace( |
| 46 | + global_config=SimpleNamespace( |
| 47 | + maim_message=SimpleNamespace(ws_server_host="10.0.0.2", ws_server_port=32000), |
| 48 | + webui=SimpleNamespace(host="10.0.0.3", port=32001), |
| 49 | + ) |
| 50 | + ) |
| 51 | + |
| 52 | + monkeypatch.setitem(sys.modules, "src.config.config", fake_config_module) |
| 53 | + |
| 54 | + assert resolve_main_bind_address() == BindAddress(host="10.0.0.2", port=32000) |
| 55 | + assert resolve_webui_bind_address() == BindAddress(host="10.0.0.3", port=32001) |
| 56 | + |
| 57 | + |
| 58 | +def test_legacy_env_bindings_are_migrated_when_fields_missing_or_default(monkeypatch): |
| 59 | + monkeypatch.setenv("HOST", "0.0.0.0") |
| 60 | + monkeypatch.setenv("PORT", "22345") |
| 61 | + monkeypatch.setenv("WEBUI_HOST", "192.168.1.8") |
| 62 | + monkeypatch.setenv("WEBUI_PORT", "19001") |
| 63 | + |
| 64 | + payload = { |
| 65 | + "maim_message": { |
| 66 | + "ws_server_host": "127.0.0.1", |
| 67 | + "ws_server_port": 8080, |
| 68 | + }, |
| 69 | + "webui": {}, |
| 70 | + } |
| 71 | + |
| 72 | + result = migrate_legacy_bind_env_to_bot_config_dict(payload) |
| 73 | + |
| 74 | + assert result.migrated is True |
| 75 | + assert payload["maim_message"]["ws_server_host"] == "0.0.0.0" |
| 76 | + assert payload["maim_message"]["ws_server_port"] == 22345 |
| 77 | + assert payload["webui"]["host"] == "192.168.1.8" |
| 78 | + assert payload["webui"]["port"] == 19001 |
| 79 | + |
| 80 | + |
| 81 | +def test_legacy_env_bindings_do_not_override_explicit_config(monkeypatch): |
| 82 | + monkeypatch.setenv("HOST", "0.0.0.0") |
| 83 | + monkeypatch.setenv("PORT", "22345") |
| 84 | + monkeypatch.setenv("WEBUI_HOST", "192.168.1.8") |
| 85 | + monkeypatch.setenv("WEBUI_PORT", "19001") |
| 86 | + |
| 87 | + payload = { |
| 88 | + "maim_message": { |
| 89 | + "ws_server_host": "10.1.1.1", |
| 90 | + "ws_server_port": 30000, |
| 91 | + }, |
| 92 | + "webui": { |
| 93 | + "host": "10.1.1.2", |
| 94 | + "port": 30001, |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + result = migrate_legacy_bind_env_to_bot_config_dict(payload) |
| 99 | + |
| 100 | + assert result.migrated is False |
| 101 | + assert payload["maim_message"]["ws_server_host"] == "10.1.1.1" |
| 102 | + assert payload["maim_message"]["ws_server_port"] == 30000 |
| 103 | + assert payload["webui"]["host"] == "10.1.1.2" |
| 104 | + assert payload["webui"]["port"] == 30001 |
0 commit comments