/
/
/
1"""Tests for the shared boundary index every queue mutation stays clear of."""
2
3from __future__ import annotations
4
5import pytest
6from music_assistant_models.player_queue import PlayerQueue
7
8from music_assistant.controllers.player_queues.helpers import committed_index
9
10
11def _queue(current_index: int | None, index_in_buffer: int | None) -> PlayerQueue:
12 """Build a queue parked at the given playing and buffered indexes."""
13 queue = PlayerQueue(queue_id="q1", active=True, display_name="Q1", available=True, items=5)
14 queue.current_index = current_index
15 queue.index_in_buffer = index_in_buffer
16 return queue
17
18
19@pytest.mark.parametrize(
20 ("current_index", "index_in_buffer", "expected"),
21 [
22 (2, 2, 2),
23 (2, 3, 3),
24 (0, 4, 4),
25 (4, 0, 4),
26 (3, None, 3),
27 (None, 1, 1),
28 (None, None, None),
29 (0, 0, 0),
30 ],
31)
32def test_committed_index(
33 current_index: int | None, index_in_buffer: int | None, expected: int | None
34) -> None:
35 """The boundary is whichever of the two positions is furthest into the queue."""
36 assert committed_index(_queue(current_index, index_in_buffer)) == expected
37
38
39def test_a_wrapped_buffer_never_reports_a_boundary_before_the_playing_track() -> None:
40 """
41 Repeat loops the buffered index to the front, and the boundary does not follow it there.
42
43 Inserting or truncating before the playing track shifts it, and its index is not re-anchored,
44 so the queue would end up naming a different track than the one being played.
45 """
46 assert committed_index(_queue(current_index=4, index_in_buffer=0)) == 4
47