forked from mtusi2026/mtusi2026lab5
67 lines
2.0 KiB
Go
67 lines
2.0 KiB
Go
package chat_test
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"messenger/internal/chat"
|
|
"messenger/internal/platform/apperr"
|
|
"messenger/internal/platform/ident"
|
|
)
|
|
|
|
func TestAttachmentMessageValidatesAndOwnsOrderedIDs(t *testing.T) {
|
|
a, b := ident.New(), ident.New()
|
|
ids := []uuid.UUID{b, a}
|
|
m, err := chat.NewMessage(ident.New(), ident.New(), ident.New(), "", ids...)
|
|
if err != nil || m.Text != "" || !reflect.DeepEqual(m.AttachmentIDs, ids) {
|
|
t.Fatalf("attachment-only message = %+v, %v", m, err)
|
|
}
|
|
ids[0] = a
|
|
if m.AttachmentIDs[0] != b {
|
|
t.Fatal("message aliases caller's attachment slice")
|
|
}
|
|
for _, tc := range []struct {
|
|
text string
|
|
ids []uuid.UUID
|
|
}{
|
|
{"ok", []uuid.UUID{a, a}},
|
|
{"ok", []uuid.UUID{uuid.Nil}},
|
|
{"ok", make([]uuid.UUID, 11)},
|
|
{strings.Repeat("x", 4001), []uuid.UUID{a}},
|
|
{"nul\x00", []uuid.UUID{a}},
|
|
} {
|
|
_, err := chat.NewMessage(ident.New(), ident.New(), ident.New(), tc.text, tc.ids...)
|
|
if err == nil || apperr.KindOf(err) != apperr.KindInvalid {
|
|
t.Fatalf("invalid message accepted: %v", err)
|
|
}
|
|
}
|
|
ten := make([]uuid.UUID, 10)
|
|
for i := range ten {
|
|
ten[i] = ident.New()
|
|
}
|
|
if _, err := chat.NewMessage(ident.New(), ident.New(), ident.New(), " \n", ten...); err != nil {
|
|
t.Fatalf("ten attachments: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSameContentIncludesOrderedAttachmentsAndTreatsNilAsEmpty(t *testing.T) {
|
|
a, b := ident.New(), ident.New()
|
|
m := chat.Message{Text: "hello", AttachmentIDs: []uuid.UUID{b, a}}
|
|
for _, tc := range []struct {
|
|
ids []uuid.UUID
|
|
equal bool
|
|
}{
|
|
{[]uuid.UUID{b, a}, true}, {[]uuid.UUID{a, b}, false}, {[]uuid.UUID{a}, false}, {nil, false},
|
|
} {
|
|
if got := m.SameContent(chat.Message{Text: "hello", AttachmentIDs: tc.ids}); got != tc.equal {
|
|
t.Fatalf("attachments %v: equality=%v, want %v", tc.ids, got, tc.equal)
|
|
}
|
|
}
|
|
if !(chat.Message{Text: "hello"}).SameContent(chat.Message{Text: "hello", AttachmentIDs: []uuid.UUID{}}) {
|
|
t.Fatal("nil attachments must equal empty attachments")
|
|
}
|
|
}
|