100 lines
3.5 KiB
Python
100 lines
3.5 KiB
Python
|
|
"""
|
|||
|
|
MCP 工具测试
|
|||
|
|
验证 psycho_screen 函数在真实 API 下的行为
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import os
|
|||
|
|
import pytest
|
|||
|
|
from psycho_screener.mcp_tool import psycho_screen
|
|||
|
|
|
|||
|
|
|
|||
|
|
@pytest.fixture
|
|||
|
|
def api_key():
|
|||
|
|
key = os.environ.get("MINIMAX_API_KEY", "")
|
|||
|
|
if not key:
|
|||
|
|
pytest.skip("MINIMAX_API_KEY not set")
|
|||
|
|
return key
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------------------------
|
|||
|
|
# 真实 API 测试
|
|||
|
|
# ---------------------------------------------------------------------------
|
|||
|
|
|
|||
|
|
class TestPsychoScreenTool:
|
|||
|
|
"""测试 MCP 工具函数 psycho_screen"""
|
|||
|
|
|
|||
|
|
def test_bullying_conversation(self, api_key):
|
|||
|
|
"""霸凌场景:多轮对话,孩子最新消息包含霸凌内容"""
|
|||
|
|
messages = [
|
|||
|
|
{"role": "system", "content": "你是一个友好的小智AI玩偶..."},
|
|||
|
|
{"role": "user", "content": "今天幼儿园有个小朋友抢了我的玩具"},
|
|||
|
|
{"role": "assistant", "content": "哎呀,那真是太过分了!"},
|
|||
|
|
{"role": "user", "content": "他还不让我告诉老师,他说如果我告诉老师就会打我。我好害怕。"},
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
result = psycho_screen(messages, include_prefix=True)
|
|||
|
|
|
|||
|
|
print(f"\n[tool] bullying result: {result}")
|
|||
|
|
assert result["detected"] is True
|
|||
|
|
assert result["category"] == "bullying"
|
|||
|
|
assert result["severity"] in ("medium", "high")
|
|||
|
|
assert "已发现特定心理问题" in result["prefix"]
|
|||
|
|
|
|||
|
|
def test_normal_conversation(self, api_key):
|
|||
|
|
"""正常对话:全程无异常"""
|
|||
|
|
messages = [
|
|||
|
|
{"role": "system", "content": "你是一个友好的小智AI玩偶..."},
|
|||
|
|
{"role": "user", "content": "今天我画了一幅画,是一只大恐龙!"},
|
|||
|
|
{"role": "assistant", "content": "哇,好厉害!"},
|
|||
|
|
{"role": "user", "content": "是绿色的!晚上妈妈还做了红烧肉,好开心!"},
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
result = psycho_screen(messages, include_prefix=False)
|
|||
|
|
|
|||
|
|
print(f"\n[tool] normal result: {result}")
|
|||
|
|
assert result["detected"] is False
|
|||
|
|
assert result["category"] == "none"
|
|||
|
|
assert result["severity"] == "none"
|
|||
|
|
|
|||
|
|
def test_no_prefix_when_not_detected(self, api_key):
|
|||
|
|
"""未检测到问题时,prefix 应为空"""
|
|||
|
|
messages = [
|
|||
|
|
{"role": "user", "content": "今天天气真好呀!"},
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
result = psycho_screen(messages, include_prefix=True)
|
|||
|
|
assert result["detected"] is False
|
|||
|
|
assert result.get("prefix", "") == ""
|
|||
|
|
|
|||
|
|
def test_empty_messages(self, api_key):
|
|||
|
|
"""空消息列表"""
|
|||
|
|
result = psycho_screen([], include_prefix=True)
|
|||
|
|
assert result["detected"] is False
|
|||
|
|
assert result["summary"] == "无儿童对话内容可分析"
|
|||
|
|
|
|||
|
|
def test_messages_without_child_content(self, api_key):
|
|||
|
|
"""只有 system 消息,无 user 消息"""
|
|||
|
|
messages = [
|
|||
|
|
{"role": "system", "content": "你是一个友好的小智AI玩偶..."},
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
result = psycho_screen(messages, include_prefix=True)
|
|||
|
|
assert result["detected"] is False
|
|||
|
|
|
|||
|
|
def test_api_key_missing(self):
|
|||
|
|
"""API key 未配置时的行为"""
|
|||
|
|
# 临时清除 API key
|
|||
|
|
original = os.environ.pop("MINIMAX_API_KEY", None)
|
|||
|
|
|
|||
|
|
result = psycho_screen(
|
|||
|
|
[{"role": "user", "content": "测试"}],
|
|||
|
|
include_prefix=True
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 恢复原值
|
|||
|
|
if original:
|
|||
|
|
os.environ["MINIMAX_API_KEY"] = original
|
|||
|
|
|
|||
|
|
assert result["detected"] is False
|
|||
|
|
assert "error" in result
|