-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_error_test.go
More file actions
135 lines (119 loc) · 3.2 KB
/
Copy pathhandler_error_test.go
File metadata and controls
135 lines (119 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package telegram
import (
"fmt"
"strings"
"testing"
)
// TestHandleCommand_ErrorSentToUser verifies that when a command handler
// returns an error, the error message is sent to the user (not just logged).
func TestHandleCommand_ErrorSentToUser(t *testing.T) {
rec := new(requestRecorder)
ts := testServer(t, rec)
defer ts.Close()
bot := testBot(t, ts)
h := NewHandler(bot)
h.Config.AllowAllUsers = true // routing test
h.OnCommand = func(chatID int64, messageID int, cmd string, args string, _ int64) (string, error) {
return "", fmt.Errorf("simulated command failure: %s", cmd)
}
upd := Update{
ID: 1,
Message: &Message{
ID: 42,
Chat: &Chat{ID: 123},
From: &User{ID: 456},
Text: "/start",
Entities: []MessageEntity{
{Type: "bot_command", Offset: 0, Length: 6},
},
},
}
h.HandleUpdate(upd)
// Verify a sendMessage was sent with the error text.
found := false
for _, r := range rec.all() {
if strings.Contains(r.Path, "sendMessage") {
if strings.Contains(r.Body, "simulated command failure") {
found = true
break
}
}
}
if !found {
t.Error("error from command handler was not sent to user")
}
}
// TestHandleCallback_ErrorSentToUser verifies that when a callback query
// handler returns an error, the error message is sent to the user.
func TestHandleCallback_ErrorSentToUser(t *testing.T) {
rec := new(requestRecorder)
ts := testServer(t, rec)
defer ts.Close()
bot := testBot(t, ts)
h := NewHandler(bot)
h.Config.AllowAllUsers = true // routing test
h.OnCallbackQuery = func(chatID int64, data string, userID int64) (string, error) {
return "", fmt.Errorf("simulated callback failure: %s", data)
}
upd := Update{
ID: 2,
CallbackQuery: &CallbackQuery{
ID: "cq_test",
From: &User{ID: 456},
Message: &Message{
Chat: &Chat{ID: 789},
},
Data: "test_data",
},
}
h.HandleUpdate(upd)
// Verify a sendMessage was sent with the error text.
found := false
for _, r := range rec.all() {
if strings.Contains(r.Path, "sendMessage") {
if strings.Contains(r.Body, "simulated callback failure") {
found = true
break
}
}
}
if !found {
t.Error("error from callback handler was not sent to user")
}
}
// TestHandleCommand_ErrorNotSentOnSuccess verifies that successful commands
// still send only the response text (not double-sending).
func TestHandleCommand_ErrorNotSentOnSuccess(t *testing.T) {
rec := new(requestRecorder)
ts := testServer(t, rec)
defer ts.Close()
bot := testBot(t, ts)
h := NewHandler(bot)
h.Config.AllowAllUsers = true // routing test
h.OnCommand = func(chatID int64, messageID int, cmd string, args string, _ int64) (string, error) {
return "ok response", nil
}
upd := Update{
ID: 3,
Message: &Message{
ID: 99,
Chat: &Chat{ID: 111},
From: &User{ID: 222},
Text: "/status",
Entities: []MessageEntity{
{Type: "bot_command", Offset: 0, Length: 7},
},
},
}
h.HandleUpdate(upd)
// Count sendMessage calls — should be exactly 1 (for the response).
sendCount := 0
for _, r := range rec.all() {
if strings.Contains(r.Path, "sendMessage") {
sendCount++
}
}
if sendCount != 1 {
t.Errorf("expected 1 sendMessage, got %d (should not double-send on success)", sendCount)
}
}