From e56b03fbaeb8c37f78f0b917c20bc7d93dbfbfe6 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:41:12 +0700 Subject: [PATCH] test(annot): assert URL scan work instead of wall-clock speed (#7695) --- crates/annot/src/links.rs | 7 +++++++ crates/annot/src/tests.rs | 15 ++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/crates/annot/src/links.rs b/crates/annot/src/links.rs index 88ac097b74..570bb7f7d3 100644 --- a/crates/annot/src/links.rs +++ b/crates/annot/src/links.rs @@ -517,6 +517,12 @@ const MAX_URL_CHARS: usize = 2048; /// it would open with a scheme the text doesn't say. Brackets inside a web address belong to it /// when they pair up (`…/wiki/Mercury_(planet)`); an unpaired one ends it. pub fn find_urls(chars: &[char]) -> Vec<(std::ops::Range, String)> { + find_urls_with_scan_observer(chars, |_| {}) +} + +/// Observe each candidate scan offset before reading it. The production caller is a no-op; +/// regression tests assert the work bound directly, independent of machine speed. +pub(crate) fn find_urls_with_scan_observer(chars: &[char], mut observe: impl FnMut(usize)) -> Vec<(std::ops::Range, String)> { let url_char = |c: char| !c.is_whitespace() && !"<>\"'()[]{}".contains(c); // Characters found inside an address but not between words: a `www.` after one is part of // a longer address, and so is an e-mail address after a slash. @@ -542,6 +548,7 @@ pub fn find_urls(chars: &[char]) -> Vec<(std::ops::Range, String)> { too_long = true; break; } + observe(j.saturating_sub(i)); match chars.get(j) { Some('(') => { if depth == 0 { diff --git a/crates/annot/src/tests.rs b/crates/annot/src/tests.rs index 980959d4d9..2a84263cdd 100644 --- a/crates/annot/src/tests.rs +++ b/crates/annot/src/tests.rs @@ -1459,9 +1459,18 @@ fn urls_keep_their_written_scheme_and_paired_brackets() { fn unclosed_brackets_in_text_without_spaces_are_read_once_per_address() { let found = |s: &str| -> Vec { crate::links::find_urls(&s.chars().collect::>()).into_iter().map(|(_, u)| u).collect() }; let text = format!("www.before.org {} www.after.org", "https://a/(".repeat(60_000)); - let started = std::time::Instant::now(); - assert_eq!(found(&text), ["http://www.before.org", "http://www.after.org"]); - assert!(started.elapsed().as_secs() < 5, "{:?}", started.elapsed()); + let chars: Vec = text.chars().collect(); + let mut reads = 0usize; + let mut furthest = 0usize; + let urls = crate::links::find_urls_with_scan_observer(&chars, |offset| { + // Fail on the first over-budget read, rather than waiting for a quadratic scan. + assert!(offset < 2048, "candidate read beyond its 2048-character bound: {offset}"); + reads += 1; + furthest = furthest.max(offset); + }); + assert_eq!(urls.into_iter().map(|(_, u)| u).collect::>(), ["http://www.before.org", "http://www.after.org"]); + assert_eq!(furthest, 2047, "the hostile fixture must exercise the scan boundary"); + assert!(reads <= (60_000 + 2) * 2048, "aggregate candidate work: {reads}"); let long = format!("https://example.com/{} www.after.org", "a".repeat(3000)); assert_eq!(found(&long), ["http://www.after.org"]); let fits = format!("https://example.com/{}", "a".repeat(2000));