"""
Tests for AgentsMeeting shared modules.
Run: pytest tests/
"""
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "src"))

from shared.bot_base import BaseBot, BotConfig
from shared.config import get_bot_config


def test_bot_config():
    """BotConfig can be instantiated with minimal args."""
    cfg = BotConfig(jid="test@yoin.fun", password="secret")
    assert cfg.nick == "test"
    assert cfg.host == "xmpp.yoin.fun"
    assert cfg.port == 3021


def test_extract_response_silent():
    """__SILENT__ prefix returns None."""
    assert BaseBot.extract_response("__SILENT__\nok") is not None
    assert BaseBot.extract_response("__SILENT__") is None
    assert BaseBot.extract_response("") is None


def test_extract_response_normal():
    """Normal text passes through."""
    assert BaseBot.extract_response("hello") == "hello"


def test_extract_response_toolcalls():
    """Tool call XML is stripped."""
    result = BaseBot.extract_response("text\n<tool_calls>\n<invoke name='x'>y</invoke>\n</tool_calls>")
    assert result == "text"


def test_config_env_override(monkeypatch):
    """Env vars override config defaults."""
    monkeypatch.setenv("XXM_JID", "override@yoin.fun")
    monkeypatch.setenv("XXM_PASS", "override123")
    monkeypatch.setenv("VOLCENGINE_KEY", "vk-test")
    # Without a real config.yaml, falls back to env
    cfg = get_bot_config("xxm")
    assert cfg.jid == "override@yoin.fun"
    assert cfg.password == "override123"
    assert cfg.providers["volcengine"]["api_key"] == "vk-test"


def test_required_env_missing():
    """required_env raises on missing var."""
    from shared.config import required_env
    try:
        required_env("THIS_ENV_DOES_NOT_EXIST_12345")
        assert False, "Should have raised"
    except RuntimeError:
        pass
