Local-Voice/test_greeting_cache.py
2025-09-25 10:41:27 +08:00

115 lines
3.4 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
测试角色greeting缓存功能
"""
import os
import sys
import json
from pathlib import Path
# 添加项目路径
sys.path.append('.')
from audio_processes import (
get_greeting_cache_path,
greeting_cache_exists,
load_cached_audio,
save_greeting_cache
)
def test_cache_functions():
"""测试缓存功能"""
print("🧪 测试角色greeting缓存功能")
print("=" * 50)
# 测试角色名称
test_character = "libai"
test_audio_data = b"test_audio_data_" + os.urandom(100)
# 1. 测试缓存路径生成
print("\n1⃣ 测试缓存路径生成")
cache_path = get_greeting_cache_path(test_character)
print(f" 缓存路径: {cache_path}")
# 2. 测试缓存存在检查
print("\n2⃣ 测试缓存存在检查")
exists_before = greeting_cache_exists(test_character)
print(f" 缓存存在检查 (保存前): {exists_before}")
# 3. 测试缓存保存
print("\n3⃣ 测试缓存保存")
save_success = save_greeting_cache(test_character, test_audio_data)
print(f" 缓存保存成功: {save_success}")
# 4. 测试缓存存在检查(保存后)
print("\n4⃣ 测试缓存存在检查(保存后)")
exists_after = greeting_cache_exists(test_character)
print(f" 缓存存在检查 (保存后): {exists_after}")
# 5. 测试缓存加载
print("\n5⃣ 测试缓存加载")
loaded_audio = load_cached_audio(test_character)
if loaded_audio:
print(f" 缓存加载成功: {len(loaded_audio)} 字节")
print(f" 数据匹配: {loaded_audio == test_audio_data}")
else:
print(" 缓存加载失败")
# 6. 检查缓存文件
print("\n6⃣ 检查缓存文件")
cache_file = Path(cache_path)
if cache_file.exists():
print(f" 缓存文件存在: {cache_file}")
print(f" 文件大小: {cache_file.stat().st_size} 字节")
else:
print(" 缓存文件不存在")
# 7. 清理测试文件
print("\n7⃣ 清理测试文件")
try:
if cache_file.exists():
cache_file.unlink()
print(" 测试文件已清理")
except Exception as e:
print(f" 清理失败: {e}")
print("\n✅ 缓存功能测试完成")
def test_character_configs():
"""测试角色配置"""
print("\n🧪 测试角色配置")
print("=" * 30)
characters_dir = Path("characters")
if not characters_dir.exists():
print("❌ characters目录不存在")
return
character_files = list(characters_dir.glob("*.json"))
print(f"📁 找到 {len(character_files)} 个角色配置文件:")
for character_file in character_files:
try:
with open(character_file, 'r', encoding='utf-8') as f:
config = json.load(f)
name = config.get("name", "未知")
greeting = config.get("greeting", "")
print(f" 📝 {name}: {greeting[:30]}...")
except Exception as e:
print(f" ❌ 读取 {character_file.name} 失败: {e}")
if __name__ == "__main__":
print("🚀 开始测试greeting缓存功能")
# 测试缓存功能
test_cache_functions()
# 测试角色配置
test_character_configs()
print("\n🎉 所有测试完成!")