Files
mtusi2026lab2/internal/chat/service_test.go
T

201 lines
6.1 KiB
Go

package chat_test
import (
"context"
"errors"
"strings"
"sync/atomic"
"testing"
"github.com/google/uuid"
"messenger/internal/chat"
"messenger/internal/chat/store"
"messenger/internal/platform/apperr"
"messenger/internal/platform/ident"
)
// knownUsers doubles chat.UserLookup and counts calls to prove that member
// checks are batched.
type knownUsers struct {
ids map[uuid.UUID]struct{}
calls atomic.Int32
}
func (k *knownUsers) ExistingIDs(_ context.Context, ids []uuid.UUID) ([]uuid.UUID, error) {
k.calls.Add(1)
out := make([]uuid.UUID, 0, len(ids))
for _, id := range ids {
if _, ok := k.ids[id]; ok {
out = append(out, id)
}
}
return out, nil
}
type fixture struct {
svc *chat.Service
users *knownUsers
alice, bob uuid.UUID
eve uuid.UUID
ctx context.Context
}
func newFixture() fixture {
alice, bob, eve := ident.New(), ident.New(), ident.New()
users := &knownUsers{ids: map[uuid.UUID]struct{}{alice: {}, bob: {}, eve: {}}}
return fixture{
svc: chat.NewService(store.NewInMemChats(), store.NewInMemMessages(), users),
users: users,
alice: alice,
bob: bob,
eve: eve,
ctx: context.Background(),
}
}
func TestCreateChatChecksMembersInOneCall(t *testing.T) {
f := newFixture()
c, err := f.svc.Create(f.ctx, f.alice, "Проект", []uuid.UUID{f.alice, f.bob, f.eve})
if err != nil {
t.Fatalf("Create error: %v", err)
}
if c.Title != "Проект" || len(c.MemberIDs) != 3 {
t.Fatalf("unexpected chat %+v", c)
}
if got := f.users.calls.Load(); got != 1 {
t.Fatalf("member existence must be checked with one batch call, got %d", got)
}
got, err := f.svc.Get(f.ctx, f.bob, c.ID)
if err != nil || got.ID != c.ID || len(got.MemberIDs) != 3 {
t.Fatalf("member Get = %+v, %v", got, err)
}
}
func TestCreateChatRejectsUnknownMember(t *testing.T) {
f := newFixture()
_, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, ident.New()})
if apperr.KindOf(err) != apperr.KindInvalid {
t.Fatalf("kind = %v, want KindInvalid (%v)", apperr.KindOf(err), err)
}
if !strings.Contains(err.Error(), "member_ids") {
t.Fatalf("message must name the field: %v", err)
}
}
func TestCreateChatRejectsInvalidInputBeforeAnyLookup(t *testing.T) {
f := newFixture()
cases := map[string]struct {
title string
members []uuid.UUID
}{
"blank title": {" ", []uuid.UUID{f.alice}},
"missing creator": {"t", []uuid.UUID{f.bob}},
"duplicate": {"t", []uuid.UUID{f.alice, f.alice}},
}
for name, tc := range cases {
_, err := f.svc.Create(f.ctx, f.alice, tc.title, tc.members)
if apperr.KindOf(err) != apperr.KindInvalid {
t.Errorf("%s: kind = %v, want KindInvalid (%v)", name, apperr.KindOf(err), err)
}
}
if got := f.users.calls.Load(); got != 0 {
t.Fatalf("invalid input must be rejected before any user lookup, got %d calls", got)
}
}
func TestGetHidesForeignAndUnknownChatsAlike(t *testing.T) {
f := newFixture()
c, _ := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob})
_, foreign := f.svc.Get(f.ctx, f.eve, c.ID)
_, unknown := f.svc.Get(f.ctx, f.eve, ident.New())
if !errors.Is(foreign, chat.ErrNotFound) || !errors.Is(unknown, chat.ErrNotFound) {
t.Fatalf("foreign = %v, unknown = %v; want both ErrNotFound", foreign, unknown)
}
if apperr.KindOf(foreign) != apperr.KindNotFound {
t.Fatalf("ErrNotFound must carry KindNotFound, got %v", apperr.KindOf(foreign))
}
}
func TestListPagesOnlyOwnChats(t *testing.T) {
f := newFixture()
var mine []uuid.UUID
for i := 0; i < 3; i++ {
c, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob})
if err != nil {
t.Fatal(err)
}
mine = append(mine, c.ID)
if _, err := f.svc.Create(f.ctx, f.eve, "t", []uuid.UUID{f.eve}); err != nil {
t.Fatal(err)
}
}
page, err := f.svc.List(f.ctx, f.alice, nil, 2)
if err != nil {
t.Fatal(err)
}
if len(page.Items) != 2 || page.Next == nil || *page.Next != mine[1] {
t.Fatalf("first page = %+v", page)
}
if len(page.Items[0].MemberIDs) != 2 {
t.Fatalf("listed chats must carry their members: %+v", page.Items[0])
}
rest, err := f.svc.List(f.ctx, f.alice, page.Next, 2)
if err != nil {
t.Fatal(err)
}
if len(rest.Items) != 1 || rest.Items[0].ID != mine[2] || rest.Next != nil {
t.Fatalf("second page = %+v", rest)
}
all, err := f.svc.List(f.ctx, f.bob, nil, 100)
if err != nil || len(all.Items) != 3 || all.Next != nil {
t.Fatalf("bob page = %+v, %v", all, err)
}
}
func TestSendChecksAccessBeforeValidation(t *testing.T) {
f := newFixture()
c, _ := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob})
_, err := f.svc.Send(f.ctx, f.eve, c.ID, " ")
if !errors.Is(err, chat.ErrNotFound) {
t.Fatalf("outsider with blank text: err = %v, want ErrNotFound", err)
}
_, err = f.svc.Send(f.ctx, f.bob, c.ID, " ")
if apperr.KindOf(err) != apperr.KindInvalid {
t.Fatalf("member with blank text: kind = %v, want KindInvalid", apperr.KindOf(err))
}
msg, err := f.svc.Send(f.ctx, f.bob, c.ID, "Привет 👋")
if err != nil || msg.SenderID != f.bob || msg.ChatID != c.ID || msg.Text != "Привет 👋" {
t.Fatalf("Send = %+v, %v", msg, err)
}
}
func TestHistoryPagesAndHidesForeignChat(t *testing.T) {
f := newFixture()
c, _ := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob})
first, _ := f.svc.Send(f.ctx, f.alice, c.ID, "1")
second, _ := f.svc.Send(f.ctx, f.bob, c.ID, "2")
page, err := f.svc.History(f.ctx, f.bob, c.ID, nil, 1)
if err != nil {
t.Fatal(err)
}
if len(page.Items) != 1 || page.Items[0].ID != first.ID || page.Next == nil || *page.Next != first.ID {
t.Fatalf("first page = %+v", page)
}
rest, err := f.svc.History(f.ctx, f.alice, c.ID, page.Next, 1)
if err != nil {
t.Fatal(err)
}
if len(rest.Items) != 1 || rest.Items[0].ID != second.ID || rest.Next != nil {
t.Fatalf("second page = %+v", rest)
}
if _, err := f.svc.History(f.ctx, f.eve, c.ID, nil, 10); !errors.Is(err, chat.ErrNotFound) {
t.Fatalf("outsider history: err = %v, want ErrNotFound", err)
}
empty, err := f.svc.History(f.ctx, f.alice, c.ID, &second.ID, 10)
if err != nil || empty.Items == nil || len(empty.Items) != 0 || empty.Next != nil {
t.Fatalf("page after last = %+v, %v; want empty non-nil items", empty, err)
}
}