feat(attachments): align list pagination

This commit is contained in:
2026-06-06 02:19:10 +08:00
parent 0fb4e6c500
commit 3c30596bad
6 changed files with 97 additions and 9 deletions
@@ -9,6 +9,7 @@ import (
"net/http/httptest"
"strconv"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
@@ -446,6 +447,41 @@ func (s *ConversationHandlerTestSuite) TestListAttachmentsReturnsChatwootPayload
assert.Contains(s.T(), item, "created_at")
}
func (s *ConversationHandlerTestSuite) TestListAttachmentsUsesChatwootFixedPageSize() {
conversation := &model.Conversation{AccountID: s.testAccount.ID, InboxID: s.testConv.InboxID, ContactID: s.testConv.ContactID, Status: "open", ChannelType: "web_widget", Channel: "web_widget"}
s.Require().NoError(s.db.Create(conversation).Error)
message := &model.Message{AccountID: s.testAccount.ID, InboxID: conversation.InboxID, ConversationID: conversation.ID, Content: "files", MessageType: "incoming", ContentType: "text", Status: "sent"}
s.Require().NoError(s.db.Create(message).Error)
baseTime := time.Now().Add(-time.Hour)
var newestID uint
for i := 0; i < 30; i++ {
attachment := &model.Attachment{
Base: model.Base{CreatedAt: baseTime.Add(time.Duration(i) * time.Minute), UpdatedAt: baseTime.Add(time.Duration(i) * time.Minute)},
AccountID: s.testAccount.ID,
MessageID: message.ID,
FileType: "file",
FileURL: fmt.Sprintf("https://cdn.example.com/file-%02d.txt", i),
FileName: fmt.Sprintf("file-%02d.txt", i),
}
s.Require().NoError(s.db.Create(attachment).Error)
newestID = attachment.ID
}
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", fmt.Sprintf("%s/conversations/%d/attachments?per_page=5", s.accountURL(), conversation.ID), nil)
s.router.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusOK, w.Code, w.Body.String())
var resp map[string]any
assert.NoError(s.T(), json.Unmarshal(w.Body.Bytes(), &resp))
assert.Equal(s.T(), float64(30), resp["meta"].(map[string]any)["total_count"])
payload := resp["payload"].([]any)
assert.Len(s.T(), payload, 30)
first := payload[0].(map[string]any)
assert.Equal(s.T(), float64(newestID), first["id"])
}
func (s *ConversationHandlerTestSuite) TestToggleTyping_InvalidAccountID() {
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/conversations/1/toggle_typing", bytes.NewBufferString(`{"typing_status":"on"}`))