use crate::api::stream_guards::check_loop_watchdog;
const PHRASE: &str = "I'll create the project files and verify everything works:";
fn fires(text: &str, chunk: usize) -> bool {
let mut scan = String::new();
let mut start = 0;
while start < text.len() {
let mut end = (start + chunk).min(text.len());
while !text.is_char_boundary(end) {
end += 1;
}
if check_loop_watchdog(&text[start..end], &mut scan, false) {
return true;
}
start = end;
}
false
}
fn phrase_with_unique_interstitials(n: usize) -> String {
let mut feed = String::new();
for i in 0..n {
feed.push_str(PHRASE);
feed.push('\n');
for j in 0..40 {
feed.push_str(&format!(
"segment {i}.{j} of the source dump under review\n"
));
}
}
feed
}
const TOKEN_SIZES: std::ops::RangeInclusive<usize> = 1..=16;
#[test]
fn a_repeat_separated_by_kilobytes_of_unique_prose_still_fires() {
let feed = phrase_with_unique_interstitials(5);
assert!(
feed.len() > 3_000,
"the interstitials must exceed the old 3 KB window to be a regression test; got {}",
feed.len()
);
for chunk in TOKEN_SIZES {
assert!(
fires(&feed, chunk),
"5 repeats across large unique interstitials must fire at chunk size {chunk}"
);
}
}
#[test]
fn three_repeats_do_not_fire() {
let feed = phrase_with_unique_interstitials(3);
for chunk in TOKEN_SIZES {
assert!(
!fires(&feed, chunk),
"three repeats are below the threshold and must not stop the stream \
(chunk size {chunk})"
);
}
}
#[test]
fn a_repeat_whose_last_instance_starts_mid_line_still_fires() {
let mut feed = String::new();
for _ in 0..3 {
feed.push_str(PHRASE);
feed.push('\n');
feed.push_str("intermediate prose\n");
}
feed.push_str(PHRASE);
feed.push_str(" let body = vec![];");
for chunk in TOKEN_SIZES {
assert!(
fires(&feed, chunk),
"substring scan must catch a repeated phrase whose last instance \
is mid-line (chunk size {chunk})"
);
}
}
#[test]
fn ordinary_varied_prose_never_fires() {
let mut feed = String::new();
for i in 0..200 {
feed.push_str(&format!(
"Step {i}: inspect the {i}th module and record what it exports.\n"
));
}
assert!(!fires(&feed, 64), "varied prose must not trip the watchdog");
}
#[test]
fn a_repeating_short_line_does_not_fire() {
let feed = "}\n".repeat(50);
assert!(!fires(&feed, 8), "short repeated lines must be ignored");
}