fix: verify audio magic bytes before extension-based short-circuit in convert_audio_format - #9651
Conversation
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The ffmpeg subprocess mocking logic (including
_FakeFFmpegProcessand thefake_execsetup) is duplicated across several tests; consider extracting a reusable helper or fixture to keep the test suite DRY and easier to maintain. - In
_FakeFFmpegProcess, thestderrparameter is accepted but never used; either wire it intocommunicate()or remove the parameter to avoid confusion about its purpose.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The ffmpeg subprocess mocking logic (including `_FakeFFmpegProcess` and the `fake_exec` setup) is duplicated across several tests; consider extracting a reusable helper or fixture to keep the test suite DRY and easier to maintain.
- In `_FakeFFmpegProcess`, the `stderr` parameter is accepted but never used; either wire it into `communicate()` or remove the parameter to avoid confusion about its purpose.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Address Sourcery review feedback on AstrBotDevs#9651: - Extract _patch_ffmpeg() helper to deduplicate the fake_exec setup across the three conversion-expected tests. - Remove the unused stderr parameter from _FakeFFmpegProcess.
|
Addressed both Sourcery notes:
Verified: |
Address Sourcery review feedback on AstrBotDevs#9651: - Extract _patch_ffmpeg() helper to deduplicate the fake_exec setup across the three conversion-expected tests. - Remove the unused stderr parameter from _FakeFFmpegProcess.
ac7b2d6 to
394d4a6
Compare
… convert_audio_format When a platform (e.g. NapCat) saves AMR-encoded audio with a .wav extension, the extension-only short-circuit returned the raw AMR file unchanged. Downstream STT providers then received malformed WAV data and returned HTTP 400. The fix reuses the existing _get_audio_magic_type() helper to verify the file content actually matches the target format before skipping ffmpeg conversion. When the detected format differs from the output format, conversion proceeds normally. Unrecognised content still falls back to extension matching to preserve existing behaviour. Closes AstrBotDevs#9594.
Address Sourcery review feedback on AstrBotDevs#9651: - Extract _patch_ffmpeg() helper to deduplicate the fake_exec setup across the three conversion-expected tests. - Remove the unused stderr parameter from _FakeFFmpegProcess.
Collapse the three duplicated fake_exec stubs in the short-circuit tests into _patch_ffmpeg_not_called(monkeypatch, reason), finishing the Sourcery review note on duplicated ffmpeg mocking.
…ication The core fix for AstrBotDevs#9594 was already merged in AstrBotDevs#9612 (detect audio format from file content). This commit adapts the test suite to match that implementation: - Update unknown-content test: master converts unrecognised formats rather than short-circuiting (safer behavior). - Add missing-file test: master returns the path as-is when the file does not exist yet (NapCat race condition handling). - Update section header to reference both AstrBotDevs#9594 and AstrBotDevs#9612.
394d4a6 to
35b8c47
Compare
|
Closing this PR — the core fix for #9594 was already merged in #9612, which also includes 3 regression tests covering the critical scenarios (AMR-as-WAV transcoding, misnamed target format, and missing-path handling). The additional positive/edge-case tests in this PR add marginal value and arent worth the review time. The PR served its purpose as a learning exercise and helped me understand the media pipeline. Thanks to anyone who looked. |
Problem
When a platform adapter (e.g. NapCat via aiocqhttp / OneBot v11) saves QQ voice messages, the actual audio encoding is AMR (magic bytes
#!AMR) but the local file is given a.wavextension. Theconvert_audio_format()function short-circuits purely on file extension:When
output_formatiswav, the extension matches and the raw AMR byte stream is returned unchanged. Downstream STT providers (e.g. Whisper-compatible APIs) receive malformed WAV data and reject it with HTTP 400.Reported in #9594.
Fix
Reuse the existing
_get_audio_magic_type()helper (already used byensure_wav()) to verify the file content actually matches the target format before short-circuiting:Three cases:
Tests
5 new tests added to
tests/test_media_utils.py, all passing:amr_with_wav_extension_does_not_short_circuit.wavext, targetwavreal_wav_with_wav_extension_short_circuits.wavext, targetwavunknown_content_with_matching_ext_short_circuits.wavext, targetwavreal_amr_with_amr_extension_short_circuits.amrext, targetamrwav_with_ogg_extension_does_not_short_circuit.oggext, targetoggFull suite: 55 passed.
ruff format/ruff checkclean.Closes #9594.
Summary by Sourcery
Validate audio content against its detected format before skipping conversion based on the file extension.
Bug Fixes:
Enhancements:
Tests: