feat: Enhance audio normalization service to handle invalid analysis values and improve fallback logic

This commit is contained in:
JSC
2025-07-28 09:48:35 +02:00
parent 0fffce53b4
commit 71da295827
2 changed files with 53 additions and 21 deletions

View File

@@ -41,7 +41,7 @@ class TestSoundScannerService:
try:
hash_value = scanner_service.get_file_hash(temp_path)
assert len(hash_value) == 32 # MD5 hash length
assert len(hash_value) == 64 # SHA-256 hash length
assert isinstance(hash_value, str)
finally:
temp_path.unlink()
@@ -77,9 +77,7 @@ class TestSoundScannerService:
@patch("app.services.sound_scanner.ffmpeg.probe")
def test_get_audio_duration_success(self, mock_probe, scanner_service):
"""Test successful audio duration extraction."""
mock_probe.return_value = {
"format": {"duration": "123.456"}
}
mock_probe.return_value = {"format": {"duration": "123.456"}}
temp_path = Path("/fake/path/test.mp3")
duration = scanner_service.get_audio_duration(temp_path)
@@ -137,8 +135,13 @@ class TestSoundScannerService:
try:
results = {
"scanned": 0, "added": 0, "updated": 0, "deleted": 0,
"skipped": 0, "errors": 0, "files": []
"scanned": 0,
"added": 0,
"updated": 0,
"deleted": 0,
"skipped": 0,
"errors": 0,
"files": [],
}
await scanner_service._sync_audio_file(
temp_path, "SDB", existing_sound, results
@@ -153,7 +156,7 @@ class TestSoundScannerService:
finally:
temp_path.unlink()
@pytest.mark.asyncio
@pytest.mark.asyncio
async def test_sync_audio_file_new(self, scanner_service):
"""Test syncing a new audio file."""
created_sound = Sound(
@@ -178,8 +181,13 @@ class TestSoundScannerService:
try:
results = {
"scanned": 0, "added": 0, "updated": 0, "deleted": 0,
"skipped": 0, "errors": 0, "files": []
"scanned": 0,
"added": 0,
"updated": 0,
"deleted": 0,
"skipped": 0,
"errors": 0,
"files": [],
}
await scanner_service._sync_audio_file(temp_path, "SDB", None, results)
@@ -188,7 +196,7 @@ class TestSoundScannerService:
assert results["updated"] == 0
assert len(results["files"]) == 1
assert results["files"][0]["status"] == "added"
# Verify sound_repo.create was called with correct data
call_args = scanner_service.sound_repo.create.call_args[0][0]
assert call_args["type"] == "SDB"
@@ -200,7 +208,7 @@ class TestSoundScannerService:
finally:
temp_path.unlink()
@pytest.mark.asyncio
@pytest.mark.asyncio
async def test_sync_audio_file_updated(self, scanner_service):
"""Test syncing a file that was modified (different hash)."""
# Existing sound with different hash than file
@@ -210,7 +218,7 @@ class TestSoundScannerService:
name="Old Sound",
filename="test.mp3",
duration=60000, # Old duration
size=512, # Old size
size=512, # Old size
hash="old_hash", # Old hash
)
@@ -227,8 +235,13 @@ class TestSoundScannerService:
try:
results = {
"scanned": 0, "added": 0, "updated": 0, "deleted": 0,
"skipped": 0, "errors": 0, "files": []
"scanned": 0,
"added": 0,
"updated": 0,
"deleted": 0,
"skipped": 0,
"errors": 0,
"files": [],
}
await scanner_service._sync_audio_file(
temp_path, "SDB", existing_sound, results
@@ -240,7 +253,7 @@ class TestSoundScannerService:
assert len(results["files"]) == 1
assert results["files"][0]["status"] == "updated"
assert results["files"][0]["reason"] == "file was modified"
# Verify sound_repo.update was called with correct data
call_args = scanner_service.sound_repo.update.call_args[0][1] # update_data
assert call_args["duration"] == 120000
@@ -251,7 +264,7 @@ class TestSoundScannerService:
finally:
temp_path.unlink()
@pytest.mark.asyncio
@pytest.mark.asyncio
async def test_sync_audio_file_custom_type(self, scanner_service):
"""Test syncing file with custom type."""
created_sound = Sound(
@@ -276,8 +289,13 @@ class TestSoundScannerService:
try:
results = {
"scanned": 0, "added": 0, "updated": 0, "deleted": 0,
"skipped": 0, "errors": 0, "files": []
"scanned": 0,
"added": 0,
"updated": 0,
"deleted": 0,
"skipped": 0,
"errors": 0,
"files": [],
}
await scanner_service._sync_audio_file(temp_path, "CUSTOM", None, results)
@@ -285,7 +303,7 @@ class TestSoundScannerService:
assert results["skipped"] == 0
assert len(results["files"]) == 1
assert results["files"][0]["status"] == "added"
# Verify sound_repo.create was called with correct data for custom type
call_args = scanner_service.sound_repo.create.call_args[0][0]
assert call_args["type"] == "CUSTOM"
@@ -293,6 +311,8 @@ class TestSoundScannerService:
assert call_args["duration"] == 60000 # Duration in ms
assert call_args["size"] == 2048
assert call_args["hash"] == "custom_hash"
assert call_args["is_deletable"] is False # All sounds are set to not deletable
assert (
call_args["is_deletable"] is False
) # All sounds are set to not deletable
finally:
temp_path.unlink()
temp_path.unlink()