51 lines
1.8 KiB
Go
51 lines
1.8 KiB
Go
package internal
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
const truncSuffix = "\n\n... (文件过大,截断显示)"
|
|
|
|
// TestTruncateSkillContentRuneBoundary 验证截断点落在多字节序列中间时
|
|
// 逐字节回退到合法 rune 边界,结果不含残缺字节
|
|
func TestTruncateSkillContentRuneBoundary(t *testing.T) {
|
|
limit := 100 * 1024
|
|
// 中文各 3 字节:分别构造截断点落在中文字符第 1、2 字节后的场景
|
|
for _, cutInChar := range []int{1, 2} {
|
|
prefix := strings.Repeat("a", limit-cutInChar)
|
|
content := prefix + "中文内容超出限制"
|
|
got := truncateSkillContent(content, limit)
|
|
body := strings.TrimSuffix(got, truncSuffix)
|
|
if !utf8.ValidString(body) {
|
|
t.Fatalf("截断点落在字符第 %d 字节后, 结果应为合法 UTF-8", cutInChar)
|
|
}
|
|
if !strings.HasPrefix(body, prefix) {
|
|
t.Fatalf("截断点落在字符第 %d 字节后, 不应吞掉 ASCII 前缀", cutInChar)
|
|
}
|
|
if len(body) > limit {
|
|
t.Fatalf("截断点落在字符第 %d 字节后, body 长度 %d 超限 %d", cutInChar, len(body), limit)
|
|
}
|
|
if !strings.HasSuffix(got, truncSuffix) {
|
|
t.Fatalf("截断后应带省略提示后缀, got %q", got[len(got)-40:])
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestTruncateSkillContentEdge 验证边界:内容不超限原样返回,恰在边界截断不误删
|
|
func TestTruncateSkillContentEdge(t *testing.T) {
|
|
limit := 100 * 1024
|
|
short := "短内容"
|
|
if got := truncateSkillContent(short, limit); got != short {
|
|
t.Fatalf("未超限内容应原样返回, got %q", got)
|
|
}
|
|
// 恰好 limit 字节后跟多字节字符:截断点在边界上,不应回退删掉合法内容
|
|
exact := strings.Repeat("a", limit)
|
|
got := truncateSkillContent(exact+"中文", limit)
|
|
body := strings.TrimSuffix(got, truncSuffix)
|
|
if body != exact {
|
|
t.Fatalf("边界截断不应删减合法内容, body 长度 = %d, 期望 %d", len(body), limit)
|
|
}
|
|
}
|