forked from mtusi2026/mtusi2026lab2
сделана пагинация чатов
This commit is contained in:
@@ -7,6 +7,8 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"sort"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
@@ -171,6 +173,240 @@ func TestCursorPaginationMultiplePages(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatCursorPaginationMultiplePages(t *testing.T) {
|
||||
ts, st := setupTestServer()
|
||||
defer ts.Close()
|
||||
|
||||
user, err := st.CreateUser("chat_paging_user", "Пейджер чатов")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create user: %v", err)
|
||||
}
|
||||
|
||||
outsider, err := st.CreateUser("chat_paging_outsider", "Посторонний")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create outsider: %v", err)
|
||||
}
|
||||
|
||||
createdChats := make([]domain.Chat, 0, 5)
|
||||
|
||||
for i := 1; i <= 5; i++ {
|
||||
chat, createErr := st.CreateChat(
|
||||
fmt.Sprintf("Chat %d", i),
|
||||
[]string{user.ID},
|
||||
)
|
||||
if createErr != nil {
|
||||
t.Fatalf("failed to create chat %d: %v", i, createErr)
|
||||
}
|
||||
|
||||
createdChats = append(createdChats, chat)
|
||||
|
||||
// Чужой чат создаётся между доступными чатами и не должен
|
||||
// влиять на размер страницы или next_cursor пользователя.
|
||||
if i == 3 {
|
||||
if _, createErr = st.CreateChat(
|
||||
"Outsider chat",
|
||||
[]string{outsider.ID},
|
||||
); createErr != nil {
|
||||
t.Fatalf("failed to create outsider chat: %v", createErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(createdChats, func(i, j int) bool {
|
||||
if createdChats[i].CreatedAt.Equal(createdChats[j].CreatedAt) {
|
||||
return createdChats[i].ID < createdChats[j].ID
|
||||
}
|
||||
|
||||
return createdChats[i].CreatedAt.Before(createdChats[j].CreatedAt)
|
||||
})
|
||||
|
||||
var receivedIDs []string
|
||||
seenIDs := make(map[string]bool)
|
||||
seenCursors := make(map[string]bool)
|
||||
cursor := ""
|
||||
finished := false
|
||||
|
||||
for pageNumber := 1; pageNumber <= 10; pageNumber++ {
|
||||
requestURL := ts.URL + "/api/v1/chats?limit=2"
|
||||
if cursor != "" {
|
||||
requestURL += "&cursor=" + url.QueryEscape(cursor)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, requestURL, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create request: %v", err)
|
||||
}
|
||||
req.Header.Set("X-User-Id", user.ID)
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to list chats: %v", err)
|
||||
}
|
||||
|
||||
var page domain.ChatPage
|
||||
decodeErr := json.NewDecoder(res.Body).Decode(&page)
|
||||
res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d", res.StatusCode)
|
||||
}
|
||||
if decodeErr != nil {
|
||||
t.Fatalf("failed to decode chat page: %v", decodeErr)
|
||||
}
|
||||
if len(page.Items) > 2 {
|
||||
t.Fatalf("page contains %d chats with limit=2", len(page.Items))
|
||||
}
|
||||
|
||||
for _, chat := range page.Items {
|
||||
if seenIDs[chat.ID] {
|
||||
t.Fatalf("chat %s was returned more than once", chat.ID)
|
||||
}
|
||||
|
||||
seenIDs[chat.ID] = true
|
||||
receivedIDs = append(receivedIDs, chat.ID)
|
||||
}
|
||||
|
||||
if page.NextCursor == nil {
|
||||
finished = true
|
||||
break
|
||||
}
|
||||
|
||||
if seenCursors[*page.NextCursor] {
|
||||
t.Fatalf("cursor cycle detected")
|
||||
}
|
||||
|
||||
seenCursors[*page.NextCursor] = true
|
||||
cursor = *page.NextCursor
|
||||
}
|
||||
|
||||
if !finished {
|
||||
t.Fatal("chat pagination did not finish")
|
||||
}
|
||||
|
||||
if len(receivedIDs) != len(createdChats) {
|
||||
t.Fatalf(
|
||||
"expected %d chats across all pages, got %d",
|
||||
len(createdChats),
|
||||
len(receivedIDs),
|
||||
)
|
||||
}
|
||||
|
||||
for i, chat := range createdChats {
|
||||
if receivedIDs[i] != chat.ID {
|
||||
t.Fatalf(
|
||||
"chat order differs at position %d: expected %s, got %s",
|
||||
i,
|
||||
chat.ID,
|
||||
receivedIDs[i],
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatCursorValidation(t *testing.T) {
|
||||
ts, st := setupTestServer()
|
||||
defer ts.Close()
|
||||
|
||||
alice, err := st.CreateUser("chat_cursor_alice", "Алиса")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create Alice: %v", err)
|
||||
}
|
||||
|
||||
bob, err := st.CreateUser("chat_cursor_bob", "Боб")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create Bob: %v", err)
|
||||
}
|
||||
|
||||
for i := 1; i <= 2; i++ {
|
||||
if _, err = st.CreateChat(
|
||||
fmt.Sprintf("Alice chat %d", i),
|
||||
[]string{alice.ID},
|
||||
); err != nil {
|
||||
t.Fatalf("failed to create Alice chat: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err = st.CreateChat("Bob chat", []string{bob.ID}); err != nil {
|
||||
t.Fatalf("failed to create Bob chat: %v", err)
|
||||
}
|
||||
|
||||
firstRequest, err := http.NewRequest(
|
||||
http.MethodGet,
|
||||
ts.URL+"/api/v1/chats?limit=1",
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create first request: %v", err)
|
||||
}
|
||||
firstRequest.Header.Set("X-User-Id", alice.ID)
|
||||
|
||||
firstResponse, err := http.DefaultClient.Do(firstRequest)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to request first page: %v", err)
|
||||
}
|
||||
|
||||
var firstPage domain.ChatPage
|
||||
decodeErr := json.NewDecoder(firstResponse.Body).Decode(&firstPage)
|
||||
firstResponse.Body.Close()
|
||||
|
||||
if firstResponse.StatusCode != http.StatusOK {
|
||||
t.Fatalf("expected first page status 200, got %d", firstResponse.StatusCode)
|
||||
}
|
||||
if decodeErr != nil {
|
||||
t.Fatalf("failed to decode first page: %v", decodeErr)
|
||||
}
|
||||
if firstPage.NextCursor == nil {
|
||||
t.Fatal("expected next_cursor for Alice")
|
||||
}
|
||||
|
||||
foreignRequest, err := http.NewRequest(
|
||||
http.MethodGet,
|
||||
ts.URL+"/api/v1/chats?limit=1&cursor="+
|
||||
url.QueryEscape(*firstPage.NextCursor),
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create foreign cursor request: %v", err)
|
||||
}
|
||||
foreignRequest.Header.Set("X-User-Id", bob.ID)
|
||||
|
||||
foreignResponse, err := http.DefaultClient.Do(foreignRequest)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to send foreign cursor request: %v", err)
|
||||
}
|
||||
foreignResponse.Body.Close()
|
||||
|
||||
if foreignResponse.StatusCode != http.StatusBadRequest {
|
||||
t.Fatalf(
|
||||
"expected 400 for another user's cursor, got %d",
|
||||
foreignResponse.StatusCode,
|
||||
)
|
||||
}
|
||||
|
||||
malformedRequest, err := http.NewRequest(
|
||||
http.MethodGet,
|
||||
ts.URL+"/api/v1/chats?cursor=not-valid-base64!",
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create malformed cursor request: %v", err)
|
||||
}
|
||||
malformedRequest.Header.Set("X-User-Id", alice.ID)
|
||||
|
||||
malformedResponse, err := http.DefaultClient.Do(malformedRequest)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to send malformed cursor request: %v", err)
|
||||
}
|
||||
malformedResponse.Body.Close()
|
||||
|
||||
if malformedResponse.StatusCode != http.StatusBadRequest {
|
||||
t.Fatalf(
|
||||
"expected 400 for malformed cursor, got %d",
|
||||
malformedResponse.StatusCode,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// 6 тест: 100 сообщений от 10 клиентов одновременно, защита от гонок
|
||||
func TestConcurrent100MessagesFrom10Clients(t *testing.T) {
|
||||
ts, st := setupTestServer()
|
||||
|
||||
Reference in New Issue
Block a user