import time
import sys
import os

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")))

from src.api_hook_client import ApiHookClient


def test_comms_volume_stress_performance(live_gui) -> None:
 time.sleep(5.0)
 client = ApiHookClient()
 time.sleep(2.0)
 baseline_resp = client.get_performance()
 baseline = baseline_resp.get("performance", {})
 baseline_ft = baseline.get("last_frame_time_ms", 0.0)
 large_session = []
 for i in range(50):
  large_session.append(
   {
    "role": "User",
    "content": f"Stress test entry {i} " * 5,
    "ts": time.time(),
    "collapsed": False,
   }
  )
 client.post_session(large_session)
 time.sleep(1.0)
 stress_resp = client.get_performance()
 stress = stress_resp.get("performance", {})
 stress_ft = stress.get("last_frame_time_ms", 0.0)
 print(f"Baseline FT: {baseline_ft:.2f}ms, Stress FT: {stress_ft:.2f}ms")
 if stress_ft > 0:
  assert stress_ft < 100.0, (
   f"Stress frame time {stress_ft:.2f}ms exceeds 10fps threshold"
  )
 session_data = client.get_session()
 entries = session_data.get("session", {}).get("entries", [])
 assert len(entries) >= 50, f"Expected at least 50 entries, got {len(entries)}"
