-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_modules.py
More file actions
119 lines (95 loc) · 3.32 KB
/
test_modules.py
File metadata and controls
119 lines (95 loc) · 3.32 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
#!/usr/bin/env python3
"""
CloudMusic 模块测试脚本
"""
import sys
from pathlib import Path
# 添加项目路径
sys.path.insert(0, str(Path(__file__).parent))
from cloudmusic.api import QUALITY_LEVELS, resolve_play_url, parse_lrc, get_current_lyric
from cloudmusic.config import Config
from cloudmusic.player_controller import PlayerController
from cloudmusic.downloader import download_songs_batch_sync
def test_quality_levels():
"""测试音质配置"""
print("=== 测试音质配置 ===")
for quality, config in QUALITY_LEVELS.items():
print(f"{quality}: br={config['br']}, level={config['level']}")
print("✓ 音质配置测试通过\n")
def test_lrc_parsing():
"""测试歌词解析"""
print("=== 测试歌词解析 ===")
lrc_text = """
[00:00.00]第一行歌词
[00:05.50]第二行歌词
[00:10.20]第三行歌词
[00:15.00]第四行歌词
"""
lyrics = parse_lrc(lrc_text)
print(f"解析到 {len(lyrics)} 行歌词:")
for timestamp, text in lyrics:
print(f" {timestamp:.2f}s: {text}")
# 测试获取当前歌词
current = get_current_lyric(lyrics, 7.5)
print(f"\n位置 7.5s 的当前歌词: {current}")
assert current == "第二行歌词", "歌词位置不匹配"
print("✓ 歌词解析测试通过\n")
def test_config():
"""测试配置管理"""
print("=== 测试配置管理 ===")
# 加载默认配置
config = Config.load()
print(f"默认配置:")
print(f" quality: {config.quality}")
print(f" download_dir: {config.download_dir}")
print(f" max_concurrent_downloads: {config.max_concurrent_downloads}")
print(f" theme: {config.theme}")
print(f" volume: {config.volume}")
print(f" loop_mode: {config.loop_mode}")
print(f" shuffle: {config.shuffle}")
# 修改配置
config.quality = "exhigh"
config.volume = 80
config.save()
# 重新加载
config2 = Config.load()
assert config2.quality == "exhigh", "配置保存失败"
assert config2.volume == 80, "配置保存失败"
print("✓ 配置管理测试通过\n")
def test_player_controller():
"""测试播放器控制器"""
print("=== 测试播放器控制器 ===")
player = PlayerController()
print(f"播放器创建成功")
print(f" socket_path: {player.socket_path}")
print(f" 当前进程: {player.proc}")
# 检查播放器是否存活(未启动时应该是 False)
assert not player.is_alive(), "未启动的播放器应该不存活"
print("✓ 播放器控制器测试通过\n")
def test_module_imports():
"""测试模块导入"""
print("=== 测试模块导入 ===")
print("✓ cloudmusic.api")
print("✓ cloudmusic.config")
print("✓ cloudmusic.player_controller")
print("✓ cloudmusic.downloader")
print("✓ 模块导入测试通过\n")
def main():
"""运行所有测试"""
print("CloudMusic 模块测试\n" + "=" * 40 + "\n")
try:
test_module_imports()
test_quality_levels()
test_lrc_parsing()
test_config()
test_player_controller()
print("=" * 40)
print("所有测试通过!✓")
return 0
except Exception as e:
print(f"\n✗ 测试失败: {e}")
import traceback
traceback.print_exc()
return 1
if __name__ == "__main__":
sys.exit(main())