import sys import os import time import subprocess # Add project root to sys.path project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) sys.path.insert(0, project_root) from src.lyricflow_core.engine.rhyme_engine import RhymeEngine def test_engine_bridge(): # 1. Start backend backend_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'LyricFlow.Core.Backend', 'LyricFlow.Backend.Api')) print(f"Starting backend from {backend_path}...") proc = subprocess.Popen( ["dotnet", "run", "--project", backend_path, "--urls", "http://localhost:5000"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, creationflags=subprocess.CREATE_NO_WINDOW if os.name == 'nt' else 0 ) engine = RhymeEngine() # Wait for backend to start print("Waiting for backend for engine tests...") max_retries = 30 success = False for i in range(max_retries): if engine.bridge.is_alive(): print(f"Backend is alive (attempt {i+1})!") success = True break time.sleep(1) if not success: print("Backend failed to start.") proc.terminate() return try: # 2. Test Syllables print("\nTesting syllable counting...") words = ["rap", "lyrical", "gravitation", "sky"] for w in words: syl = engine.count_syllables(w) print(f"'{w}': {syl} syllables") # 3. Test Rhyme Suggestions print("\nTesting rhyme suggestions...") test_word = "flow" rhymes = engine.find_suggestions(test_word) print(f"Rhymes for '{test_word}': {len(rhymes['perfect'])} perfect, {len(rhymes['slant'])} slant") if rhymes['perfect']: print(f"Perfect example: {rhymes['perfect'][0]}") # 4. Test Line Densities (Heavy processing) print("\nTesting line density batch processing...") test_text = """ I'm the lyrical miracle, spiritual individual In the physical world, it's getting critical My rhymes are mystical, highly statistical You're just typical, completely satirical """ start_time = time.time() densities = engine.get_line_densities(test_text) end_time = time.time() print(f"Line densities: {[round(d, 2) for d in densities]}") print(f"Processing time (Batch C#): {end_time - start_time:.4f}s") finally: print("\nTerminating backend...") proc.terminate() if __name__ == "__main__": test_engine_bridge()