from smart_monitor import Dashboard
import time

def test_tui_parsing():
    print("Testing Dashboard Logic...")
    db = Dashboard(1)
    
    # Test 1: Encoding Progress
    msg = "Encoding av1 | 45.2% | 2.3x"
    db.update_worker(0, "Test File 1.mkv", msg)
    status = db.worker_status[0]
    print(f"Test 1 (Parsing): Progress={status['progress']} (Expected 45.2), Speed={status['speed']} (Expected 2.3x)")
    assert status['progress'] == 45.2
    assert status['speed'] == "2.3x"
    
    # Test 2: Stats - Skipped
    print("\nTest 2: Stats - Skipped")
    # Simulate worker wrapper logic
    msg = "Already AV1 (Skipping)"
    if "Skipping" in msg:
        db.update_stats("skipped")
    
    print(f"Skipped Count: {db.stats['skipped']} (Expected 1)")
    assert db.stats['skipped'] == 1
        
    # Test 3: Stats - Rejected
    print("\nTest 3: Stats - Rejected")
    msg = "HEVC Rejected"
    if "Rejected" in msg:
        db.update_stats("rejected")
        
    print(f"Rejected Count: {db.stats['rejected']} (Expected 1)")
    assert db.stats['rejected'] == 1
    
    # Test 4: Stats - Failed
    print("\nTest 4: Stats - Failed")
    msg = "Error: FFMPEG failed"
    if "Error" in msg:
        db.update_stats("failed")
        
    print(f"Failed Count: {db.stats['failed']} (Expected 1)")
    assert db.stats['failed'] == 1

    print("\nAll Tests Passed!")

if __name__ == "__main__":
    test_tui_parsing()
