"""
Tests for mma_agent_focus_ux_20260302 — Phase 1: Tier Tagging at Emission.
These tests affirm that ai_client and session_logger correctly preserve 'current_tier'
state when logging comms and tools.
"""

from src import ai_client

def reset_tier():
 ai_client.set_current_tier(None)
 yield
 ai_client.set_current_tier(None)

def test_get_current_tier_exists() -> None:
 """ai_client must expose a get_current_tier function."""
 assert hasattr(ai_client, "get_current_tier")
 assert callable(ai_client.get_current_tier)

def test_append_comms_has_source_tier_key() -> None:
 """Dict entries in comms log must have a 'source_tier' key."""
 ai_client.reset_session()
 ai_client.set_provider("gemini", "gemini-2.5-flash-lite")
 ai_client._append_comms("OUT", "request", {"msg": "hello"})
 
 log = ai_client.get_comms_log()
 assert len(log) > 0
 assert "source_tier" in log[-1]

def test_append_comms_source_tier_none_when_unset() -> None:
 """When current_tier is None, source_tier in log must be None."""
 ai_client.reset_session()
 ai_client.set_current_tier(None)
 ai_client._append_comms("OUT", "request", {"msg": "hello"})
 
 log = ai_client.get_comms_log()
 assert log[-1]["source_tier"] is None

def test_append_comms_source_tier_set_when_current_tier_set() -> None:
 """When current_tier is 'Tier 1', source_tier in log must be 'Tier 1'."""
 ai_client.reset_session()
 ai_client.set_current_tier("Tier 1")
 ai_client._append_comms("OUT", "request", {"msg": "hello"})
 
 log = ai_client.get_comms_log()
 assert log[-1]["source_tier"] == "Tier 1"
 ai_client.set_current_tier(None)

def test_append_comms_source_tier_tier2() -> None:
 """When current_tier is 'Tier 2', source_tier in log must be 'Tier 2'."""
 ai_client.reset_session()
 ai_client.set_current_tier("Tier 2")
 ai_client._append_comms("OUT", "request", {"msg": "hello"})
 
 log = ai_client.get_comms_log()
 assert log[-1]["source_tier"] == "Tier 2"
 ai_client.set_current_tier(None)

def test_append_tool_log_stores_dict(app_instance) -> None:
 """App._append_tool_log must store a dict in self._tool_log."""
 app = app_instance
 app.controller._append_tool_log("pwd", "/projects")
 
 assert len(app.controller._tool_log) > 0
 entry = app.controller._tool_log[-1]
 assert isinstance(entry, dict)

def test_append_tool_log_dict_has_source_tier(app_instance) -> None:
 """Dict entry must have 'source_tier' key."""
 app = app_instance
 app.controller._append_tool_log("pwd", "/projects")
 
 entry = app.controller._tool_log[-1]
 assert "source_tier" in entry

def test_append_tool_log_dict_keys(app_instance) -> None:
 """Dict entry must have script, result, ts, source_tier keys."""
 app = app_instance
 app.controller._append_tool_log("pwd", "/projects")
 
 entry = app.controller._tool_log[-1]
 for key in ("script", "result", "ts", "source_tier"):
  assert key in entry, f"key '{key}' missing from tool log entry: {entry}"
 assert entry["script"] == "pwd"
 assert entry["result"] == "/projects"
 assert entry["source_tier"] is None