235 lines
6.8 KiB
Go
235 lines
6.8 KiB
Go
package chat_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"messenger/internal/chat"
|
|
"messenger/internal/chat/store"
|
|
"messenger/internal/platform/apperr"
|
|
"messenger/internal/platform/ident"
|
|
"messenger/internal/testenv"
|
|
"messenger/internal/user"
|
|
userstore "messenger/internal/user/store"
|
|
)
|
|
|
|
var db *testenv.DB
|
|
|
|
func TestMain(m *testing.M) {
|
|
var err error
|
|
db, err = testenv.StartPostgres(context.Background())
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
code := m.Run()
|
|
db.Close()
|
|
os.Exit(code)
|
|
}
|
|
|
|
// countingUsers passes through to the real users service and counts the
|
|
// lookups, which proves that member checks are batched.
|
|
type countingUsers struct {
|
|
*user.Service
|
|
calls atomic.Int32
|
|
}
|
|
|
|
func (c *countingUsers) ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error) {
|
|
c.calls.Add(1)
|
|
return c.Service.ExistingIDs(ctx, ids)
|
|
}
|
|
|
|
type fixture struct {
|
|
svc *chat.Service
|
|
users *countingUsers
|
|
alice, bob uuid.UUID
|
|
eve uuid.UUID
|
|
ctx context.Context
|
|
}
|
|
|
|
func newFixture(t *testing.T) fixture {
|
|
t.Helper()
|
|
db.Reset(t)
|
|
ctx := context.Background()
|
|
users := &countingUsers{Service: user.NewService(userstore.NewUsers(db.Pool))}
|
|
register := func(name string) uuid.UUID {
|
|
u, err := users.Register(ctx, name, name, "correct-horse-battery")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return u.ID
|
|
}
|
|
return fixture{
|
|
svc: chat.NewService(store.NewChats(db.Pool), store.NewMessages(db.Pool), users),
|
|
users: users,
|
|
alice: register("alice"),
|
|
bob: register("bob"),
|
|
eve: register("eve"),
|
|
ctx: ctx,
|
|
}
|
|
}
|
|
|
|
func TestCreateChatChecksMembersInOneCall(t *testing.T) {
|
|
f := newFixture(t)
|
|
c, err := f.svc.Create(f.ctx, f.alice, "Проект", []uuid.UUID{f.eve, f.alice, f.bob})
|
|
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 {
|
|
t.Fatalf("member Get error: %v", err)
|
|
}
|
|
if fmt.Sprint(got.MemberIDs) != fmt.Sprint(c.MemberIDs) || !got.CreatedAt.Equal(c.CreatedAt) || got.Title != c.Title {
|
|
t.Fatalf("Get = %+v, want what Create returned %+v", got, c)
|
|
}
|
|
}
|
|
|
|
func TestCreateChatRejectsUnknownMember(t *testing.T) {
|
|
f := newFixture(t)
|
|
_, 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(t)
|
|
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(t)
|
|
c, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, 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(t)
|
|
var mine []uuid.UUID
|
|
for range 3 {
|
|
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(t)
|
|
c, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, 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(t)
|
|
c, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
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] != first || 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] != second || 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)
|
|
}
|
|
}
|