mirror of
https://github.com/tiennm99/goclaw.git
synced 2026-10-14 08:18:19 +00:00
Prevent invalid UTF-8 from being persisted when auto-setting followups by sanitizing message content and truncating by rune count. Add regression tests for emoji truncation and malformed byte sequences.
35 lines
927 B
Go
35 lines
927 B
Go
package cmd
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
func TestTruncateForReminder_TruncatesByRuneAndKeepsUTF8Valid(t *testing.T) {
|
|
input := strings.Repeat("a", 199) + "✌️"
|
|
|
|
got := truncateForReminder(input, 200)
|
|
if !utf8.ValidString(got) {
|
|
t.Fatalf("truncateForReminder() produced invalid UTF-8: %q", got)
|
|
}
|
|
if !strings.HasSuffix(got, "...") {
|
|
t.Fatalf("truncateForReminder() = %q, want suffix ...", got)
|
|
}
|
|
if strings.Contains(got, "\uFFFD") {
|
|
t.Fatalf("truncateForReminder() introduced replacement rune: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestTruncateForReminder_StripsInvalidUTF8(t *testing.T) {
|
|
invalid := string([]byte{'a', 0xef, 0xb8, '.', 'b'})
|
|
|
|
got := truncateForReminder(invalid, 200)
|
|
if !utf8.ValidString(got) {
|
|
t.Fatalf("truncateForReminder() produced invalid UTF-8: %q", got)
|
|
}
|
|
if strings.ContainsRune(got, '\uFFFD') {
|
|
t.Fatalf("truncateForReminder() = %q, want no replacement rune", got)
|
|
}
|
|
}
|