AI-video-lifeplan-demo/tests/test_video_processor.py

143 lines
3.9 KiB
Python

"""
Unit tests for video_processor module
"""
import os
import pytest
from pathlib import Path
# Add project root to path
import sys
sys.path.insert(0, str(Path(__file__).parent.parent))
from backend.video_processor import analyze_video
from backend.video_processor.utils import (
check_ffmpeg_installed,
get_video_duration,
extract_audio
)
from backend.video_processor.schemas import (
VideoSegment,
KeyMoment,
VideoAnalysisResult
)
class TestSchemas:
"""Test data structure definitions."""
def test_video_segment_creation(self):
seg = VideoSegment(
start_time=0.0,
end_time=5.5,
description="Test segment",
transcript="こんにちは",
importance="high",
tags=["greeting"]
)
assert seg.duration == 5.5
assert seg.importance == "high"
seg_dict = seg.to_dict()
assert seg_dict["startTime"] == 0.0
assert seg_dict["transcript"] == "こんにちは"
def test_key_moment_creation(self):
moment = KeyMoment(
timestamp=10.5,
type="quote",
content="重要な発言",
context="冒頭の挨拶"
)
moment_dict = moment.to_dict()
assert moment_dict["timestamp"] == 10.5
assert moment_dict["type"] == "quote"
def test_video_analysis_result(self):
result = VideoAnalysisResult(
source_video="test.mp4",
duration=60.0,
strategy="audio-first",
segments=[
VideoSegment(0, 10, "Seg 1", importance="high"),
VideoSegment(10, 20, "Seg 2", importance="low"),
],
key_moments=[
KeyMoment(5.0, "quote", "Test")
]
)
assert len(result.get_high_importance_segments()) == 1
assert len(result.get_quotes()) == 1
result_dict = result.to_dict()
assert result_dict["duration"] == 60.0
assert len(result_dict["segments"]) == 2
class TestUtils:
"""Test utility functions."""
def test_check_ffmpeg_installed(self):
# This should pass if FFmpeg is in PATH
is_installed = check_ffmpeg_installed()
assert isinstance(is_installed, bool)
@pytest.mark.skipif(
not os.path.exists("tests/fixtures/sample.mp4"),
reason="Sample video not found"
)
def test_get_video_duration(self):
duration = get_video_duration("tests/fixtures/sample.mp4")
assert duration > 0
@pytest.mark.skipif(
not os.path.exists("tests/fixtures/sample.mp4"),
reason="Sample video not found"
)
def test_extract_audio(self):
audio_path = extract_audio(
"tests/fixtures/sample.mp4",
output_path="tests/fixtures/test_audio.wav"
)
assert os.path.exists(audio_path)
# Cleanup
if os.path.exists(audio_path):
os.remove(audio_path)
@pytest.mark.skipif(
not os.getenv("OPENAI_API_KEY"),
reason="OPENAI_API_KEY not set"
)
@pytest.mark.skipif(
not os.path.exists("tests/fixtures/sample.mp4"),
reason="Sample video not found"
)
class TestAudioFirstStrategy:
"""Test audio-first strategy (requires API key and sample video)."""
def test_analyze_video(self):
result = analyze_video(
video_path="tests/fixtures/sample.mp4",
strategy="audio",
config={"language": "ja"}
)
assert result.strategy == "audio-first"
assert result.duration > 0
assert len(result.segments) > 0
# Check that segments have transcripts
for seg in result.segments:
assert seg.transcript is not None
assert len(seg.transcript) > 0
if __name__ == "__main__":
pytest.main([__file__, "-v"])