From 20bf30eae80f045ef4850c33cdbaca7faaae96ac Mon Sep 17 00:00:00 2001 From: Sanapach Date: Thu, 19 Nov 2026 23:26:36 +0200 Subject: [PATCH] Test podcast search events and subscribing from a card Co-Authored-By: Claude Opus 5.5 --- src/podcasts/discovery_tests.rs | 41 ++- src/podcasts/mod.rs | 2 + src/podcasts/postgres_tests.rs | 2 +- src/podcasts/search_tests.rs | 504 ++++++++++++++++++++++++++++++++ 4 files changed, 547 insertions(+), 2 deletions(-) create mode 100644 src/podcasts/search_tests.rs diff --git a/src/podcasts/discovery_tests.rs b/src/podcasts/discovery_tests.rs index 364fffe..7a6337d 100644 --- a/src/podcasts/discovery_tests.rs +++ b/src/podcasts/discovery_tests.rs @@ -413,6 +413,9 @@ async fn loopback_scenario() { let store = PodcastStore::new(pool.clone()); let users = accounts::<2>(&pool).await; let [u1, u2] = users; + // An account of the stranger's side (another server in real life) that + // subscribes from the card it found. + let finder = random_user(); let tag = random_hex(6); let directory = std::env::temp_dir().join(format!("furumusic-podcast-dht-{tag}")); let network = NetworkId::from_name(&format!("furumusic-podcast-loopback-{tag}")); @@ -441,7 +444,19 @@ async fn loopback_scenario() { // Two accounts share one podcast; one of them also has a private one. let title = format!("Loopback Radio {tag}"); let hidden = format!("Hidden Garden {tag}"); - let feed = format!("https://podcasts.example/loopback-{tag}.xml"); + let server = super::test_server::TestServer::start(); + server.set( + "/loopback.xml", + super::test_server::Route::new( + format!( + r#"{title} +First1Mon, 01 Jan 2024 10:00:00 GMT +"# + ), + "application/rss+xml", + ), + ); + let feed = server.url("/loopback.xml"); let podcast_id = subscribe(&store, u1, &feed, &title).await; subscribe(&store, u2, &feed, &title).await; subscribe( @@ -484,6 +499,26 @@ async fn loopback_scenario() { // The private podcast is not discoverable, by text or by id. assert!(stranger.dht.search(&hidden, 10).await.unwrap().is_empty()); + // The finder sees the card in the federated search, looks at its + // episodes and subscribes: its own private subscription with the + // card's id, fetched from the feed (never from the publisher). + let views = super::search::hit_views(&store, finder, &title, &[], vec![hit.clone()]) + .await + .unwrap(); + assert_eq!(views.len(), 1); + assert!(!views[0].subscribed && views[0].exact_match); + let found = json!({ "podcast_id": views[0].podcast_id, "feed_url": views[0].feed_url }); + let fetcher = super::postgres_tests::local_fetcher(); + let preview = api::preview_found(&store, &fetcher, finder, found.clone()) + .await + .unwrap(); + assert_eq!(preview.episodes.len(), 1); + let subscribed = api::subscribe_found(&store, &fetcher, finder, found, now_ms()) + .await + .unwrap(); + assert_eq!(subscribed.podcast.podcast_id, podcast_id.to_string()); + assert!(subscribed.podcast.subscribed && !subscribed.podcast.shared); + // Idempotent; one account stopping keeps the card of the other. done(share(&store, u1, &podcast_id, json!({"shared": false})).await); let report = discovery::reconcile_once(&pool, &web.dht, Some(&users)) @@ -527,6 +562,10 @@ async fn loopback_scenario() { .await .unwrap(); assert!(!view.shared && !view.published); + // The revocation does not touch the finder's subscription. + let kept = store.podcast(finder, &podcast_id).await.unwrap().unwrap(); + assert!(kept.subscribed && !kept.shared); + cleanup(&pool, [finder]).await; web.stop().await; stranger.stop().await; diff --git a/src/podcasts/mod.rs b/src/podcasts/mod.rs index 3bf25ca..ebbc133 100644 --- a/src/podcasts/mod.rs +++ b/src/podcasts/mod.rs @@ -91,6 +91,8 @@ pub(crate) mod postgres_tests; #[cfg(test)] mod proxy_tests; #[cfg(test)] +mod search_tests; +#[cfg(test)] pub(crate) mod test_server; #[cfg(test)] mod tests; diff --git a/src/podcasts/postgres_tests.rs b/src/podcasts/postgres_tests.rs index 8c32023..aa91a4d 100644 --- a/src/podcasts/postgres_tests.rs +++ b/src/podcasts/postgres_tests.rs @@ -1600,7 +1600,7 @@ fn refresh_item(guid: &str) -> String { /// A fetcher that may reach the local test server (loopback is allowed /// only by the test policy). -fn local_fetcher() -> FeedFetcher { +pub(crate) fn local_fetcher() -> FeedFetcher { FeedFetcher::with_http( net::SafeHttp::with_lookup( fetch::FEED_HTTP, diff --git a/src/podcasts/search_tests.rs b/src/podcasts/search_tests.rs new file mode 100644 index 0000000..2569f3c --- /dev/null +++ b/src/podcasts/search_tests.rs @@ -0,0 +1,504 @@ +//! Public podcasts in the federated search against PostgreSQL: the search +//! events (music unchanged, podcast hits added), the "already subscribed" +//! marker and subscribing from a card with the card's id. +//! +//! Ignored by default, like the other store tests: set +//! `FURU_DEVICE_SYNC_TEST_DATABASE_URL` to a disposable database. Feeds come +//! from a local test HTTP server. + +use std::collections::BTreeSet; + +use music_dht::device_sync::{SyncOpPayload, SyncOpWire, random_hex}; +use music_dht::podcast::{FeedUrl, PodcastId, PodcastMeta}; +use music_dht::podcast_dht::{PodcastCard, PodcastCardSpec, PodcastSearchHit}; +use music_dht::{ItemId, ItemKind, LibraryItem, NetworkId, SecretKey, normalize_name}; +use serde_json::{Value, json}; + +use super::api; +use super::postgres_tests::{cleanup, local_fetcher, random_user, test_pool}; +use super::test_server::{Route, TestServer}; +use super::*; +use crate::federation::client::{music_search_events, podcast_events}; + +fn url(value: &str) -> FeedUrl { + FeedUrl::parse(value).unwrap() +} + +/// A search hit as `PodcastDht::search` returns it, for the origin URL +/// `feed` (published by one device). +fn hit(feed: &str, title: &str) -> PodcastSearchHit { + let feed_url = url(feed); + let podcast_id = PodcastId::from_feed_url(&feed_url); + let publisher = SecretKey::from_bytes(&[9; 32]).public(); + let card = PodcastCard::from_spec( + PodcastCardSpec { + podcast_id: podcast_id.clone(), + feed_url: feed_url.clone(), + id_feed_url: None, + title: title.into(), + author: Some("Host".into()), + description: Some("Not markup".into()), + image_url: Some("https://cdn.example/art.jpg".into()), + link: Some("https://show.example/".into()), + language: Some("en".into()), + categories: vec!["Technology".into()], + episode_count: Some(12), + latest_episode_at_ms: Some(1_700_000_000_000), + }, + NetworkId::from_name("search-tests"), + publisher, + 1, + 1_700_000_000_000, + ); + PodcastSearchHit { + podcast_id, + feed_url, + card, + publishers: vec![publisher], + claimed_feed_urls: Vec::new(), + } +} + +fn item(kind: ItemKind, name: &str, content_id: Option<&str>) -> LibraryItem { + LibraryItem { + id: ItemId::from_bytes(*blake3::hash(name.as_bytes()).as_bytes()), + owner: SecretKey::from_bytes(&[5; 32]).public(), + kind, + name: name.into(), + normalized_name: normalize_name(name), + artist_names: vec!["Night Owls".into()], + featured_artist_names: Vec::new(), + year: Some(2024), + release_type: None, + release_title: (kind == ItemKind::Track).then(|| "Night Radio".into()), + track_number: Some(1), + disc_number: None, + duration_seconds: Some(180.0), + content_id: content_id.map(str::to_string), + revision: 1, + deleted: false, + updated_at_ms: 1, + } +} + +fn keys(value: &Value) -> BTreeSet { + value.as_object().unwrap().keys().cloned().collect() +} + +fn set(names: &[&str]) -> BTreeSet { + names.iter().map(|name| name.to_string()).collect() +} + +fn meta(feed: &str, title: &str) -> PodcastMeta { + PodcastMeta { + feed_url: url(feed), + title: title.into(), + author: None, + description: None, + link: None, + image_url: None, + } +} + +#[tokio::test] +#[ignore = "requires a disposable database in FURU_DEVICE_SYNC_TEST_DATABASE_URL"] +async fn postgres_federated_search_keeps_music_events_and_adds_podcast_hits() { + let pool = test_pool().await; + let store = PodcastStore::new(pool.clone()); + let (user, other) = (random_user(), random_user()); + cleanup(&pool, [user, other]).await; + let tag = random_hex(6); + let content_id = format!("b3:{}", blake3::hash(tag.as_bytes()).to_hex()); + + // Music: the contract of the existing events is unchanged; only the + // `type` marker is added. + let music = vec![ + item(ItemKind::Track, "Night Radio Theme", Some(&content_id)), + item(ItemKind::Artist, "Night Owls", None), + item(ItemKind::Release, "Night Radio", None), + ]; + let own = SecretKey::from_bytes(&[1; 32]).public(); + let music_events = music_search_events(&pool, "search-1", &music, own) + .await + .unwrap(); + let music_json: Vec = music_events + .iter() + .map(|event| serde_json::to_value(event).unwrap()) + .collect(); + let kinds: Vec<&str> = music_json + .iter() + .map(|event| event["kind"].as_str().unwrap()) + .collect(); + assert_eq!( + kinds, + vec![ + "federation.track", + "federation.artist", + "federation.release" + ] + ); + for (index, event) in music_json.iter().enumerate() { + assert_eq!( + keys(event), + set(&[ + "search_id", + "sequence", + "kind", + "type", + "peer", + "entity_key", + "entity" + ]) + ); + assert_eq!(event["type"], "music"); + assert_eq!(event["search_id"], "search-1"); + assert_eq!(event["sequence"], index as u64 + 1); + } + let track = &music_json[0]; + assert_eq!(track["entity_key"], json!({ "content_id": content_id })); + assert_eq!( + keys(&track["entity"]), + set(&["key", "metadata", "availability"]) + ); + assert_eq!(track["entity"]["metadata"]["title"], "Night Radio Theme"); + assert_eq!(track["entity"]["availability"]["state"], "federated"); + assert_eq!(track["peer"], music[0].owner.to_string()); + assert_eq!( + keys(&music_json[1]["entity"]), + set(&["key", "name", "image_url", "peers"]) + ); + assert_eq!( + keys(&music_json[2]["entity"]), + set(&["key", "title", "artists", "year", "cover_url", "sources"]) + ); + + // The account has podcast A, and podcast C through an alias. + let feed_a = format!("https://podcasts.example/a-{tag}.xml"); + let feed_b = format!("https://podcasts.example/b-{tag}.xml"); + let feed_c = format!("https://podcasts.example/c-{tag}.xml"); + let alias_c = format!("https://moved.example/c-{tag}.xml"); + let id_a = store + .subscribe(user, &meta(&feed_a, "Night Radio")) + .await + .unwrap() + .podcast_id; + let id_c = store + .subscribe(user, &meta(&feed_c, "Cellar Talks")) + .await + .unwrap() + .podcast_id; + let aliases = SyncOpWire { + op_id: format!("tui-{tag}:1"), + origin_device_id: format!("tui-{tag}"), + seq: 1, + hlc_ms: 9_000_000_000_000, + payload: SyncOpPayload::PodcastFeedAliasesAdded { + podcast_id: id_c.clone(), + feed_aliases: vec![url(&feed_c), url(&alias_c)], + }, + }; + store.apply_remote_op(user, &aliases).await.unwrap(); + + // Someone published a card for the alias URL: another podcast id, but + // the account already has it. + let hits = vec![ + hit(&feed_b, "Night Radio Stories"), + hit(&alias_c, "Night Radio Cellar"), + hit(&feed_a, "Night Radio"), + ]; + let first = music_events.len() as u64 + 1; + let events = podcast_events( + &pool, + user, + "search-1", + "night radio", + &music, + first, + hits.clone(), + ) + .await + .unwrap(); + let json: Vec = events + .iter() + .map(|event| serde_json::to_value(event).unwrap()) + .collect(); + assert_eq!(json.len(), 3); + for (index, event) in json.iter().enumerate() { + assert_eq!(event["kind"], "federation.podcast"); + assert_eq!(event["type"], "podcast"); + assert_eq!(event["peer"], Value::Null); + assert_eq!(event["sequence"], first + index as u64); + assert_eq!(event["entity"]["type"], "podcast"); + assert_eq!(event["entity"]["kind"], "podcast"); + assert_eq!( + event["entity_key"], + json!({ "podcast_id": event["entity"]["podcast_id"] }) + ); + // The publisher's personal state never appears: no registers, + // devices or publisher keys, only the card's public projection. + assert_eq!( + keys(&event["entity"]), + set(&[ + "type", + "kind", + "podcast_id", + "feed_url", + "title", + "author", + "description", + "image_url", + "link", + "language", + "categories", + "episode_count", + "latest_episode_at_ms", + "publisher_count", + "rank", + "exact_match", + "subscribed", + "local_podcast_id", + ]) + ); + } + // The exact title leads (after the exact music release), then the rest + // in the overlay's order. + let entity = |index: usize| &json[index]["entity"]; + assert_eq!(entity(0)["podcast_id"], id_a.to_string()); + assert_eq!(entity(0)["exact_match"], true); + assert_eq!(entity(0)["rank"], 1); + assert_eq!(entity(0)["subscribed"], true); + assert_eq!(entity(0)["local_podcast_id"], id_a.to_string()); + assert_eq!(entity(0)["feed_url"], feed_a); + assert_eq!(entity(0)["description"], "Not markup"); + assert_eq!(entity(0)["publisher_count"], 1); + assert_eq!(entity(1)["podcast_id"], hits[0].podcast_id.to_string()); + assert_eq!(entity(1)["subscribed"], false); + assert_eq!(entity(1)["local_podcast_id"], Value::Null); + assert_eq!(entity(2)["podcast_id"], hits[1].podcast_id.to_string()); + assert_ne!(hits[1].podcast_id, id_c); + assert_eq!(entity(2)["subscribed"], true, "known through the alias"); + assert_eq!(entity(2)["local_podcast_id"], id_c.to_string()); + + // The marker belongs to the searching account. + let theirs = podcast_events(&pool, other, "search-2", "night radio", &music, 1, hits) + .await + .unwrap(); + assert!( + theirs + .iter() + .all(|event| event.entity["subscribed"] == false + && event.entity["local_podcast_id"].is_null()) + ); + + // Unsubscribed: known, but no longer marked as subscribed. + api::unsubscribe(&store, user, &id_a).await.unwrap(); + let events = podcast_events( + &pool, + user, + "search-3", + "night radio", + &[], + 1, + vec![hit(&feed_a, "Night Radio")], + ) + .await + .unwrap(); + assert_eq!(events[0].entity["subscribed"], false); + assert_eq!(events[0].entity["local_podcast_id"], id_a.to_string()); + assert_eq!(events[0].entity["rank"], 0); + + sqlx::query("DELETE FROM furumusic__track_ref WHERE content_id = $1") + .bind(&content_id) + .execute(&pool) + .await + .unwrap(); + cleanup(&pool, [user, other]).await; +} + +fn rss(title: &str, items: &str) -> String { + format!( + r#" + + + {title} + https://show.example/ + Found in the federation + {items} + +"# + ) +} + +const ITEMS: &str = r#" + Pilots1e11 + 1Mon, 01 Jan 2024 10:00:00 GMT + + Seconds1e21 + 2Mon, 08 Jan 2024 10:00:00 GMT + + Bonusbonus + "#; + +async fn log_len(pool: &sqlx::PgPool, user: i64) -> i64 { + sqlx::query_scalar("SELECT COUNT(*) FROM furumusic__fed_sync_ops WHERE user_id = $1") + .bind(user) + .fetch_one(pool) + .await + .unwrap() +} + +#[tokio::test] +#[ignore = "requires a disposable database in FURU_DEVICE_SYNC_TEST_DATABASE_URL"] +async fn postgres_subscribing_from_a_card_keeps_the_card_id() { + let pool = test_pool().await; + let store = PodcastStore::new(pool.clone()); + let (user, other) = (random_user(), random_user()); + cleanup(&pool, [user, other]).await; + let server = TestServer::start(); + server.set( + "/show.xml", + Route::new(rss("Found Show", ITEMS), "application/rss+xml"), + ); + server.set("/old.xml", Route::redirect(301, "/new.xml")); + server.set( + "/new.xml", + Route::new(rss("Moved Show", ITEMS), "application/rss+xml"), + ); + let fetcher = local_fetcher(); + let now = super::now_ms(); + let show = hit(&server.url("/show.xml"), "Found Show"); + let body = |hit: &PodcastSearchHit| json!({ "podcast_id": hit.podcast_id.to_string(), "feed_url": hit.feed_url.to_string() }); + + // Invalid requests fetch nothing. + let other_id = PodcastId::from_feed_url(&url("https://podcasts.example/other.xml")); + for (request, status) in [ + (json!({}), 400), + ( + json!({ "podcast_id": "x", "feed_url": show.feed_url.to_string() }), + 400, + ), + ( + json!({ "podcast_id": show.podcast_id.to_string(), "feed_url": "ftp://x/y" }), + 400, + ), + // A URL the id does not come from (a claimed move, another podcast). + ( + json!({ "podcast_id": other_id.to_string(), "feed_url": show.feed_url.to_string() }), + 422, + ), + ] { + let error = api::preview_found(&store, &fetcher, user, request.clone()) + .await + .unwrap_err(); + assert_eq!(error.status, status, "{request}: {}", error.message); + let error = api::subscribe_found(&store, &fetcher, user, request.clone(), now) + .await + .unwrap_err(); + assert_eq!(error.status, status, "{request}: {}", error.message); + } + assert_eq!(server.requests("/show.xml").len(), 0); + // The production policy never reaches the local server. + let refused = api::preview_found(&store, &FeedFetcher::new().unwrap(), user, body(&show)) + .await + .unwrap_err(); + assert_eq!(refused.status, 422, "{}", refused.message); + + // The preview reads the feed and stores nothing. + let preview = api::preview_found(&store, &fetcher, user, body(&show)) + .await + .unwrap(); + assert_eq!(preview.title, "Found Show"); + assert_eq!(preview.podcast_id, show.podcast_id.to_string()); + assert_eq!(preview.episode_count, 3); + let seasons: Vec<_> = preview + .seasons + .iter() + .map(|season| (season.season, season.episode_count)) + .collect(); + assert_eq!(seasons, vec![(Some(1), 2), (None, 1)]); + assert_eq!(preview.episodes[0].title, "Second"); + assert!(!preview.subscribed && !preview.moved); + assert_eq!(server.requests("/show.xml").len(), 1); + assert!( + store + .podcast(user, &show.podcast_id) + .await + .unwrap() + .is_none() + ); + assert_eq!(log_len(&pool, user).await, 0); + + // Subscribing: the account's own private subscription with the card's id. + let subscribed = api::subscribe_found(&store, &fetcher, user, body(&show), now) + .await + .unwrap(); + assert_eq!(subscribed.podcast.podcast_id, show.podcast_id.to_string()); + assert!(!subscribed.already_subscribed); + assert_eq!(subscribed.new_episodes, 3); + assert!(subscribed.podcast.subscribed); + assert!( + !subscribed.podcast.shared, + "a subscription from a card is private" + ); + assert_eq!(server.requests("/show.xml").len(), 2); + let marked = super::search::hit_views(&store, user, "found show", &[], vec![show.clone()]) + .await + .unwrap(); + assert!(marked[0].subscribed); + + // Again: already subscribed, nothing fetched or written. + let log = log_len(&pool, user).await; + let again = api::subscribe_found(&store, &fetcher, user, body(&show), now) + .await + .unwrap(); + assert!(again.already_subscribed); + assert_eq!(again.podcast.podcast_id, show.podcast_id.to_string()); + assert_eq!(server.requests("/show.xml").len(), 2); + assert_eq!(log_len(&pool, user).await, log); + let preview = api::preview_found(&store, &fetcher, user, body(&show)) + .await + .unwrap(); + assert!(preview.subscribed); + assert_eq!( + preview.local_podcast_id.as_deref(), + Some(show.podcast_id.as_str()) + ); + + // Another account subscribing from the same card gets its own podcast + // with the same id; the first account's state is untouched. + let theirs = api::subscribe_found(&store, &fetcher, other, body(&show), now) + .await + .unwrap(); + assert_eq!(theirs.podcast.podcast_id, show.podcast_id.to_string()); + assert!(!theirs.already_subscribed); + + // A card whose origin URL moved permanently: the id stays the card's, + // the new URL becomes an alias and is fetched from now on. + let old = hit(&server.url("/old.xml"), "Moved Show"); + let moved = api::subscribe_found(&store, &fetcher, user, body(&old), now) + .await + .unwrap(); + assert_eq!(moved.podcast.podcast_id, old.podcast_id.to_string()); + assert_eq!(moved.podcast.feed_url, server.url("/new.xml")); + let aliases = store.feed_aliases(user, &old.podcast_id).await.unwrap(); + assert!(aliases.contains(&url(&server.url("/new.xml")))); + // Someone's card for the new URL is the same podcast for this account. + let new = hit(&server.url("/new.xml"), "Moved Show"); + assert_ne!(new.podcast_id, old.podcast_id); + let requests = server.requests("/new.xml").len(); + let again = api::subscribe_found(&store, &fetcher, user, body(&new), now) + .await + .unwrap(); + assert!(again.already_subscribed); + assert_eq!(again.podcast.podcast_id, old.podcast_id.to_string()); + assert_eq!(server.requests("/new.xml").len(), requests); + assert!( + store + .podcast(user, &new.podcast_id) + .await + .unwrap() + .is_none() + ); + + cleanup(&pool, [user, other]).await; +}