Spaces:
Running
on
Zero
Running
on
Zero
File size: 6,334 Bytes
ea5eb99 af6d2bd ea5eb99 89f9a59 ea5eb99 89f9a59 ea5eb99 89f9a59 ea5eb99 89f9a59 ea5eb99 89f9a59 ea5eb99 89f9a59 ea5eb99 89f9a59 ea5eb99 89f9a59 ea5eb99 89f9a59 ea5eb99 89f9a59 bd80f38 fd3c6d5 bd80f38 fd3c6d5 bd80f38 fd3c6d5 89f9a59 bd80f38 89f9a59 4644da6 89f9a59 4644da6 89f9a59 4644da6 89f9a59 4644da6 89f9a59 4644da6 89f9a59 4644da6 89f9a59 4644da6 89f9a59 4644da6 89f9a59 4644da6 89f9a59 4644da6 89f9a59 4644da6 89f9a59 4644da6 7eafc96 4644da6 89f9a59 c8f1234 89f9a59 c8f1234 89f9a59 c8f1234 89f9a59 2bb36fc 89f9a59 2bb36fc 89f9a59 2bb36fc 89f9a59 2bb36fc 89f9a59 2bb36fc 89f9a59 2bb36fc 89f9a59 2bb36fc 89f9a59 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
import pytest
import os
import cv2
from PIL import Image
from pathlib import Path
import tempfile
from utils import get_frames, process_video, process_user_input, process_history, extract_pdf_text, update_custom_prompt, check_file_size, MAX_VIDEO_SIZE, MAX_IMAGE_SIZE
# Get the project root directory
ROOT_DIR = Path(__file__).parent.parent
def test_get_frames():
"""Test that get_frames returns correct structure and handles video processing."""
video_path = os.path.join(ROOT_DIR, "assets", "test_video.mp4")
assert os.path.exists(video_path)
frames = get_frames(video_path, 3)
assert isinstance(frames, list)
assert all(isinstance(item, tuple) and len(item) == 2 for item in frames)
assert all(isinstance(img, Image.Image) and isinstance(ts, float) for img, ts in frames)
def test_process_video():
"""Test video processing returns expected structure."""
video_path = os.path.join(ROOT_DIR, "assets", "test_video.mp4")
result = process_video(video_path, 2)
assert len(result) == 4 # 2 frames * 2 items per frame
assert result[0]["type"] == "text" and result[0]["text"].startswith("Frame ")
assert result[1]["type"] == "image" and os.path.exists(result[1]["url"])
def test_process_video_invalid_path():
"""Test that process_video handles invalid paths appropriately."""
with pytest.raises(ValueError):
process_video("nonexistent_video.mp4", 3)
def test_process_user_input():
"""Test processing user input with different file types."""
# Text only
message = {"text": "Test message", "files": []}
result = process_user_input(message, 5)
assert len(result) == 1
assert result[0]["type"] == "text"
# With video
video_path = os.path.join(ROOT_DIR, "assets", "test_video.mp4")
if os.path.exists(video_path):
message = {"text": "Video analysis", "files": [video_path]}
result = process_user_input(message, 2)
assert len(result) >= 3 # text + frames
assert result[0]["text"] == "Video analysis"
assert result[1]["text"].startswith("Frame ")
def test_process_history():
"""Test basic conversation processing."""
history = [
{"role": "user", "content": "Hello"},
{"role": "assistant", "content": "Hi there!"},
{"role": "user", "content": "How are you?"}
]
result = process_history(history)
assert len(result) == 3
assert result[0] == {"role": "user", "content": [{"type": "text", "text": "Hello"}]}
assert result[1] == {"role": "assistant", "content": [{"type": "text", "text": "Hi there!"}]}
def test_extract_pdf_text():
"""Test PDF text extraction."""
import fitz
# Test non-existent file
with pytest.raises(ValueError, match="File not found"):
extract_pdf_text("nonexistent_file.pdf")
# Test with valid PDF
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as temp_pdf:
pdf_path = temp_pdf.name
try:
doc = fitz.open()
page = doc.new_page()
page.insert_text((50, 100), "Test PDF content.", fontsize=12)
doc.save(pdf_path)
doc.close()
result = extract_pdf_text(pdf_path)
assert "Test PDF content" in result
assert "Page 1:" in result
finally:
if os.path.exists(pdf_path):
os.unlink(pdf_path)
def test_process_user_input_with_pdf():
"""Test processing user input with PDF."""
import fitz
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as temp_pdf:
pdf_path = temp_pdf.name
try:
doc = fitz.open()
page = doc.new_page()
page.insert_text((50, 100), "Test PDF content.", fontsize=12)
doc.save(pdf_path)
doc.close()
message = {"text": "Analyze PDF", "files": [pdf_path]}
result = process_user_input(message, 3)
assert len(result) == 2
assert result[0]["text"] == "Analyze PDF"
assert "PDF Content:" in result[1]["text"]
assert "Test PDF content" in result[1]["text"]
finally:
if os.path.exists(pdf_path):
os.unlink(pdf_path)
def test_process_user_input_pdf_error_handling():
"""Test PDF error handling."""
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as temp_file:
temp_file.write(b"Invalid PDF content")
invalid_pdf_path = temp_file.name
try:
message = {"text": "Process invalid PDF", "files": [invalid_pdf_path]}
result = process_user_input(message, 3)
assert len(result) == 2
assert result[0]["text"] == "Process invalid PDF"
assert "Failed to extract text from PDF:" in result[1]["text"]
finally:
if os.path.exists(invalid_pdf_path):
os.unlink(invalid_pdf_path)
def test_update_custom_prompt():
"""Test system prompt selection."""
# Test key prompts
general = update_custom_prompt("General Assistant")
assert "images" in general.lower() and "videos" in general.lower()
document = update_custom_prompt("Document Analyzer")
assert "document" in document.lower() and "analysis" in document.lower()
# Test custom returns empty
assert update_custom_prompt("Custom") == ""
assert update_custom_prompt("Invalid") == ""
def test_check_file_size():
"""Test file size validation."""
# Test non-existent file
with pytest.raises(ValueError, match="File not found"):
check_file_size("nonexistent_file.txt")
# Test valid small files
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file:
temp_file.write(b"small content")
temp_path = temp_file.name
try:
assert check_file_size(temp_path) is True
finally:
if os.path.exists(temp_path):
os.unlink(temp_path)
# Test oversized image
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
temp_file.write(b"x" * (MAX_IMAGE_SIZE + 1024))
temp_path = temp_file.name
try:
with pytest.raises(ValueError, match="Image file too large"):
check_file_size(temp_path)
finally:
if os.path.exists(temp_path):
os.unlink(temp_path) |