| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- import os
- import sys
- PLUGIN_ROOT = os.path.abspath(os.path.dirname(__file__))
- sys.path.insert(0, PLUGIN_ROOT)
- sys.path.insert(0, os.environ.get("HERMES_ROOT", "/opt/hermes"))
- from platform_config import (
- ChattoConfiguration,
- _get_env_or_extra_int,
- _get_env_or_extra_list,
- _get_env_or_extra_str,
- _get_env_or_extra_str_opt,
- _get_env_or_extra_truthy,
- _split_str_to_list,
- )
- class TestPlatformConfigHelpers:
- """Test Chatto configuration helper functions."""
- def test_get_env_or_extra_str_prefers_env(self, monkeypatch):
- monkeypatch.setenv("CHATTO_TEST", " env-value ")
- assert _get_env_or_extra_str("CHATTO_TEST", "extra-value") == "env-value"
- def test_get_env_or_extra_str_uses_extra_when_no_env(self, monkeypatch):
- monkeypatch.delenv("CHATTO_TEST", raising=False)
- assert _get_env_or_extra_str("CHATTO_TEST", " extra-value ") == "extra-value"
- def test_get_env_or_extra_str_returns_empty_when_missing(self, monkeypatch):
- """The non-optional helper coerces a missing value to "" — use
- _get_env_or_extra_str_opt() when None has to stay distinguishable."""
- monkeypatch.delenv("CHATTO_TEST", raising=False)
- assert _get_env_or_extra_str("CHATTO_TEST", None) == ""
- def test_get_env_or_extra_str_opt_returns_none_when_missing(self, monkeypatch):
- monkeypatch.delenv("CHATTO_TEST", raising=False)
- assert _get_env_or_extra_str_opt("CHATTO_TEST", None) is None
- def test_get_env_or_extra_str_opt_prefers_env(self, monkeypatch):
- monkeypatch.setenv("CHATTO_TEST", " env-value ")
- assert _get_env_or_extra_str_opt("CHATTO_TEST", "extra-value") == "env-value"
- def test_get_env_or_extra_truthy_parses_truthy_values(self, monkeypatch):
- monkeypatch.setenv("CHATTO_TEST", " yes ")
- assert _get_env_or_extra_truthy("CHATTO_TEST", None) is True
- def test_get_env_or_extra_truthy_uses_extra_when_no_env(self, monkeypatch):
- monkeypatch.delenv("CHATTO_TEST", raising=False)
- assert _get_env_or_extra_truthy("CHATTO_TEST", " true ") is True
- def test_get_env_or_extra_truthy_uses_default_false(self, monkeypatch):
- monkeypatch.delenv("CHATTO_TEST", raising=False)
- assert _get_env_or_extra_truthy("CHATTO_TEST", "false") is False
- def test_get_env_or_extra_truthy_default_true_when_none_and_no_extra(
- self, monkeypatch
- ):
- monkeypatch.delenv("CHATTO_TEST", raising=False)
- assert _get_env_or_extra_truthy("CHATTO_TEST", None, default=True) is True
- def test_get_env_or_extra_truthy_default_false_when_none_and_no_extra(
- self, monkeypatch
- ):
- monkeypatch.delenv("CHATTO_TEST", raising=False)
- assert _get_env_or_extra_truthy("CHATTO_TEST", None, default=False) is False
- def test_get_env_or_extra_truthy_explicit_bool_true(self, monkeypatch):
- monkeypatch.delenv("CHATTO_TEST", raising=False)
- assert _get_env_or_extra_truthy("CHATTO_TEST", True, default=False) is True
- def test_get_env_or_extra_truthy_explicit_bool_false(self, monkeypatch):
- monkeypatch.delenv("CHATTO_TEST", raising=False)
- assert _get_env_or_extra_truthy("CHATTO_TEST", False, default=True) is False
- def test_split_str_to_list_handles_comma_separated_values(self):
- assert _split_str_to_list("a,b, c ,d") == ["a", "b", "c", "d"]
- def test_get_env_or_extra_list_prefers_env(self, monkeypatch):
- monkeypatch.setenv("CHATTO_TEST", " one,two , three")
- assert _get_env_or_extra_list("CHATTO_TEST", ["ignored", "list"]) == [
- "one",
- "two",
- "three",
- ]
- def test_get_env_or_extra_list_uses_extra_list(self, monkeypatch):
- monkeypatch.delenv("CHATTO_TEST", raising=False)
- assert _get_env_or_extra_list("CHATTO_TEST", ["a", " b "]) == ["a", "b"]
- def test_get_env_or_extra_list_uses_extra_string(self, monkeypatch):
- monkeypatch.delenv("CHATTO_TEST", raising=False)
- assert _get_env_or_extra_list("CHATTO_TEST", "x,y") == ["x", "y"]
- def test_get_env_or_extra_list_returns_empty_for_none(self, monkeypatch):
- monkeypatch.delenv("CHATTO_TEST", raising=False)
- assert _get_env_or_extra_list("CHATTO_TEST", None) == []
- def test_get_env_or_extra_int_prefers_env(self, monkeypatch):
- monkeypatch.setenv("CHATTO_TEST", " 45 ")
- assert _get_env_or_extra_int("CHATTO_TEST", 300, default=300) == 45
- def test_get_env_or_extra_int_uses_extra_when_no_env(self, monkeypatch):
- monkeypatch.delenv("CHATTO_TEST", raising=False)
- assert _get_env_or_extra_int("CHATTO_TEST", 60, default=300) == 60
- def test_get_env_or_extra_int_parses_extra_string(self, monkeypatch):
- """config.yaml values arrive as strings when set via env-style files."""
- monkeypatch.delenv("CHATTO_TEST", raising=False)
- assert _get_env_or_extra_int("CHATTO_TEST", "90", default=300) == 90
- def test_get_env_or_extra_int_falls_back_to_default_for_junk(self, monkeypatch):
- monkeypatch.setenv("CHATTO_TEST", "soon-ish")
- assert _get_env_or_extra_int("CHATTO_TEST", None, default=300) == 300
- def test_get_env_or_extra_int_treats_yaml_bool_as_missing(self, monkeypatch):
- """A YAML true/false is not a number — silently take the default."""
- monkeypatch.delenv("CHATTO_TEST", raising=False)
- assert _get_env_or_extra_int("CHATTO_TEST", True, default=300) == 300
- def test_get_env_or_extra_int_defaults_when_both_missing(self, monkeypatch):
- monkeypatch.delenv("CHATTO_TEST", raising=False)
- assert _get_env_or_extra_int("CHATTO_TEST", None, default=300) == 300
- class TestMentionRoomFields:
- """The mention lists derive env names from their config keys and resolve
- to empty lists when nothing is set — channels are opt-in."""
- def test_env_names_derive_from_the_config_keys(self):
- assert (
- ChattoConfiguration.require_mention_rooms.env_name
- == "CHATTO_REQUIRE_MENTION_ROOMS"
- )
- assert (
- ChattoConfiguration.optional_mention_rooms.env_name
- == "CHATTO_OPTIONAL_MENTION_ROOMS"
- )
- def test_lists_default_to_empty_without_any_configuration(self, monkeypatch):
- for var in (
- "CHATTO_REQUIRE_MENTION_ROOMS",
- "CHATTO_OPTIONAL_MENTION_ROOMS",
- ):
- monkeypatch.delenv(var, raising=False)
- cfg = ChattoConfiguration(
- pconfig=PlatformConfigStub(extra={"base_url": "https://chat.test"})
- )
- assert cfg.require_mention_rooms.value == []
- assert cfg.optional_mention_rooms.value == []
- def test_lists_resolve_from_env_comma_separated(self, monkeypatch):
- monkeypatch.setenv("CHATTO_REQUIRE_MENTION_ROOMS", " room-1 , room-2 ")
- cfg = ChattoConfiguration(
- pconfig=PlatformConfigStub(extra={"base_url": "https://chat.test"})
- )
- assert cfg.require_mention_rooms.value == ["room-1", "room-2"]
- class PlatformConfigStub:
- """Just the attribute ChattoConfiguration reads off a PlatformConfig."""
- def __init__(self, extra):
- self.extra = extra
|