fix(qconnect): badge clear, multi-track enqueue, shuffle WS-authoritative, cursor/history/pause

- badge: gate is-remote on transport_connected so it clears on every session-end
  path (transport drop / watchdog / reconnect-exhausted), not only toggle-off
- multi-track: route a whole album/playlist/favorites enqueue to the peer in one
  QueueAddTracks/QueueInsertTracks (all-or-nothing admission), not single-track only
- shuffle inbound: SetShuffleMode renderer arm is flag-only (set_shuffle_flag) so the
  renderer never invents a local order; the cloud's echoed order is authoritative
- shuffle outbound: send shuffle_seed + shuffle_pivot_queue_item_id (the cloud needs
  the seed to build the order); hoist resolve_qconnect_shuffle_pivot to qconnect-app
- start index: resolve_remote_start_index validates the track_id before trusting a
  qid, so a freshly-pushed album no longer inherits a stale cursor (wrong NowPlayingBar track)
- history: play_index records history only when actually moving, so the current track
  is no longer duplicated in the History tab
- pause-to-peer: resolve current_queue_item from the core-aligned cursor instead of the
  stale renderer snapshot, so pausing an iOS renderer (reports null qid) is accepted

cc
This commit is contained in:
vicrodh committed 2026-06-04 21:59:58 -06:00
1 parent 712447eb00
commit 1f1d7303ef
10 files changed
+499 -49

No files matched your search

+135 -1
View File
@@ -374,7 +374,22 @@ pub fn resolve_remote_start_index(
.iter()
.position(|item| item.queue_item_id == queue_item_id)
{
return Some(index);
// Only trust the queue_item_id when the track at that position matches
// the renderer's reported track (or no track was reported). A qid that
// resolves to a DIFFERENT track means the cached renderer projection is
// STALE relative to THIS queue — e.g. a fresh album was just pushed (new
// track_context_uuid, autoplay_reset) while the projection still names
// the PREVIOUS queue's item. Trusting the stale qid lands the cursor on
// the wrong track (the "NowPlayingBar shows track 4 on a freshly-pushed
// album" bug, controlling a peer that was already rendering). Fall
// through to the track_id lookup; it won't find the old track in the new
// queue, so the caller defaults to the queue head.
let track_matches = renderer_track_id
.map(|track_id| queue_state.queue_items[index].track_id == track_id)
.unwrap_or(true);
if track_matches {
return Some(index);
}
}
}
@@ -391,6 +406,38 @@ pub fn resolve_remote_start_index(
None
}
/// Resolve the `shuffle_pivot_queue_item_id` for an outbound
/// `CtrlSrvrSetShuffleMode` command: the queue item the cloud keeps fixed while
/// it generates the shuffled order (so the currently-playing track stays at the
/// front). Prefers the renderer's reported `queue_item_id`; falls back to the
/// item carrying the renderer's `track_id` when the qid is a placeholder; `None`
/// when the renderer has no current track. Frontend-agnostic (ADR-006): used by
/// both the Tauri and Slint controller shuffle paths.
pub fn resolve_qconnect_shuffle_pivot(
queue: &QConnectQueueState,
renderer: &QConnectRendererState,
) -> Option<u64> {
let current_track = renderer.current_track.as_ref()?;
if queue
.queue_items
.iter()
.any(|item| item.queue_item_id == current_track.queue_item_id)
{
return Some(current_track.queue_item_id);
}
if let Some(item) = queue
.queue_items
.iter()
.find(|item| item.track_id == current_track.track_id)
{
return Some(item.queue_item_id);
}
None
}
pub fn resolve_core_shuffle_order(
queue_state: &QConnectQueueState,
renderer_queue_item_id: Option<u64>,
@@ -479,3 +526,90 @@ pub fn normalize_current_queue_item_id_from_queue_state(
queue.queue_items[current_index].queue_item_id
}
}
#[cfg(test)]
mod tests {
use super::*;
use qconnect_core::QueueItem;
fn item(queue_item_id: u64, track_id: u64) -> QueueItem {
QueueItem {
track_context_uuid: String::new(),
track_id,
queue_item_id,
}
}
fn queue(items: Vec<QueueItem>) -> QConnectQueueState {
QConnectQueueState {
queue_items: items,
..Default::default()
}
}
/// Regression (controller of a peer that was already rendering): a fresh album
/// is pushed, but the cached renderer projection still names the PREVIOUS
/// queue's item (qid=3 + the old track_id). The old track is absent from the
/// new queue, so the stale qid must NOT resolve the cursor to the new queue's
/// item 3 ("NowPlayingBar shows track 4 on a freshly-pushed album"). It falls
/// through to the track_id lookup (miss) → None → caller defaults to the head.
#[test]
fn stale_qid_with_mismatched_track_does_not_land_on_wrong_track() {
let q = queue(vec![
item(0, 52848233),
item(1, 52848234),
item(2, 52848235),
item(3, 52848236),
]);
assert_eq!(
resolve_remote_start_index(&q, Some(3), Some(126886856)),
None
);
}
/// A consistent projection (qid + the matching track at that position) still
/// resolves to its index — the normal track-change / takeback path is unchanged.
#[test]
fn consistent_qid_and_track_resolves_to_index() {
let q = queue(vec![item(0, 100), item(1, 200), item(2, 300)]);
assert_eq!(resolve_remote_start_index(&q, Some(2), Some(300)), Some(2));
}
/// When no track is reported, the qid is trusted as before (some events carry
/// only a queue_item_id).
#[test]
fn qid_without_track_id_is_trusted() {
let q = queue(vec![item(0, 100), item(1, 200)]);
assert_eq!(resolve_remote_start_index(&q, Some(1), None), Some(1));
}
/// An absent qid falls through to the track_id lookup.
#[test]
fn track_id_lookup_when_qid_absent_from_queue() {
let q = queue(vec![item(0, 100), item(7, 200)]);
assert_eq!(resolve_remote_start_index(&q, Some(99), Some(200)), Some(1));
}
/// Shuffle pivot prefers the renderer's reported queue_item_id.
#[test]
fn shuffle_pivot_from_renderer_queue_item_id() {
let q = queue(vec![item(10, 100), item(11, 101), item(12, 102)]);
let renderer = QConnectRendererState {
current_track: Some(item(11, 101)),
..Default::default()
};
assert_eq!(resolve_qconnect_shuffle_pivot(&q, &renderer), Some(11));
}
/// When the renderer's qid is a placeholder (0), fall back to the item that
/// carries the renderer's track_id.
#[test]
fn shuffle_pivot_by_track_id_when_qid_is_placeholder() {
let q = queue(vec![item(20, 200), item(21, 201), item(22, 202)]);
let renderer = QConnectRendererState {
current_track: Some(item(0, 202)),
..Default::default()
};
assert_eq!(resolve_qconnect_shuffle_pivot(&q, &renderer), Some(22));
}
}
+40 -1
View File
@@ -480,7 +480,12 @@ pub async fn apply_renderer_command(
}
RendererCommand::SetShuffleMode { shuffle_mode } => {
let enabled = renderer_state.shuffle_mode.unwrap_or(*shuffle_mode);
engine.set_shuffle(enabled).await;
// WS-authoritative: flip ONLY the flag here. Never generate a local
// shuffle order — the cloud owns queue order, which arrives separately
// via `sync_remote_shuffle_projection` / `materialize_remote_queue`.
// Calling the order-generating `set_shuffle` would produce a divergent
// local random order ("es un infierno" — the documented failure mode).
engine.set_shuffle_flag(enabled).await;
}
}
@@ -839,6 +844,7 @@ mod tests {
set_volumes: Vec<f32>,
set_repeat_modes: u32,
set_shuffles: Vec<bool>,
set_shuffle_flags: Vec<bool>,
set_queue_with_order: Vec<(bool, Option<Vec<usize>>)>,
set_queues: u32,
clear_queues: Vec<bool>,
@@ -907,6 +913,9 @@ mod tests {
async fn set_shuffle(&self, enabled: bool) {
self.calls().set_shuffles.push(enabled);
}
async fn set_shuffle_flag(&self, enabled: bool) {
self.calls().set_shuffle_flags.push(enabled);
}
async fn get_all_queue_tracks(&self) -> (Vec<QueueTrack>, Option<usize>) {
(self.queue_tracks.clone(), self.queue_index)
}
@@ -1079,6 +1088,36 @@ mod tests {
assert_eq!(engine.calls().seeks, vec![40]);
}
/// #4 — WS-authoritative shuffle: a standalone SetShuffleMode must flip the
/// flag ONLY (set_shuffle_flag), NEVER generate a local order. It must not
/// call the order-generating set_shuffle, and must not apply any queue order
/// (set_queue_with_order) — the cloud's order arrives separately.
#[tokio::test]
async fn apply_renderer_command_setshufflemode_is_flag_only() {
let mut engine = MockEngine::new();
engine.queue_tracks = vec![mock_queue_track(1), mock_queue_track(2), mock_queue_track(3)];
engine.queue_index = Some(0);
let sync = sync();
let cmd = RendererCommand::SetShuffleMode { shuffle_mode: true };
apply_renderer_command(&engine, &sync, &cmd, &QConnectRendererState::default())
.await
.unwrap();
let calls = engine.calls();
assert_eq!(
calls.set_shuffle_flags,
vec![true],
"SetShuffleMode must take the flag-only path"
);
assert!(
calls.set_shuffles.is_empty(),
"SetShuffleMode must NEVER call the order-generating set_shuffle (WS-authoritative rule)"
);
assert!(
calls.set_queue_with_order.is_empty(),
"SetShuffleMode must not apply any local order; the cloud's order arrives separately"
);
}
/// #3 — a state-only SetState (command.current_track = None) must NOT align the
/// cursor or load a track from the renderer_state's stale current_track (the
/// iOS-pause-jumped-back fix); the playing_state is still applied.
@@ -52,6 +52,14 @@ pub trait QconnectRendererEngine: Send + Sync {
// ---- queue / mode (async) ----
async fn set_repeat_mode(&self, mode: RepeatMode);
async fn set_shuffle(&self, enabled: bool);
/// Set the shuffle FLAG only, WITHOUT generating any local order. QConnect is
/// WS-authoritative for shuffle: the renderer must never invent a random
/// order; the cloud's authoritative order is applied separately via
/// `set_queue_with_order` (`sync_remote_shuffle_projection` /
/// `materialize_remote_queue`). Forwards to
/// `QbzCore::set_shuffle_with_order(enabled, None)` -> identity order on
/// enable (current order preserved, NO RNG), clear on disable.
async fn set_shuffle_flag(&self, enabled: bool);
async fn get_all_queue_tracks(&self) -> (Vec<QueueTrack>, Option<usize>);
async fn set_queue(&self, tracks: Vec<QueueTrack>, start_index: Option<usize>);
/// The deferred-shuffle core: a real shuffle order, never an invented