Перенести чаты и сообщения в PostgreSQL

This commit is contained in:
2026-10-06 14:57:44 +03:00
parent 618d3919d1
commit 0b2db7eb2d
9 changed files with 559 additions and 499 deletions
+73 -39
View File
@@ -3,6 +3,8 @@ package chat_test
import (
"context"
"errors"
"fmt"
"os"
"strings"
"sync/atomic"
"testing"
@@ -13,50 +15,70 @@ import (
"messenger/internal/chat/store"
"messenger/internal/platform/apperr"
"messenger/internal/platform/ident"
"messenger/internal/testenv"
"messenger/internal/user"
userstore "messenger/internal/user/store"
)
// knownUsers doubles chat.UserLookup and counts calls to prove that member
// checks are batched.
type knownUsers struct {
ids map[uuid.UUID]struct{}
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 (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
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 *knownUsers
users *countingUsers
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: {}}}
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.NewInMemChats(), store.NewInMemMessages(), users),
svc: chat.NewService(store.NewChats(db.Pool), store.NewMessages(db.Pool), users),
users: users,
alice: alice,
bob: bob,
eve: eve,
ctx: context.Background(),
alice: register("alice"),
bob: register("bob"),
eve: register("eve"),
ctx: ctx,
}
}
func TestCreateChatChecksMembersInOneCall(t *testing.T) {
f := newFixture()
c, err := f.svc.Create(f.ctx, f.alice, "Проект", []uuid.UUID{f.alice, f.bob, f.eve})
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)
}
@@ -67,13 +89,16 @@ func TestCreateChatChecksMembersInOneCall(t *testing.T) {
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)
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()
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)
@@ -84,7 +109,7 @@ func TestCreateChatRejectsUnknownMember(t *testing.T) {
}
func TestCreateChatRejectsInvalidInputBeforeAnyLookup(t *testing.T) {
f := newFixture()
f := newFixture(t)
cases := map[string]struct {
title string
members []uuid.UUID
@@ -105,8 +130,11 @@ func TestCreateChatRejectsInvalidInputBeforeAnyLookup(t *testing.T) {
}
func TestGetHidesForeignAndUnknownChatsAlike(t *testing.T) {
f := newFixture()
c, _ := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob})
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) {
@@ -118,9 +146,9 @@ func TestGetHidesForeignAndUnknownChatsAlike(t *testing.T) {
}
func TestListPagesOnlyOwnChats(t *testing.T) {
f := newFixture()
f := newFixture(t)
var mine []uuid.UUID
for i := 0; i < 3; i++ {
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)
@@ -154,9 +182,12 @@ func TestListPagesOnlyOwnChats(t *testing.T) {
}
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, " ")
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)
}
@@ -171,8 +202,11 @@ func TestSendChecksAccessBeforeValidation(t *testing.T) {
}
func TestHistoryPagesAndHidesForeignChat(t *testing.T) {
f := newFixture()
c, _ := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob})
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")
@@ -180,14 +214,14 @@ func TestHistoryPagesAndHidesForeignChat(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if len(page.Items) != 1 || page.Items[0].ID != first.ID || page.Next == nil || *page.Next != first.ID {
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].ID != second.ID || rest.Next != nil {
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) {