"""AgentParam / AgentMeta 基础行为。"""

from __future__ import annotations

from linksoul_agentsdk.agent_meta import AgentMeta
from linksoul_agentsdk.agent_param import AgentParam


# ---------- AgentParam ----------


def test_agent_param_chained_setters():
    p = (
        AgentParam.create()
        .set_string("k1", "v1")
        .set_integer("k2", 3)
        .set_double("k3", 1.5)
        .set_boolean("k4", True)
        .set_list("k5", [1, 2, 3])
    )
    assert p.get_string("k1") == "v1"
    assert p.get_integer("k2") == 3
    assert p.get_long("k2") == 3
    assert p.get_double("k3") == 1.5
    assert p.get_boolean("k4") is True
    assert p.get_list("k5") == [1, 2, 3]
    assert p.get_ext_param() == {"k1": "v1", "k2": 3, "k3": 1.5, "k4": True, "k5": [1, 2, 3]}


def test_agent_param_missing_returns_none():
    p = AgentParam.create()
    assert p.get_string("nope") is None
    assert p.get_integer("nope") is None
    assert p.get_boolean("nope") is None
    assert p.get_list("nope") is None


def test_agent_param_string_boolean_coerce():
    p = AgentParam.create().set_string("x", "true").set_string("y", "False")
    assert p.get_boolean("x") is True
    assert p.get_boolean("y") is False


def test_agent_param_does_not_misread_bool_as_int():
    # bool 是 int 的子类，需要排除掉，否则 get_integer 会返回 1 / 0
    p = AgentParam.create().set_boolean("flag", True)
    assert p.get_integer("flag") is None


def test_agent_param_str_is_json():
    p = AgentParam.create().set_string("a", "b")
    assert str(p) == '{"a": "b"}'


def test_agent_param_init_copies_dict():
    src = {"a": 1}
    p = AgentParam(src)
    src["a"] = 2
    assert p.get_integer("a") == 1


# ---------- AgentMeta ----------


def test_agent_meta_well_known_keys():
    m = AgentMeta({
        "def.wakeupWord": "小灵小灵",
        "def.city": "上海",
        "def.robotName": "灵心",
        "def.buzzWord": "你好",
        "custom_key": "X",
    })
    assert m.get_wakeup_word() == "小灵小灵"
    assert m.get_city() == "上海"
    assert m.get_robot_name() == "灵心"
    assert m.get_buzz_word() == "你好"
    assert m.get_string("custom_key") == "X"
    assert m.get_string("missing") is None


def test_agent_meta_empty_defaults():
    m = AgentMeta()
    assert m.get_wakeup_word() is None
    assert m.get_city() is None


def test_agent_meta_contains():
    m = AgentMeta({"k": 1})
    assert "k" in m
    assert "z" not in m
