aboutsummaryrefslogtreecommitdiff
path: root/tests/test_audio.py
blob: a629224ddada9957cdcc4c73ebead2b883faaf09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Tests for audio helpers: speed parameters and chunk cleanup."""

import tempfile
import unittest
from pathlib import Path

from converter import config
from converter.audio import cleanup_chunks, speed_export_params


class SpeedExportParamsTests(unittest.TestCase):
    def test_normal_speed_no_filter(self):
        self.assertEqual(speed_export_params(1.0), [])

    def test_simple_speedup(self):
        self.assertEqual(speed_export_params(1.5), ["-filter:a", "atempo=1.5"])

    def test_simple_slowdown(self):
        self.assertEqual(speed_export_params(0.75), ["-filter:a", "atempo=0.75"])

    def test_chained_speedup_beyond_2x(self):
        self.assertEqual(speed_export_params(3.0), ["-filter:a", "atempo=2.0,atempo=1.5"])

    def test_chained_slowdown_below_half(self):
        self.assertEqual(speed_export_params(0.25), ["-filter:a", "atempo=0.5,atempo=0.5"])

    def test_zero_speed_rejected(self):
        with self.assertRaises(ValueError):
            speed_export_params(0)

    def test_negative_speed_rejected(self):
        with self.assertRaises(ValueError):
            speed_export_params(-1.5)


class CleanupChunksTests(unittest.TestCase):
    def test_removes_only_chunk_files(self):
        with tempfile.TemporaryDirectory() as tmp:
            chunks_dir = Path(tmp)
            (chunks_dir / "chunk_0001.wav").write_bytes(b"stale")
            (chunks_dir / "chunk_0002.wav").write_bytes(b"stale")
            (chunks_dir / "keep.txt").write_bytes(b"keep")

            original = config.CHUNKS_FOLDER
            config.CHUNKS_FOLDER = chunks_dir
            try:
                cleanup_chunks()
            finally:
                config.CHUNKS_FOLDER = original

            self.assertFalse((chunks_dir / "chunk_0001.wav").exists())
            self.assertFalse((chunks_dir / "chunk_0002.wav").exists())
            self.assertTrue((chunks_dir / "keep.txt").exists())


if __name__ == "__main__":
    unittest.main()