"""Test Matrix configuration loading from env vars and files.""" from __future__ import annotations import os from pathlib import Path import pytest from matrix_hermes.config import MatrixConfig, _parse_csv class TestParseCSV: """Tests for the CSV parsing helper.""" def test_empty_string(self) -> None: assert _parse_csv("") == [] def test_single_value(self) -> None: assert _parse_csv("alice") == ["alice"] def test_multiple_values(self) -> None: assert _parse_csv("alice, bob , charlie") == ["alice", "bob", "charlie"] def test_whitespace_only(self) -> None: assert _parse_csv(" , , ") == [] class TestMatrixConfigDefaults: """Tests for default config values.""" def test_empty_config(self) -> None: cfg = MatrixConfig() assert cfg.homeserver == "" assert cfg.access_token == "" assert cfg.password == "" def test_is_configured_false(self) -> None: cfg = MatrixConfig() assert cfg.is_configured() is False def test_is_configured_with_homeserver_and_token(self) -> None: cfg = MatrixConfig(homeserver="https://matrix.org", access_token="fake-token") assert cfg.is_configured() is True def test_is_configured_with_homeserver_and_password(self) -> None: cfg = MatrixConfig( homeserver="https://matrix.org", user_id="@test:matrix.org", password="secret", ) assert cfg.is_configured() is True def test_validate_empty_config(self) -> None: cfg = MatrixConfig() errors = cfg.validate() assert len(errors) > 0 assert any("MATRIX_HOMESERVER" in e for e in errors) def test_validate_missing_auth(self) -> None: cfg = MatrixConfig(homeserver="https://matrix.org") errors = cfg.validate() assert any("ACCESS_TOKEN" in e or "PASSWORD" in e for e in errors) def test_resolved_session_dir_default(self) -> None: cfg = MatrixConfig() assert cfg.resolved_session_dir == str( Path.home() / ".hermes" / "matrix_sessions" ) def test_resolved_session_dir_custom(self) -> None: cfg = MatrixConfig(session_dir="/tmp/matrix_test") assert cfg.resolved_session_dir == "/tmp/matrix_test" class TestFromEnv: """Tests for env var loading.""" def test_loads_from_env(self, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("MATRIX_HOMESERVER", "https://example.com") monkeypatch.setenv("MATRIX_ACCESS_TOKEN", "syt_abc123") monkeypatch.setenv("MATRIX_USER_ID", "@bot:example.com") monkeypatch.setenv("MATRIX_REQUIRE_MENTION", "false") cfg = MatrixConfig.from_env() assert cfg.homeserver == "https://example.com" assert cfg.access_token == "syt_abc123" assert cfg.user_id == "@bot:example.com" assert cfg.require_mention is False def test_encryption_flag(self, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("MATRIX_ENCRYPTION", "true") cfg = MatrixConfig.from_env() assert cfg.encryption is True def test_allowed_users(self, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("MATRIX_ALLOWED_USERS", "@a:example.com, @b:example.com") cfg = MatrixConfig.from_env() assert cfg.allowed_users == ["@a:example.com", "@b:example.com"] def test_auto_thread_default(self) -> None: cfg = MatrixConfig.from_env() assert cfg.auto_thread is True def test_require_mention_default(self) -> None: cfg = MatrixConfig.from_env() assert cfg.require_mention is True