Skip to content

Commit 5d2006b

Browse files
Copilotchinkan
andcommitted
refactor(utils): rename utils::str to utils::strings to avoid shadowing Rust primitive; fix rag test assertion to use chars().count(); add zero-max edge case test
Co-authored-by: chinkan <16433287+chinkan@users.noreply.github.com> Agent-Logs-Url: https://github.com/chinkan/RustFox/sessions/df38c695-4336-437e-8324-5b6db9d2c378
1 parent 977f1f0 commit 5d2006b

5 files changed

Lines changed: 16 additions & 8 deletions

File tree

src/memory/query_rewriter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn format_history(messages: &[ChatMessage]) -> String {
9494
.iter()
9595
.filter_map(|m| {
9696
m.content.as_ref().map(|c| {
97-
let snippet = crate::utils::str::truncate_chars(c, 200);
97+
let snippet = crate::utils::strings::truncate_chars(c, 200);
9898
format!("{}: {}", m.role, snippet)
9999
})
100100
})

src/memory/rag.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub async fn auto_retrieve_context(
4242
for msg in &results {
4343
if let Some(content) = &msg.content {
4444
let role = &msg.role;
45-
let snippet = crate::utils::str::truncate_chars(content, 300);
45+
let snippet = crate::utils::strings::truncate_chars(content, 300);
4646
block.push_str(&format!("[{}] {}\n", role, snippet));
4747
}
4848
}
@@ -136,8 +136,8 @@ mod tests {
136136
async fn test_auto_retrieve_truncates_long_snippets() {
137137
// Verify the 300-char truncation logic via truncate_chars
138138
let content = "x".repeat(500);
139-
let snippet = crate::utils::str::truncate_chars(&content, 300);
140-
assert_eq!(snippet.len(), 303); // 300 + "..."
139+
let snippet = crate::utils::strings::truncate_chars(&content, 300);
140+
assert_eq!(snippet.chars().count(), 303); // 300 chars + "..."
141141
assert!(snippet.ends_with("..."));
142142
}
143143

@@ -147,7 +147,7 @@ mod tests {
147147
// content longer than 300 bytes with Chinese characters.
148148
// Old &content[..300] would panic here.
149149
let long_chinese = "每日論文摘要(香港時間)人工智能".repeat(25); // ~400 chars, >1200 bytes
150-
let result = crate::utils::str::truncate_chars(&long_chinese, 300);
150+
let result = crate::utils::strings::truncate_chars(&long_chinese, 300);
151151
assert!(result.ends_with("..."), "should be truncated");
152152
assert!(
153153
std::str::from_utf8(result.as_bytes()).is_ok(),

src/platform/tool_notifier.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ pub fn format_args_preview(args_json: &str) -> String {
2929
serde_json::Value::String(s) => s.clone(),
3030
other => other.to_string(),
3131
};
32-
let truncated = crate::utils::str::truncate_chars(&s, 60);
32+
let truncated = crate::utils::strings::truncate_chars(&s, 60);
3333
return format!("\"{}\"", truncated);
3434
}
3535
}
3636
}
3737
}
3838
// Fallback: truncate raw JSON
39-
crate::utils::str::truncate_chars(args_json, 60)
39+
crate::utils::strings::truncate_chars(args_json, 60)
4040
}
4141

4242
/// Manages the live-edited Telegram status message during agent tool execution.

src/utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pub mod str;
1+
pub mod strings;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,12 @@ mod tests {
6060
assert!(result.ends_with("..."));
6161
assert!(std::str::from_utf8(result.as_bytes()).is_ok());
6262
}
63+
64+
#[test]
65+
fn test_truncate_chars_zero_max() {
66+
// max_chars=0: every non-empty string is truncated immediately to "..."
67+
assert_eq!(truncate_chars("hello", 0), "...");
68+
// Empty string is never truncated (loop body never entered)
69+
assert_eq!(truncate_chars("", 0), "");
70+
}
6371
}

0 commit comments

Comments
 (0)