"""Unit tests for handle_transcode waveform peaks storage.""" from unittest.mock import AsyncMock, MagicMock, patch, call import uuid import numpy as np import pytest @pytest.fixture def mock_audio(sine_440hz): audio, sr = sine_440hz return audio, sr @pytest.mark.asyncio async def test_handle_transcode_stores_both_peak_resolutions(mock_audio): """After handle_transcode, waveform_peaks (500) and waveform_peaks_mini (100) are stored in DB.""" audio, sr = mock_audio version_id = uuid.uuid4() # Capture the statement passed to session.execute executed_stmts = [] async def capture_execute(stmt): executed_stmts.append(stmt) return MagicMock() mock_session = AsyncMock() mock_session.execute = capture_execute mock_session.commit = AsyncMock() mock_settings = MagicMock() mock_settings.nextcloud_url = "http://nc.test" mock_settings.nextcloud_user = "user" mock_settings.nextcloud_pass = "pass" mock_settings.target_sample_rate = 44100 mock_settings.audio_tmp_dir = "/tmp" payload = { "version_id": str(version_id), "nc_file_path": "/bands/test/songs/test/v1.wav", } with ( patch("worker.main.load_audio", return_value=(audio, sr, "/tmp/v1.wav")), patch("worker.main.get_duration_ms", return_value=5000), patch("worker.main.transcode_to_hls", new_callable=AsyncMock), patch("worker.main.run_full_analysis", new_callable=AsyncMock), ): from worker.main import handle_transcode await handle_transcode(payload, mock_session, mock_settings) assert len(executed_stmts) == 1, "Expected exactly one UPDATE statement" stmt = executed_stmts[0] # Extract the values dict from the SQLAlchemy Update statement values = stmt._values value_keys = {col.key for col, _ in values.items()} assert "waveform_peaks" in value_keys, f"waveform_peaks not in UPDATE values: {value_keys}" assert "waveform_peaks_mini" in value_keys, f"waveform_peaks_mini not in UPDATE values: {value_keys}" # Resolve the actual peak lists from the BindParameter objects peaks_500 = next(val.value for col, val in values.items() if col.key == "waveform_peaks") peaks_100 = next(val.value for col, val in values.items() if col.key == "waveform_peaks_mini") assert len(peaks_500) == 500, f"Expected 500 peaks, got {len(peaks_500)}" assert len(peaks_100) == 100, f"Expected 100 mini peaks, got {len(peaks_100)}" assert all(0.0 <= p <= 1.0 for p in peaks_500), "Full peaks out of [0, 1] range" assert all(0.0 <= p <= 1.0 for p in peaks_100), "Mini peaks out of [0, 1] range"