mirror of
https://github.com/tiennm99/openai-status-bot.git
synced 2026-07-14 11:08:46 +00:00
c119386073
Fixed-index truncation could split an HTML tag or entity or drop a closing tag, making Telegram reject the whole message with 'can't parse entities'. Cut on a newline boundary where tags are balanced, reserve room for a truncation notice, and strip any trailing partial markup.
77 lines
2.3 KiB
Go
77 lines
2.3 KiB
Go
package bot
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestTruncateMessageKeepsHTMLValidOnNewlineBoundary(t *testing.T) {
|
|
var b strings.Builder
|
|
for b.Len() <= telegramMessageLimit {
|
|
b.WriteString("- <b>Component</b>: <code>operational</code>\n")
|
|
}
|
|
out := truncateMessage(b.String())
|
|
|
|
if len([]rune(out)) > telegramMessageLimit {
|
|
t.Fatalf("len = %d, want <= %d", len([]rune(out)), telegramMessageLimit)
|
|
}
|
|
if !strings.HasSuffix(out, truncationNotice) {
|
|
t.Fatalf("missing truncation notice: %q", out[len(out)-40:])
|
|
}
|
|
body := strings.TrimSuffix(out, truncationNotice)
|
|
// Newline-boundary cut must leave equal numbers of opening/closing tags.
|
|
for _, tag := range []string{"b", "code"} {
|
|
if open, close := strings.Count(body, "<"+tag+">"), strings.Count(body, "</"+tag+">"); open != close {
|
|
t.Fatalf("unbalanced <%s>: %d open, %d close", tag, open, close)
|
|
}
|
|
}
|
|
if strings.Contains(body, "<co") && !strings.Contains(body, "<code>") {
|
|
t.Fatal("body ends inside a tag")
|
|
}
|
|
}
|
|
|
|
func TestNormalizeCommandStripsOwnBotUsername(t *testing.T) {
|
|
command, fields := normalizeCommand("/history@OpenAIStatusBot 10", "OpenAIStatusBot")
|
|
if command != "/history" {
|
|
t.Fatalf("command = %q, want /history", command)
|
|
}
|
|
if len(fields) != 2 || fields[1] != "10" {
|
|
t.Fatalf("fields = %#v", fields)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeCommandIgnoresOtherBotUsername(t *testing.T) {
|
|
command, _ := normalizeCommand("/start@OtherBot", "OpenAIStatusBot")
|
|
if command != "" {
|
|
t.Fatalf("command = %q, want empty", command)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeCommandIgnoresTargetedCommandWhenUsernameUnknown(t *testing.T) {
|
|
command, _ := normalizeCommand("/start@OtherBot", "")
|
|
if command != "" {
|
|
t.Fatalf("command = %q, want empty", command)
|
|
}
|
|
}
|
|
|
|
func TestParseHistoryCount(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
fields []string
|
|
want int
|
|
}{
|
|
{name: "default", fields: []string{"/history"}, want: 5},
|
|
{name: "valid", fields: []string{"/history", "3"}, want: 3},
|
|
{name: "invalid", fields: []string{"/history", "abc"}, want: 5},
|
|
{name: "max", fields: []string{"/history", "99"}, want: 10},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := parseHistoryCount(tt.fields); got != tt.want {
|
|
t.Fatalf("parseHistoryCount(%v) = %d, want %d", tt.fields, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|