diff --git a/crates/plugin-web/src/fetch.rs b/crates/plugin-web/src/fetch.rs index 12ce6ed..83185a1 100644 --- a/crates/plugin-web/src/fetch.rs +++ b/crates/plugin-web/src/fetch.rs @@ -185,8 +185,9 @@ impl FetchClient { Err(e) => return soft_or_hard(&e), }; let base_url = final_url.clone(); - let rendering = - on_blocking_thread(move || extract_html(&decoded, Some(&base_url), raw)); + let rendering = on_blocking_thread(move || { + extract_html(&decoded, Some(&base_url), raw, max_chars) + }); let Some((markdown, extraction)) = rendering.await else { return Err(did_not_finish()); }; diff --git a/crates/plugin-web/src/fetch/tests-body.rs b/crates/plugin-web/src/fetch/tests-body.rs index 27da9a5..9b02872 100644 --- a/crates/plugin-web/src/fetch/tests-body.rs +++ b/crates/plugin-web/src/fetch/tests-body.rs @@ -73,6 +73,98 @@ async fn gzip_bomb_refused_on_decompressed_count() { assert!(result.contains("exceeds"), "got: {result}"); } +/// The whole page is over the default `max_chars`, so falling back to it would +/// return only the menu at its top. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_short_article_below_a_long_menu_is_returned_and_raw_returns_the_menu() { + let (port, _hits) = spawn_server().await; + let tool = loopback_tool(port); + let url = format!("http://localhost:{port}/archive"); + + let out = tool + .call(serde_json::json!({ "url": url })) + .await + .expect("an archive fetch must succeed") + .text() + .to_owned(); + let (header, body) = split_header(&out); + assert!( + header.contains("extraction: readability") && header.contains("truncated: false"), + "got: {header}" + ); + assert!( + body.contains("The article a reader came for"), + "got: {body}" + ); + assert!(!body.contains("Archive entry"), "got: {body}"); + + let out = tool + .call(serde_json::json!({ "url": url, "raw": true })) + .await + .expect("a raw archive fetch must succeed") + .text() + .to_owned(); + let (header, body) = split_header(&out); + assert!( + header.contains("extraction: raw-html") && header.contains("truncated: true"), + "got: {header}" + ); + assert!(body.contains("Archive entry number 0"), "got: {body}"); + assert!( + !body.contains("The article a reader came for"), + "got: {body}" + ); +} + +/// `readabilityrs` finds no article, so the whole page is returned and cut +/// at `max_chars`, though it is over it. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_page_with_no_article_over_max_chars_returns_the_cut_whole_page() { + let (port, _hits) = spawn_server().await; + let tool = loopback_tool(port); + let url = format!("http://localhost:{port}/topics"); + + let out = tool + .call(serde_json::json!({ "url": url })) + .await + .expect("a topics fetch must succeed") + .text() + .to_owned(); + let (header, body) = split_header(&out); + + assert!( + header.contains("extraction: raw-html") && header.contains("truncated: true"), + "got: {header}" + ); + assert!(body.contains("Forum topic number 0"), "got: {body}"); +} + +/// Trimmed, the whole page fits the default `max_chars`, but the fetch cuts +/// it untrimmed, and its leading spaces alone fill the cut. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_short_article_under_leading_spaces_is_returned_when_they_push_the_page_over() { + let (port, _hits) = spawn_server().await; + let tool = loopback_tool(port); + let url = format!("http://localhost:{port}/padded"); + + let out = tool + .call(serde_json::json!({ "url": url })) + .await + .expect("a padded fetch must succeed") + .text() + .to_owned(); + let (header, body) = split_header(&out); + + assert!( + header.contains("extraction: readability") && header.contains("truncated: false"), + "got: {header}" + ); + assert!( + body.contains("The article a reader came for"), + "got: {body}" + ); +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn text_over_max_chars_is_truncated_on_char_boundary() { let (port, _hits) = spawn_server().await; diff --git a/crates/plugin-web/src/fetch/tests.rs b/crates/plugin-web/src/fetch/tests.rs index e243bbe..2b6302b 100644 --- a/crates/plugin-web/src/fetch/tests.rs +++ b/crates/plugin-web/src/fetch/tests.rs @@ -6,6 +6,7 @@ //! the body tests (size caps, truncation, content types, charsets) sit in //! the child modules and share these fixtures. +use std::fmt::Write as _; use std::io::Write; use std::net::{IpAddr, SocketAddr}; use std::sync::atomic::{AtomicUsize, Ordering}; @@ -158,6 +159,63 @@ async fn large() -> Html { Html(format!("

{filler}

")) } +/// A short article below an archive menu longer than the default +/// `max_chars`. +async fn archive() -> Html { + let mut menu = String::new(); + for i in 0..1_500 { + write!( + menu, + r#"
  • Archive entry number {i}
  • "# + ) + .expect("writing to a String cannot fail"); + } + let prose = + "

    The article a reader came for, which says one thing in plain words.

    ".repeat(5); + Html(format!( + "T\ +

    The article

    {prose}
    " + )) +} + +/// A forum topic list, longer than the default `max_chars`, served only +/// inside `