forked from mtusi2026/mtusi2026lab2
181 lines
4.7 KiB
Plaintext
181 lines
4.7 KiB
Plaintext
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/charawein/mtusi2026lab2/internal/repository/memory"
|
|
)
|
|
|
|
type chatFixture struct {
|
|
users *UserService
|
|
chats *ChatService
|
|
aliceID string
|
|
bobID string
|
|
carolID string
|
|
}
|
|
|
|
func newChatFixture(t *testing.T) *chatFixture {
|
|
t.Helper()
|
|
userRepo := memory.NewUserRepository()
|
|
chatRepo := memory.NewChatRepository()
|
|
userSvc := NewUserService(userRepo)
|
|
chatSvc := NewChatService(chatRepo, userRepo)
|
|
ctx := context.Background()
|
|
|
|
alice, err := userSvc.Register(ctx, "alice", "Алиса")
|
|
if err != nil {
|
|
t.Fatalf("register alice: %v", err)
|
|
}
|
|
bob, err := userSvc.Register(ctx, "bob", "Боб")
|
|
if err != nil {
|
|
t.Fatalf("register bob: %v", err)
|
|
}
|
|
carol, err := userSvc.Register(ctx, "carol", "Кэрол")
|
|
if err != nil {
|
|
t.Fatalf("register carol: %v", err)
|
|
}
|
|
return &chatFixture{
|
|
users: userSvc,
|
|
chats: chatSvc,
|
|
aliceID: alice.ID,
|
|
bobID: bob.ID,
|
|
carolID: carol.ID,
|
|
}
|
|
}
|
|
|
|
func TestChatService_Create_OK(t *testing.T) {
|
|
f := newChatFixture(t)
|
|
ctx := context.Background()
|
|
|
|
c, err := f.chats.Create(ctx, f.aliceID, "Project", []string{f.aliceID, f.bobID})
|
|
if err != nil {
|
|
t.Fatalf("Create failed: %v", err)
|
|
}
|
|
if len(c.MemberIDs) != 2 {
|
|
t.Errorf("expected 2 members, got %d", len(c.MemberIDs))
|
|
}
|
|
}
|
|
|
|
func TestChatService_Create_CallerNotInMembers(t *testing.T) {
|
|
f := newChatFixture(t)
|
|
ctx := context.Background()
|
|
|
|
// Alice создаёт чат, но в member_ids только Bob.
|
|
_, err := f.chats.Create(ctx, f.aliceID, "Project", []string{f.bobID})
|
|
if !errors.Is(err, ErrInvalidInput) {
|
|
t.Fatalf("expected ErrInvalidInput, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestChatService_Create_UnknownMember(t *testing.T) {
|
|
f := newChatFixture(t)
|
|
ctx := context.Background()
|
|
|
|
_, err := f.chats.Create(ctx, f.aliceID, "Project", []string{f.aliceID, "nonexistent-uuid"})
|
|
if !errors.Is(err, ErrInvalidInput) {
|
|
t.Fatalf("expected ErrInvalidInput, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestChatService_Create_DuplicateMembers(t *testing.T) {
|
|
f := newChatFixture(t)
|
|
ctx := context.Background()
|
|
|
|
_, err := f.chats.Create(ctx, f.aliceID, "Project", []string{f.aliceID, f.aliceID})
|
|
if !errors.Is(err, ErrInvalidInput) {
|
|
t.Fatalf("expected ErrInvalidInput, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestChatService_Create_InvalidTitle(t *testing.T) {
|
|
f := newChatFixture(t)
|
|
ctx := context.Background()
|
|
|
|
cases := []string{"", " "}
|
|
for _, title := range cases {
|
|
_, err := f.chats.Create(ctx, f.aliceID, title, []string{f.aliceID})
|
|
if !errors.Is(err, ErrInvalidInput) {
|
|
t.Errorf("title %q: expected ErrInvalidInput, got %v", title, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestChatService_Get_Member(t *testing.T) {
|
|
f := newChatFixture(t)
|
|
ctx := context.Background()
|
|
|
|
c, _ := f.chats.Create(ctx, f.aliceID, "Project", []string{f.aliceID, f.bobID})
|
|
|
|
got, err := f.chats.Get(ctx, f.bobID, c.ID)
|
|
if err != nil {
|
|
t.Fatalf("Get failed: %v", err)
|
|
}
|
|
if got.ID != c.ID {
|
|
t.Errorf("id mismatch")
|
|
}
|
|
}
|
|
|
|
func TestChatService_Get_Stranger(t *testing.T) {
|
|
f := newChatFixture(t)
|
|
ctx := context.Background()
|
|
|
|
c, _ := f.chats.Create(ctx, f.aliceID, "Project", []string{f.aliceID, f.bobID})
|
|
|
|
// Carol не член — должна получить ErrNotFound, не ErrForbidden.
|
|
_, err := f.chats.Get(ctx, f.carolID, c.ID)
|
|
if !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("expected ErrNotFound for stranger, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestChatService_Get_Nonexistent(t *testing.T) {
|
|
f := newChatFixture(t)
|
|
ctx := context.Background()
|
|
|
|
_, err := f.chats.Get(ctx, f.aliceID, "nonexistent-chat")
|
|
if !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("expected ErrNotFound, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestChatService_List_OnlyMemberChats(t *testing.T) {
|
|
f := newChatFixture(t)
|
|
ctx := context.Background()
|
|
|
|
_, _ = f.chats.Create(ctx, f.aliceID, "A-B", []string{f.aliceID, f.bobID})
|
|
_, _ = f.chats.Create(ctx, f.aliceID, "A-C", []string{f.aliceID, f.carolID})
|
|
_, _ = f.chats.Create(ctx, f.bobID, "B-C", []string{f.bobID, f.carolID})
|
|
|
|
items, _, err := f.chats.List(ctx, f.aliceID, "", 10)
|
|
if err != nil {
|
|
t.Fatalf("List failed: %v", err)
|
|
}
|
|
if len(items) != 2 {
|
|
t.Errorf("expected 2 chats for alice, got %d", len(items))
|
|
}
|
|
}
|
|
|
|
func TestChatService_List_InvalidLimit(t *testing.T) {
|
|
f := newChatFixture(t)
|
|
ctx := context.Background()
|
|
|
|
for _, limit := range []int{0, -1, 101} {
|
|
_, _, err := f.chats.List(ctx, f.aliceID, "", limit)
|
|
if !errors.Is(err, ErrInvalidInput) {
|
|
t.Errorf("limit %d: expected ErrInvalidInput, got %v", limit, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestChatService_List_InvalidCursor(t *testing.T) {
|
|
f := newChatFixture(t)
|
|
ctx := context.Background()
|
|
|
|
_, _, err := f.chats.List(ctx, f.aliceID, "!!!bad-cursor!!!", 10)
|
|
if !errors.Is(err, ErrInvalidInput) {
|
|
t.Fatalf("expected ErrInvalidInput, got %v", err)
|
|
}
|
|
}
|