import sys import os import subprocess import time import requests # Add project root to sys.path so that 'from src.lyricflow_core...' works project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) sys.path.insert(0, project_root) from src.lyricflow_core.api.backend_bridge import BackendBridge def test_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}...") # Ensure any existing backend on port 5000 is cleared (optional, but good for local dev) 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 ) bridge = BackendBridge("http://localhost:5000") # Wait for backend to start print("Waiting for backend to start...") max_retries = 30 success = False for i in range(max_retries): if 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 Snippets print("Testing snapshots...") test_file = "test_file_bridge.lmd" test_content = f"Hello from C# Bridge! {time.time()}" bridge.save_snapshot(test_file, test_content) time.sleep(0.5) # Give it a moment to commit snapshots = bridge.get_snapshots(test_file) print(f"Found {len(snapshots)} snapshots.") if snapshots and snapshots[0].get('content') == test_content: print("Snapshot test PASSED") else: print(f"Snapshot test FAILED. Expected: {test_content}, Got: {snapshots[0].get('content') if snapshots else 'None'}") # 3. Test Scratchpad print("Testing scratchpad...") project_id = "test_proj_bridge" scratch_content = "Some ideas in C# Bridge" bridge.save_scratchpad(project_id, scratch_content) time.sleep(0.5) retrieved = bridge.get_scratchpad(project_id) if retrieved == scratch_content: print("Scratchpad test PASSED") else: print(f"Scratchpad test FAILED (got: {retrieved})") finally: print("Terminating backend...") proc.terminate() if __name__ == "__main__": test_bridge()