Modern Apps git

← Modern-Apps.git

youpipe: add extractor Rust tests, fix subscriber count

Commit
b8a2e4c1457f1a0b1123ba4b42d8880a3498dfb9
Author
Vayun Mathur <[email protected]>
When
2026-11-18 16:13 UTC (17 days ago)
Parents
6115277
Raise youpipe_extractor line coverage from 65.2% to 94.9% with browse/stream/search integration tests over a stub HTTP client. Production fix: parse_channel read subscriber_count from videoCountText, so a channel's video count was reported as its subscribers; it now reads subscriberCountText. Verified with cargo test -p youpipe_extractor (81 passed) and cargo llvm-cov (94.92%).

Diff

youpipe/src/main/rust/src/search.rs
diff --git a/youpipe/src/main/rust/src/search.rs b/youpipe/src/main/rust/src/search.rsindex 7c685ea089..735e2991df 100644--- a/youpipe/src/main/rust/src/search.rs+++ b/youpipe/src/main/rust/src/search.rs@@ -251,7 +251,7 @@ fn parse_channel(renderer: &Value) -> std::result::Result<ChannelItem, String> {     let url = channel_url_from_navigation_endpoint(renderer.obj("navigationEndpoint"))         .unwrap_or_else(|| crate::parsing::channel_url(channel_id)); -    let subscriber_count = text_from_object(renderer.obj("videoCountText"))+    let subscriber_count = text_from_object(renderer.obj("subscriberCountText"))         .as_deref()         .map_or(-1, parse_abbreviated_count); 
youpipe/src/main/rust/tests/coverage_browse.rs
diff --git a/youpipe/src/main/rust/tests/coverage_browse.rs b/youpipe/src/main/rust/tests/coverage_browse.rsnew file mode 100644index 0000000000..7ce8c199c4--- /dev/null+++ b/youpipe/src/main/rust/tests/coverage_browse.rs@@ -0,0 +1,200 @@+//! Coverage for browse.rs / browse_part1.rs via public API with a stub transport.+use std::sync::{Mutex, MutexGuard};+use serde_json::json;+use youpipe_extractor::browse;+use youpipe_extractor::decoder::clear_caches;+use youpipe_extractor::http::{HttpClient, Request, Response, Result};+use youpipe_extractor::innertube::reset_client_version;+use youpipe_extractor::model::SearchItem;++static LOCK: Mutex<()> = Mutex::new(());+fn lock() -> MutexGuard<'static, ()> { let g = LOCK.lock().unwrap_or_else(|e| e.into_inner()); reset_client_version(); clear_caches(); g }+fn resp(url: &str, body: String) -> Response { Response { code: 200, body: body.into_bytes(), latest_url: url.to_string(), headers: [("Content-Type".into(), vec!["application/json".into()])].into_iter().collect() } }++#[derive(Default, Clone)]+struct Client { browse: String, browse_cont: String, next: String, next_cont: String }+impl HttpClient for Client {+    fn execute(&self, req: Request<'_>) -> Result<Response> {+        if req.url.contains("sw.js") { return Ok(resp(&req.url, r#"{"INNERTUBE_CONTEXT_CLIENT_VERSION":"2.20260120.01.00"}"#.into())); }+        if req.url.contains("latest-player") { return Ok(resp(&req.url, r#"{"player":"testplayer","signatureTimestamp":12345}"#.into())); }+        let body = req.body.map(|b| String::from_utf8_lossy(b).into_owned()).unwrap_or_default();+        let is_cont = body.contains("\"continuation\"");+        if req.url.contains("youtubei/v1/browse") { return Ok(resp(&req.url, if is_cont { self.browse_cont.clone() } else { self.browse.clone() })); }+        if req.url.contains("youtubei/v1/next") { return Ok(resp(&req.url, if is_cont { self.next_cont.clone() } else { self.next.clone() })); }+        Ok(resp(&req.url, "{}".into()))+    }+}+fn vid(id: &str, title: &str) -> serde_json::Value { json!({"videoRenderer":{"videoId":id,"title":{"simpleText":title},"lengthText":{"simpleText":"4:20"},"viewCountText":{"simpleText":"1,234 views"},"publishedTimeText":{"simpleText":"2 days ago"},"longBylineText":{"runs":[{"text":"Chan","navigationEndpoint":{"browseEndpoint":{"browseId":"UC12345678901234567890"}}}]},"thumbnail":{"thumbnails":[{"url":"//i.ytimg.com/a.jpg","width":120,"height":90}]},"ownerBadges":[{"metadataBadgeRenderer":{"style":"BADGE_STYLE_TYPE_VERIFIED"}}]}}) }++fn channel_fixture() -> String {+    json!({+      "metadata":{"channelMetadataRenderer":{"externalId":"UC12345678901234567890","title":"My Channel","description":"desc","channelUrl":"https://www.youtube.com/@my","avatar":{"thumbnails":[{"url":"//i.ytimg.com/a.jpg","width":88,"height":88}]}}},+      "header":{"c4TabbedHeaderRenderer":{"title":{"simpleText":"My Channel"},"subscriberCountText":{"simpleText":"1.2M subscribers"},"banner":{"thumbnails":[{"url":"https://i.ytimg.com/b.jpg","width":100,"height":20}]},"badges":[{"metadataBadgeRenderer":{"style":"BADGE_STYLE_TYPE_VERIFIED"}}]}},+      "contents":{"twoColumnBrowseResultsRenderer":{"tabs":[+        {"tabRenderer":{"content":{"richGridRenderer":{"contents":[+            {"richItemRenderer":{"content": vid("aaaaaaaaaaa","Vid1")}},+            {"continuationItemRenderer":{"continuationEndpoint":{"continuationCommand":{"token":"CONT1"}}}}+        ]}}}},+        {"tabRenderer":{"content":{"sectionListRenderer":{"contents":[+            {"itemSectionRenderer":{"contents":[+                {"shelfRenderer":{"content":{"verticalListRenderer":{"items":[vid("bbbbbbbbbbb","Vid2")]}}}},+                {"gridRenderer":{"items":[{"gridVideoRenderer":{"videoId":"ccccccccccc","title":{"simpleText":"Vid3"}}}]}},+                vid("ddddddddddd","Vid4"),+                {"playlistVideoRenderer":{"videoId":"eeeeeeeeeee","title":{"runs":[{"text":"Vid5"}]},"lengthSeconds":"125"}}+            ]}}+        ]}}}}+      ]}}+    }).to_string()+}++#[test]+fn channel_info_full() {+    let _g = lock();+    let c = Client { browse: channel_fixture(), ..Default::default() };+    let info = browse::channel_info(&c, "UC12345678901234567890", "en", "US").unwrap();+    assert_eq!(info.id, "UC12345678901234567890");+    assert_eq!(info.name, "My Channel");+    assert_eq!(info.description.as_deref(), Some("desc"));+    assert_eq!(info.subscriber_count, 1_200_000);+    assert!(info.verified);+    assert_eq!(info.avatars.len(), 1);+    assert_eq!(info.banners.len(), 1);+    assert_eq!(info.items.len(), 5, "items: {:?}", info.items);+    assert_eq!(info.next_page_token.as_deref(), Some("CONT1"));+    match &info.items[0] { SearchItem::Stream(s) => { assert_eq!(s.duration_seconds, Some(260)); assert_eq!(s.view_count, 1234); assert!(s.uploader_verified); assert_eq!(s.uploader_url.as_deref(), Some("https://www.youtube.com/channel/UC12345678901234567890")); } _ => panic!("stream") }+}++#[test]+fn channel_info_header_fallback_and_url_fallback() {+    let _g = lock();+    // No metadata title/channelUrl, header is pageHeaderRenderer only+    let fixture = json!({"metadata":{"channelMetadataRenderer":{"externalId":"UC99999999999999999999"}},"header":{"pageHeaderRenderer":{"title":{"simpleText":"Fallback Name"},"subscriberCountText":{"runs":[{"text":"15K subscribers"}]}}},"contents":{"twoColumnBrowseResultsRenderer":{"tabs":[]}}}).to_string();+    let c = Client { browse: fixture, ..Default::default() };+    let info = browse::channel_info(&c, "UC99999999999999999999", "en", "US").unwrap();+    assert_eq!(info.name, "Fallback Name");+    assert_eq!(info.subscriber_count, 15_000);+    assert!(info.url.contains("UC99999999999999999999"));+    assert!(info.items.is_empty());+    // carousel header variant+    let fixture2 = json!({"header":{"carouselHeaderRenderer":{"title":{"simpleText":"Carousel"}}},"contents":{}}).to_string();+    let c2 = Client { browse: fixture2, ..Default::default() };+    let info2 = browse::channel_info(&c2, "@handle", "en", "US").unwrap();+    assert_eq!(info2.name, "Carousel");+}++#[test]+fn playlist_info_header_and_fallbacks() {+    let _g = lock();+    let fixture = json!({+      "microformat":{"microformatDataRenderer":{"title":"Micro Title","description":"mdesc","thumbnail":{"thumbnails":[{"url":"https://i.ytimg.com/p.jpg","width":10,"height":10}]}}},+      "sidebar":{"playlistSidebarRenderer":{"items":[{"playlistSidebarPrimaryInfoRenderer":{"title":{"simpleText":"Sidebar Title"}}}]}},+      "header":{"playlistHeaderRenderer":{"title":{"runs":[{"text":"Header Title"}]},"numVideosText":{"simpleText":"42 videos"}}},+      "contents":{"twoColumnBrowseResultsRenderer":{"tabs":[{"tabRenderer":{"content":{"sectionListRenderer":{"contents":[{"itemSectionRenderer":{"contents":[{"playlistVideoListRenderer":{"contents":[vid("aaaaaaaaaaa","P1"), vid("bbbbbbbbbbb","P2")]}}]}}]}}}}]}}+    }).to_string();+    let c = Client { browse: fixture, ..Default::default() };+    let info = browse::playlist_info(&c, "PLabc", "en", "US").unwrap();+    assert_eq!(info.name, "Header Title");+    assert_eq!(info.stream_count, 42);+    assert_eq!(info.items.len(), 2);+    assert_eq!(info.url, "https://www.youtube.com/playlist?list=PLabc");+    // sidebar fallback (no header title)+    let fixture2 = json!({"microformat":{"microformatDataRenderer":{"title":"Micro Title"}},"sidebar":{"playlistSidebarRenderer":{"items":[{"playlistSidebarPrimaryInfoRenderer":{"title":{"simpleText":"Sidebar Title"}}}]}},"header":{"playlistHeaderRenderer":{}},"contents":{"twoColumnBrowseResultsRenderer":{"tabs":[]}}}).to_string();+    let info2 = browse::playlist_info(&Client{browse: fixture2, ..Default::default()}, "PLx", "en", "US").unwrap();+    assert_eq!(info2.name, "Sidebar Title");+    // microformat fallback+    let fixture3 = json!({"microformat":{"microformatDataRenderer":{"title":"Micro Title"}},"contents":{"twoColumnBrowseResultsRenderer":{"tabs":[]}}}).to_string();+    let info3 = browse::playlist_info(&Client{browse: fixture3, ..Default::default()}, "PLx", "en", "US").unwrap();+    assert_eq!(info3.name, "Micro Title");+    assert_eq!(info3.stream_count, -1);+}++#[test]+fn trending_and_browse_continuation() {+    let _g = lock();+    let c = Client { browse: channel_fixture(), ..Default::default() };+    let page = browse::trending(&c, "en", "US").unwrap();+    assert_eq!(page.items.len(), 5);+    // continuation: both actions + endpoints, richItem, direct, continuation token (button spelling)+    let cont = json!({+      "onResponseReceivedActions":[{"appendContinuationItemsAction":{"continuationItems":[+          {"richItemRenderer":{"content": vid("aaaaaaaaaaa","C1")}},+          vid("bbbbbbbbbbb","C2"),+          {"continuationItemRenderer":{"button":{"buttonRenderer":{"command":{"continuationCommand":{"token":"NEXTTOK"}}}}}}+      ]}}],+      "onResponseReceivedEndpoints":[{"appendContinuationItemsAction":{"continuationItems":[vid("ccccccccccc","C3")]}}]+    }).to_string();+    let page2 = browse::browse_continuation(&Client{browse_cont: cont, ..Default::default()}, "tok", "en", "US").unwrap();+    assert_eq!(page2.items.len(), 3);+    assert_eq!(page2.next_page_token.as_deref(), Some("NEXTTOK"));+}++#[test]+fn comments_viewmodel_and_legacy() {+    let _g = lock();+    let next_primary = json!({"contents":{"twoColumnWatchNextResults":{"results":{"results":{"contents":[+        {"itemSectionRenderer":{"contents":[{"continuationItemRenderer":{"continuationEndpoint":{"continuationCommand":{"token":"CTOK"}}}}]}}+    ]}}}}}).to_string();+    let comments_page = json!({+      "frameworkUpdates":{"entityBatchUpdate":{"mutations":[{"entityKey":"k1","payload":{"commentEntityPayload":{"properties":{"commentId":"c1","content":{"content":"hello vm"},"publishedTime":"3 days ago"},"author":{"displayName":"Alice","channelId":"UC12345678901234567890","isVerified":true,"avatarThumbnailUrl":{"thumbnails":[{"url":"https://i.ytimg.com/av.jpg","width":40,"height":40}]}},"toolbar":{"likeCountNotliked":"1.2K","replyCount":"5"}}}}]}},+      "onResponseReceivedEndpoints":[+        {"reloadContinuationItemsCommand":{"continuationItems":[+            {"commentThreadRenderer":{"commentViewModel":{"commentViewModel":{"commentKey":"k1"}}}},+            {"commentThreadRenderer":{"comment":{"commentRenderer":{"commentId":"c2","contentText":{"simpleText":"legacy hi"},"authorText":{"simpleText":"Bob"},"authorEndpoint":{"browseEndpoint":{"browseId":"UC12345678901234567890"}},"authorThumbnail":{"thumbnails":[{"url":"https://i.ytimg.com/b.jpg","width":40,"height":40}]},"authorCommentBadge":[{"metadataBadgeRenderer":{"style":"BADGE_STYLE_TYPE_VERIFIED"}}],"likeCount":42,"replyCount":3,"publishedTimeText":{"simpleText":"1 day ago"},"pinnedCommentBadge":{"x":1},"actionButtons":{"commentActionButtonsRenderer":{"creatorHeart":{"y":1}}}}}}},+            {"continuationItemRenderer":{"continuationEndpoint":{"continuationCommand":{"token":"NEXTC"}}}}+        ]}},+        {"appendContinuationItemsAction":{"continuationItems":[+            {"commentThreadRenderer":{"comment":{"commentRenderer":{"commentId":"c3","contentText":{"runs":[{"text":"second legacy"}]}}}}}+        ]}}+      ]+    }).to_string();+    let c = Client { next: next_primary, next_cont: comments_page.clone(), ..Default::default() };+    let page = browse::comments(&c, "aaaaaaaaaaa", "en", "US").unwrap();+    assert_eq!(page.comments.len(), 3, "{:?}", page.comments);+    assert_eq!(page.next_page_token.as_deref(), Some("NEXTC"));+    let vm = &page.comments[0];+    assert_eq!(vm.id, "c1"); assert_eq!(vm.text, "hello vm"); assert_eq!(vm.author_name.as_deref(), Some("Alice")); assert!(vm.author_verified); assert_eq!(vm.like_count, 1200); assert_eq!(vm.reply_count, 5);+    let legacy = &page.comments[1];+    assert_eq!(legacy.id, "c2"); assert_eq!(legacy.text, "legacy hi"); assert!(legacy.is_pinned); assert!(legacy.is_hearted); assert_eq!(legacy.like_count, 42);+    // direct comments_page entry+    let direct = browse::comments_page(&Client{next_cont: comments_page, ..Default::default()}, "tok", "en", "US").unwrap();+    assert_eq!(direct.comments.len(), 3);+}++#[test]+fn comments_disabled_returns_empty() {+    let _g = lock();+    let next_primary = json!({"contents":{"twoColumnWatchNextResults":{"results":{"results":{"contents":[{"itemSectionRenderer":{"contents":[{"videoRenderer":{}}]}}]}}}}}).to_string();+    let c = Client { next: next_primary, ..Default::default() };+    let page = browse::comments(&c, "aaaaaaaaaaa", "en", "US").unwrap();+    assert!(page.comments.is_empty());+    // token found via second search path inside itemSection contents+    let next2 = json!({"contents":{"twoColumnWatchNextResults":{"results":{"results":{"contents":[+        {"itemSectionRenderer":{"contents":[{"foo":1},{"continuationItemRenderer":{"continuationEndpoint":{"continuationCommand":{"token":"T2"}}}}]}}+    ]}}}}}).to_string();+    let cont_page = json!({"onResponseReceivedActions":[{"appendContinuationItemsAction":{"continuationItems":[]}}]}).to_string();+    let page2 = browse::comments(&Client{next: next2, next_cont: cont_page, ..Default::default()}, "aaaaaaaaaaa", "en", "US").unwrap();+    assert!(page2.comments.is_empty());+}++#[test]+fn browse_error_paths() {+    let _g = lock();+    struct Code(u16, String);+    impl HttpClient for Code {+        fn execute(&self, req: Request<'_>) -> Result<Response> {+            if req.url.contains("sw.js") { return Ok(resp(&req.url, r#"{"INNERTUBE_CONTEXT_CLIENT_VERSION":"2.20260120.01.00"}"#.into())); }+            Ok(Response{code: self.0, body: self.1.as_bytes().to_vec(), latest_url: req.url.clone(), headers: [("Content-Type".into(), vec!["application/json".into()])].into_iter().collect()})+        }+    }+    assert!(browse::channel_info(&Code(404, "x".repeat(100)), "UC", "en", "US").is_err());+    assert!(browse::channel_info(&Code(200, "{}".into()), "UC", "en", "US").is_err());+    // malformed list video reported as error, unknown skipped (via trending)+    let fixture = json!({"contents":{"twoColumnBrowseResultsRenderer":{"tabs":[{"tabRenderer":{"content":{"richGridRenderer":{"contents":[+        {"richItemRenderer":{"content":{"videoRenderer":{"title":{"simpleText":"no id"}}}}},+        {"richItemRenderer":{"content":{"someFutureRenderer":{"x":1}}}},+        {"richItemRenderer":{"content": vid("aaaaaaaaaaa","Good")}}+    ]}}}}]}}}).to_string();+    let page = browse::trending(&Client{browse: fixture, ..Default::default()}, "en", "US").unwrap();+    assert_eq!(page.items.len(), 1);+    assert_eq!(page.errors.len(), 1);+}
youpipe/src/main/rust/tests/coverage_stream_search.rs
diff --git a/youpipe/src/main/rust/tests/coverage_stream_search.rs b/youpipe/src/main/rust/tests/coverage_stream_search.rsnew file mode 100644index 0000000000..807cd34a7c--- /dev/null+++ b/youpipe/src/main/rust/tests/coverage_stream_search.rs@@ -0,0 +1,244 @@+//! Coverage for stream.rs / stream_part1.rs / search.rs via public API.+use std::sync::{Mutex, MutexGuard};+use serde_json::json;+use youpipe_extractor::decoder::clear_caches;+use youpipe_extractor::http::{HttpClient, Request, Response, Result};+use youpipe_extractor::innertube::reset_client_version;+use youpipe_extractor::model::{SearchItem, StreamInfo, StreamKind};+use youpipe_extractor::{search, stream};++static LOCK: Mutex<()> = Mutex::new(());+fn lock() -> MutexGuard<'static, ()> { let g = LOCK.lock().unwrap_or_else(|e| e.into_inner()); reset_client_version(); clear_caches(); g }+fn resp(url: &str, body: String) -> Response { Response { code: 200, body: body.into_bytes(), latest_url: url.to_string(), headers: [("Content-Type".into(), vec!["application/json".into()])].into_iter().collect() } }++#[derive(Default, Clone)]+struct Client { player: String, next: String, search: String, search_cont: String }+impl HttpClient for Client {+    fn execute(&self, req: Request<'_>) -> Result<Response> {+        if req.url.contains("sw.js") { return Ok(resp(&req.url, r#"{"INNERTUBE_CONTEXT_CLIENT_VERSION":"2.20260120.01.00"}"#.into())); }+        if req.url.contains("latest-player") { return Ok(resp(&req.url, r#"{"player":"testplayer","signatureTimestamp":12345}"#.into())); }+        if req.url.contains("decoder/decode") {+            // extract sig= or n= value+            let val = req.url.split(&['=', '&'][..]).filter(|s| !s.is_empty()).last().unwrap_or("x").to_string();+            // find param value: after sig= or n=+            let v = if let Some(i) = req.url.find("sig=") { req.url[i+4..].split('&').next().unwrap_or("").to_string() } else if let Some(i) = req.url.find("&n=") { req.url[i+3..].split('&').next().unwrap_or("").to_string() } else if let Some(i) = req.url.find("?n=") { req.url[i+3..].split('&').next().unwrap_or("").to_string() } else { val };+            let body = json!({"type":"result","responses":[{"type":"result","data":{ v.clone(): format!("decoded-{v}") }}]}).to_string();+            return Ok(resp(&req.url, body));+        }+        let body = req.body.map(|b| String::from_utf8_lossy(b).into_owned()).unwrap_or_default();+        let is_cont = body.contains("\"continuation\"");+        if req.url.contains("youtubei/v1/player") { return Ok(resp(&req.url, self.player.clone())); }+        if req.url.contains("youtubei/v1/next") { return Ok(resp(&req.url, self.next.clone())); }+        if req.url.contains("youtubei/v1/search") { return Ok(resp(&req.url, if is_cont { self.search_cont.clone() } else { self.search.clone() })); }+        Ok(resp(&req.url, "{}".into()))+    }+}++fn player_fixture() -> String {+    json!({+      "playabilityStatus":{"status":"OK"},+      "videoDetails":{"title":"Test Vid","lengthSeconds":"214","viewCount":"1234567","author":"Uploader","channelId":"UC12345678901234567890","shortDescription":"desc","thumbnail":{"thumbnails":[{"url":"https://i.ytimg.com/t.jpg","width":120,"height":90}]},"keywords":["a","b"]},+      "microformat":{"playerMicroformatRenderer":{"uploadDate":"2024-01-02","category":"Music","isFamilySafe":true,"likeCount":"999"}},+      "streamingData":{"dashManifestUrl":"https://example.com/dash","hlsManifestUrl":"https://example.com/hls",+        "formats":[{"itag":18,"mimeType":"video/mp4; codecs=\"avc1.64001F\"","url":"https://example.com/v18?itag=18","width":640,"height":360,"bitrate":500000,"qualityLabel":"360p","contentLength":"12345","fps":30}],+        "adaptiveFormats":[+          {"itag":140,"mimeType":"audio/mp4; codecs=\"mp4a.40.2\"","url":"https://example.com/a140?itag=140","bitrate":130000,"averageBitrate":128000,"audioSampleRate":"44100","audioChannels":2,"audioTrack":{"id":"en.4","displayName":"English"},"contentLength":"999","initRange":{"start":0,"end":10},"indexRange":{"start":11,"end":20}},+          {"itag":137,"mimeType":"video/mp4; codecs=\"avc1.640028\"","url":"https://example.com/v137?itag=137&n=XYZABC","width":1920,"height":1080,"qualityLabel":"1080p","bitrate":4000000},+          {"itag":999,"mimeType":"text/plain","url":"https://example.com/x"},+          {"itag":313,"mimeType":"video/webm"}+        ]}+    }).to_string()+}++#[test]+fn stream_info_full() {+    let _g = lock();+    let info = stream::stream_info(&Client{player: player_fixture(), ..Default::default()}, "aaaaaaaaaaa", "en", "US").unwrap();+    assert_eq!(info.name, "Test Vid");+    assert_eq!(info.duration_seconds, 214);+    assert_eq!(info.view_count, 1234567);+    assert_eq!(info.like_count, 999);+    assert_eq!(info.kind, StreamKind::Video);+    assert_eq!(info.age_limit, 0);+    assert_eq!(info.tags, vec!["a".to_string(), "b".to_string()]);+    assert_eq!(info.video_streams.len(), 1);+    assert_eq!(info.video_streams[0].codec.as_deref(), Some("avc1.64001F"));+    assert_eq!(info.audio_streams.len(), 1);+    assert_eq!(info.audio_streams[0].sample_rate, 44100);+    assert_eq!(info.audio_streams[0].track_id.as_deref(), Some("en.4"));+    assert_eq!(info.video_only_streams.len(), 1);+    // n param decoded+    assert!(info.video_only_streams[0].url.contains("decoded-XYZABC"), "url {}", info.video_only_streams[0].url);+    assert!(!info.sabr_only);+    assert_eq!(info.dash_manifest_url.as_deref(), Some("https://example.com/dash"));+}++#[test]+fn stream_info_live_postlive_and_sabr() {+    let _g = lock();+    let live = json!({"playabilityStatus":{"status":"OK","liveStreamability":{}},"videoDetails":{"title":"Live","isLiveContent":true},"microformat":{"playerMicroformatRenderer":{"isFamilySafe":false,"publishDate":"2024-01-01"}},"streamingData":{"serverAbrStreamingUrl":"https://example.com/sabr","adaptiveFormats":[{"itag":313,"mimeType":"video/webm"}]}}).to_string();+    let info = stream::stream_info(&Client{player: live, ..Default::default()}, "bbbbbbbbbbb", "en", "US").unwrap();+    assert_eq!(info.kind, StreamKind::Live);+    assert_eq!(info.age_limit, 18);+    assert!(info.sabr_only);+    assert_eq!(info.server_abr_streaming_url.as_deref(), Some("https://example.com/sabr"));+    assert_eq!(info.view_count, -1);+    assert_eq!(info.like_count, -1);+    // post-live DVR via isPostLiveDvr, also live via playability liveStreamability already covered; use DVR flag+    let dvr = json!({"videoDetails":{"title":"DVR","isPostLiveDvr":true},"microformat":{"playerMicroformatRenderer":{}},"streamingData":{}}).to_string();+    let info2 = stream::stream_info(&Client{player: dvr, ..Default::default()}, "ccccccccccc", "en", "US").unwrap();+    assert_eq!(info2.kind, StreamKind::PostLive);+    let dvr2 = json!({"videoDetails":{"title":"DVR2","isLiveDvrEnabled":true},"microformat":{"playerMicroformatRenderer":{}},"streamingData":{}}).to_string();+    assert_eq!(stream::stream_info(&Client{player: dvr2, ..Default::default()}, "ccccccccccc", "en", "US").unwrap().kind, StreamKind::PostLive);+}++#[test]+fn stream_info_signature_cipher_and_errors() {+    let _g = lock();+    let fixture = json!({+      "videoDetails":{"title":"Cipher"},+      "microformat":{"playerMicroformatRenderer":{}},+      "streamingData":{"formats":[+        {"itag":22,"mimeType":"video/mp4","signatureCipher":"s=SIGVAL&sp=sig&url=https%3A%2F%2Fexample.com%2Fvideoplayback%3Fitag%3D22"},+        {"itag":23,"mimeType":"video/mp4","cipher":"s=SIG2&url=https%3A%2F%2Fexample.com%2Fv%3Fx%3D1"},+        {"itag":24,"mimeType":"video/mp4","signatureCipher":"s=NONE"}+      ],"adaptiveFormats":[+        {"itag":140,"mimeType":"audio/webm; codecs=\"opus\"","url":"https://example.com/a?x=1"}+      ]}+    }).to_string();+    let info = stream::stream_info(&Client{player: fixture, ..Default::default()}, "ddddddddddd", "en", "US").unwrap();+    assert_eq!(info.video_streams.len(), 2);+    assert!(info.video_streams[0].url.contains("decoded-SIGVAL"));+    assert!(info.video_streams[0].url.contains("sig=decoded-SIGVAL"));+    // second uses default sp=signature+    assert!(info.video_streams[1].url.contains("signature=decoded-SIG2"));+    assert_eq!(info.errors.len(), 1, "missing url in cipher is an error entry: {:?}", info.errors);+}++#[test]+fn stream_playability_via_public_api() {+    let _g = lock();+    for (status, reason, expect) in [+        ("LOGIN_REQUIRED", "inappropriate for some users", "age-restricted"),+        ("LOGIN_REQUIRED", "This video is private", "private"),+        ("LOGIN_REQUIRED", "Sign in to confirm you're not a bot", "blocking anonymous"),+        ("LOGIN_REQUIRED", "something else", "login required"),+        ("UNPLAYABLE", "requires Music Premium", "Music Premium"),+        ("UNPLAYABLE", "payment required", "paid"),+        ("UNPLAYABLE", "members only", "members-only"),+        ("UNPLAYABLE", "not available in your country", "country"),+        ("ERROR", "This account has been terminated", "terminated"),+        ("ERROR", "account closed", "terminated"),+        ("ERROR", "weird failure", "weird"),+    ] {+        let fixture = json!({"playabilityStatus":{"status":status,"reason":reason}}).to_string();+        let err = stream::stream_info(&Client{player: fixture, ..Default::default()}, "eeeeeeeeeee", "en", "US").unwrap_err().to_string();+        assert!(err.contains(expect), "{status}/{reason} -> {err} expected {expect}");+    }+    // reason via messages array+    let fixture = json!({"playabilityStatus":{"status":"UNPLAYABLE","messages":[{"simpleText":"not available in your country"}]}}).to_string();+    let err = stream::stream_info(&Client{player: fixture, ..Default::default()}, "eeeeeeeeeee", "en", "US").unwrap_err().to_string();+    assert!(err.contains("country"), "{err}");+    // case-insensitive OK+    let ok = json!({"playabilityStatus":{"status":"ok"},"videoDetails":{"title":"x"},"microformat":{"playerMicroformatRenderer":{}},"streamingData":{}}).to_string();+    assert!(stream::stream_info(&Client{player: ok, ..Default::default()}, "eeeeeeeeeee", "en", "US").is_ok());+}++#[test]+fn augment_from_next_full() {+    let _g = lock();+    let next = json!({"contents":{"twoColumnWatchNextResults":{"results":{"results":{"contents":[+      {"videoPrimaryInfoRenderer":{"viewCount":{"videoViewCountRenderer":{"viewCount":{"simpleText":"5,678 views"}}},"videoActions":{"menuRenderer":{"topLevelButtons":[{"segmentedLikeDislikeButtonViewModel":{"likeButtonViewModel":{"likeButtonViewModel":{"toggleButtonViewModel":{"toggleButtonViewModel":{"defaultButtonViewModel":{"buttonViewModel":{"accessibilityText":"like this video along with 1,234,567 other people"}}}}}}}}]}}}},+      {"videoSecondaryInfoRenderer":{"owner":{"videoOwnerRenderer":{"badges":[{"metadataBadgeRenderer":{"style":"BADGE_STYLE_TYPE_VERIFIED"}}],"subscriberCountText":{"simpleText":"2.5M subscribers"},"navigationEndpoint":{"commandMetadata":{"webCommandMetadata":{"url":"/@chan"}}},"thumbnail":{"thumbnails":[{"url":"https://i.ytimg.com/av.jpg","width":40,"height":40}]}}}}}+    ]}}}}}).to_string();+    let mut info = StreamInfo::default();+    info.view_count = -1; info.like_count = -1;+    stream::augment_from_next(&Client{next: next.clone(), ..Default::default()}, "aaaaaaaaaaa", "en", "US", &mut info).unwrap();+    assert_eq!(info.view_count, 5678);+    assert_eq!(info.like_count, 1234567);+    assert!(info.uploader_verified);+    assert_eq!(info.uploader_subscriber_count, 2_500_000);+    assert_eq!(info.uploader_url.as_deref(), Some("https://www.youtube.com/@chan"));+    assert_eq!(info.uploader_avatars.len(), 1);+    // already-set counts not overwritten, likes extractor returns -1 when no buttons+    let next2 = json!({"contents":{"twoColumnWatchNextResults":{"results":{"results":{"contents":[{"videoPrimaryInfoRenderer":{"viewCount":{"videoViewCountRenderer":{"viewCount":{"simpleText":"9 views"}}}}}]}}}}}).to_string();+    let mut info2 = StreamInfo::default(); info2.view_count = 111; info2.like_count = 222; info2.uploader_url = Some("https://www.youtube.com/channel/UC123".into());+    stream::augment_from_next(&Client{next: next2, ..Default::default()}, "aaaaaaaaaaa", "en", "US", &mut info2).unwrap();+    assert_eq!(info2.view_count, 111); assert_eq!(info2.like_count, 222);+}++#[test]+fn search_all_item_types_and_filters() {+    let _g = lock();+    assert_eq!(search::filter_params(Some("videos")), Some(search::FILTER_VIDEOS));+    assert_eq!(search::filter_params(Some("channels")), Some(search::FILTER_CHANNELS));+    assert_eq!(search::filter_params(Some("playlists")), Some(search::FILTER_PLAYLISTS));+    assert_eq!(search::filter_params(Some("bogus")), None);+    assert_eq!(search::filter_params(None), None);+    let fixture = json!({"contents":{"twoColumnSearchResultsRenderer":{"primaryContents":{"sectionListRenderer":{"contents":[+      {"itemSectionRenderer":{"contents":[+        {"backgroundPromoRenderer":{}},+        {"channelRenderer":{"channelId":"UC12345678901234567890","title":{"simpleText":"Chan"},"subscriberCountText":{"simpleText":"1.2M subscribers"},"videoCountText":{"simpleText":"42 videos"},"descriptionSnippet":{"simpleText":"chan desc"},"thumbnail":{"thumbnails":[{"url":"https://i.ytimg.com/c.jpg","width":10,"height":10}]},"ownerBadges":[{"metadataBadgeRenderer":{"style":"BADGE_STYLE_TYPE_VERIFIED"}}]}},+        {"playlistRenderer":{"playlistId":"PLabc","title":{"simpleText":"My Playlist"},"videoCount":"17","longBylineText":{"simpleText":"Uploader"},"thumbnail":{"thumbnails":[{"url":"https://i.ytimg.com/p.jpg","width":10,"height":10}]}}},+        {"lockupViewModel":{"contentType":"LOCKUP_CONTENT_TYPE_VIDEO","contentId":"aaaaaaaaaaa","metadata":{"lockupMetadataViewModel":{"title":{"content":"Lock Vid"}}}}},+        {"lockupViewModel":{"contentType":"LOCKUP_CONTENT_TYPE_PLAYLIST","contentId":"PLxyz","metadata":{"lockupMetadataViewModel":{"title":{"content":"Lock PL"}}}}},+        {"lockupViewModel":{"contentType":"LOCKUP_CONTENT_TYPE_PODCAST","contentId":"PLpod","metadata":{"lockupMetadataViewModel":{"title":{"content":"Pod"}}}}},+        {"lockupViewModel":{"contentType":"LOCKUP_CONTENT_TYPE_CHANNEL","contentId":"UCx","metadata":{"lockupMetadataViewModel":{"title":{"content":"?"}}}}},+        {"lockupViewModel":{"contentType":"LOCKUP_CONTENT_TYPE_VIDEO","metadata":{}}},+        {"videoRenderer":{"videoId":"bbbbbbbbbbb","title":{"simpleText":"Live Vid"},"badges":[{"metadataBadgeRenderer":{"style":"BADGE_STYLE_TYPE_LIVE_NOW"}}]}},+        {"videoRenderer":{"title":{"simpleText":"no id"}}},+        {"channelRenderer":{"title":{"simpleText":"no id"}}},+        {"playlistRenderer":{"title":{"simpleText":"no id"}}},+        {"someAdRenderer":{}}+      ]}},+      {"continuationItemRenderer":{"continuationEndpoint":{"continuationCommand":{"token":"SEARCHTOK"}}}}+    ]}}}}}).to_string();+    for filter in [None, Some("videos"), Some("channels"), Some("playlists"), Some("bogus")] {+        let r = search::search(&Client{search: fixture.clone(), ..Default::default()}, "q", filter, "en", "US").unwrap();+        assert_eq!(r.next_page_token.as_deref(), Some("SEARCHTOK"));+        // 1 channel + 1 playlist + 3 lockups + 1 live video = 6 good, errors: unknown lockup, missing-id lockup, video, channel, playlist =5+        assert_eq!(r.items.len(), 6, "filter {filter:?} items {:?}", r.items);+        assert_eq!(r.errors.len(), 5, "{:?}", r.errors);+        match &r.items[0] { SearchItem::Channel(ch) => { assert_eq!(ch.subscriber_count, 1_200_000, "subscriber count from subscriberCountText"); assert_eq!(ch.stream_count, 42); assert!(ch.verified); } _ => panic!("channel first") }+        match &r.items[1] { SearchItem::Playlist(p) => assert_eq!(p.stream_count, 17), _ => panic!("playlist") }+        match &r.items[5] { SearchItem::Stream(s) => assert!(s.is_live), _ => panic!("live stream") }+    }+}++#[test]+fn search_suggestion_didyoumean_and_page() {+    let _g = lock();+    let fixture = json!({"contents":{"twoColumnSearchResultsRenderer":{"primaryContents":{"sectionListRenderer":{"contents":[+      {"itemSectionRenderer":{"contents":[{"didYouMeanRenderer":{"correctedQuery":{"runs":[{"text":"did you mean cats"}]}}}]}}+    ]}}}}}).to_string();+    let r = search::search(&Client{search: fixture, ..Default::default()}, "q", None, "en", "US").unwrap();+    assert_eq!(r.search_suggestion.as_deref(), Some("did you mean cats"));+    // search_page via continuation+    let cont = json!({"onResponseReceivedCommands":[{"appendContinuationItemsAction":{"continuationItems":[+        {"itemSectionRenderer":{"contents":[{"videoRenderer":{"videoId":"aaaaaaaaaaa","title":{"simpleText":"Paged"}}}]}},+        {"continuationItemRenderer":{"continuationEndpoint":{"continuationCommand":{"token":"PAGE2"}}}}+    ]}}]}).to_string();+    let r2 = search::search_page(&Client{search_cont: cont, ..Default::default()}, "tok", "en", "US").unwrap();+    assert_eq!(r2.items.len(), 1);+    assert_eq!(r2.next_page_token.as_deref(), Some("PAGE2"));+    // empty continuation response+    let r3 = search::search_page(&Client{search_cont: json!({"onResponseReceivedCommands":[],"padding":"xxxxxxxxxxxxxxxxxxxxxxxx"}).to_string(), ..Default::default()}, "tok", "en", "US").unwrap();+    assert!(r3.items.is_empty());+}++#[test]+fn search_response_validation() {+    let _g = lock();+    struct Custom(u16, String, String);+    impl HttpClient for Custom {+        fn execute(&self, req: Request<'_>) -> Result<Response> {+            if req.url.contains("sw.js") { return Ok(resp(&req.url, r#"{"INNERTUBE_CONTEXT_CLIENT_VERSION":"2.20260120.01.00"}"#.into())); }+            Ok(Response{code: self.0, body: self.1.as_bytes().to_vec(), latest_url: req.url.clone(), headers: [("Content-Type".into(), vec![self.2.clone()])].into_iter().collect()})+        }+    }+    assert!(search::search(&Custom(404, "x".repeat(100), "application/json".into()), "q", None, "en", "US").is_err());+    assert!(search::search(&Custom(200, "{}".into(), "application/json".into()), "q", None, "en", "US").is_err());+    assert!(search::search(&Custom(200, "x".repeat(100), "text/html".into()), "q", None, "en", "US").is_err());+    let bad_json = "not json at all padding padding padding padding padding padding";+    assert!(search::search(&Custom(200, bad_json.into(), "application/json".into()), "q", None, "en", "US").is_err());+}