#!/usr/bin/env python3
"""Test ASR processor with debug output."""

import sys
import os
import subprocess
import tempfile
import time

# Use a smaller test file first
test_video = "../test_video/BigBuckBunny_320x180.mp4"
if not os.path.exists(test_video):
    # Try another file
    test_video = "../test_video/big_buck_bunny_480p_h264.mov"

print(f"Testing: {test_video}")
size_gb = os.path.getsize(test_video) / (1024**3)
print(f"Size: {size_gb:.2f} GB")

# Create temp output
temp_dir = tempfile.mkdtemp(prefix="asr_debug_loop_")
output_path = os.path.join(temp_dir, "output.json")

# Use debug version
cmd = [
    "/opt/homebrew/bin/python3.11",
    "scripts/asr_processor_debug.py",
    test_video,
    output_path,
    "--uuid",
    "debug_loop_test",
]

print(f"Running command...")
print(f"Temp dir: {temp_dir}")

# Run with short timeout
timeout = 30
start = time.time()

proc = subprocess.Popen(
    cmd,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    text=True,
)

try:
    stdout, stderr = proc.communicate(timeout=timeout)
    elapsed = time.time() - start
    success = proc.returncode == 0
    timeout_hit = False
except subprocess.TimeoutExpired:
    elapsed = time.time() - start
    success = False
    timeout_hit = True
    proc.kill()
    stdout, stderr = proc.communicate()

print(f"\nElapsed: {elapsed:.1f}s")
print(f"Success: {success}")
print(f"Timeout: {timeout_hit}")

print("\n=== STDERR ===")
print(stderr)

print("\n=== STDOUT ===")
print(stdout)

if os.path.exists(output_path):
    print(f"\nOutput file exists: {output_path}")
else:
    print(f"\nOutput file does not exist")

print(f"\nTemp directory: {temp_dir}")
