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

139 lines
4.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. 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_character_greeting_cache():
"""测试角色greeting缓存流程"""
print("🧪 测试角色greeting缓存流程")
print("=" * 50)
# 测试角色配置
characters = [
{"name": "李白", "greeting": "吾乃李白,字太白,号青莲居士。今天有幸与君相会,让我们畅谈诗词人生吧!"},
{"name": "猪八戒", "greeting": "嘿!俺老猪来也!今天咱聊点啥好吃的?要不要一起去化缘啊?"}
]
for character in characters:
character_name = character["name"]
greeting_text = character["greeting"]
print(f"\n📝 测试角色: {character_name}")
print(f" 打招呼文本: {greeting_text}")
# 1. 检查缓存是否已存在
cache_exists = greeting_cache_exists(character_name)
print(f" 1. 缓存存在检查: {cache_exists}")
# 2. 如果不存在缓存,模拟生成并保存
if not cache_exists:
print(f" 2. 模拟生成TTS音频...")
# 模拟音频数据实际使用时会是真实的TTS生成的音频
mock_audio_data = f"mock_audio_for_{character_name}".encode('utf-8') + os.urandom(1000)
print(f" 3. 保存到缓存...")
save_success = save_greeting_cache(character_name, mock_audio_data)
print(f" 4. 保存结果: {save_success}")
else:
print(f" 2. 缓存已存在,跳过生成")
# 3. 验证缓存可以正常加载
print(f" 5. 验证缓存加载...")
cached_audio = load_cached_audio(character_name)
if cached_audio:
print(f" 6. 缓存加载成功: {len(cached_audio)} 字节")
else:
print(f" 6. 缓存加载失败")
print(f"{character_name} 的缓存流程测试完成")
print(f"\n🎉 所有角色的缓存流程测试完成")
def test_cache_hit_miss_scenario():
"""测试缓存命中和未命中场景"""
print("\n🧪 测试缓存命中和未命中场景")
print("=" * 50)
character_name = "测试角色"
greeting_text = "这是一个测试角色的打招呼文本。"
# 第一次调用 - 缓存未命中
print(f"\n📝 第一次调用(缓存未命中)")
cache_exists_before = greeting_cache_exists(character_name)
print(f" 缓存存在: {cache_exists_before}")
if not cache_exists_before:
print(" 模拟TTS生成和保存缓存...")
mock_audio = b"first_call_audio_data" + os.urandom(500)
save_greeting_cache(character_name, mock_audio)
# 第二次调用 - 缓存命中
print(f"\n📝 第二次调用(缓存命中)")
cache_exists_after = greeting_cache_exists(character_name)
print(f" 缓存存在: {cache_exists_after}")
if cache_exists_after:
cached_audio = load_cached_audio(character_name)
print(f" 成功加载缓存: {len(cached_audio)} 字节")
print(f"\n✅ 缓存命中/未命中场景测试完成")
def cleanup_test_files():
"""清理测试文件"""
print("\n🧹 清理测试文件")
print("=" * 30)
test_characters = ["李白", "猪八戒", "测试角色"]
for character_name in test_characters:
cache_path = get_greeting_cache_path(character_name)
cache_file = Path(cache_path)
if cache_file.exists():
try:
cache_file.unlink()
print(f" 已删除: {cache_file.name}")
except Exception as e:
print(f" 删除失败 {cache_file.name}: {e}")
# 检查缓存目录是否为空
cache_dir = Path("greeting_cache")
if cache_dir.exists() and not any(cache_dir.iterdir()):
try:
cache_dir.rmdir()
print(f" 已删除空目录: {cache_dir}")
except Exception as e:
print(f" 删除目录失败: {e}")
if __name__ == "__main__":
print("🚀 开始测试完整的角色greeting缓存流程")
try:
# 测试角色greeting缓存流程
test_character_greeting_cache()
# 测试缓存命中和未命中场景
test_cache_hit_miss_scenario()
finally:
# 清理测试文件
cleanup_test_files()
print("\n🎉 所有测试完成!")