Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / youpipe / src / main / rust / tests / search_response.rs
search_response.rs120 lines · 4.4 KiBRaw
| 1 | //! End-to-end parse of a real InnerTube search response. |
| 2 | //! |
| 3 | //! `fixtures/search_response.json` is a trimmed capture of a live `youtubei/v1/search` reply for |
| 4 | //! "never gonna give you up" (2026-07-31). It keeps four `videoRenderer` entries — deliberately a |
| 5 | //! mix of verified and unverified channels, and of `/channel/UC…` and `/@handle` uploader URLs — |
| 6 | //! plus a `lockupViewModel` and the continuation section, so it covers the shapes that actually |
| 7 | //! vary between results. |
| 8 | //! |
| 9 | //! Drives the full `search()` path against a stub transport, exercising response validation, |
| 10 | //! section walking and every item parser. Only the socket is faked. |
| 11 | |
| 12 | use youpipe_extractor::http::{Error, HttpClient, Request, Response, Result}; |
| 13 | use youpipe_extractor::model::SearchItem; |
| 14 | use youpipe_extractor::search; |
| 15 | |
| 16 | const FIXTURE: &str = include_str!("fixtures/search_response.json"); |
| 17 | |
| 18 | fn json_response(url: String, body: Vec<u8>) -> Response { |
| 19 | Response { |
| 20 | code: 200, |
| 21 | body, |
| 22 | latest_url: url, |
| 23 | headers: [("Content-Type".to_string(), vec!["application/json".to_string()])] |
| 24 | .into_iter() |
| 25 | .collect(), |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | /// Serves the fixture for the search POST and a canned client version for `sw.js`. |
| 30 | struct StubHttp; |
| 31 | |
| 32 | impl HttpClient for StubHttp { |
| 33 | fn execute(&self, request: Request<'_>) -> Result<Response> { |
| 34 | let body = if request.url.contains("sw.js") { |
| 35 | br#"{"INNERTUBE_CONTEXT_CLIENT_VERSION":"2.20260731.00.00"}"#.to_vec() |
| 36 | } else { |
| 37 | FIXTURE.as_bytes().to_vec() |
| 38 | }; |
| 39 | Ok(json_response(request.url, body)) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | #[test] |
| 44 | fn parses_a_real_search_response() { |
| 45 | let result = search::search(&StubHttp, "never gonna give you up", None, "en-GB", "GB") |
| 46 | .expect("search should succeed"); |
| 47 | |
| 48 | assert!(result.errors.is_empty(), "unexpected parse errors: {:?}", result.errors); |
| 49 | assert_eq!(result.items.len(), 5, "four videoRenderers plus one lockup"); |
| 50 | assert!(result.next_page_token.is_some(), "continuation token should be found"); |
| 51 | |
| 52 | // No spelling correction on this query — the case that threw in the Kotlin port. |
| 53 | assert_eq!(result.search_suggestion, None); |
| 54 | assert!(!result.is_corrected_search); |
| 55 | |
| 56 | let streams: Vec<_> = result |
| 57 | .items |
| 58 | .iter() |
| 59 | .filter_map(|i| match i { |
| 60 | SearchItem::Stream(s) => Some(s), |
| 61 | _ => None, |
| 62 | }) |
| 63 | .collect(); |
| 64 | assert!(streams.len() >= 4); |
| 65 | |
| 66 | let rick = streams |
| 67 | .iter() |
| 68 | .find(|s| s.url.contains("dQw4w9WgXcQ")) |
| 69 | .expect("the canonical result should be present"); |
| 70 | assert!(rick.name.contains("Never Gonna Give You Up")); |
| 71 | assert_eq!(rick.duration_seconds, Some(214)); |
| 72 | assert!(rick.view_count > 1_000_000_000, "view count parsed as {}", rick.view_count); |
| 73 | assert_eq!(rick.uploader_name.as_deref(), Some("Rick Astley")); |
| 74 | assert!(rick.uploader_verified); |
| 75 | assert!(!rick.thumbnails.is_empty()); |
| 76 | assert!(rick.thumbnails.iter().all(|t| t.url.starts_with("https://"))); |
| 77 | |
| 78 | // Both uploader URL shapes YouTube emits must resolve. |
| 79 | assert!( |
| 80 | streams.iter().any(|s| s.uploader_url.as_deref().is_some_and(|u| u.contains("/channel/"))), |
| 81 | "expected at least one /channel/ uploader URL" |
| 82 | ); |
| 83 | |
| 84 | // Unverified channels carry no ownerBadges key at all — must parse, not fail. |
| 85 | assert!( |
| 86 | streams.iter().any(|s| !s.uploader_verified), |
| 87 | "fixture should include an unverified uploader" |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | #[test] |
| 92 | fn a_transport_failure_surfaces_as_an_error_not_a_panic() { |
| 93 | struct Failing; |
| 94 | impl HttpClient for Failing { |
| 95 | fn execute(&self, _: Request<'_>) -> Result<Response> { |
| 96 | Err(Error::Network("offline".into())) |
| 97 | } |
| 98 | } |
| 99 | // client_version falls back to the hardcoded value, then the search POST fails. |
| 100 | assert!(search::search(&Failing, "q", None, "en-GB", "GB").is_err()); |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn an_html_error_page_is_rejected_rather_than_parsed() { |
| 105 | struct HtmlPage; |
| 106 | impl HttpClient for HtmlPage { |
| 107 | fn execute(&self, request: Request<'_>) -> Result<Response> { |
| 108 | Ok(Response { |
| 109 | code: 200, |
| 110 | body: vec![b'x'; 500], |
| 111 | latest_url: request.url, |
| 112 | headers: [("Content-Type".to_string(), vec!["text/html".to_string()])] |
| 113 | .into_iter() |
| 114 | .collect(), |
| 115 | }) |
| 116 | } |
| 117 | } |
| 118 | let err = search::search(&HtmlPage, "q", None, "en-GB", "GB").unwrap_err(); |
| 119 | assert!(err.to_string().contains("HTML"), "got: {err}"); |
| 120 | } |