Spaces:
Paused
Paused
File size: 5,177 Bytes
04684e6 | 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 | """
Unit tests for pure-function modules: formatter and chunker.
Run with: python -m pytest test.py -v
"""
import sys
import numpy as np
import pytest
# ---------------------------------------------------------------------------
# Basic import smoke test
# ---------------------------------------------------------------------------
def test_imports():
try:
import pkg_resources
import setuptools
import yaml
import gradio
import torch
import transformers
import librosa
import soundfile
import matplotlib
except ImportError as e:
pytest.fail(f"Missing dependency: {e}")
# ---------------------------------------------------------------------------
# OutputFormatter
# ---------------------------------------------------------------------------
from src.output.formatter import OutputFormatter
class TestTimestamp:
def test_zero(self):
assert OutputFormatter._format_timestamp(0, "srt") == "00:00:00,000"
def test_sub_minute(self):
assert OutputFormatter._format_timestamp(5.5, "srt") == "00:00:05,500"
def test_over_one_hour(self):
# 3661.25 s = 1h 1m 1.25s
result = OutputFormatter._format_timestamp(3661.25, "srt")
assert result == "01:01:01,250"
def test_vtt_under_one_hour(self):
result = OutputFormatter._format_timestamp(90.0, "vtt")
assert result == "01:30.000"
def test_vtt_over_one_hour(self):
result = OutputFormatter._format_timestamp(3700.0, "vtt")
assert result == "01:01:40.000"
class TestSRT:
def test_empty_segments(self):
assert OutputFormatter.to_srt({"segments": []}) == ""
def test_basic_srt(self):
result = {"segments": [{"start": 0.0, "end": 2.5, "text": "Hello world"}]}
srt = OutputFormatter.to_srt(result)
assert "1" in srt
assert "00:00:00,000 --> 00:00:02,500" in srt
assert "Hello world" in srt
def test_multi_segment(self):
result = {"segments": [
{"start": 0.0, "end": 1.0, "text": "First"},
{"start": 1.0, "end": 2.0, "text": "Second"},
]}
srt = OutputFormatter.to_srt(result)
assert "First" in srt
assert "Second" in srt
class TestVTT:
def test_empty_returns_webvtt_header(self):
assert OutputFormatter.to_vtt({"segments": []}).startswith("WEBVTT")
def test_basic_vtt(self):
result = {"segments": [{"start": 0.0, "end": 3.0, "text": "Test"}]}
vtt = OutputFormatter.to_vtt(result)
assert vtt.startswith("WEBVTT")
assert "00:03.000" in vtt
class TestFormatDispatch:
def test_txt(self):
assert OutputFormatter.format({"text": "hi"}, "txt") == "hi"
def test_unsupported_raises(self):
with pytest.raises(ValueError, match="Unsupported"):
OutputFormatter.format({}, "docx")
# ---------------------------------------------------------------------------
# AudioChunker
# ---------------------------------------------------------------------------
from src.audio_processor.chunker import AudioChunker, AudioChunk
class TestChunker:
SR = 16000
def _make_audio(self, seconds: float) -> np.ndarray:
return np.random.rand(int(seconds * self.SR)).astype(np.float32)
def test_short_audio_no_chunk(self):
chunker = AudioChunker(chunk_duration=30, overlap_duration=2, target_sr=self.SR)
audio = self._make_audio(10)
chunks = chunker.create_chunks(audio, self.SR)
assert len(chunks) == 1
assert chunks[0].index == 0
assert not chunks[0].overlap_with_prev
def test_long_audio_multiple_chunks(self):
chunker = AudioChunker(chunk_duration=30, overlap_duration=2, target_sr=self.SR)
audio = self._make_audio(90)
chunks = chunker.create_chunks(audio, self.SR)
assert len(chunks) > 1
def test_should_chunk(self):
chunker = AudioChunker(chunk_duration=30, overlap_duration=2, target_sr=self.SR)
assert not chunker.should_chunk(20)
assert chunker.should_chunk(60)
def test_merge_empty(self):
chunker = AudioChunker(chunk_duration=30, overlap_duration=2, target_sr=self.SR)
result = chunker.merge_chunk_results([])
assert result["text"] == ""
assert result["segments"] == []
def test_merge_single_chunk(self):
chunker = AudioChunker(chunk_duration=30, overlap_duration=2, target_sr=self.SR)
audio = self._make_audio(5)
chunk = AudioChunk(audio=audio, start_time=0, end_time=5, index=0)
mock_result = {"text": "Hello", "segments": [{"start": 0, "end": 1, "text": "Hello"}]}
merged = chunker.merge_chunk_results([(chunk, mock_result)])
assert "Hello" in merged["text"]
# ---------------------------------------------------------------------------
# Run directly
# ---------------------------------------------------------------------------
if __name__ == "__main__":
print("Running basic import check...")
test_imports()
print("✓ All imports OK")
print("\nRun 'python -m pytest test.py -v' for full test suite.")
|