mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
062fa7a2bb
- move the shared byte-based middle truncation logic from `core` into `codex-utils-string` - keep token-specific truncation in `codex-core` so rollout can reuse the shared helper in the next stacked PR --------- Co-authored-by: Codex <noreply@openai.com>
72 lines
2.2 KiB
Rust
72 lines
2.2 KiB
Rust
use super::split_string;
|
|
use super::truncate_middle_chars;
|
|
use super::truncate_middle_with_token_budget;
|
|
use pretty_assertions::assert_eq;
|
|
|
|
#[test]
|
|
fn split_string_works() {
|
|
assert_eq!(split_string("hello world", 5, 5), (1, "hello", "world"));
|
|
assert_eq!(split_string("abc", 0, 0), (3, "", ""));
|
|
}
|
|
|
|
#[test]
|
|
fn split_string_handles_empty_string() {
|
|
assert_eq!(split_string("", 4, 4), (0, "", ""));
|
|
}
|
|
|
|
#[test]
|
|
fn split_string_only_keeps_prefix_when_tail_budget_is_zero() {
|
|
assert_eq!(split_string("abcdef", 3, 0), (3, "abc", ""));
|
|
}
|
|
|
|
#[test]
|
|
fn split_string_only_keeps_suffix_when_prefix_budget_is_zero() {
|
|
assert_eq!(split_string("abcdef", 0, 3), (3, "", "def"));
|
|
}
|
|
|
|
#[test]
|
|
fn split_string_handles_overlapping_budgets_without_removal() {
|
|
assert_eq!(split_string("abcdef", 4, 4), (0, "abcd", "ef"));
|
|
}
|
|
|
|
#[test]
|
|
fn split_string_respects_utf8_boundaries() {
|
|
assert_eq!(split_string("πabcπ", 5, 5), (1, "πa", "cπ"));
|
|
|
|
assert_eq!(split_string("πππππ", 1, 1), (5, "", ""));
|
|
assert_eq!(split_string("πππππ", 7, 7), (3, "π", "π"));
|
|
assert_eq!(split_string("πππππ", 8, 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, 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, 8);
|
|
assert_eq!(out, "ππππβ¦8 tokens truncatedβ¦ line with text\n");
|
|
assert_eq!(tokens, Some(16));
|
|
}
|
|
|
|
#[test]
|
|
fn truncate_middle_bytes_handles_utf8_content() {
|
|
let s = "ππππππππππ\nsecond line with text\n";
|
|
let out = truncate_middle_chars(s, 20);
|
|
assert_eq!(out, "ππβ¦21 chars truncatedβ¦with text\n");
|
|
}
|