#!/usr/bin/env python3 """Test script for VLChan synchronization logic.""" import sys from vlchan import VLChanPlayer def test_playback_rate_calculation(): """Test the playback rate calculation logic.""" print("Testing playback rate calculation...") # Create player instance to access the method player = VLChanPlayer() # Test cases from the TypeScript implementation test_cases = [ (0.0, 1.0), # No difference (0.1, 1.05), # Small positive difference (-0.1, 0.95), # Small negative difference (0.2, 1.05), # Large positive difference (capped) (-0.2, 0.95), # Large negative difference (capped) (1.0, 1.05), # Very large positive difference (capped) (-1.0, 0.95), # Very large negative difference (capped) ] for diff, expected in test_cases: result = player._get_playback_rate(diff) print(f" diff={diff:6.1f}s -> rate={result:.3f} (expected {expected:.3f})") assert abs(result - expected) < 0.001, f"Expected {expected}, got {result}" print("✓ Playback rate calculation tests passed") player.cleanup() def test_latency_compensation(): """Test latency compensation logic.""" print("\nTesting latency compensation...") player = VLChanPlayer() # Test latency compensation test_cases = [ (100.0, 10.0, 10.1), # 100ms latency, 10s position -> 10.1s compensated (50.0, 5.5, 5.55), # 50ms latency, 5.5s position -> 5.55s compensated (0.0, 3.0, 3.0), # No latency ] for latency, position, expected in test_cases: # Mock the position player.player.set_time(int(position * 1000)) result = player.get_compensated_position(latency) print(f" latency={latency:4.0f}ms, pos={position:.1f}s -> {result:.3f}s (expected {expected:.1f}s)") assert abs(result - expected) < 0.01, f"Expected {expected}, got {result}" print("✓ Latency compensation tests passed") player.cleanup() def test_sync_threshold_logic(): """Test the synchronization threshold logic.""" print("\nTesting sync threshold logic...") player = VLChanPlayer() # Test cases for seeking vs rate adjustment test_cases = [ (0.5, False, "Small diff should not seek"), (1.5, True, "Large diff should seek"), (0.05, False, "Very small diff should not seek"), (2.0, True, "Very large diff should seek"), ] for diff, should_seek, description in test_cases: seek_condition = diff > 1.0 print(f" diff={diff:.2f}s -> seek={seek_condition} ({description})") assert seek_condition == should_seek, f"Expected {should_seek}, got {seek_condition}" print("✓ Sync threshold logic tests passed") def main(): """Run all synchronization tests.""" print("VLChan Synchronization Test Suite") print("=" * 40) try: test_playback_rate_calculation() test_latency_compensation() test_sync_threshold_logic() print("\n" + "=" * 40) print("✓ All synchronization tests passed!") return 0 except Exception as e: print(f"\n✗ Test failed: {e}") return 1 if __name__ == "__main__": sys.exit(main())