2026-11-19 16:58:29 +02:00
|
|
|
//! Version gating and lossless queue bridge (device-sync protocol v4).
|
|
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
use crate::player::queue_items::{self, test_episode};
|
|
|
|
|
use music_dht::podcast::{
|
|
|
|
|
EpisodeId, FeedUrl, ListenSession, PodcastId, PodcastMeta, ProgressStatus,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const CONTENT_ID: &str = "b3:2222222222222222222222222222222222222222222222222222222222222222";
|
|
|
|
|
|
|
|
|
|
fn podcast_id() -> PodcastId {
|
|
|
|
|
PodcastId::from_feed_url(&FeedUrl::parse("https://podcasts.example/feed.xml").unwrap())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn episode_id() -> EpisodeId {
|
|
|
|
|
EpisodeId::from_guid(&podcast_id(), "ep-1").unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn music_track(id: i64) -> PlaybackTrack {
|
|
|
|
|
let mut track = PlaybackTrack::private_placeholder();
|
|
|
|
|
track.source = None;
|
|
|
|
|
track.id = id;
|
|
|
|
|
track.title = format!("Song {id}");
|
|
|
|
|
track
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn state(queue: Vec<PlaybackTrack>) -> PlaybackStateWire {
|
|
|
|
|
PlaybackStateWire {
|
|
|
|
|
queue,
|
|
|
|
|
queue_pos: 0,
|
|
|
|
|
playing: true,
|
|
|
|
|
paused: false,
|
|
|
|
|
idle_since_ms: None,
|
|
|
|
|
position_secs: 12.5,
|
|
|
|
|
volume: 70,
|
|
|
|
|
shuffle: false,
|
|
|
|
|
repeat: PlaybackRepeat::Off,
|
|
|
|
|
playback_rate: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn podcast_state() -> PlaybackStateWire {
|
|
|
|
|
state(vec![
|
|
|
|
|
music_track(1),
|
|
|
|
|
PlaybackTrack::podcast_episode(test_episode()),
|
|
|
|
|
])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// One payload of every operation kind (music-only playback commands).
|
|
|
|
|
fn every_payload(playback: PlaybackStateWire) -> Vec<SyncOpPayload> {
|
|
|
|
|
let session = ListenSession {
|
|
|
|
|
session_id: "s1".into(),
|
|
|
|
|
episode_id: episode_id(),
|
|
|
|
|
podcast_id: podcast_id(),
|
|
|
|
|
device_id: "tui_a".into(),
|
|
|
|
|
started_at_ms: 1,
|
|
|
|
|
ended_at_ms: 2,
|
|
|
|
|
start_position_ms: 0,
|
|
|
|
|
end_position_ms: 1,
|
|
|
|
|
listened_ms: 1,
|
|
|
|
|
completed: false,
|
|
|
|
|
};
|
|
|
|
|
let meta: PodcastMeta = serde_json::from_value(serde_json::json!({
|
|
|
|
|
"feed_url": "https://podcasts.example/feed.xml", "title": "Cast"
|
|
|
|
|
}))
|
|
|
|
|
.unwrap();
|
|
|
|
|
vec![
|
|
|
|
|
SyncOpPayload::TrackLikeSet {
|
|
|
|
|
content_id: CONTENT_ID.into(),
|
|
|
|
|
liked: true,
|
|
|
|
|
fed: None,
|
|
|
|
|
},
|
|
|
|
|
SyncOpPayload::PlaylistCreated {
|
|
|
|
|
playlist_id: "p".into(),
|
|
|
|
|
title: "t".into(),
|
|
|
|
|
},
|
|
|
|
|
SyncOpPayload::PlaylistRenamed {
|
|
|
|
|
playlist_id: "p".into(),
|
|
|
|
|
title: "t".into(),
|
|
|
|
|
},
|
|
|
|
|
SyncOpPayload::PlaylistDeleted {
|
|
|
|
|
playlist_id: "p".into(),
|
|
|
|
|
},
|
|
|
|
|
SyncOpPayload::PlaylistTrackAdded {
|
|
|
|
|
playlist_id: "p".into(),
|
|
|
|
|
content_id: CONTENT_ID.into(),
|
|
|
|
|
position: 0,
|
|
|
|
|
fed: None,
|
|
|
|
|
},
|
|
|
|
|
SyncOpPayload::PlaylistTrackRemoved {
|
|
|
|
|
playlist_id: "p".into(),
|
|
|
|
|
content_id: CONTENT_ID.into(),
|
|
|
|
|
},
|
|
|
|
|
SyncOpPayload::DeviceProfileSet {
|
|
|
|
|
name: "n".into(),
|
|
|
|
|
client_version: "v".into(),
|
|
|
|
|
endpoint_ticket: "t".into(),
|
|
|
|
|
endpoint_id: "e".into(),
|
|
|
|
|
protocol_version: Some(4),
|
|
|
|
|
},
|
|
|
|
|
SyncOpPayload::DeviceTrusted {
|
|
|
|
|
target_device_id: "d".into(),
|
|
|
|
|
},
|
|
|
|
|
SyncOpPayload::DeviceRevoked {
|
|
|
|
|
target_device_id: "d".into(),
|
|
|
|
|
target_max_seq_seen: 1,
|
|
|
|
|
},
|
|
|
|
|
SyncOpPayload::PlaybackCommand {
|
|
|
|
|
target_device_id: "d".into(),
|
|
|
|
|
command: PlaybackCommand::SetState {
|
|
|
|
|
state: playback,
|
|
|
|
|
seek: false,
|
|
|
|
|
},
|
|
|
|
|
authority: None,
|
|
|
|
|
},
|
|
|
|
|
SyncOpPayload::ListenRecorded {
|
|
|
|
|
event: ListenEvent {
|
|
|
|
|
listen_id: "l".into(),
|
|
|
|
|
content_id: CONTENT_ID.into(),
|
|
|
|
|
started_at_ms: 1,
|
|
|
|
|
listened_ms: 1,
|
|
|
|
|
track_duration_ms: None,
|
|
|
|
|
ended_reason: ListenEndReason::Finished,
|
|
|
|
|
track: ListenTrackMetadata {
|
|
|
|
|
title: "t".into(),
|
|
|
|
|
artist_names: vec!["a".into()],
|
|
|
|
|
featured_artist_names: vec![],
|
|
|
|
|
release_title: None,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
SyncOpPayload::PodcastSubscriptionSet {
|
|
|
|
|
podcast_id: podcast_id(),
|
|
|
|
|
subscribed: true,
|
|
|
|
|
podcast: meta,
|
|
|
|
|
feed_aliases: vec![],
|
|
|
|
|
generation: 1,
|
|
|
|
|
},
|
|
|
|
|
SyncOpPayload::PodcastShareSet {
|
|
|
|
|
podcast_id: podcast_id(),
|
|
|
|
|
shared: false,
|
|
|
|
|
generation: 1,
|
|
|
|
|
},
|
|
|
|
|
SyncOpPayload::PodcastFeedAliasesAdded {
|
|
|
|
|
podcast_id: podcast_id(),
|
|
|
|
|
feed_aliases: vec![FeedUrl::parse("https://podcasts.example/new.xml").unwrap()],
|
|
|
|
|
},
|
|
|
|
|
SyncOpPayload::PodcastProgressSet {
|
|
|
|
|
episode_id: episode_id(),
|
|
|
|
|
podcast_id: podcast_id(),
|
|
|
|
|
position_ms: 1,
|
|
|
|
|
status: ProgressStatus::InProgress,
|
|
|
|
|
generation: 1,
|
|
|
|
|
session_id: "s1".into(),
|
|
|
|
|
episode: None,
|
|
|
|
|
},
|
|
|
|
|
SyncOpPayload::PodcastListenRecorded {
|
|
|
|
|
session,
|
|
|
|
|
episode: None,
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn stored_kind_is_the_wire_tag() {
|
|
|
|
|
for payload in every_payload(state(vec![])) {
|
|
|
|
|
let value = serde_json::to_value(&payload).unwrap();
|
|
|
|
|
assert_eq!(payload_kind(&payload), value["kind"].as_str().unwrap());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn op_gating_matrix_matches_the_shared_helper() {
|
|
|
|
|
// The SQL kind filter must hide exactly what `op_allowed_for_peer`
|
|
|
|
|
// rejects (playback commands are content-dependent and checked in Rust).
|
|
|
|
|
for version in 1..=PROTOCOL_VERSION {
|
|
|
|
|
let hidden = op_kinds_hidden_from(version);
|
|
|
|
|
for payload in every_payload(state(vec![music_track(1)])) {
|
|
|
|
|
let kind = payload_kind(&payload);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
hidden.contains(&kind),
|
|
|
|
|
!op_allowed_for_peer(&payload, version),
|
|
|
|
|
"{kind} for v{version}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
assert!(
|
|
|
|
|
op_kinds_hidden_from(3)
|
|
|
|
|
.iter()
|
|
|
|
|
.all(|kind| kind.starts_with("podcast_"))
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(op_kinds_hidden_from(3).len(), 5);
|
|
|
|
|
assert!(op_kinds_hidden_from(4).is_empty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn playback_gating_matrix() {
|
|
|
|
|
let music = state(vec![music_track(1), music_track(2)]);
|
|
|
|
|
let podcasts = podcast_state();
|
|
|
|
|
let private = state(vec![PlaybackTrack::private_placeholder()]);
|
|
|
|
|
for version in 1..=PROTOCOL_VERSION {
|
|
|
|
|
let v4 = version >= 4;
|
|
|
|
|
// Snapshots and handoffs.
|
|
|
|
|
assert!(playback_allowed_for_peer(&music, version));
|
|
|
|
|
assert_eq!(playback_allowed_for_peer(&podcasts, version), v4);
|
|
|
|
|
assert_eq!(playback_allowed_for_peer(&private, version), v4);
|
|
|
|
|
// Commands from the peer.
|
|
|
|
|
assert!(playback_command_allowed_from_peer(&music, version));
|
|
|
|
|
assert_eq!(playback_command_allowed_from_peer(&podcasts, version), v4);
|
|
|
|
|
// Stamped playback command operations.
|
|
|
|
|
let command = |state: &PlaybackStateWire| SyncOpPayload::PlaybackCommand {
|
|
|
|
|
target_device_id: "d".into(),
|
|
|
|
|
command: PlaybackCommand::ActiveChanged {
|
|
|
|
|
active_device_id: "d".into(),
|
|
|
|
|
active_device_name: "D".into(),
|
|
|
|
|
state: state.clone(),
|
|
|
|
|
},
|
|
|
|
|
authority: None,
|
|
|
|
|
};
|
|
|
|
|
assert!(op_allowed_for_peer(&command(&music), version));
|
|
|
|
|
assert_eq!(op_allowed_for_peer(&command(&podcasts), version), v4);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn browser_state_gating_sees_podcast_items() {
|
|
|
|
|
let with_podcast = serde_json::json!({
|
|
|
|
|
"tracks": [
|
|
|
|
|
{"id": 5, "title": "Library song"},
|
|
|
|
|
queue_items::external_track_json(&PlaybackTrack::podcast_episode(test_episode()), 1, None),
|
|
|
|
|
],
|
|
|
|
|
"index": 0,
|
|
|
|
|
});
|
|
|
|
|
let shadow = browser_state_shadow(&with_podcast);
|
|
|
|
|
assert!(!playback_command_allowed_from_peer(&shadow, 3));
|
|
|
|
|
assert!(playback_command_allowed_from_peer(&shadow, 4));
|
|
|
|
|
let music_only = serde_json::json!({"tracks": [{"id": 5}], "index": 0});
|
|
|
|
|
assert!(playback_command_allowed_from_peer(
|
|
|
|
|
&browser_state_shadow(&music_only),
|
|
|
|
|
3
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn relayed_versions_never_downgrade_and_self_reports_replace() {
|
|
|
|
|
let profile = |id: &str, version: u16| DeviceProfileWire {
|
|
|
|
|
device_id: id.into(),
|
|
|
|
|
name: "D".into(),
|
|
|
|
|
client_version: "x".into(),
|
|
|
|
|
protocol_version: version,
|
|
|
|
|
endpoint_id: String::new(),
|
|
|
|
|
endpoint_ticket: String::new(),
|
|
|
|
|
revoked: false,
|
|
|
|
|
revoke_cutoff_seq: None,
|
|
|
|
|
updated_at_ms: 0,
|
|
|
|
|
};
|
|
|
|
|
// A v3 relay lists every device with its own constant.
|
|
|
|
|
let relayed = VersionReport::for_listed(&profile("tui_b", 3), Some("tui_a"));
|
|
|
|
|
assert_eq!(relayed, VersionReport::Relayed(3));
|
|
|
|
|
assert_eq!(relayed.resolve(Some(4)), 4);
|
|
|
|
|
assert_eq!(relayed.resolve(None), 3);
|
|
|
|
|
assert_eq!(VersionReport::Relayed(4).resolve(Some(3)), 4);
|
|
|
|
|
// The sender's own entry is a self-report and may lower the version.
|
|
|
|
|
let own = VersionReport::for_listed(&profile("tui_a", 3), Some("tui_a"));
|
|
|
|
|
assert_eq!(own, VersionReport::SelfReported(Some(3)));
|
|
|
|
|
assert_eq!(own.resolve(Some(4)), 3);
|
|
|
|
|
// Profiles without a version (pending pairing, pre-v4 DeviceProfileSet)
|
|
|
|
|
// keep the stored value, or the unreported version 1.
|
|
|
|
|
assert_eq!(VersionReport::SelfReported(None).resolve(Some(4)), 4);
|
|
|
|
|
assert_eq!(VersionReport::SelfReported(None).resolve(None), 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn podcast_snapshot_sections_are_bounded_per_message() {
|
|
|
|
|
let mut snapshot = SyncSnapshot::default();
|
|
|
|
|
let progress = music_dht::podcast::ProgressRecord {
|
|
|
|
|
episode_id: episode_id(),
|
|
|
|
|
podcast_id: podcast_id(),
|
|
|
|
|
position_ms: 1,
|
|
|
|
|
status: ProgressStatus::InProgress,
|
|
|
|
|
generation: 1,
|
|
|
|
|
session_id: "s".into(),
|
|
|
|
|
hlc_ms: 1,
|
|
|
|
|
device_id: "tui_a".into(),
|
|
|
|
|
op_id: "tui_a:1".into(),
|
|
|
|
|
};
|
|
|
|
|
snapshot.podcast_progress = vec![progress; 10];
|
|
|
|
|
// Large other parts leave (almost) no budget for podcast sections.
|
|
|
|
|
let big_title = "x".repeat(music_dht::device_sync::MAX_SYNC_LINE - 16 * 1024);
|
|
|
|
|
let mut message = WireMessage::Hello {
|
|
|
|
|
group_id: "g".into(),
|
|
|
|
|
profile: DeviceProfileWire {
|
|
|
|
|
device_id: "web".into(),
|
|
|
|
|
name: big_title,
|
|
|
|
|
client_version: String::new(),
|
|
|
|
|
protocol_version: PROTOCOL_VERSION,
|
|
|
|
|
endpoint_id: String::new(),
|
|
|
|
|
endpoint_ticket: String::new(),
|
|
|
|
|
revoked: false,
|
|
|
|
|
revoke_cutoff_seq: None,
|
|
|
|
|
updated_at_ms: 0,
|
|
|
|
|
},
|
|
|
|
|
devices: vec![],
|
|
|
|
|
vector: BTreeMap::new(),
|
|
|
|
|
ops: vec![],
|
|
|
|
|
snapshot: snapshot.clone(),
|
|
|
|
|
playback: None,
|
|
|
|
|
};
|
|
|
|
|
fit_podcast_snapshot(&mut message);
|
|
|
|
|
let WireMessage::Hello {
|
|
|
|
|
snapshot: fitted, ..
|
|
|
|
|
} = &message
|
|
|
|
|
else {
|
|
|
|
|
unreachable!()
|
|
|
|
|
};
|
|
|
|
|
assert!(fitted.podcast_progress.is_empty());
|
|
|
|
|
|
|
|
|
|
let mut small = WireMessage::SyncResponse {
|
|
|
|
|
accepted: true,
|
|
|
|
|
error: None,
|
|
|
|
|
devices: vec![],
|
|
|
|
|
vector: BTreeMap::new(),
|
|
|
|
|
ops: vec![],
|
|
|
|
|
snapshot,
|
|
|
|
|
playback: None,
|
|
|
|
|
};
|
|
|
|
|
fit_podcast_snapshot(&mut small);
|
|
|
|
|
let WireMessage::SyncResponse { snapshot, .. } = &small else {
|
|
|
|
|
unreachable!()
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(snapshot.podcast_progress.len(), 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Pool that is never connected: the bridge paths under test resolve
|
|
|
|
|
/// podcast items, placeholders and inline content ids without queries.
|
|
|
|
|
fn offline_pool() -> sqlx::PgPool {
|
|
|
|
|
sqlx::postgres::PgPoolOptions::new()
|
|
|
|
|
.connect_lazy("postgres://offline@127.0.0.1:1/offline")
|
|
|
|
|
.unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn podcast_queue_survives_server_browser_server_round_trip() {
|
|
|
|
|
let pool = offline_pool();
|
|
|
|
|
let mut other = test_episode();
|
|
|
|
|
other.episode_title = "Episode 43".into();
|
|
|
|
|
other.episode_id = EpisodeId::from_guid(&other.podcast_id, "episode-43").unwrap();
|
|
|
|
|
let mut unresolved = music_track(0);
|
|
|
|
|
unresolved.title = "Remote song".into();
|
|
|
|
|
let queue = vec![
|
|
|
|
|
PlaybackTrack::podcast_episode(test_episode()),
|
|
|
|
|
PlaybackTrack::private_placeholder(),
|
|
|
|
|
unresolved,
|
|
|
|
|
PlaybackTrack::podcast_episode(other),
|
|
|
|
|
];
|
|
|
|
|
let mut incoming = state(queue.clone());
|
|
|
|
|
incoming.queue_pos = 3;
|
|
|
|
|
incoming.paused = true;
|
2026-11-19 18:35:49 +02:00
|
|
|
incoming.position_secs = 1200.0;
|
2026-11-19 16:58:29 +02:00
|
|
|
|
|
|
|
|
// peer -> server -> browser
|
|
|
|
|
let payload = web_playback_payload(&pool, &incoming).await.unwrap();
|
|
|
|
|
let tracks = payload["tracks"].as_array().unwrap();
|
|
|
|
|
assert_eq!(tracks.len(), 4, "no item is dropped");
|
|
|
|
|
assert_eq!(payload["index"], 3);
|
2026-11-19 18:35:49 +02:00
|
|
|
assert_eq!(payload["position_seconds"], 1200.0);
|
2026-11-19 16:58:29 +02:00
|
|
|
assert_eq!(tracks[0]["source"]["kind"], "podcast_episode");
|
2026-11-19 20:48:22 +02:00
|
|
|
assert_eq!(
|
|
|
|
|
tracks[0]["unavailable"], false,
|
|
|
|
|
"episodes play on the web player"
|
|
|
|
|
);
|
2026-11-19 16:58:29 +02:00
|
|
|
assert_eq!(tracks[1]["source"]["kind"], "private");
|
|
|
|
|
|
|
|
|
|
// The browser keeps its queue and sends it back (plus a library track
|
|
|
|
|
// it added at the end, with its content id inline).
|
|
|
|
|
let mut browser: serde_json::Value = serde_json::from_str(&payload.to_string()).unwrap();
|
|
|
|
|
browser["tracks"]
|
|
|
|
|
.as_array_mut()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.push(serde_json::json!({
|
|
|
|
|
"id": 77, "title": "Library song", "content_id": CONTENT_ID,
|
|
|
|
|
"duration_seconds": 100.0, "artists": [{"id": 1, "name": "A"}],
|
|
|
|
|
"release_id": 9, "release_title": "Album",
|
|
|
|
|
}));
|
|
|
|
|
|
2026-11-19 20:48:22 +02:00
|
|
|
// browser -> server -> peer (the bridge itself; the progress-register
|
|
|
|
|
// lookups of `playback_state_from_browser_json` need a database and
|
|
|
|
|
// are covered by `apply_progress_generations_*` and the Postgres tests)
|
|
|
|
|
let outgoing = browser_json_to_wire(&pool, browser).await.unwrap();
|
2026-11-19 16:58:29 +02:00
|
|
|
assert_eq!(outgoing.queue[..4], queue[..]);
|
|
|
|
|
assert_eq!(outgoing.queue_pos, 3);
|
|
|
|
|
assert!(outgoing.paused);
|
2026-11-19 18:35:49 +02:00
|
|
|
// The episode position goes back to the peer (handoff web -> TUI).
|
|
|
|
|
assert_eq!(outgoing.position_secs, 1200.0);
|
2026-11-19 16:58:29 +02:00
|
|
|
let library = &outgoing.queue[4];
|
|
|
|
|
assert_eq!(library.id, 77);
|
|
|
|
|
assert_eq!(library.content_id.as_deref(), Some(CONTENT_ID));
|
|
|
|
|
assert_eq!(library.source, None);
|
|
|
|
|
// Library music keeps the v3 shape.
|
|
|
|
|
assert!(!serde_json::to_string(library).unwrap().contains("source"));
|
|
|
|
|
// Peers below v4 get no playback snapshot with these items.
|
|
|
|
|
assert!(!playback_allowed_for_peer(&outgoing, 3));
|
|
|
|
|
assert!(playback_allowed_for_peer(&outgoing, 4));
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-19 20:48:22 +02:00
|
|
|
#[test]
|
|
|
|
|
fn apply_progress_generations_raises_stale_handoff_hints_only() {
|
|
|
|
|
let mut first = test_episode();
|
|
|
|
|
first.progress_generation = Some(9);
|
|
|
|
|
let mut second = test_episode();
|
|
|
|
|
second.episode_id = EpisodeId::from_guid(&second.podcast_id, "episode-43").unwrap();
|
|
|
|
|
second.progress_generation = None;
|
|
|
|
|
let mut third = test_episode();
|
|
|
|
|
third.episode_id = EpisodeId::from_guid(&third.podcast_id, "episode-44").unwrap();
|
|
|
|
|
third.progress_generation = Some(2);
|
|
|
|
|
let mut wire = state(vec![
|
|
|
|
|
PlaybackTrack::podcast_episode(first.clone()),
|
|
|
|
|
PlaybackTrack::podcast_episode(second.clone()),
|
|
|
|
|
music_track(3),
|
|
|
|
|
PlaybackTrack::podcast_episode(third.clone()),
|
|
|
|
|
]);
|
|
|
|
|
let generations = std::collections::HashMap::from([
|
|
|
|
|
(first.episode_id.clone(), 4),
|
|
|
|
|
(second.episode_id.clone(), 6),
|
|
|
|
|
(third.episode_id.clone(), 5),
|
|
|
|
|
]);
|
|
|
|
|
apply_progress_generations(&mut wire, &generations);
|
|
|
|
|
let hints: Vec<Option<u64>> = wire
|
|
|
|
|
.queue
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|track| {
|
|
|
|
|
track
|
|
|
|
|
.podcast_episode_ref()
|
|
|
|
|
.and_then(|e| e.progress_generation)
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
// A newer carried hint stays; missing and older ones are raised.
|
|
|
|
|
assert_eq!(hints, vec![Some(9), Some(6), None, Some(5)]);
|
|
|
|
|
assert_eq!(wire.queue[2], music_track(3), "music is untouched");
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-19 16:58:29 +02:00
|
|
|
#[tokio::test]
|
|
|
|
|
async fn malformed_browser_items_keep_the_position_on_its_item() {
|
|
|
|
|
let pool = offline_pool();
|
|
|
|
|
let browser = serde_json::json!({
|
|
|
|
|
"tracks": [
|
|
|
|
|
{"title": "no id"},
|
|
|
|
|
{"id": 5, "content_id": CONTENT_ID},
|
|
|
|
|
{"id": 6, "content_id": CONTENT_ID},
|
|
|
|
|
],
|
|
|
|
|
"index": 2,
|
|
|
|
|
"track": {"id": 6},
|
|
|
|
|
});
|
2026-11-19 19:21:25 +02:00
|
|
|
let wire = playback_state_from_browser_json(&pool, 1, browser)
|
2026-11-19 16:58:29 +02:00
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
assert_eq!(wire.queue.len(), 2);
|
|
|
|
|
assert_eq!(wire.queue[wire.queue_pos].id, 6);
|
|
|
|
|
}
|
2026-11-19 19:21:25 +02:00
|
|
|
|
|
|
|
|
fn progress_record(position_ms: i64, status: ProgressStatus, generation: u64) -> ProgressRecord {
|
|
|
|
|
let episode = test_episode();
|
|
|
|
|
ProgressRecord {
|
|
|
|
|
episode_id: episode.episode_id,
|
|
|
|
|
podcast_id: episode.podcast_id,
|
|
|
|
|
position_ms,
|
|
|
|
|
status,
|
|
|
|
|
generation,
|
|
|
|
|
session_id: "session-1".into(),
|
|
|
|
|
hlc_ms: 1_700_000_000_000,
|
|
|
|
|
device_id: "tui".into(),
|
|
|
|
|
op_id: "op-1".into(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Browser state of `[library song, episode]` with the song current at 200 s.
|
|
|
|
|
fn song_then_episode() -> serde_json::Value {
|
|
|
|
|
let episode = PlaybackTrack::podcast_episode(test_episode());
|
|
|
|
|
let song = serde_json::json!({
|
|
|
|
|
"id": 77, "title": "Library song", "content_id": CONTENT_ID,
|
|
|
|
|
"duration_seconds": 300.0, "stream_url": "/api/player/stream/77",
|
|
|
|
|
});
|
|
|
|
|
serde_json::json!({
|
|
|
|
|
"tracks": [song.clone(), queue_items::external_track_json(&episode, 1, None)],
|
|
|
|
|
"index": 0,
|
|
|
|
|
"track": song,
|
|
|
|
|
"position_seconds": 200.0,
|
|
|
|
|
"paused": false,
|
|
|
|
|
"repeat_mode": "off",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn position_unknown(state: &serde_json::Value) -> bool {
|
|
|
|
|
state[queue_items::POSITION_UNKNOWN_KEY] == true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn commands_that_select_an_item_do_not_know_its_position() {
|
|
|
|
|
let current = song_then_episode();
|
|
|
|
|
// Next and Prev onto another item, and play commands (which the browser
|
|
|
|
|
// always sends with position 0), leave the position to the server.
|
|
|
|
|
let next =
|
|
|
|
|
browser_state_for_command("next", serde_json::json!({}), Some(current.clone())).unwrap();
|
|
|
|
|
assert_eq!(next["index"], 1);
|
|
|
|
|
assert_eq!(next["position_seconds"], 0.0);
|
|
|
|
|
assert!(position_unknown(&next));
|
|
|
|
|
let mut play = current.clone();
|
|
|
|
|
play["index"] = serde_json::json!(1);
|
|
|
|
|
let played = browser_state_for_command("play_from_index", play.clone(), None).unwrap();
|
|
|
|
|
assert!(position_unknown(&played));
|
|
|
|
|
let prev =
|
|
|
|
|
browser_state_for_command("prev", serde_json::json!({}), Some(next.clone())).unwrap();
|
|
|
|
|
assert_eq!(prev["index"], 0);
|
|
|
|
|
assert!(position_unknown(&prev));
|
|
|
|
|
|
|
|
|
|
// Restarting the same item and seeking are explicit positions.
|
|
|
|
|
let mut late = next.clone();
|
|
|
|
|
late["position_seconds"] = serde_json::json!(900.0);
|
|
|
|
|
let restart = browser_state_for_command("prev", serde_json::json!({}), Some(late)).unwrap();
|
|
|
|
|
assert_eq!(restart["index"], 1);
|
|
|
|
|
assert!(!position_unknown(&restart));
|
|
|
|
|
let repeat = browser_state_for_command(
|
|
|
|
|
"next",
|
|
|
|
|
serde_json::json!({"repeat_mode": "one"}),
|
|
|
|
|
Some(next.clone()),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
assert!(!position_unknown(&repeat));
|
|
|
|
|
let seek =
|
|
|
|
|
browser_state_for_command("seek", serde_json::json!({"time": 60.0}), Some(next)).unwrap();
|
|
|
|
|
assert_eq!(seek["position_seconds"], 60.0);
|
|
|
|
|
assert!(!position_unknown(&seek));
|
|
|
|
|
|
|
|
|
|
// A transfer keeps what the sender knew.
|
|
|
|
|
let mut handed_off = play;
|
|
|
|
|
handed_off["position_seconds"] = serde_json::json!(1200.0);
|
|
|
|
|
let transfer = browser_state_for_command("transfer_state", handed_off.clone(), None).unwrap();
|
|
|
|
|
assert_eq!(transfer["position_seconds"], 1200.0);
|
|
|
|
|
assert!(!position_unknown(&transfer));
|
|
|
|
|
handed_off[queue_items::POSITION_UNKNOWN_KEY] = serde_json::json!(true);
|
|
|
|
|
let transfer = browser_state_for_command("transfer_state", handed_off, None).unwrap();
|
|
|
|
|
assert!(position_unknown(&transfer));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn unknown_episode_position_resumes_from_the_progress_register() {
|
|
|
|
|
let pool = offline_pool();
|
|
|
|
|
let next = browser_state_for_command("next", serde_json::json!({}), Some(song_then_episode()))
|
|
|
|
|
.unwrap();
|
|
|
|
|
let mut wire = browser_json_to_wire(&pool, next).await.unwrap();
|
|
|
|
|
assert_eq!(wire.queue_pos, 1);
|
|
|
|
|
assert_eq!(wire.position_secs, 0.0);
|
|
|
|
|
|
|
|
|
|
// In progress at 1200 s, generation 9 (the queue item has seen 7).
|
|
|
|
|
let mut resumed = wire.clone();
|
|
|
|
|
apply_resume_position(
|
|
|
|
|
&mut resumed,
|
|
|
|
|
Some(&progress_record(1_200_000, ProgressStatus::InProgress, 9)),
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(resumed.position_secs, 1200.0);
|
|
|
|
|
let episode = resumed.queue[1].podcast_episode_ref().unwrap();
|
|
|
|
|
assert_eq!(episode.progress_generation, Some(9));
|
|
|
|
|
// The receiver's explicit write then orders after the register.
|
|
|
|
|
assert_eq!(
|
|
|
|
|
ProgressRecord::explicit_generation(episode.progress_generation),
|
|
|
|
|
10
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Completed: ordinary Play starts from the beginning; an older register
|
|
|
|
|
// does not lower the generation the item has seen.
|
|
|
|
|
let mut completed = wire.clone();
|
|
|
|
|
apply_resume_position(
|
|
|
|
|
&mut completed,
|
|
|
|
|
Some(&progress_record(1_790_000, ProgressStatus::Completed, 3)),
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(completed.position_secs, 0.0);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
completed.queue[1]
|
|
|
|
|
.podcast_episode_ref()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.progress_generation,
|
|
|
|
|
Some(7)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Music keeps the position it has.
|
|
|
|
|
wire.queue_pos = 0;
|
|
|
|
|
wire.position_secs = 5.0;
|
|
|
|
|
let before = wire.clone();
|
|
|
|
|
apply_resume_position(
|
|
|
|
|
&mut wire,
|
|
|
|
|
Some(&progress_record(1_200_000, ProgressStatus::InProgress, 9)),
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(wire, before);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
2026-11-19 20:48:22 +02:00
|
|
|
async fn known_positions_and_music_never_take_the_register_position() {
|
|
|
|
|
// The bridge itself runs without queries (offline pool); which state
|
|
|
|
|
// reads the episode's progress register is decided on the result.
|
2026-11-19 19:21:25 +02:00
|
|
|
let pool = offline_pool();
|
|
|
|
|
let mut handed_off = song_then_episode();
|
|
|
|
|
handed_off["index"] = serde_json::json!(1);
|
|
|
|
|
handed_off["position_seconds"] = serde_json::json!(1200.0);
|
2026-11-19 20:48:22 +02:00
|
|
|
let wire = browser_json_to_wire(&pool, handed_off).await.unwrap();
|
2026-11-19 19:21:25 +02:00
|
|
|
assert_eq!(wire.position_secs, 1200.0);
|
2026-11-19 20:48:22 +02:00
|
|
|
assert_eq!(episode_needing_resume_position(false, &wire), None);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
episode_needing_resume_position(true, &wire),
|
|
|
|
|
Some(test_episode().episode_id),
|
|
|
|
|
"an episode the browser did not know the position of"
|
|
|
|
|
);
|
2026-11-19 19:21:25 +02:00
|
|
|
|
2026-11-19 20:48:22 +02:00
|
|
|
let music = song_then_episode();
|
|
|
|
|
let wire = browser_json_to_wire(&pool, music).await.unwrap();
|
2026-11-19 19:21:25 +02:00
|
|
|
assert_eq!(wire.position_secs, 200.0);
|
2026-11-19 20:48:22 +02:00
|
|
|
assert_eq!(
|
|
|
|
|
episode_needing_resume_position(true, &wire),
|
|
|
|
|
None,
|
|
|
|
|
"music keeps its position even when flagged"
|
|
|
|
|
);
|
2026-11-19 19:21:25 +02:00
|
|
|
}
|