fix: remove redundant SplitMessage in Send() per review feedback

WithMaxMessageLength(4000) already ensures msg.Content ≤ 4000 chars
before reaching Send(), making the SplitMessage call redundant.
The HTML expansion safety net (re-split when >4096 after conversion)
is still preserved.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
I Putu Eddy Irawan
2026-03-07 22:01:04 +07:00
parent 11017ac7ba
commit f07dbd1db2
2 changed files with 11 additions and 12 deletions
+4 -8
View File
@@ -237,14 +237,10 @@ func (c *TelegramChannel) Send(ctx context.Context, msg bus.OutboundMessage) err
return nil
}
// Split the raw markdown before converting to HTML so that
// SplitMessage's code-fence-aware logic works correctly and
// we never break HTML tags/entities by splitting converted output.
mdChunks := channels.SplitMessage(msg.Content, 4000)
// Use a queue so that chunks whose HTML expansion still exceeds
// Telegram's 4096-char limit can be re-split until every chunk fits.
queue := append([]string{}, mdChunks...)
// The Manager already splits messages to ≤4000 chars (WithMaxMessageLength),
// so msg.Content is guaranteed to be within that limit. We still need to
// check if HTML expansion pushes it beyond Telegram's 4096-char API limit.
queue := []string{msg.Content}
for len(queue) > 0 {
chunk := queue[0]
queue = queue[1:]
+7 -4
View File
@@ -115,7 +115,11 @@ func TestSend_ShortMessage_SingleCall(t *testing.T) {
assert.Len(t, caller.calls, 1, "short message should result in exactly one SendMessage call")
}
func TestSend_LongMessage_MultipleCalls(t *testing.T) {
func TestSend_LongMessage_SingleCall(t *testing.T) {
// With WithMaxMessageLength(4000), the Manager pre-splits messages before
// they reach Send(). A message at exactly 4000 chars should go through
// as a single SendMessage call (no re-split needed since HTML expansion
// won't exceed 4096 for plain text).
caller := &stubCaller{
callFn: func(ctx context.Context, url string, data *ta.RequestData) (*ta.Response, error) {
return successResponse(t), nil
@@ -123,8 +127,7 @@ func TestSend_LongMessage_MultipleCalls(t *testing.T) {
}
ch := newTestChannel(t, caller)
// Create a message over 4000 chars so it gets split into multiple chunks.
longContent := strings.Repeat("a", 4001)
longContent := strings.Repeat("a", 4000)
err := ch.Send(context.Background(), bus.OutboundMessage{
ChatID: "12345",
@@ -132,7 +135,7 @@ func TestSend_LongMessage_MultipleCalls(t *testing.T) {
})
assert.NoError(t, err)
assert.Greater(t, len(caller.calls), 1, "long message should be split into multiple SendMessage calls")
assert.Len(t, caller.calls, 1, "pre-split message within limit should result in one SendMessage call")
}
func TestSend_HTMLFallback_PerChunk(t *testing.T) {