mirror of
https://github.com/zhinianboke/xianyu-auto-reply.git
synced 2025-08-02 04:27:36 +08:00
167 lines
5.5 KiB
Python
167 lines
5.5 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
AI回复修复验证脚本
|
||
验证settings变量作用域问题是否已修复
|
||
"""
|
||
|
||
import asyncio
|
||
import sys
|
||
import os
|
||
|
||
# 添加项目根目录到Python路径
|
||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||
|
||
from ai_reply_engine import ai_reply_engine
|
||
from db_manager import db_manager
|
||
|
||
def setup_test_account():
|
||
"""设置测试账号"""
|
||
test_cookie_id = "test_fix_001"
|
||
|
||
# 配置AI回复设置
|
||
ai_settings = {
|
||
'ai_enabled': True,
|
||
'model_name': 'qwen-plus',
|
||
'api_key': 'test-api-key', # 测试用假密钥
|
||
'base_url': 'https://dashscope.aliyuncs.com/compatible-mode/v1',
|
||
'max_discount_percent': 10,
|
||
'max_discount_amount': 100,
|
||
'max_bargain_rounds': 3,
|
||
'custom_prompts': ''
|
||
}
|
||
|
||
success = db_manager.save_ai_reply_settings(test_cookie_id, ai_settings)
|
||
return test_cookie_id if success else None
|
||
|
||
def test_settings_variable_scope():
|
||
"""测试settings变量作用域问题"""
|
||
print("🔧 测试settings变量作用域修复...")
|
||
|
||
test_cookie_id = setup_test_account()
|
||
if not test_cookie_id:
|
||
print(" ❌ 测试账号设置失败")
|
||
return False
|
||
|
||
# 测试数据
|
||
test_item_info = {
|
||
'title': '测试商品',
|
||
'price': 100,
|
||
'desc': '测试商品描述'
|
||
}
|
||
|
||
test_chat_id = "test_chat_fix_001"
|
||
|
||
# 清理测试数据
|
||
try:
|
||
with db_manager.lock:
|
||
cursor = db_manager.conn.cursor()
|
||
cursor.execute('DELETE FROM ai_conversations WHERE cookie_id = ? AND chat_id = ?',
|
||
(test_cookie_id, test_chat_id))
|
||
db_manager.conn.commit()
|
||
except:
|
||
pass
|
||
|
||
print(f" 测试账号: {test_cookie_id}")
|
||
print(f" 测试对话: {test_chat_id}")
|
||
|
||
# 测试1: 普通消息(非议价)
|
||
print(f"\n1️⃣ 测试普通消息处理...")
|
||
try:
|
||
reply = ai_reply_engine.generate_reply(
|
||
message="你好",
|
||
item_info=test_item_info,
|
||
chat_id=test_chat_id,
|
||
cookie_id=test_cookie_id,
|
||
user_id="test_user",
|
||
item_id="test_item"
|
||
)
|
||
|
||
# 由于使用测试API密钥,预期会失败,但不应该出现settings变量错误
|
||
print(f" 普通消息测试完成(预期API调用失败)")
|
||
|
||
except Exception as e:
|
||
error_msg = str(e)
|
||
if "cannot access local variable 'settings'" in error_msg:
|
||
print(f" ❌ settings变量作用域问题仍然存在: {error_msg}")
|
||
return False
|
||
else:
|
||
print(f" ✅ settings变量作用域问题已修复(其他错误: {error_msg[:50]}...)")
|
||
|
||
# 测试2: 议价消息
|
||
print(f"\n2️⃣ 测试议价消息处理...")
|
||
try:
|
||
# 先添加一些议价记录,测试轮数限制逻辑
|
||
for i in range(3): # 添加3轮议价记录
|
||
ai_reply_engine.save_conversation(
|
||
chat_id=test_chat_id,
|
||
cookie_id=test_cookie_id,
|
||
user_id="test_user",
|
||
item_id="test_item",
|
||
role="user",
|
||
content=f"第{i+1}次议价",
|
||
intent="price"
|
||
)
|
||
|
||
# 现在测试第4轮议价(应该被拒绝)
|
||
reply = ai_reply_engine.generate_reply(
|
||
message="能再便宜点吗?",
|
||
item_info=test_item_info,
|
||
chat_id=test_chat_id,
|
||
cookie_id=test_cookie_id,
|
||
user_id="test_user",
|
||
item_id="test_item"
|
||
)
|
||
|
||
if reply and "不能再便宜" in reply:
|
||
print(f" ✅ 议价轮数限制正常工作: {reply}")
|
||
else:
|
||
print(f" ⚠️ 议价消息处理完成,但结果可能不符合预期")
|
||
|
||
except Exception as e:
|
||
error_msg = str(e)
|
||
if "cannot access local variable 'settings'" in error_msg:
|
||
print(f" ❌ settings变量作用域问题仍然存在: {error_msg}")
|
||
return False
|
||
else:
|
||
print(f" ✅ settings变量作用域问题已修复(其他错误: {error_msg[:50]}...)")
|
||
|
||
# 测试3: 验证settings获取
|
||
print(f"\n3️⃣ 测试settings获取...")
|
||
try:
|
||
settings = db_manager.get_ai_reply_settings(test_cookie_id)
|
||
print(f" ✅ settings获取成功:")
|
||
print(f" AI启用: {settings.get('ai_enabled')}")
|
||
print(f" 最大议价轮数: {settings.get('max_bargain_rounds')}")
|
||
print(f" 最大优惠百分比: {settings.get('max_discount_percent')}%")
|
||
|
||
except Exception as e:
|
||
print(f" ❌ settings获取失败: {e}")
|
||
return False
|
||
|
||
return True
|
||
|
||
def main():
|
||
"""主测试函数"""
|
||
print("🚀 AI回复settings变量修复验证")
|
||
print("=" * 50)
|
||
|
||
# 测试修复
|
||
fix_ok = test_settings_variable_scope()
|
||
|
||
if fix_ok:
|
||
print("\n" + "=" * 50)
|
||
print("🎉 修复验证成功!")
|
||
print("\n✅ 修复内容:")
|
||
print(" • settings变量作用域问题已解决")
|
||
print(" • 议价轮数限制功能正常")
|
||
print(" • AI回复流程完整")
|
||
print("\n💡 说明:")
|
||
print(" • 由于使用测试API密钥,AI调用会失败")
|
||
print(" • 但不会再出现settings变量错误")
|
||
print(" • 配置真实API密钥后即可正常使用")
|
||
else:
|
||
print("\n❌ 修复验证失败,请检查代码")
|
||
|
||
if __name__ == "__main__":
|
||
main()
|