From d64e8a21565c333a2a35ca06984ad460dd2381d5 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 15:25:33 +0100 Subject: 0.2.0: streaming line-range reads read seeks to offset with a buffered reader and consumes over-long lines without keeping them: 16 MB RSS on a 200 MB file, 0.8s to line 999000. Adds CHANGELOG and the versioning policy. --- tests/tools.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) (limited to 'tests/tools.rs') diff --git a/tests/tools.rs b/tests/tools.rs index 0189af8..3df353f 100644 --- a/tests/tools.rs +++ b/tests/tools.rs @@ -76,7 +76,7 @@ fn read_numbers_lines_and_reports_truncation() { let text = tools::run("read", &json!({ "path": path, "limit": 2 })).unwrap(); assert!(text.contains(" 1\tone"), "{text}"); assert!(text.contains(" 2\ttwo"), "{text}"); - assert!(text.contains("2 more line(s); continue with offset=3"), "{text}"); + assert!(text.contains("continue with offset=3"), "{text}"); assert!(!text.contains("three"), "{text}"); } @@ -140,3 +140,64 @@ fn unknown_tool_is_an_error_not_a_panic() { let error = tools::run("teleport", &json!({})).err().unwrap(); assert!(error.contains("unknown tool"), "{error}"); } + +#[test] +fn read_streams_large_files_and_reports_the_next_offset() { + let dir = temp_dir("read-large"); + let path = dir.join("big.txt"); + let mut body = String::new(); + for n in 1..=1000 { + body.push_str(&format!("line {n}\n")); + } + fs::write(&path, body).unwrap(); + + let text = tools::run("read", &json!({ "path": path, "offset": 990, "limit": 5 })).unwrap(); + assert!(text.contains(" 990\tline 990"), "{text}"); + assert!(text.contains(" 994\tline 994"), "{text}"); + assert!(!text.contains("line 995"), "{text}"); + assert!(text.contains("continue with offset=995"), "{text}"); +} + +#[test] +fn read_clips_a_single_enormous_line() { + let dir = temp_dir("read-huge-line"); + let path = dir.join("one-line.txt"); + fs::write(&path, "x".repeat(5 * 1024 * 1024)).unwrap(); + + let text = tools::run("read", &json!({ "path": path })).unwrap(); + assert!(text.starts_with(" 1\tx"), "{text}"); + assert!(text.contains("line truncated"), "{text}"); + // Bounded by the per-line cap, not by the file size. + assert!(text.len() < 1000, "read produced {} bytes", text.len()); +} + +#[test] +fn read_reports_an_offset_past_the_end() { + let dir = temp_dir("read-eof"); + let path = dir.join("short.txt"); + fs::write(&path, "only\n").unwrap(); + let text = tools::run("read", &json!({ "path": path, "offset": 9 })).unwrap(); + assert!(text.contains("fewer than 9 lines"), "{text}"); +} + +#[test] +fn read_refuses_binaries_and_directories() { + let dir = temp_dir("read-binary"); + let binary = dir.join("blob.bin"); + fs::write(&binary, [0u8, 1, 2, 3, 0, 9]).unwrap(); + let error = tools::run("read", &json!({ "path": binary })).err().unwrap(); + assert!(error.contains("binary"), "{error}"); + + let error = tools::run("read", &json!({ "path": dir })).err().unwrap(); + assert!(error.contains("is a directory"), "{error}"); +} + +#[test] +fn read_handles_a_missing_trailing_newline() { + let dir = temp_dir("read-no-newline"); + let path = dir.join("f.txt"); + fs::write(&path, "one\ntwo").unwrap(); + let text = tools::run("read", &json!({ "path": path })).unwrap(); + assert!(text.contains(" 2\ttwo"), "{text}"); + assert!(!text.contains("continue with offset"), "{text}"); +} -- cgit v1.2.3