"""Unit tests for the Conan Exiles bridge helpers (no Discord/RCON needed)."""
import conanjurer_functions as cf


def test_parse_players_basic():
    out = (
        "Idx | Char name | Player name | User ID\n"
        "0 | Conan | SteamGuy | 1\n"
        "1 | Khasar | OtherGuy | 2\n"
        "--- | --- | --- | ---"
    )
    assert cf.parse_players(out) == {"Conan", "Khasar"}


def test_parse_players_empty_inputs():
    assert cf.parse_players("No players connected.") == set()
    assert cf.parse_players("") == set()


def test_parse_line_login():
    event = cf.parse_line("SomeGuy joined the server")
    assert event is not None
    assert event.kind == "login"
    assert "SomeGuy" in event.text


def test_parse_line_chat():
    event = cf.parse_line("Chat: Bob: hello there")
    assert event is not None
    assert event.kind == "chat"
    assert "Bob" in event.text and "hello" in event.text


def test_parse_line_unrecognised_is_ignored():
    assert cf.parse_line("random server noise") is None


def test_conanconfig_disabled_when_unconfigured():
    cfg = cf.ConanConfig("", 25575, "", "local", "", "", 22, "", "")
    assert cfg.rcon_enabled is False
    assert cfg.log_enabled is False


def test_conanconfig_local_log_enabled():
    cfg = cf.ConanConfig("", 0, "", "local", "/tmp/conan.log", "", 22, "", "")
    assert cfg.log_enabled is True


def test_conanconfig_rcon_enabled_requires_lib(monkeypatch):
    # Without aiomcrcon installed, rcon stays disabled even when host+pw are set.
    monkeypatch.setattr(cf, "_Rcon", None)
    cfg = cf.ConanConfig("1.2.3.4", 25575, "pw", "local", "", "", 22, "", "")
    assert cfg.rcon_enabled is False
    # With the lib present, it enables.
    monkeypatch.setattr(cf, "_Rcon", object)
    cfg2 = cf.ConanConfig("1.2.3.4", 25575, "pw", "local", "", "", 22, "", "")
    assert cfg2.rcon_enabled is True


def test_conanconfig_sftp_log_requires_asyncssh(monkeypatch):
    monkeypatch.setattr(cf, "asyncssh", None)
    cfg = cf.ConanConfig("", 0, "", "sftp", "/log", "sftp.host", 22, "u", "p")
    assert cfg.log_enabled is False
