test_interact.py
5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
AIfeng/2025-07-02 14:01:37
interact 模块功能测试(简化版)
测试简化后的 interact 模块的各项功能,包括:
- 模块导入测试
- Interact 类基础功能测试
- 项目集成测试
"""
import sys
import os
# 添加项目根目录到路径
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
try:
from core import interact
from utils import util
except ImportError as e:
print(f"导入错误: {e}")
sys.exit(1)
def test_module_import():
"""测试模块导入"""
print("\n=== 测试模块导入 ===")
try:
from core import interact
print("✓ interact 模块导入成功")
# 检查主要类
assert hasattr(interact, 'Interact'), "缺少 Interact 类"
print("✓ Interact 类检查通过")
return True
except Exception as e:
print(f"✗ 模块导入测试失败: {e}")
return False
def test_interact_basic():
"""测试 Interact 类基础功能"""
print("\n=== 测试 Interact 类基础功能 ===")
try:
# 创建基本交互(模拟recorder_sync.py中的用法)
intt = interact.Interact("auto_play", 2, {'user': 'test_user', 'text': "在呢,你说?"})
print(f"✓ 创建交互成功: {intt}")
# 测试属性
assert intt.type == "auto_play", f"交互类型不正确,期望 'auto_play',实际 '{intt.type}'"
assert intt.priority == 2, f"优先级不正确,期望 2,实际 {intt.priority}"
assert intt.data['user'] == 'test_user', "用户数据不正确"
assert intt.data['text'] == "在呢,你说?", "文本数据不正确"
print("✓ 基础属性检查通过")
# 测试字符串表示
str_repr = str(intt)
assert "auto_play" in str_repr, "字符串表示应包含交互类型"
assert "2" in str_repr, "字符串表示应包含优先级"
print(f"✓ 字符串表示正确: {str_repr}")
return True
except Exception as e:
print(f"✗ Interact 类测试失败: {e}")
return False
def test_project_integration():
"""测试项目集成"""
print("\n=== 测试项目集成 ===")
try:
# 测试 recorder_sync.py 能否导入 interact
import recorder_sync
print("✓ recorder_sync 模块导入成功")
# 模拟创建交互(类似 recorder_sync.py 中的代码)
intt = interact.Interact("auto_play", 2, {'user': 'User', 'text': "在呢,你说?"})
print(f"✓ 模拟交互创建成功: {intt}")
# 验证交互对象的属性符合预期
assert hasattr(intt, 'type'), "交互对象应有type属性"
assert hasattr(intt, 'priority'), "交互对象应有priority属性"
assert hasattr(intt, 'data'), "交互对象应有data属性"
print("✓ 交互对象属性验证通过")
# 验证 interact 模块在 recorder_sync 中可用
assert hasattr(recorder_sync, 'interact'), "recorder_sync 应能访问 interact 模块"
print("✓ recorder_sync 中 interact 模块可用")
return True
except Exception as e:
print(f"✗ 项目集成测试失败: {e}")
return False
def test_multiple_interactions():
"""测试创建多个交互"""
print("\n=== 测试创建多个交互 ===")
try:
# 创建不同类型的交互
interactions = [
interact.Interact("auto_play", 2, {'user': 'user1', 'text': "自动播放"}),
interact.Interact("user_input", 1, {'user': 'user2', 'text': "用户输入"}),
interact.Interact("system", 3, {'message': "系统消息"})
]
print(f"✓ 成功创建 {len(interactions)} 个交互")
# 验证每个交互
for i, intt in enumerate(interactions):
assert intt.type is not None, f"交互 {i} 的类型不能为空"
assert intt.priority is not None, f"交互 {i} 的优先级不能为空"
assert intt.data is not None, f"交互 {i} 的数据不能为空"
print(f" ✓ 交互 {i}: {intt}")
return True
except Exception as e:
print(f"✗ 多交互测试失败: {e}")
return False
def main():
"""主测试函数"""
print("开始 interact 模块功能测试(简化版)...")
tests = [
("模块导入", test_module_import),
("Interact类基础功能", test_interact_basic),
("项目集成", test_project_integration),
("多交互创建", test_multiple_interactions)
]
passed = 0
failed = 0
for test_name, test_func in tests:
print(f"\n{'='*50}")
print(f"执行测试: {test_name}")
print(f"{'='*50}")
try:
if test_func():
print(f"\n✓ {test_name} 测试通过")
passed += 1
else:
print(f"\n✗ {test_name} 测试失败")
failed += 1
except Exception as e:
print(f"\n✗ {test_name} 测试异常: {e}")
failed += 1
print(f"\n{'='*50}")
print(f"测试总结")
print(f"{'='*50}")
print(f"通过: {passed}")
print(f"失败: {failed}")
print(f"总计: {passed + failed}")
if failed == 0:
print("\n🎉 所有测试通过!简化的 interact 模块功能正常。")
print("\n📝 模块特点:")
print(" - 轻量级设计,专注核心功能")
print(" - 简单的数据封装,满足项目需求")
print(" - 无冗余功能,易于维护")
return True
else:
print(f"\n❌ 有 {failed} 个测试失败,请检查问题。")
return False
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)