137 lines
4.8 KiB
Python
137 lines
4.8 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
"""
|
|||
|
|
API一致性验证测试
|
|||
|
|
验证所有修复后的API调用是否正常工作
|
|||
|
|
"""
|
|||
|
|
import sys
|
|||
|
|
import os
|
|||
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
|
|||
|
|
|
|||
|
|
from kards_battle.core.battle_engine import BattleEngine
|
|||
|
|
from kards_battle.core.enums import LineType
|
|||
|
|
from kards_battle.examples.sample_units import create_german_infantry, create_american_gi
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_api_consistency():
|
|||
|
|
"""测试API一致性"""
|
|||
|
|
print("\n🔍 API一致性验证测试")
|
|||
|
|
print("=" * 40)
|
|||
|
|
|
|||
|
|
# 创建引擎
|
|||
|
|
engine = BattleEngine("Germany", "USA", debug_mode=True)
|
|||
|
|
|
|||
|
|
# 创建单位
|
|||
|
|
german_unit = create_german_infantry()
|
|||
|
|
american_unit = create_american_gi()
|
|||
|
|
german_unit.stats.operation_cost = 1
|
|||
|
|
american_unit.stats.operation_cost = 1
|
|||
|
|
|
|||
|
|
test_results = []
|
|||
|
|
|
|||
|
|
# 测试1: 部署API(使用整数player_id)
|
|||
|
|
try:
|
|||
|
|
result1 = engine.deploy_unit_to_support(german_unit, 0)
|
|||
|
|
result2 = engine.deploy_unit_to_support(american_unit, 1)
|
|||
|
|
|
|||
|
|
deploy_success = result1.get('success', False) and result2.get('success', False)
|
|||
|
|
test_results.append(("部署API", deploy_success))
|
|||
|
|
print(f"✅ 部署API测试: {'通过' if deploy_success else '失败'}")
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
test_results.append(("部署API", False))
|
|||
|
|
print(f"❌ 部署API测试失败: {e}")
|
|||
|
|
|
|||
|
|
# 测试2: 移动API(使用整数player_id)
|
|||
|
|
try:
|
|||
|
|
# 等待到下一回合,这样单位就能移动了
|
|||
|
|
engine.end_turn() # 切换到美军
|
|||
|
|
engine.end_turn() # 切换回德军(新回合)
|
|||
|
|
|
|||
|
|
engine.debug_set_kredits(0, kredits=10)
|
|||
|
|
result3 = engine.move_unit(german_unit.id, (LineType.FRONT, 0), 0)
|
|||
|
|
|
|||
|
|
move_success = result3.get('success', False)
|
|||
|
|
test_results.append(("移动API", move_success))
|
|||
|
|
if move_success:
|
|||
|
|
print("✅ 移动API测试: 通过")
|
|||
|
|
else:
|
|||
|
|
print(f"❌ 移动API测试失败: {result3.get('reason', 'Unknown')}")
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
test_results.append(("移动API", False))
|
|||
|
|
print(f"❌ 移动API测试异常: {e}")
|
|||
|
|
|
|||
|
|
# 测试3: 攻击API(使用整数player_id)
|
|||
|
|
try:
|
|||
|
|
engine.end_turn() # 切换到美军
|
|||
|
|
engine.debug_set_kredits(1, kredits=10)
|
|||
|
|
result4 = engine.attack_target(american_unit.id, german_unit.id, 1)
|
|||
|
|
|
|||
|
|
attack_success = result4.get('success', False)
|
|||
|
|
test_results.append(("攻击API", attack_success))
|
|||
|
|
print(f"✅ 攻击API测试: {'通过' if attack_success else '失败'}")
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
test_results.append(("攻击API", False))
|
|||
|
|
print(f"❌ 攻击API测试失败: {e}")
|
|||
|
|
|
|||
|
|
# 测试4: 回合管理API
|
|||
|
|
try:
|
|||
|
|
result5 = engine.end_turn()
|
|||
|
|
|
|||
|
|
turn_success = result5.get('turn_ended', False)
|
|||
|
|
test_results.append(("回合管理API", turn_success))
|
|||
|
|
if turn_success:
|
|||
|
|
print("✅ 回合管理API测试: 通过")
|
|||
|
|
else:
|
|||
|
|
print(f"❌ 回合管理API测试失败: {result5.get('reason', 'Unknown')}")
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
test_results.append(("回合管理API", False))
|
|||
|
|
print(f"❌ 回合管理API测试异常: {e}")
|
|||
|
|
|
|||
|
|
# 测试5: 资源管理API(使用整数player_id)
|
|||
|
|
try:
|
|||
|
|
kredits_p1 = engine.get_kredits(0)
|
|||
|
|
kredits_p2 = engine.get_kredits(1)
|
|||
|
|
slot_p1 = engine.get_kredits_slot(0)
|
|||
|
|
slot_p2 = engine.get_kredits_slot(1)
|
|||
|
|
|
|||
|
|
resource_success = all([
|
|||
|
|
isinstance(kredits_p1, int),
|
|||
|
|
isinstance(kredits_p2, int),
|
|||
|
|
isinstance(slot_p1, int),
|
|||
|
|
isinstance(slot_p2, int)
|
|||
|
|
])
|
|||
|
|
test_results.append(("资源管理API", resource_success))
|
|||
|
|
print(f"✅ 资源管理API测试: {'通过' if resource_success else '失败'}")
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
test_results.append(("资源管理API", False))
|
|||
|
|
print(f"❌ 资源管理API测试失败: {e}")
|
|||
|
|
|
|||
|
|
# 汇总结果
|
|||
|
|
print("\n📊 测试结果汇总:")
|
|||
|
|
print("-" * 40)
|
|||
|
|
|
|||
|
|
total_tests = len(test_results)
|
|||
|
|
passed_tests = sum(1 for _, success in test_results if success)
|
|||
|
|
|
|||
|
|
for test_name, success in test_results:
|
|||
|
|
status = "✅ 通过" if success else "❌ 失败"
|
|||
|
|
print(f"{test_name}: {status}")
|
|||
|
|
|
|||
|
|
overall_success = passed_tests == total_tests
|
|||
|
|
print(f"\n总体结果: {passed_tests}/{total_tests} 通过")
|
|||
|
|
print(f"{'🎉 所有API一致性测试通过!' if overall_success else '❌ 部分API测试失败'}")
|
|||
|
|
|
|||
|
|
assert overall_success, f"API consistency test failed: {passed_tests}/{total_tests} passed"
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
try:
|
|||
|
|
test_api_consistency()
|
|||
|
|
print("✅ All tests passed!")
|
|||
|
|
except AssertionError as e:
|
|||
|
|
print(f"❌ {e}")
|
|||
|
|
sys.exit(1)
|