Files
goclaw/cmd/gateway_consumer_followup_test.go
Tai Nguyen 06de8b41cf fix(cmd): sanitize followup reminder UTF-8 truncation (#609)
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.
2026-03-31 23:14:39 +07:00

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)
}
}