Compare commits
3 Commits
de2847d41c
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 3593698854 | |||
| a57125b9cd | |||
| 86b6901638 |
+10
-6
@@ -188,7 +188,11 @@ pub async fn add_artist(
|
||||
})
|
||||
.collect();
|
||||
|
||||
tracing::info!(total = disc.len(), filtered = filtered.len(), "discography loaded");
|
||||
tracing::info!(
|
||||
total = disc.len(),
|
||||
filtered = filtered.len(),
|
||||
"discography loaded"
|
||||
);
|
||||
|
||||
for rec in &filtered {
|
||||
if !seen_mbids.insert(rec.mbid.clone()) {
|
||||
@@ -352,7 +356,7 @@ pub async fn add_track(
|
||||
async fn finish_add_track(
|
||||
conn: &DatabaseConnection,
|
||||
title: &str,
|
||||
artist_name: &str,
|
||||
_artist_name: &str,
|
||||
recording_mbid: Option<String>,
|
||||
artist: artist::Model,
|
||||
user_id: Option<i32>,
|
||||
@@ -509,10 +513,10 @@ async fn load_or_build_discography(
|
||||
}
|
||||
|
||||
// Cache the result if we found anything
|
||||
if !recordings.is_empty() {
|
||||
if let Ok(json) = serde_json::to_string(&recordings) {
|
||||
let _ = queries::cache::set(conn, &cache_key, "computed", &json, 7 * 86400).await;
|
||||
}
|
||||
if !recordings.is_empty()
|
||||
&& let Ok(json) = serde_json::to_string(&recordings)
|
||||
{
|
||||
let _ = queries::cache::set(conn, &cache_key, "computed", &json, 7 * 86400).await;
|
||||
}
|
||||
|
||||
recordings
|
||||
|
||||
+77
-6
@@ -14,7 +14,7 @@ async fn test_db() -> Database {
|
||||
}
|
||||
|
||||
/// Insert a fake track into the DB to simulate an indexed library.
|
||||
async fn insert_track(db: &Database, artist: &str, title: &str, album: &str) {
|
||||
async fn insert_track(db: &Database, artist: &str, title: &str, album: &str, mbid: Option<&str>) {
|
||||
let now = Utc::now().naive_utc();
|
||||
let artist_rec = queries::artists::upsert(db.conn(), artist, None)
|
||||
.await
|
||||
@@ -28,6 +28,7 @@ async fn insert_track(db: &Database, artist: &str, title: &str, album: &str) {
|
||||
artist: Set(Some(artist.to_string())),
|
||||
album: Set(Some(album.to_string())),
|
||||
album_artist: Set(Some(artist.to_string())),
|
||||
musicbrainz_id: Set(mbid.map(String::from)),
|
||||
file_size: Set(1_000_000),
|
||||
artist_id: Set(Some(artist_rec.id)),
|
||||
album_id: Set(Some(album_rec.id)),
|
||||
@@ -38,6 +39,26 @@ async fn insert_track(db: &Database, artist: &str, title: &str, album: &str) {
|
||||
queries::tracks::upsert(db.conn(), active).await.unwrap();
|
||||
}
|
||||
|
||||
/// Populate the artist_rg_tracks cache so add_artist/add_track can resolve recordings.
|
||||
async fn populate_discography_cache(db: &Database, _artist_mbid: &str) {
|
||||
let rg_tracks = serde_json::json!({
|
||||
"release_mbid": "release-123",
|
||||
"tracks": [
|
||||
{"recording_mbid": "rec-1", "title": "Track One"},
|
||||
{"recording_mbid": "rec-2", "title": "Track Two"},
|
||||
]
|
||||
});
|
||||
queries::cache::set(
|
||||
db.conn(),
|
||||
&format!("artist_rg_tracks:rg-123"),
|
||||
"musicbrainz",
|
||||
&rg_tracks.to_string(),
|
||||
86400,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// Mock provider that returns a tracklist for known releases.
|
||||
struct MockProvider;
|
||||
|
||||
@@ -60,7 +81,19 @@ impl MetadataProvider for MockProvider {
|
||||
score: 100,
|
||||
}])
|
||||
}
|
||||
async fn get_recording(&self, _mbid: &str) -> DataResult<RecordingDetails> {
|
||||
async fn get_recording(&self, mbid: &str) -> DataResult<RecordingDetails> {
|
||||
// Return details for known test MBIDs
|
||||
if mbid == "rec-time" {
|
||||
return Ok(RecordingDetails {
|
||||
mbid: "rec-time".into(),
|
||||
title: "Time".into(),
|
||||
artist: "Pink Floyd".into(),
|
||||
artist_mbid: None,
|
||||
genres: vec![],
|
||||
releases: vec![],
|
||||
duration_ms: None,
|
||||
});
|
||||
}
|
||||
Err(shanty_data::DataError::Other("not found".into()))
|
||||
}
|
||||
async fn search_artist(
|
||||
@@ -160,13 +193,31 @@ async fn test_add_track_auto_owned() {
|
||||
let db = test_db().await;
|
||||
let provider = MockProvider;
|
||||
|
||||
insert_track(&db, "Pink Floyd", "Time", "DSOTM").await;
|
||||
// Create artist with MBID so the fast path can resolve
|
||||
queries::artists::upsert(db.conn(), "Pink Floyd", Some("pf-mbid"))
|
||||
.await
|
||||
.unwrap();
|
||||
// Populate discography cache so fast path finds "Time" → "rec-time"
|
||||
let known = serde_json::json!([
|
||||
{"mbid": "rec-time", "title": "Time", "rg_type": "Album", "rg_date": "1973"}
|
||||
]);
|
||||
queries::cache::set(
|
||||
db.conn(),
|
||||
"artist_known_recordings:pf-mbid",
|
||||
"computed",
|
||||
&known.to_string(),
|
||||
86400,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
insert_track(&db, "Pink Floyd", "Time", "DSOTM", Some("rec-time")).await;
|
||||
|
||||
let entry = add_track(
|
||||
db.conn(),
|
||||
Some("Pink Floyd"),
|
||||
Some("Time"),
|
||||
None,
|
||||
Some("rec-time"),
|
||||
&provider,
|
||||
None,
|
||||
)
|
||||
@@ -204,6 +255,9 @@ async fn test_add_artist_expands_to_tracks() {
|
||||
let db = test_db().await;
|
||||
let provider = MockProvider;
|
||||
|
||||
// Pre-populate the discography cache (normally done by enrich_artist)
|
||||
populate_discography_cache(&db, "artist-456").await;
|
||||
|
||||
let summary = add_artist(db.conn(), Some("Test Artist"), None, &provider, &[], None)
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -278,7 +332,24 @@ async fn test_library_summary() {
|
||||
let db = test_db().await;
|
||||
let provider = MockProvider;
|
||||
|
||||
insert_track(&db, "Pink Floyd", "Time", "DSOTM").await;
|
||||
// Create artist with MBID and populate discography cache
|
||||
queries::artists::upsert(db.conn(), "Pink Floyd", Some("pf-mbid"))
|
||||
.await
|
||||
.unwrap();
|
||||
let known = serde_json::json!([
|
||||
{"mbid": "rec-time", "title": "Time", "rg_type": "Album", "rg_date": "1973"}
|
||||
]);
|
||||
queries::cache::set(
|
||||
db.conn(),
|
||||
"artist_known_recordings:pf-mbid",
|
||||
"computed",
|
||||
&known.to_string(),
|
||||
86400,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
insert_track(&db, "Pink Floyd", "Time", "DSOTM", Some("rec-time")).await;
|
||||
|
||||
add_track(
|
||||
db.conn(),
|
||||
@@ -294,7 +365,7 @@ async fn test_library_summary() {
|
||||
db.conn(),
|
||||
Some("Pink Floyd"),
|
||||
Some("Time"),
|
||||
None,
|
||||
Some("rec-time"),
|
||||
&provider,
|
||||
None,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user