mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-03-22 13:42:12 -03:00
- Add standalone mode detection via LORA_MANAGER_STANDALONE environment variable - Improve error handling for settings file loading with specific JSON decode errors - Add startup messages system to communicate configuration warnings and errors to users - Include settings file path and startup messages in settings API response - Automatically save settings when bootstrapping from defaults due to missing/invalid settings file - Add configuration warnings collection for environment variables and other settings issues The changes improve robustness of settings management and provide better user feedback when configuration issues occur.
94 lines
2.9 KiB
Python
94 lines
2.9 KiB
Python
import importlib
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from py.services.settings_manager import get_settings_manager, reset_settings_manager
|
|
from py.utils import settings_paths
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def reset_settings(tmp_path, monkeypatch):
|
|
"""Reset the settings manager and redirect config to a temp directory."""
|
|
def fake_user_config_dir(*args, **kwargs):
|
|
return str(tmp_path / "config")
|
|
|
|
monkeypatch.setattr(settings_paths, "user_config_dir", fake_user_config_dir)
|
|
monkeypatch.setenv("LORA_MANAGER_STANDALONE", "1")
|
|
reset_settings_manager()
|
|
yield
|
|
reset_settings_manager()
|
|
|
|
|
|
def read_settings_file(path: Path) -> dict:
|
|
with path.open('r', encoding='utf-8') as handle:
|
|
return json.load(handle)
|
|
|
|
|
|
def test_missing_settings_creates_defaults_and_emits_warnings(tmp_path):
|
|
manager = get_settings_manager()
|
|
settings_path = Path(manager.settings_file)
|
|
|
|
assert settings_path.exists()
|
|
assert read_settings_file(settings_path)
|
|
|
|
messages = manager.get_startup_messages()
|
|
codes = {message["code"] for message in messages}
|
|
assert codes == {"missing-model-paths"}
|
|
|
|
warning = messages[0]
|
|
assert "default settings.json" in warning["message"].lower()
|
|
assert warning["dismissible"] is False
|
|
|
|
actions = warning.get("actions") or []
|
|
assert actions == [
|
|
{
|
|
"action": "open-settings-location",
|
|
"label": "Open settings folder",
|
|
"type": "primary",
|
|
"icon": "fas fa-folder-open",
|
|
}
|
|
]
|
|
|
|
|
|
def test_invalid_settings_recovers_with_defaults(tmp_path):
|
|
config_dir = Path(settings_paths.user_config_dir())
|
|
config_dir.mkdir(parents=True, exist_ok=True)
|
|
settings_path = config_dir / "settings.json"
|
|
settings_path.write_text("{ invalid json", encoding='utf-8')
|
|
|
|
manager = get_settings_manager()
|
|
|
|
assert settings_path.exists()
|
|
data = read_settings_file(settings_path)
|
|
assert isinstance(data, dict)
|
|
|
|
codes = {message["code"] for message in manager.get_startup_messages()}
|
|
assert "settings-json-invalid" in codes
|
|
|
|
|
|
def test_missing_settings_skips_warning_when_embedded(tmp_path, monkeypatch):
|
|
monkeypatch.setenv("LORA_MANAGER_STANDALONE", "0")
|
|
reset_settings_manager()
|
|
|
|
manager = get_settings_manager()
|
|
|
|
assert manager.get_startup_messages() == []
|
|
|
|
|
|
def test_validate_settings_logs_warnings(tmp_path, monkeypatch, caplog):
|
|
monkeypatch.setattr(settings_paths, "user_config_dir", lambda *args, **kwargs: str(tmp_path / "config"))
|
|
|
|
reset_settings_manager()
|
|
import standalone
|
|
importlib.reload(standalone)
|
|
|
|
reset_settings_manager()
|
|
|
|
with caplog.at_level("INFO", logger="lora-manager-standalone"):
|
|
assert standalone.validate_settings() is True
|
|
|
|
messages = [record.message for record in caplog.records]
|
|
assert any("Standalone mode is using fallback configuration values." in message for message in messages)
|