aKode fase 5: rinomina interna (crate akode-*, akode-rs/, akode-cli/, binario akode, AKODE_*, ~/.akode, snapshot)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
165
akode-rs/utils/string/src/truncate/tests.rs
Normal file
165
akode-rs/utils/string/src/truncate/tests.rs
Normal file
@@ -0,0 +1,165 @@
|
||||
use super::split_string;
|
||||
use super::truncate_middle_chars;
|
||||
use super::truncate_middle_with_marker;
|
||||
use super::truncate_middle_with_token_budget;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn split_string_works() {
|
||||
assert_eq!(
|
||||
split_string(
|
||||
"hello world",
|
||||
/*beginning_bytes*/ 5,
|
||||
/*end_bytes*/ 5
|
||||
),
|
||||
(1, "hello", "world")
|
||||
);
|
||||
assert_eq!(
|
||||
split_string("abc", /*beginning_bytes*/ 0, /*end_bytes*/ 0),
|
||||
(3, "", "")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_string_handles_empty_string() {
|
||||
assert_eq!(
|
||||
split_string("", /*beginning_bytes*/ 4, /*end_bytes*/ 4),
|
||||
(0, "", "")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_string_only_keeps_prefix_when_tail_budget_is_zero() {
|
||||
assert_eq!(
|
||||
split_string("abcdef", /*beginning_bytes*/ 3, /*end_bytes*/ 0),
|
||||
(3, "abc", "")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_string_only_keeps_suffix_when_prefix_budget_is_zero() {
|
||||
assert_eq!(
|
||||
split_string("abcdef", /*beginning_bytes*/ 0, /*end_bytes*/ 3),
|
||||
(3, "", "def")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_string_handles_overlapping_budgets_without_removal() {
|
||||
assert_eq!(
|
||||
split_string("abcdef", /*beginning_bytes*/ 4, /*end_bytes*/ 4),
|
||||
(0, "abcd", "ef")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_string_respects_utf8_boundaries() {
|
||||
assert_eq!(
|
||||
split_string("😀abc😀", /*beginning_bytes*/ 5, /*end_bytes*/ 5),
|
||||
(1, "😀a", "c😀")
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
split_string(
|
||||
"😀😀😀😀😀",
|
||||
/*beginning_bytes*/ 1,
|
||||
/*end_bytes*/ 1
|
||||
),
|
||||
(5, "", "")
|
||||
);
|
||||
assert_eq!(
|
||||
split_string(
|
||||
"😀😀😀😀😀",
|
||||
/*beginning_bytes*/ 7,
|
||||
/*end_bytes*/ 7
|
||||
),
|
||||
(3, "😀", "😀")
|
||||
);
|
||||
assert_eq!(
|
||||
split_string(
|
||||
"😀😀😀😀😀",
|
||||
/*beginning_bytes*/ 8,
|
||||
/*end_bytes*/ 8
|
||||
),
|
||||
(1, "😀😀", "😀😀")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_with_token_budget_returns_original_when_under_limit() {
|
||||
let s = "short output";
|
||||
let limit = 100;
|
||||
let (out, original) = truncate_middle_with_token_budget(s, limit);
|
||||
assert_eq!(out, s);
|
||||
assert_eq!(original, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_with_token_budget_reports_truncation_at_zero_limit() {
|
||||
let s = "abcdef";
|
||||
let (out, original) = truncate_middle_with_token_budget(s, /*max_tokens*/ 0);
|
||||
assert_eq!(out, "…2 tokens truncated…");
|
||||
assert_eq!(original, Some(2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_middle_tokens_handles_utf8_content() {
|
||||
let s = "😀😀😀😀😀😀😀😀😀😀\nsecond line with text\n";
|
||||
let (out, tokens) = truncate_middle_with_token_budget(s, /*max_tokens*/ 8);
|
||||
assert_eq!(out, "😀😀😀😀…8 tokens truncated… line with text\n");
|
||||
assert_eq!(tokens, Some(16));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_middle_tokens_keeps_partial_characters_out_of_both_ends() {
|
||||
for (input, expected) in [
|
||||
("a😀b😀c", "a…2 tokens truncated…c"),
|
||||
("é中😀é", "é…2 tokens truncated…é"),
|
||||
("😀😀😀", "…2 tokens truncated…"),
|
||||
] {
|
||||
assert_eq!(
|
||||
truncate_middle_with_token_budget(input, /*max_tokens*/ 1),
|
||||
(expected.to_string(), Some(3)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_middle_bytes_handles_utf8_content() {
|
||||
let s = "😀😀😀😀😀😀😀😀😀😀\nsecond line with text\n";
|
||||
let out = truncate_middle_chars(s, /*max_bytes*/ 20);
|
||||
assert_eq!(out, "😀😀…21 chars truncated…with text\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_middle_with_marker_enforces_the_total_byte_limit() {
|
||||
let input = format!("head{}tail", "x".repeat(100));
|
||||
assert_eq!(
|
||||
truncate_middle_with_marker(&input, /*max_bytes*/ 32, "<truncated>"),
|
||||
"headxxxxxx<truncated>xxxxxxxtail"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_middle_with_marker_preserves_utf8_boundaries() {
|
||||
assert_eq!(
|
||||
truncate_middle_with_marker("head🦀🦀🦀🦀🦀tail", /*max_bytes*/ 18, "..."),
|
||||
"head...🦀tail"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_middle_with_marker_returns_input_that_fits() {
|
||||
assert_eq!(
|
||||
truncate_middle_with_marker("short", /*max_bytes*/ 5, "..."),
|
||||
"short"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_middle_with_marker_keeps_a_prefix_when_the_marker_does_not_fit() {
|
||||
assert_eq!(
|
||||
truncate_middle_with_marker("🦀abc", /*max_bytes*/ 4, "marker"),
|
||||
"🦀"
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user