feat: implement lab4 persistent attachments and replicated API

This commit is contained in:
Fedoroff
2026-11-11 21:45:09 +03:00
parent ec540a1a72
commit 827d9c577d
44 changed files with 3507 additions and 31 deletions
+46
View File
@@ -0,0 +1,46 @@
package handler
import (
"github.com/google/uuid"
"mtusi2026lab2/internal/model"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestStrictJSON(t *testing.T) {
for _, body := range []string{`{"text":"hello"} garbage`, `{"text":"hello"}{"text":"again"}`} {
req := httptest.NewRequest("POST", "/", strings.NewReader(body))
var v createMessageRequest
if err := decodeOneObject(req, &v); err == nil {
t.Fatalf("accepted trailing JSON: %q", body)
}
}
}
func TestMessageCursorBoundToChat(t *testing.T) {
chatA, chatB := uuid.New(), uuid.New()
cursor := encodeMessageCursor(model.Message{ID: uuid.New(), ChatID: chatA, CreatedAt: time.Now().UTC()})
if _, _, err := decodeMessageCursor(cursor, chatA); err != nil {
t.Fatal(err)
}
if _, _, err := decodeMessageCursor(cursor, chatB); err == nil {
t.Fatal("accepted cursor for another chat")
}
}
func TestChatCursorBoundToUser(t *testing.T) {
a, b := uuid.New(), uuid.New()
cursor := encodeChatCursor(a, model.Chat{ID: uuid.New(), CreatedAt: time.Now().UTC()})
if _, _, err := decodeChatCursor(cursor, a); err != nil {
t.Fatal(err)
}
if _, _, err := decodeChatCursor(cursor, b); err == nil {
t.Fatal("accepted cursor for another user")
}
}
func TestEmptyLoginBody(t *testing.T) {
req := httptest.NewRequest("POST", "/", strings.NewReader(`{"ignored":true}`))
if err := requireEmptyBody(req); err == nil {
t.Fatal("accepted login body")
}
}