/
/
/
1"""Unit tests for the deterministic CLAP target-start planner."""
2
3from __future__ import annotations
4
5from music_assistant.providers.sonic_analysis import (
6 CLAP_SKIP_SECONDS,
7 CLAP_WINDOW_SECONDS,
8 compute_clap_target_starts,
9)
10
11SR = 22050
12
13
14def test_target_starts_below_one_second_returns_empty() -> None:
15 """Tracks under 1s of audio are unusable; caller must skip CLAP entirely."""
16 assert compute_clap_target_starts(0.5, 8, SR) == []
17 assert compute_clap_target_starts(0.99, 1, SR) == []
18
19
20def test_target_starts_under_window_seconds_returns_whole_clip() -> None:
21 """Tracks shorter than one window feed the whole clip (CLAP wrapper pads)."""
22 assert compute_clap_target_starts(3.0, 8, SR) == [0]
23 assert compute_clap_target_starts(1.0, 1, SR) == [0]
24
25
26def test_target_starts_under_skip_plus_window_uses_middle_seven() -> None:
27 """Tracks shorter than skip+window fall back to middle-7s placement."""
28 duration = 30.0
29 expected_start_seconds = (duration - CLAP_WINDOW_SECONDS) / 2.0
30 assert compute_clap_target_starts(duration, 8, SR) == [int(expected_start_seconds * SR)]
31
32
33def test_target_starts_long_track_preset_one() -> None:
34 """Long track + Fast preset â single window at the skip mark."""
35 assert compute_clap_target_starts(120.0, 1, SR) == [CLAP_SKIP_SECONDS * SR]
36
37
38def test_target_starts_long_track_thorough_evenly_spaced() -> None:
39 """Long track + Thorough â 8 windows from skip mark to track tail."""
40 duration = 240.0
41 result = compute_clap_target_starts(duration, 8, SR)
42 assert len(result) == 8
43 assert result[0] == CLAP_SKIP_SECONDS * SR
44 assert result[-1] == int((duration - CLAP_WINDOW_SECONDS) * SR)
45 assert all(result[i] < result[i + 1] for i in range(7))
46
47
48def test_target_starts_short_track_caps_effective_n() -> None:
49 """A 60s track can only fit 2 non-overlapping 7s windows past the 45s skip."""
50 sr = SR
51 result = compute_clap_target_starts(60.0, 8, sr)
52 assert len(result) == 2
53 assert result[0] == CLAP_SKIP_SECONDS * sr
54 assert result[1] == int((60.0 - CLAP_WINDOW_SECONDS) * sr)
55
56
57def test_target_starts_balanced_preset_three_windows_long_track() -> None:
58 """Balanced preset (N=3) returns 3 evenly spaced starts on a long track."""
59 duration = 120.0
60 result = compute_clap_target_starts(duration, 3, SR)
61 assert len(result) == 3
62 assert result[0] == CLAP_SKIP_SECONDS * SR
63 assert result[-1] == int((duration - CLAP_WINDOW_SECONDS) * SR)
64
65
66def test_target_starts_exactly_skip_plus_window_collapses_to_single() -> None:
67 """A track of exactly skip+window seconds has zero usable spread â single window."""
68 duration = float(CLAP_SKIP_SECONDS + CLAP_WINDOW_SECONDS)
69 assert compute_clap_target_starts(duration, 8, SR) == [CLAP_SKIP_SECONDS * SR]
70
71
72def test_target_starts_just_below_skip_plus_window_uses_middle() -> None:
73 """A 51.5s track is just under skip+window â middle-7s fallback."""
74 duration = 51.5
75 expected_start = int(((duration - CLAP_WINDOW_SECONDS) / 2.0) * SR)
76 assert compute_clap_target_starts(duration, 8, SR) == [expected_start]
77
78
79def test_target_starts_deterministic() -> None:
80 """Same inputs return identical lists across repeated calls."""
81 a = compute_clap_target_starts(180.0, 5, SR)
82 b = compute_clap_target_starts(180.0, 5, SR)
83 assert a == b
84