feat: enhance Safew chat handling and testing
- Updated `GroupsFromUpdates` function to return the number of updates processed, improving the polling mechanism. - Added a new test `TestGroupsFromUpdatesCallbackAndJoinRequest` to validate handling of callback queries and join requests. - Introduced `safewAllowedUpdates` to specify allowed update types in the polling request, enhancing chat management. - Implemented `ensureWebhookCleared` method to manage webhook state before polling, improving reliability.
This commit is contained in:
@@ -18,7 +18,7 @@ func TestGroupsFromUpdatesKeepsGroupsDropsPrivate(t *testing.T) {
|
||||
{"update_id": 100000003, "my_chat_member": {"chat": {"id": 22, "type": "supergroup", "title": "SG", "username": "sg_name"}}}
|
||||
]
|
||||
}`)
|
||||
chats, maxID, err := GroupsFromUpdates(body)
|
||||
chats, maxID, _, err := GroupsFromUpdates(body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -48,6 +48,26 @@ func TestGroupsFromUpdatesKeepsGroupsDropsPrivate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGroupsFromUpdatesCallbackAndJoinRequest(t *testing.T) {
|
||||
body := []byte(`{
|
||||
"ok": true,
|
||||
"result": [
|
||||
{"update_id": 1, "callback_query": {"message": {"chat": {"id": 10000778141, "type": "group", "title": "测试AI"}}}},
|
||||
{"update_id": 2, "chat_join_request": {"chat": {"id": 33, "type": "group", "title": "join-me"}}}
|
||||
]
|
||||
}`)
|
||||
chats, maxID, _, err := GroupsFromUpdates(body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if maxID != 2 {
|
||||
t.Fatalf("maxID=%d", maxID)
|
||||
}
|
||||
if len(chats) != 2 {
|
||||
t.Fatalf("len=%d %#v", len(chats), chats)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterSafewChats(t *testing.T) {
|
||||
chats := []SafewChat{
|
||||
{ID: "10000778141", Type: "group", Title: "测试AI"},
|
||||
@@ -69,6 +89,11 @@ func TestFilterSafewChats(t *testing.T) {
|
||||
|
||||
func TestPollGroupChatsSuccess(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/bottok/deleteWebhook" {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(`{"ok":true,"result":true}`))
|
||||
return
|
||||
}
|
||||
if r.URL.Path != "/bottok/getUpdates" {
|
||||
t.Errorf("path=%s", r.URL.Path)
|
||||
}
|
||||
@@ -81,6 +106,10 @@ func TestPollGroupChatsSuccess(t *testing.T) {
|
||||
if body["offset"] != float64(5) {
|
||||
t.Errorf("offset=%v", body["offset"])
|
||||
}
|
||||
got, _ := body["allowed_updates"].([]any)
|
||||
if len(got) == 0 {
|
||||
t.Errorf("missing allowed_updates: %s", raw)
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(`{"ok":true,"result":[{"update_id":10,"message":{"chat":{"id":10000778141,"type":"group","title":"测试AI"}}}]}`))
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user