
#!/usr/bin/env python3
"""
朝堂议政引擎 - 简单测试脚本
"""

import requests
import json
import time

BASE_URL = "http://localhost:7891/api"

print("="*80)
print("  朝堂议政引擎 - 简单测试")
print("="*80)

# 1. 测试列出官员
print("\n[1] 列出官员...")
try:
    r = requests.get(f"{BASE_URL}/court-discuss/officials")
    result = r.json()
    if result.get('ok'):
        officials = list(result.get('officials', {}).keys())
        print("  成功！可用官员: " + ", ".join(officials))
    else:
        print("  失败: " + str(result.get('error')))
except Exception as e:
    print("  错误: " + str(e))

# 2. 创建会话
print("\n[2] 创建议政会话...")
session_id = None
try:
    r = requests.post(
        f"{BASE_URL}/court-discuss/start",
        json={
            'topic': '测试朝堂议政功能',
            'officials': ['zhongshu', 'menxia', 'shangshu']
        }
    )
    result = r.json()
    if result.get('ok'):
        session_id = result.get('taskId') or result.get('sessionId')
        print("  成功！会话ID: " + str(session_id))
    else:
        print("  失败: " + str(result.get('error')))
except Exception as e:
    print("  错误: " + str(e))

if not session_id:
    print("\n  创建会话失败，退出测试")
    exit(1)

# 3. 等待一下
time.sleep(2)

# 4. 推进讨论
print("\n[3] 推进讨论...")
try:
    r = requests.post(
        f"{BASE_URL}/court-discuss/advance",
        json={'sessionId': session_id}
    )
    result = r.json()
    if result.get('ok'):
        print("  成功！第 " + str(result.get('round')) + " 轮讨论")
        msgs = result.get('new_messages', [])
        print("  新增 " + str(len(msgs)) + " 条消息")
        for msg in msgs:
            name = msg.get('name', '?')
            content = msg.get('content', '')
            # 简单处理，不做长度判断
            print("    " + name + ": " + content)
    else:
        print("  失败: " + str(result.get('error')))
except Exception as e:
    print("  错误: " + str(e))

# 5. 结束会话
time.sleep(2)
print("\n[4] 结束议政...")
try:
    r = requests.post(
        f"{BASE_URL}/court-discuss/conclude",
        json={'sessionId': session_id}
    )
    result = r.json()
    if result.get('ok'):
        print("  成功！总结: " + str(result.get('summary')))
    else:
        print("  失败: " + str(result.get('error')))
except Exception as e:
    print("  错误: " + str(e))

print("\n" + "="*80)
print("  测试完成！")
print("="*80)

