diff --git a/src/multi_swarm/config.py b/src/multi_swarm/config.py new file mode 100644 index 0000000..3e95f6c --- /dev/null +++ b/src/multi_swarm/config.py @@ -0,0 +1,37 @@ +"""Pydantic settings loader for Multi_Swarm_Coevolutive. + +Loads configuration from environment variables and an optional ``.env`` file +in the project root. Required secrets are validated at instantiation time. +""" + +from pathlib import Path + +from pydantic import Field, SecretStr +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class Settings(BaseSettings): + model_config = SettingsConfigDict( + env_file=".env", + env_file_encoding="utf-8", + extra="ignore", + case_sensitive=False, + ) + + cerbero_base_url: str = "http://localhost:9000" + cerbero_testnet_token: SecretStr + cerbero_mainnet_token: SecretStr | None = None + cerbero_bot_tag: str = "swarm-poc-phase1" + + openrouter_api_key: SecretStr + anthropic_api_key: SecretStr | None = None + + run_name: str = "phase1-spike-001" + data_dir: Path = Field(default=Path("./data")) + series_dir: Path = Field(default=Path("./series")) + db_path: Path = Field(default=Path("./runs.db")) + + +def load_settings() -> Settings: + # Required fields are populated from environment / .env, not init kwargs. + return Settings() # type: ignore[call-arg] diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py new file mode 100644 index 0000000..436db38 --- /dev/null +++ b/tests/unit/test_config.py @@ -0,0 +1,42 @@ +"""Tests for multi_swarm.config.Settings. + +Note on .env isolation: +The happy-path test relies on monkeypatch.setenv to provide values. +The "requires tokens" test forces _env_file=None when constructing Settings, +so that a developer's local .env (if present and populated) cannot mask the +absence of required env vars. This keeps the test deterministic both in CI +(no .env) and in local dev (.env may exist). +""" + +import pytest + +from multi_swarm.config import Settings + + +def test_settings_loads_from_env(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("CERBERO_BASE_URL", "http://test:9000") + monkeypatch.setenv("CERBERO_TESTNET_TOKEN", "tok-test") + monkeypatch.setenv("CERBERO_MAINNET_TOKEN", "tok-main") + monkeypatch.setenv("CERBERO_BOT_TAG", "swarm-poc-phase1") + monkeypatch.setenv("OPENROUTER_API_KEY", "or-key") + monkeypatch.setenv("ANTHROPIC_API_KEY", "an-key") + monkeypatch.setenv("RUN_NAME", "test-run") + + s = Settings() # type: ignore[call-arg] + + assert s.cerbero_base_url == "http://test:9000" + assert s.cerbero_testnet_token.get_secret_value() == "tok-test" + assert s.run_name == "test-run" + assert s.data_dir.name == "data" + assert s.db_path.name == "runs.db" + + +def test_settings_requires_tokens(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.delenv("CERBERO_TESTNET_TOKEN", raising=False) + monkeypatch.delenv("OPENROUTER_API_KEY", raising=False) + from pydantic import ValidationError + + with pytest.raises(ValidationError): + # Disable .env loading to keep the test deterministic regardless of + # whether a developer's local .env exists and is populated. + Settings(_env_file=None) # type: ignore[call-arg]