98 lines
3.0 KiB
Python
98 lines
3.0 KiB
Python
import os
|
||
import tempfile
|
||
import subprocess
|
||
import json
|
||
from flask import Flask
|
||
from werkzeug.utils import secure_filename
|
||
|
||
|
||
# Test that our updated kokoro TTS wrapper works correctly
|
||
def test_speaker_parsing():
|
||
"""Test that speaker text parsing works correctly"""
|
||
with open("test.txt", "r") as f:
|
||
text = f.read()
|
||
|
||
# Import the function from app.py
|
||
from app import parse_speaker_text
|
||
|
||
speakers = parse_speaker_text(text)
|
||
|
||
# Should have 2 speakers
|
||
assert len(speakers) == 2, f"Expected 2 speakers, got {len(speakers)}"
|
||
|
||
# Check that Alex and Jamie are present
|
||
assert "Alex" in speakers, "Alex should be a speaker"
|
||
assert "Jamie" in speakers, "Jamie should be a speaker"
|
||
|
||
# Check that both have content
|
||
assert speakers["Alex"], "Alex should have content"
|
||
assert speakers["Jamie"], "Jamie should have content"
|
||
|
||
print("✓ Speaker parsing test passed")
|
||
|
||
|
||
def test_voice_assignment():
|
||
"""Test that voice assignment works correctly"""
|
||
from app import parse_speaker_text, assign_voices_to_speakers
|
||
|
||
with open("test.txt", "r") as f:
|
||
text = f.read()
|
||
|
||
speakers = parse_speaker_text(text)
|
||
speaker_voice_map = assign_voices_to_speakers(speakers)
|
||
|
||
# Should have assignments for both speakers
|
||
assert len(speaker_voice_map) == 2, (
|
||
f"Expected 2 voice assignments, got {len(speaker_voice_map)}"
|
||
)
|
||
|
||
# Both should have assigned voices
|
||
for speaker, voice in speaker_voice_map.items():
|
||
assert voice in ["bm_fable", "bm_lewis", "bm_george"], (
|
||
f"Invalid voice {voice} for {speaker}"
|
||
)
|
||
|
||
print("✓ Voice assignment test passed")
|
||
|
||
|
||
def test_full_integration():
|
||
"""Test that the full integration works with a simple example"""
|
||
# For this test we'll create a basic Flask app to test our functions
|
||
from app import generate_wav_files, parse_speaker_text, assign_voices_to_speakers
|
||
|
||
# Simple test data
|
||
test_text = """Alex: Hello world
|
||
Jamie: This is a test"""
|
||
|
||
speakers = parse_speaker_text(test_text)
|
||
speaker_voice_map = assign_voices_to_speakers(speakers)
|
||
|
||
# Test generating files (using temporary directory for output)
|
||
with tempfile.TemporaryDirectory() as tmpdir:
|
||
# Change output directory for this test
|
||
original_output_dir = os.environ.get("OUTPUT_DIR", "output")
|
||
os.environ["OUTPUT_DIR"] = tmpdir
|
||
|
||
try:
|
||
wav_files = generate_wav_files(
|
||
speakers, speaker_voice_map, "test_integration"
|
||
)
|
||
|
||
# Check that files were created
|
||
if wav_files:
|
||
print("✓ Integration test passed - WAV files generated")
|
||
else:
|
||
print("<EFBFBD><EFBFBD><EFBFBD> Integration test - No files generated (but no error)")
|
||
finally:
|
||
# Restore original output directory
|
||
os.environ["OUTPUT_DIR"] = original_output_dir
|
||
|
||
print("✓ Full integration test completed")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
test_speaker_parsing()
|
||
test_voice_assignment()
|
||
test_full_integration()
|
||
print("\nAll tests passed!")
|