""" 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.")