fix(downloads): queue the whole album, and make every queued track findable offline

An album download put a handful of its tracks on the device while the button
reported the album as downloaded. Two independent gaps, one shared cause.

- `download_album` read its track list from `items WHERE album_id = ?` — the
  local catalog cache. Jellyfin does not return `AlbumId` on every listing
  endpoint, so tracks cached from one of those sit in `items` with a NULL
  `album_id` and are invisible to that query. On the reported database three
  whole albums (18, 12 and 9 tracks) had it NULL on every track; a partially
  linked album queued only the linked subset.
- The frontend then resolved one stream URL per track from its own list and
  paired it with the returned row ids by position. The ids came back in the
  backend's `index_number` order over a different set of rows, so a row could
  be handed another track's URL and any track past the end of the shorter list
  was never started. On Android that loop also stopped wherever the webview was
  suspended.
- `album_id` is what `OfflineRepository::get_items` joins a track to its album
  on, so a track that did download stayed invisible under its album offline —
  the same missing link seen from the other side.

The operation now belongs to Rust end to end:

- `HybridRepository::get_album_tracks` asks the server what the album contains.
  Cache-first `get_items` is right for browsing and wrong for deciding what to
  download; it errors offline so the caller falls back to the ungated local
  catalog, keeping the queue-while-offline flow.
- `queue_album_tracks` writes the album link onto every track it queues, and
  creates an `items` row for tracks the cache has never seen.
- Stream URLs resolve here, through the existing reconnect resolver, now scoped
  to the rows just queued so one album cannot start every unrelated pending row.
  Only the album id crosses the IPC boundary.
- `album_file_names` gives each track its own file. A title repeated inside one
  album (deluxe edition, two discs) mapped to one path, so those downloads
  overwrote each other.

Re-tapping download on a broken album heals it: missing tracks are queued and
the tracks already on disk get their link.

`download_series`/`download_season` still derive their episode lists from the
cache the same way and want the same treatment.

DR-173, UT-170..172. Rust 673 tests, frontend 975 tests, svelte-check and
check:boundary clean.

Note: this tree is shared with a concurrent session. Only the files above are
committed; docs/traceability.md is left to be regenerated once that work lands.
This commit is contained in:
2026-08-16 09:20:32 +02:00
parent 82b6982d68
commit 1a9805f0f3
9 changed files with 790 additions and 84 deletions
+111 -18
View File
@@ -520,15 +520,27 @@ pub(crate) async fn requeue_mistyped_video_downloads(
/// `pending`/`stream_url IS NULL` row, resolve each via `resolve` (returning
/// `None` leaves the row pending), and heal the row so the pump can start it.
/// The `resolve` closure receives `(item_id, media_type, quality_preset)`.
///
/// `only_ids` restricts the sweep to specific download rows. Reconnect passes
/// `None` and heals everything; a bulk enqueue (an album, say) passes the rows
/// it just created, so clicking download on one album cannot also start every
/// unrelated row that has been sitting pending.
pub(crate) async fn resolve_pending_download_urls<F, Fut>(
db_service: &Arc<crate::storage::db_service::RusqliteService>,
target_dir: &str,
only_ids: Option<&[i64]>,
resolve: F,
) -> Result<ResumeQueuedResult, String>
where
F: Fn(String, String, String) -> Fut,
Fut: std::future::Future<Output = Option<String>>,
{
if only_ids.is_some_and(|ids| ids.is_empty()) {
return Ok(ResumeQueuedResult {
resolved: 0,
failed: 0,
});
}
// A row's own media_type wins; otherwise the *item's* type decides. Rows
// queued from a media card never carry one (`download_item` does not record
// it), and defaulting that NULL to 'audio' resolved movies against
@@ -541,6 +553,16 @@ where
.map(|t| format!("'{t}'"))
.collect::<Vec<_>>()
.join(", ");
let id_filter = match only_ids {
Some(ids) => format!(
" AND d.id IN ({})",
ids.iter()
.map(|id| id.to_string())
.collect::<Vec<_>>()
.join(", ")
),
None => String::new(),
};
let rows_query = Query::new(&format!(
"SELECT d.id, d.item_id,
COALESCE(
@@ -552,7 +574,7 @@ where
COALESCE(d.quality_preset, 'original')
FROM downloads d
LEFT JOIN items i ON i.id = d.item_id
WHERE d.status = 'pending' AND d.stream_url IS NULL"
WHERE d.status = 'pending' AND d.stream_url IS NULL{id_filter}"
));
let rows: Vec<(i64, String, String, String)> = db_service
.query_many(rows_query, |row| {
@@ -676,6 +698,7 @@ pub async fn resume_queued_downloads(
let outcome = resolve_pending_download_urls(
&db_service,
&target_dir,
None,
move |item_id: String, media_type: String, quality: String| {
let repo = Arc::clone(&repo_for_resolve);
async move {
@@ -861,12 +884,14 @@ mod tests {
// A completed row: irrelevant.
insert_download(&db, "done", "completed", Some("http://done/url"), None).await;
let out =
resolve_pending_download_urls(&db, "/data/downloads", |item_id, _mt, _q| async move {
Some(format!("http://resolved/{item_id}"))
})
.await
.unwrap();
let out = resolve_pending_download_urls(
&db,
"/data/downloads",
None,
|item_id, _mt, _q| async move { Some(format!("http://resolved/{item_id}")) },
)
.await
.unwrap();
assert_eq!(out.resolved, 1);
assert_eq!(out.failed, 0);
@@ -882,15 +907,79 @@ mod tests {
assert_eq!(url2.as_deref(), Some("http://existing/url"));
}
/// A bulk enqueue resolves only the rows it just created. Downloading one
/// album must not also start every unrelated row that has been sitting
/// pending with no URL (the smart cache leaves plenty of those).
///
/// TRACES: UR-018, UR-055 | DR-173 | UT-171
#[tokio::test]
async fn only_ids_restricts_the_sweep_to_the_given_rows() {
let db = test_db();
insert_download(&db, "mine", "pending", None, Some("audio")).await;
insert_download(&db, "someone-elses", "pending", None, Some("audio")).await;
let mine: i64 = db
.query_one(
Query::new("SELECT id FROM downloads WHERE item_id = 'mine'"),
|row| row.get(0),
)
.await
.unwrap();
let out = resolve_pending_download_urls(
&db,
"/data",
Some(&[mine]),
|item_id, _mt, _q| async move { Some(format!("http://resolved/{item_id}")) },
)
.await
.unwrap();
assert_eq!(out.resolved, 1);
assert_eq!(out.failed, 0);
let (_s, url, _t) = get_row(&db, "mine").await;
assert_eq!(url.as_deref(), Some("http://resolved/mine"));
let (status, other_url, _t) = get_row(&db, "someone-elses").await;
assert_eq!(status, "pending");
assert_eq!(
other_url, None,
"a scoped resolve must leave unrelated pending rows alone"
);
}
/// An empty id list resolves nothing — it must not fall through to "sweep
/// everything", which is what an unguarded `IN ()` would amount to.
///
/// TRACES: UR-018, UR-055 | DR-173 | UT-171
#[tokio::test]
async fn an_empty_id_list_resolves_nothing() {
let db = test_db();
insert_download(&db, "untouched", "pending", None, Some("audio")).await;
let out =
resolve_pending_download_urls(&db, "/data", Some(&[]), |item_id, _mt, _q| async move {
Some(format!("http://resolved/{item_id}"))
})
.await
.unwrap();
assert_eq!(out.resolved, 0);
let (_s, url, _t) = get_row(&db, "untouched").await;
assert_eq!(url, None);
}
#[tokio::test]
async fn counts_unresolvable_rows_as_failed_and_leaves_them_pending() {
let db = test_db();
insert_download(&db, "bad", "pending", None, None).await;
// Resolver returns None (e.g. server lookup failed).
let out = resolve_pending_download_urls(&db, "/data", |_id, _mt, _q| async move { None })
.await
.unwrap();
let out =
resolve_pending_download_urls(&db, "/data", None, |_id, _mt, _q| async move { None })
.await
.unwrap();
assert_eq!(out.resolved, 0);
assert_eq!(out.failed, 1);
@@ -920,7 +1009,7 @@ mod tests {
let seen = Arc::new(Mutex::new(Vec::new()));
let seen_c = Arc::clone(&seen);
resolve_pending_download_urls(&db, "/data", move |item_id, media_type, _q| {
resolve_pending_download_urls(&db, "/data", None, move |item_id, media_type, _q| {
let seen = Arc::clone(&seen_c);
async move {
seen.lock().unwrap().push((item_id.clone(), media_type));
@@ -953,7 +1042,7 @@ mod tests {
let seen = Arc::new(Mutex::new(String::new()));
let seen_c = Arc::clone(&seen);
resolve_pending_download_urls(&db, "/data", move |_id, media_type, _q| {
resolve_pending_download_urls(&db, "/data", None, move |_id, media_type, _q| {
let seen = Arc::clone(&seen_c);
async move {
*seen.lock().unwrap() = media_type;
@@ -977,7 +1066,7 @@ mod tests {
let seen = Arc::new(Mutex::new(String::new()));
let seen_c = Arc::clone(&seen);
resolve_pending_download_urls(&db, "/data", move |_id, media_type, _q| {
resolve_pending_download_urls(&db, "/data", None, move |_id, media_type, _q| {
let seen = Arc::clone(&seen_c);
async move {
*seen.lock().unwrap() = media_type;
@@ -1037,13 +1126,17 @@ mod tests {
let db = test_db();
insert_download(&db, "vid-1", "pending", None, Some("video")).await;
let out =
resolve_pending_download_urls(&db, "/data", |item_id, media_type, _q| async move {
let out = resolve_pending_download_urls(
&db,
"/data",
None,
|item_id, media_type, _q| async move {
assert_eq!(media_type, "video");
Some(format!("http://transcode/{item_id}"))
})
.await
.unwrap();
},
)
.await
.unwrap();
assert_eq!(out.resolved, 1);
let (_s, url, _t) = get_row(&db, "vid-1").await;
+599 -34
View File
@@ -350,57 +350,209 @@ pub async fn download_item(
Ok(download_id)
}
/// Queue an entire album for download
#[tauri::command]
#[specta::specta]
pub async fn download_album(
db: State<'_, DatabaseWrapper>,
album_id: String,
user_id: String,
base_path: String,
) -> Result<Vec<i64>, String> {
let db_service = {
let database = db.0.lock().map_err(|e| e.to_string())?;
Arc::new(database.service())
};
/// One track of an album, as the album-download path queues it.
///
/// `artist_name` carries whatever the catalog holds for the track's artists (a
/// JSON array, as stored on `items.artists`); it is display metadata for the
/// downloads list, not a lookup key.
///
/// TRACES: UR-018, UR-055 | DR-173
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct AlbumTrack {
pub id: String,
pub name: String,
pub artist_name: Option<String>,
pub album_name: Option<String>,
pub index_number: Option<i32>,
}
// Get all tracks in the album with metadata
impl From<&crate::repository::types::MediaItem> for AlbumTrack {
fn from(item: &crate::repository::types::MediaItem) -> Self {
Self {
id: item.id.clone(),
name: item.name.clone(),
artist_name: item
.artists
.as_ref()
.and_then(|a| serde_json::to_string(a).ok()),
album_name: item.album_name.clone(),
index_number: item.index_number,
}
}
}
/// The album's tracks as the local catalog cache knows them.
///
/// Only a fallback for [`download_album`]: the cache links a track to its album
/// through `items.album_id`, which Jellyfin does not populate on every listing
/// endpoint, so this can legitimately return fewer tracks than the album has.
///
/// TRACES: UR-018, UR-055 | DR-173
pub(crate) async fn cached_album_tracks(
db_service: &Arc<crate::storage::db_service::RusqliteService>,
album_id: &str,
) -> Result<Vec<AlbumTrack>, String> {
let tracks_query = Query::with_params(
"SELECT id, name, artists, album_name FROM items
WHERE album_id = ? AND item_type = 'Audio'
"SELECT id, name, artists, album_name, index_number FROM items
WHERE (album_id = ? OR parent_id = ?) AND item_type = 'Audio'
ORDER BY index_number",
vec![QueryParam::String(album_id)],
vec![
QueryParam::String(album_id.to_string()),
QueryParam::String(album_id.to_string()),
],
);
let tracks: Vec<(String, String, Option<String>, Option<String>)> = db_service
db_service
.query_many(tracks_query, |row| {
Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?))
Ok(AlbumTrack {
id: row.get(0)?,
name: row.get(1)?,
artist_name: row.get(2)?,
album_name: row.get(3)?,
index_number: row.get(4)?,
})
})
.await
.map_err(|e| e.to_string())?;
.map_err(|e| e.to_string())
}
let mut download_ids = Vec::new();
/// Queue one download row per track and link every track to its album.
///
/// The linkage is the half that is easy to miss: offline browsing joins a track
/// to its album on `items.album_id` (see `OfflineRepository::get_items`), so a
/// track whose cached row lacks it stays invisible under the album even after
/// its file is on disk. Queuing a track *is* the statement that it belongs to
/// this album, so the link is written here rather than hoped for from whichever
/// listing endpoint happened to cache the row.
///
/// Idempotent: re-queuing an album fills in what is missing and returns the same
/// row ids, in the order the tracks were given.
///
/// A file name per track, unique within the album.
///
/// A title is not a unique name inside its own album: a deluxe edition carries
/// the album version and a demo of the same song, and a two-disc set repeats
/// titles across discs. Naming files after the title alone gave those tracks one
/// path, and each download overwrote the previous one — an album that quietly
/// ends up short by however many titles it repeats. The track number
/// disambiguates the ordinary case; anything still colliding falls back to the
/// item id, which is unique by construction.
///
/// TRACES: UR-018, UR-055 | DR-173 | UT-172
pub(crate) fn album_file_names(tracks: &[AlbumTrack]) -> Vec<String> {
let mut counts: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
for track in tracks {
*counts.entry(track.name.to_lowercase()).or_default() += 1;
}
// Queue each track with album priority (100) and metadata
for (track_id, track_name, artist_name, album_name) in tracks {
let file_path = format!("{}/{}.mp3", base_path, sanitize_filename(&track_name));
tracks
.iter()
.map(|track| {
let title = sanitize_filename(&track.name);
if counts.get(&track.name.to_lowercase()).copied().unwrap_or(0) <= 1 {
return format!("{}.mp3", title);
}
match track.index_number {
Some(n) => format!("{:02} - {} [{}].mp3", n, title, track.id),
None => format!("{} [{}].mp3", title, track.id),
}
})
.collect()
}
/// TRACES: UR-018, UR-055 | DR-173 | UT-170
pub(crate) async fn queue_album_tracks(
db_service: &Arc<crate::storage::db_service::RusqliteService>,
album_id: &str,
tracks: &[AlbumTrack],
user_id: &str,
base_path: &str,
) -> Result<Vec<i64>, String> {
let mut download_ids = Vec::with_capacity(tracks.len());
let file_names = album_file_names(tracks);
for (track, file_name) in tracks.iter().zip(file_names) {
// Cache a row for a track the catalog has never seen, borrowing the
// album's server. Nothing is inserted when the album itself is unknown,
// which also keeps the parent_id foreign key satisfiable.
let cache_query = Query::with_params(
"INSERT OR IGNORE INTO items
(id, server_id, parent_id, name, item_type, album_id, album_name, artists, index_number)
SELECT ?, a.server_id, a.id, ?, 'Audio', a.id, ?, ?, ?
FROM items a WHERE a.id = ?",
vec![
QueryParam::String(track.id.clone()),
QueryParam::String(track.name.clone()),
track
.album_name
.clone()
.map(QueryParam::String)
.unwrap_or(QueryParam::Null),
track
.artist_name
.clone()
.map(QueryParam::String)
.unwrap_or(QueryParam::Null),
track
.index_number
.map(QueryParam::Int)
.unwrap_or(QueryParam::Null),
QueryParam::String(album_id.to_string()),
],
);
db_service
.execute(cache_query)
.await
.map_err(|e| e.to_string())?;
// Link an already-cached track to the album. The parent_id subquery
// resolves to NULL when the album is not cached, so the foreign key
// holds either way.
let link_query = Query::with_params(
"UPDATE items
SET album_id = ?,
parent_id = COALESCE(parent_id, (SELECT id FROM items WHERE id = ?))
WHERE id = ?",
vec![
QueryParam::String(album_id.to_string()),
QueryParam::String(album_id.to_string()),
QueryParam::String(track.id.clone()),
],
);
db_service
.execute(link_query)
.await
.map_err(|e| e.to_string())?;
let file_path = format!("{}/{}", base_path, file_name);
// Queue at album priority (100). A track already downloaded stays
// completed — re-queuing an album must fill the gaps, not re-fetch it.
let insert_query = Query::with_params(
"INSERT INTO downloads (item_id, user_id, file_path, status, priority, queued_at, item_name, artist_name, album_name)
VALUES (?, ?, ?, 'pending', 100, CURRENT_TIMESTAMP, ?, ?, ?)
"INSERT INTO downloads (item_id, user_id, file_path, status, priority, queued_at, item_name, artist_name, album_name, media_type)
VALUES (?, ?, ?, 'pending', 100, CURRENT_TIMESTAMP, ?, ?, ?, 'audio')
ON CONFLICT(item_id, user_id) DO UPDATE SET
priority = 100,
status = 'pending',
status = CASE WHEN downloads.status = 'completed' THEN 'completed' ELSE 'pending' END,
media_type = 'audio',
item_name = COALESCE(excluded.item_name, downloads.item_name),
artist_name = COALESCE(excluded.artist_name, downloads.artist_name),
album_name = COALESCE(excluded.album_name, downloads.album_name)",
vec![
QueryParam::String(track_id.clone()),
QueryParam::String(user_id.clone()),
QueryParam::String(track.id.clone()),
QueryParam::String(user_id.to_string()),
QueryParam::String(file_path),
QueryParam::String(track_name),
artist_name.map(QueryParam::String).unwrap_or(QueryParam::Null),
album_name.map(QueryParam::String).unwrap_or(QueryParam::Null),
QueryParam::String(track.name.clone()),
track
.artist_name
.clone()
.map(QueryParam::String)
.unwrap_or(QueryParam::Null),
track
.album_name
.clone()
.map(QueryParam::String)
.unwrap_or(QueryParam::Null),
],
);
@@ -413,8 +565,8 @@ pub async fn download_album(
let id_query = Query::with_params(
"SELECT id FROM downloads WHERE item_id = ? AND user_id = ?",
vec![
QueryParam::String(track_id),
QueryParam::String(user_id.clone()),
QueryParam::String(track.id.clone()),
QueryParam::String(user_id.to_string()),
],
);
@@ -428,6 +580,129 @@ pub async fn download_album(
Ok(download_ids)
}
/// Queue an entire album for download.
///
/// Owns the whole operation: the album's track list comes from the server (the
/// only place that knows all of it), every track is queued and linked to its
/// album, each row's stream URL is resolved here, and the queue is pumped.
///
/// The frontend used to do the second half — resolve one URL per track and pair
/// it with the returned ids **by position**. That pairing had no basis: the ids
/// came back in the backend's own order over a different set of rows, so
/// whenever the two lists disagreed a row was handed another track's URL, and
/// any track past the end of the shorter list was never started at all. Nothing
/// crosses the boundary now except the album id.
///
/// TRACES: UR-018, UR-055 | DR-173 | UT-170
#[tauri::command]
#[specta::specta]
pub async fn download_album(
db: State<'_, DatabaseWrapper>,
repository: State<'_, crate::commands::repository::RepositoryManagerWrapper>,
download_manager: State<'_, DownloadManagerWrapper>,
app: tauri::AppHandle,
handle: String,
album_id: String,
user_id: String,
base_path: String,
) -> Result<Vec<i64>, String> {
let db_service = {
let database = db.0.lock().map_err(|e| e.to_string())?;
Arc::new(database.service())
};
let repo = repository.0.get(&handle);
// Ask the server what the album contains; the cache is only a fallback for
// when it cannot answer.
let tracks: Vec<AlbumTrack> = match &repo {
Some(repo) => match repo.get_album_tracks(&album_id).await {
Ok(items) if !items.is_empty() => items.iter().map(AlbumTrack::from).collect(),
Ok(_) => cached_album_tracks(&db_service, &album_id).await?,
Err(e) => {
warn!(
"[download_album] Could not list album {} from the repository ({:?}); \
falling back to the cached track list",
album_id, e
);
cached_album_tracks(&db_service, &album_id).await?
}
},
None => cached_album_tracks(&db_service, &album_id).await?,
};
if tracks.is_empty() {
warn!("[download_album] No tracks found for album {}", album_id);
return Ok(Vec::new());
}
let download_ids =
queue_album_tracks(&db_service, &album_id, &tracks, &user_id, &base_path).await?;
info!(
"[download_album] Queued {} track(s) for album {}",
download_ids.len(),
album_id
);
// Resolve each queued row's stream URL here, then pump. Without a
// repository (or while offline) the rows stay pending with no URL and
// `resume_queued_downloads` picks them up on reconnect.
let Some(repo) = repo else {
return Ok(download_ids);
};
let target_dir = {
let database = db.0.lock().map_err(|e| e.to_string())?;
database
.path()
.parent()
.ok_or_else(|| "Database path has no parent directory".to_string())?
.to_string_lossy()
.to_string()
};
let repo_for_resolve = Arc::clone(&repo);
let outcome = crate::commands::catalog::resolve_pending_download_urls(
&db_service,
&target_dir,
Some(&download_ids),
move |item_id: String, _media_type: String, _quality: String| {
let repo = Arc::clone(&repo_for_resolve);
async move {
use crate::repository::MediaRepository;
match repo.get_audio_stream_url(&item_id).await {
Ok(url) => Some(url),
Err(e) => {
warn!(
"[download_album] Failed to resolve stream URL for {}: {:?}",
item_id, e
);
None
}
}
}
},
)
.await?;
if outcome.failed > 0 {
warn!(
"[download_album] {} track(s) could not be resolved and stay queued for the next \
reconnect",
outcome.failed
);
}
let active_downloads = {
let manager = download_manager.0.lock().map_err(|e| e.to_string())?;
manager.get_active_downloads()
};
pump_download_queue(app, db_service, active_downloads).await;
Ok(download_ids)
}
/// Queue a video item (movie or episode) for download with quality preset
#[tauri::command]
#[specta::specta]
@@ -2658,4 +2933,294 @@ mod tests {
download_source: "user".to_string(),
}
}
// ===== Album download: track sourcing and album linkage =====
/// A database with just the tables the album-download path touches.
fn album_test_db() -> Arc<crate::storage::db_service::RusqliteService> {
let conn = rusqlite::Connection::open_in_memory().unwrap();
conn.execute_batch(
r#"
CREATE TABLE items (
id TEXT PRIMARY KEY,
server_id TEXT NOT NULL,
parent_id TEXT,
name TEXT NOT NULL,
item_type TEXT NOT NULL,
album_id TEXT,
album_name TEXT,
album_artist TEXT,
artists TEXT,
index_number INTEGER
);
CREATE TABLE downloads (
id INTEGER PRIMARY KEY AUTOINCREMENT,
item_id TEXT NOT NULL,
user_id TEXT NOT NULL,
file_path TEXT NOT NULL,
status TEXT DEFAULT 'pending',
priority INTEGER DEFAULT 0,
progress REAL DEFAULT 0,
queued_at TEXT,
item_name TEXT,
artist_name TEXT,
album_name TEXT,
media_type TEXT,
stream_url TEXT,
target_dir TEXT,
UNIQUE(item_id, user_id)
);
INSERT INTO items (id, server_id, name, item_type)
VALUES ('album1', 'server1', 'The Golden Age', 'MusicAlbum');
"#,
)
.unwrap();
Arc::new(crate::storage::db_service::RusqliteService::new(Arc::new(
Mutex::new(conn),
)))
}
fn album_track(id: &str, name: &str, index: i32) -> AlbumTrack {
AlbumTrack {
id: id.to_string(),
name: name.to_string(),
artist_name: Some("Woodkid".to_string()),
album_name: Some("The Golden Age".to_string()),
index_number: Some(index),
}
}
/// The album-download regression: every track the album actually has must be
/// queued, and each queued track must be linked to its album.
///
/// `download_album` used to take its track list from
/// `items WHERE album_id = ?`. Jellyfin does not return `AlbumId` on every
/// listing endpoint, so tracks cached from those endpoints sit in `items`
/// with a NULL `album_id` — invisible to that query. "Download album" then
/// silently queued only the subset that happened to carry the link, which is
/// the reported "only 4-5 songs downloaded". The same column is what offline
/// browsing joins tracks to their album on (`i.album_id = ?` in
/// `OfflineRepository::get_items`), so even a track that did download stayed
/// invisible under its album offline.
///
/// TRACES: UR-018, UR-055 | DR-173 | UT-170
#[tokio::test]
async fn test_queue_album_tracks_queues_every_track_and_links_it_to_the_album() {
let db = album_test_db();
// The cache holds all three tracks, but only one carries `album_id` —
// exactly the state the bug report's database is in.
for sql in [
"INSERT INTO items (id, server_id, name, item_type, album_id) \
VALUES ('t1', 'server1', 'Run Boy Run', 'Audio', 'album1')",
"INSERT INTO items (id, server_id, name, item_type, album_id) \
VALUES ('t2', 'server1', 'The Great Escape', 'Audio', NULL)",
"INSERT INTO items (id, server_id, name, item_type, album_id) \
VALUES ('t3', 'server1', 'Boat Song', 'Audio', NULL)",
] {
db.execute(Query::new(sql)).await.unwrap();
}
let tracks = vec![
album_track("t1", "Run Boy Run", 1),
album_track("t2", "The Great Escape", 2),
album_track("t3", "Boat Song", 3),
];
let ids = queue_album_tracks(&db, "album1", &tracks, "user1", "albums/album1")
.await
.unwrap();
assert_eq!(
ids.len(),
3,
"every track of the album must get a download row"
);
let queued: i64 = db
.query_one(
Query::new("SELECT COUNT(*) FROM downloads WHERE status = 'pending'"),
|row| row.get(0),
)
.await
.unwrap();
assert_eq!(queued, 3);
// Each track is now linked to its album, so the offline album page can
// find it once the download completes.
let linked: i64 = db
.query_one(
Query::new("SELECT COUNT(*) FROM items WHERE album_id = 'album1'"),
|row| row.get(0),
)
.await
.unwrap();
assert_eq!(
linked, 3,
"queued tracks must be linked to their album; offline browsing joins on album_id"
);
}
/// The returned ids must line up with the tracks that were passed in. The
/// frontend used to pair `downloadIds[i]` with its own `tracks[i]`, which is
/// only sound if both lists agree — they did not, because the backend
/// ordered by `index_number` over a different set of rows. Resolving URLs in
/// Rust removes the pairing entirely, but the order is still the contract
/// for anything that reads the ids back.
///
/// TRACES: UR-018, UR-055 | DR-173 | UT-170
#[tokio::test]
async fn test_queue_album_tracks_returns_ids_in_track_order() {
let db = album_test_db();
let tracks = vec![
album_track("t1", "Run Boy Run", 1),
album_track("t2", "The Great Escape", 2),
];
let ids = queue_album_tracks(&db, "album1", &tracks, "user1", "albums/album1")
.await
.unwrap();
for (id, track) in ids.iter().zip(tracks.iter()) {
let item_id: String = db
.query_one(
Query::with_params(
"SELECT item_id FROM downloads WHERE id = ?",
vec![QueryParam::Int64(*id)],
),
|row| row.get(0),
)
.await
.unwrap();
assert_eq!(&item_id, &track.id, "id {} must be {}'s row", id, track.id);
}
}
/// Re-queueing an album already partly downloaded must not duplicate rows or
/// reset a completed track — it fills in what is missing.
///
/// TRACES: UR-018, UR-055 | DR-173 | UT-170
#[tokio::test]
async fn test_queue_album_tracks_is_idempotent() {
let db = album_test_db();
let tracks = vec![
album_track("t1", "Run Boy Run", 1),
album_track("t2", "The Great Escape", 2),
];
let first = queue_album_tracks(&db, "album1", &tracks, "user1", "albums/album1")
.await
.unwrap();
let second = queue_album_tracks(&db, "album1", &tracks, "user1", "albums/album1")
.await
.unwrap();
assert_eq!(first, second, "the same tracks must map to the same rows");
let rows: i64 = db
.query_one(Query::new("SELECT COUNT(*) FROM downloads"), |row| {
row.get(0)
})
.await
.unwrap();
assert_eq!(rows, 2, "re-queueing must not duplicate download rows");
}
/// Two tracks of one album can share a title — a deluxe edition carrying the
/// album version and a demo of the same song, or the same song on two discs.
/// Naming the file after the title alone gave them one path, so the second
/// download overwrote the first and the album ended up short however many
/// duplicates it had.
///
/// TRACES: UR-018, UR-055 | DR-173 | UT-172
#[test]
fn test_album_file_names_are_unique_within_the_album() {
let tracks = vec![
album_track("t1", "Crucified Again", 5),
album_track("t2", "Crucified Again", 5),
album_track("t3", "Get Right", 7),
];
let names = album_file_names(&tracks);
assert_eq!(names.len(), 3);
let unique: std::collections::HashSet<_> = names.iter().collect();
assert_eq!(
unique.len(),
3,
"every track of an album needs its own file: {:?}",
names
);
assert!(names.iter().all(|n| n.ends_with(".mp3")), "{:?}", names);
assert!(
names[2].contains("Get Right"),
"an unambiguous title keeps its name: {}",
names[2]
);
}
/// Path separators in a track title must not escape the album directory.
///
/// TRACES: UR-018, UR-055 | DR-173 | UT-172
#[test]
fn test_album_file_names_sanitize_the_title() {
let names = album_file_names(&[album_track("t1", "AC/DC: Live?", 1)]);
assert!(!names[0].contains('/'), "{}", names[0]);
assert!(!names[0].contains(':'), "{}", names[0]);
}
/// The offline fallback reads the catalog directly, not through the
/// availability-gated offline listing: queueing an album while the server is
/// unreachable is a supported flow (the rows resolve on reconnect), and
/// gating it on what is already downloaded would queue only the tracks the
/// device already has.
///
/// TRACES: UR-018, UR-055 | DR-173 | UT-170
#[tokio::test]
async fn test_cached_album_tracks_finds_tracks_by_either_album_link() {
let db = album_test_db();
for sql in [
"INSERT INTO items (id, server_id, name, item_type, album_id, index_number) \
VALUES ('t1', 'server1', 'Run Boy Run', 'Audio', 'album1', 1)",
// Linked by parent_id only — how a track cached from a folder
// listing lands in the catalog.
"INSERT INTO items (id, server_id, name, item_type, parent_id, index_number) \
VALUES ('t2', 'server1', 'The Great Escape', 'Audio', 'album1', 2)",
// A different album's track must not be swept in.
"INSERT INTO items (id, server_id, name, item_type, album_id) \
VALUES ('other', 'server1', 'Iron', 'Audio', 'album2')",
] {
db.execute(Query::new(sql)).await.unwrap();
}
let tracks = cached_album_tracks(&db, "album1").await.unwrap();
let ids: Vec<_> = tracks.iter().map(|t| t.id.as_str()).collect();
assert_eq!(ids, vec!["t1", "t2"]);
}
/// Tracks the cache has never seen still get queued: the row is created and
/// an `items` row is written for it, so the download is both startable and
/// visible offline afterwards.
///
/// TRACES: UR-018, UR-055 | DR-173 | UT-170
#[tokio::test]
async fn test_queue_album_tracks_handles_tracks_absent_from_the_cache() {
let db = album_test_db();
let tracks = vec![album_track("never-cached", "Iron", 1)];
let ids = queue_album_tracks(&db, "album1", &tracks, "user1", "albums/album1")
.await
.unwrap();
assert_eq!(ids.len(), 1);
let (item_type, album_id): (String, Option<String>) = db
.query_one(
Query::new("SELECT item_type, album_id FROM items WHERE id = 'never-cached'"),
|row| Ok((row.get(0)?, row.get(1)?)),
)
.await
.unwrap();
assert_eq!(item_type, "Audio");
assert_eq!(album_id.as_deref(), Some("album1"));
}
}
+34
View File
@@ -119,6 +119,40 @@ impl HybridRepository {
.await
}
/// Every track of an album, asked of the **server** rather than the cache.
///
/// Deliberately not `get_items`, which is cache-first: it answers from SQLite
/// the moment the cache has any content. That is right for browsing and wrong
/// for deciding what to download, because a partial or unlinked cache then
/// decides how much of the album gets queued while the user is told the whole
/// album is downloading. Downloading is the one operation that must know the
/// album's *complete* contents.
///
/// Errors when the server cannot answer (offline); the caller falls back to
/// the local catalog and the rows are queued either way, resolving on
/// reconnect. Server results are written back to the cache, so browsing
/// benefits from the round trip too.
///
/// TRACES: UR-018, UR-055 | DR-173
pub async fn get_album_tracks(&self, album_id: &str) -> Result<Vec<MediaItem>, RepoError> {
let options = Some(GetItemsOptions {
include_item_types: Some(vec!["Audio".to_string()]),
sort_by: Some("ParentIndexNumber,IndexNumber,SortName".to_string()),
limit: Some(1000),
..Default::default()
});
let result = self.online.get_items(album_id, options).await?;
if !result.items.is_empty() {
if let Err(e) = self.offline.save_to_cache(album_id, &result.items).await {
warn!("[HybridRepo] Failed to cache album tracks: {:?}", e);
}
}
Ok(result.items)
}
/// Search only the local SQLite cache (downloaded content).
///
/// Fast (100ms timeout) — used to render instant results before the server