返回 douyin-downloader
test_silent_audio.py
根目录 / tests / test_silent_audio.py
1 """Tests for ``core.silent_audio``.
2
3 The bytes are baked in at source-edit time via the recipe in
4 ``core/silent_audio.py``'s docstring; these tests just guard against
5 accidental corruption (truncation, base64 typos, encoding drift).
6 """
7 from __future__ import annotations
8
9 from core.silent_audio import SILENT_1S_MP3_BYTES
10
11
12 def test_silent_mp3_is_nonempty() -> None:
13 """The embedded payload must decode to a non-empty byte string."""
14 assert isinstance(SILENT_1S_MP3_BYTES, bytes)
15 assert len(SILENT_1S_MP3_BYTES) > 0
16
17
18 def test_silent_mp3_starts_with_valid_magic() -> None:
19 """The first bytes must be a legal MP3 frame sync (``\\xff\\xfb``…)
20 or an ID3v2 tag (``ID3``). Either is what ffmpeg's libmp3lame
21 produces; a corrupted base64 literal would land on something else
22 and should fail loudly here, not at probe-time on the user's box.
23 """
24 assert SILENT_1S_MP3_BYTES.startswith(b"ID3") or (
25 SILENT_1S_MP3_BYTES[0] == 0xFF and (SILENT_1S_MP3_BYTES[1] & 0xE0) == 0xE0
26 ), f"unexpected mp3 magic: {SILENT_1S_MP3_BYTES[:4]!r}"
27
28
29 def test_silent_mp3_size_within_expected_band() -> None:
30 """A 1-second 32 kbps mono MP3 is ~4 KB. Guard against accidental
31 truncation that would break the connectivity probe payload."""
32 assert 1024 <= len(SILENT_1S_MP3_BYTES) <= 16 * 1024, (
33 f"unexpected size: {len(SILENT_1S_MP3_BYTES)} bytes "
34 "(expected 1-16 KB for 1s mono mp3)"
35 )
36
36 lines PYTHON