from langchain_core.messages import HumanMessage

from app.agents.graph import master_node
from app.agents.state import AgentRole


class FailIfCalledLLM:
    async def ainvoke(self, messages):
        raise AssertionError('LLM should not be called for simple greetings')


async def test_master_node_returns_stable_reply_for_simple_greeting(monkeypatch):
    monkeypatch.setattr('app.agents.graph._get_llm_for_state', lambda state: FailIfCalledLLM())

    state = {
        'messages': [HumanMessage(content='你好')],
        'user_id': 'u1',
        'conversation_id': 'c1',
        'current_agent': AgentRole.MASTER,
        'active_agents': [AgentRole.MASTER],
        'pending_tasks': [],
        'completed_tasks': [],
        'tool_calls': [],
        'last_tool_result': None,
        'knowledge_context': None,
        'graph_context': None,
        'plan': None,
        'plan_steps': [],
        'analysis_report': None,
        'final_response': None,
        'should_respond': True,
        'memory_context': None,
        'user_llm_config': None,
    }

    result = await master_node(state)

    assert result['final_response'] == '您好。我在。\n\n您把问题给我，我先帮您收束重点，再往下推。'
    assert result['current_agent'] == AgentRole.MASTER
    assert result['active_agents'] == [AgentRole.MASTER]


async def test_master_node_returns_stable_reply_for_identity_question(monkeypatch):
    monkeypatch.setattr('app.agents.graph._get_llm_for_state', lambda state: FailIfCalledLLM())

    state = {
        'messages': [HumanMessage(content='你是谁')],
        'user_id': 'u1',
        'conversation_id': 'c1',
        'current_agent': AgentRole.MASTER,
        'active_agents': [AgentRole.MASTER],
        'pending_tasks': [],
        'completed_tasks': [],
        'tool_calls': [],
        'last_tool_result': None,
        'knowledge_context': None,
        'graph_context': None,
        'plan': None,
        'plan_steps': [],
        'analysis_report': None,
        'final_response': None,
        'should_respond': True,
        'memory_context': None,
        'user_llm_config': None,
    }

    result = await master_node(state)

    assert result['final_response'] == '我是 Jarvis。\n\n比起做一个泛泛的助手，我更像您的判断型协作伙伴：帮您看清问题、压缩路径、把事情往前推进。'
    assert result['current_agent'] == AgentRole.MASTER
    assert result['active_agents'] == [AgentRole.MASTER]


async def test_master_node_returns_stable_reply_for_capability_question(monkeypatch):
    monkeypatch.setattr('app.agents.graph._get_llm_for_state', lambda state: FailIfCalledLLM())

    state = {
        'messages': [HumanMessage(content='你能做什么')],
        'user_id': 'u1',
        'conversation_id': 'c1',
        'current_agent': AgentRole.MASTER,
        'active_agents': [AgentRole.MASTER],
        'pending_tasks': [],
        'completed_tasks': [],
        'tool_calls': [],
        'last_tool_result': None,
        'knowledge_context': None,
        'graph_context': None,
        'plan': None,
        'plan_steps': [],
        'analysis_report': None,
        'final_response': None,
        'should_respond': True,
        'memory_context': None,
        'user_llm_config': None,
    }

    result = await master_node(state)

    assert result['final_response'] == '主要做三件事。\n- 帮您判断：看问题本质、梳理取舍、给出方向\n- 帮您收束：把复杂内容理顺，把重点拎出来\n- 帮您推进：拆任务、定步骤、把下一步变清楚\n\n如果您现在有具体目标，我可以直接进入处理。'
    assert result['current_agent'] == AgentRole.MASTER
    assert result['active_agents'] == [AgentRole.MASTER]
