struct CountingReader<R> {
inner: R,
total_read: std::rc::Rc<std::cell::Cell<usize>>,
}
impl<R: std::io::Read> std::io::Read for CountingReader<R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let n = self.inner.read(buf)?;
self.total_read.set(self.total_read.get() + n);
Ok(n)
}
}
#[test]
fn test_read_line_bounded_oversized_unterminated_reads_are_capped_per_call() {
let max_line_bytes: usize = 64;
let buf_capacity: usize = 256;
let data = vec![b'x'; 200_000];
let total_read = std::rc::Rc::new(std::cell::Cell::new(0));
let counting = CountingReader {
inner: std::io::Cursor::new(data),
total_read: total_read.clone(),
};
let mut reader = std::io::BufReader::with_capacity(buf_capacity, counting);
let mut buf = Vec::new();
let outcome = read_line_bounded(&mut reader, &mut buf, max_line_bytes, false)
.expect("read must not error");
match outcome {
LineRead::OversizedUnterminated { bytes } => {
assert!(
bytes > max_line_bytes,
"must have detected the crossing of the cap, got {bytes}"
);
}
other => panic!("expected OversizedUnterminated, got {other:?}"),
}
assert!(
buf.is_empty(),
"buf must never buffer anything once the line is flagged oversized"
);
let read_bytes = total_read.get();
assert!(
read_bytes <= max_line_bytes + buf_capacity * 4,
"read_line_bounded pulled {read_bytes} bytes from the source for an \
unterminated oversized line — expected at most {} (bounded to the \
cap plus a few buffer refills), not an unbounded scan toward EOF",
max_line_bytes + buf_capacity * 4
);
}
#[tokio::test]
async fn test_oversized_unterminated_line_persists_skip_progress_and_resumes_after_terminator() {
let (rt, _dir) = setup().await;
let max_line_bytes: usize = 256;
let huge_unterminated = "z".repeat(max_line_bytes + 4 * 8 * 1024);
let mut file = NamedTempFile::new().expect("tmpfile");
file.write_all(huge_unterminated.as_bytes())
.expect("write unterminated line");
let path = file.path().to_path_buf();
let initial_file_len = std::fs::metadata(&path).unwrap().len();
assert!(
huge_unterminated.len() > max_line_bytes + 3 * 8 * 1024,
"fixture must extend several reader windows beyond the line cap so the \
first bounded read stops mid-line rather than draining the whole file"
);
let limits = MirrorLimits {
max_bytes_per_pass: MIRROR_MAX_BYTES_PER_PASS,
max_events_per_pass: MIRROR_MAX_EVENTS_PER_PASS,
max_line_bytes,
};
let stats1 = mirror_file_with_limits(&rt, &path, 0, LineTailSource::ClaudeCode, None, limits)
.await
.expect("first pass over an unterminated oversized line");
assert!(
stats1.new_offset > 0 && stats1.new_offset < initial_file_len,
"the first bounded skip must land strictly inside the oversized line \
(not at EOF), so the next pass exercises a genuine mid-line resume \
through the persisted-cursor path rather than a fresh read starting \
after a coincidental full drain"
);
assert_eq!(stats1.scanned, 0);
assert_eq!(stats1.inserted, 0);
assert_eq!(
count_rows(&rt, "session_messages").await,
0,
"no partial/garbage row may be written for an unterminated oversized line"
);
assert_eq!(
cursor_offset(&rt, &path.to_string_lossy()).await,
Some(stats1.new_offset as i64),
"mid-skip progress must survive a daemon restart"
);
assert_ne!(
huge_unterminated.as_bytes()[stats1.new_offset as usize - 1],
b'\n',
"the persisted cursor must sit strictly inside the oversized line, not \
just after a line terminator, to exercise the mid-line resume path"
);
let mut offset = stats1.new_offset;
for _ in 0..32 {
let stats =
mirror_file_with_limits(&rt, &path, offset, LineTailSource::ClaudeCode, None, limits)
.await
.expect("bounded continuation over the same unterminated line");
assert_eq!(stats.scanned, 0);
assert_eq!(stats.inserted, 0);
if stats.new_offset == offset {
break;
}
assert!(stats.new_offset > offset);
offset = stats.new_offset;
}
assert!(offset <= initial_file_len);
let stats2 =
mirror_file_with_limits(&rt, &path, offset, LineTailSource::ClaudeCode, None, limits)
.await
.expect("simulated daemon restart at the stable unterminated tail");
assert_eq!(stats2.new_offset, offset);
assert_eq!(stats2.scanned, 0);
assert_eq!(stats2.inserted, 0);
assert_eq!(count_rows(&rt, "session_messages").await, 0);
let small_after = user_line("uuid-after-huge", "sess-UNTERM", "after");
{
let mut f = std::fs::OpenOptions::new()
.append(true)
.open(&path)
.expect("reopen for append");
writeln!(f).unwrap(); writeln!(f, "{small_after}").unwrap();
}
let file_len = std::fs::metadata(&path).unwrap().len();
let stats3 = mirror_file_with_limits(
&rt,
&path,
stats2.new_offset,
LineTailSource::ClaudeCode,
None,
limits,
)
.await
.expect("third pass once the huge line terminates");
assert_eq!(
stats3.new_offset, file_len,
"once terminated, the skip-and-advance path must clear past the whole \
oversized line plus the following valid line"
);
assert_eq!(stats3.scanned, 1, "only the small trailing line is scanned");
assert_eq!(stats3.inserted, 1);
assert_eq!(count_rows(&rt, "session_messages").await, 1);
}
#[tokio::test]
async fn test_still_growing_partial_line_under_cap_is_unaffected() {
let (rt, _dir) = setup().await;
let small1 = user_line("uuid-g1", "sess-GROW", "first");
let mut file = NamedTempFile::new().expect("tmpfile");
writeln!(file, "{small1}").unwrap();
let partial_prefix = user_line("uuid-g2", "sess-GROW", "second");
file.write_all(partial_prefix.as_bytes())
.expect("write partial line, no trailing newline");
let path = file.path().to_path_buf();
let limits = MirrorLimits::production();
let stats1 = mirror_file_with_limits(&rt, &path, 0, LineTailSource::ClaudeCode, None, limits)
.await
.expect("first pass: complete line + partial trailing line");
assert_eq!(stats1.scanned, 1, "only the complete first line is scanned");
assert_eq!(stats1.inserted, 1);
assert_eq!(
stats1.new_offset,
(small1.len() + 1) as u64,
"cursor must stop right after the first complete line, not consume the partial tail"
);
writeln!(file).unwrap();
let file_len = std::fs::metadata(&path).unwrap().len();
let stats2 = mirror_file_with_limits(
&rt,
&path,
stats1.new_offset,
LineTailSource::ClaudeCode,
None,
limits,
)
.await
.expect("second pass: the previously-partial line now completes");
assert_eq!(stats2.new_offset, file_len);
assert_eq!(stats2.scanned, 1);
assert_eq!(stats2.inserted, 1);
assert_eq!(count_rows(&rt, "session_messages").await, 2);
}
#[tokio::test]
async fn test_large_run_of_blank_lines_is_bounded_and_persists_cursor() {
let (rt, _dir) = setup().await;
let mut file = NamedTempFile::new().expect("tmpfile");
for _ in 0..500 {
writeln!(file).unwrap(); }
let path = file.path().to_path_buf();
let file_len = std::fs::metadata(&path).unwrap().len();
assert_eq!(file_len, 500, "500 one-byte blank lines");
let limits = MirrorLimits {
max_bytes_per_pass: 50,
max_events_per_pass: MIRROR_MAX_EVENTS_PER_PASS,
max_line_bytes: MIRROR_MAX_LINE_BYTES,
};
let stats1 = mirror_file_with_limits(&rt, &path, 0, LineTailSource::ClaudeCode, None, limits)
.await
.expect("first blank-line pass");
assert_eq!(stats1.inserted, 0);
assert_eq!(stats1.scanned, 0, "blank lines never count toward scanned");
assert!(
stats1.new_offset > 0,
"the pass cap must trip after at least one blank line, not read unbounded"
);
assert!(
stats1.new_offset < file_len,
"a bounded pass over an all-blank file must not reach EOF in one call"
);
let stored_offset = cursor_offset(&rt, &path.to_string_lossy()).await;
assert_eq!(
stored_offset,
Some(stats1.new_offset as i64),
"cursor must be persisted even when the pass scanned zero events"
);
let mut offset = stats1.new_offset;
loop {
let stats =
mirror_file_with_limits(&rt, &path, offset, LineTailSource::ClaudeCode, None, limits)
.await
.expect("subsequent blank-line pass");
assert_eq!(stats.inserted, 0);
if stats.new_offset == offset {
break; }
offset = stats.new_offset;
}
assert_eq!(
offset, file_len,
"all blank lines eventually consumed to EOF"
);
}