package service import ( "context" "errors" "strings" "testing" "github.com/charawein/mtusi2026lab2/internal/repository/memory" ) type messageFixture struct { users *UserService chats *ChatService messages *MessageService aliceID string bobID string carolID string chatID string } func newMessageFixture(t *testing.T) *messageFixture { t.Helper() ctx := context.Background() userRepo := memory.NewUserRepository() chatRepo := memory.NewChatRepository() msgRepo := memory.NewMessageRepository() userSvc := NewUserService(userRepo) chatSvc := NewChatService(chatRepo, userRepo) msgSvc := NewMessageService(msgRepo, chatRepo) 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) } chat, err := chatSvc.Create(ctx, alice.ID, "Project", []string{alice.ID, bob.ID}) if err != nil { t.Fatalf("create chat: %v", err) } return &messageFixture{ users: userSvc, chats: chatSvc, messages: msgSvc, aliceID: alice.ID, bobID: bob.ID, carolID: carol.ID, chatID: chat.ID, } } func TestMessageService_Send_OK(t *testing.T) { f := newMessageFixture(t) ctx := context.Background() m, err := f.messages.Send(ctx, f.aliceID, f.chatID, "Привет, Боб!") if err != nil { t.Fatalf("Send failed: %v", err) } if m.Text != "Привет, Боб!" { t.Errorf("text mismatch: got %q", m.Text) } if m.SenderID != f.aliceID { t.Errorf("sender mismatch: got %q", m.SenderID) } } func TestMessageService_Send_Stranger(t *testing.T) { f := newMessageFixture(t) ctx := context.Background() _, err := f.messages.Send(ctx, f.carolID, f.chatID, "Hello") if !errors.Is(err, ErrNotFound) { t.Fatalf("expected ErrNotFound for stranger, got %v", err) } } func TestMessageService_Send_NonexistentChat(t *testing.T) { f := newMessageFixture(t) ctx := context.Background() _, err := f.messages.Send(ctx, f.aliceID, "nonexistent-chat", "Hello") if !errors.Is(err, ErrNotFound) { t.Fatalf("expected ErrNotFound, got %v", err) } } func TestMessageService_Send_EmptyText(t *testing.T) { f := newMessageFixture(t) ctx := context.Background() cases := []string{"", " ", "\t\n"} for _, text := range cases { _, err := f.messages.Send(ctx, f.aliceID, f.chatID, text) if !errors.Is(err, ErrInvalidInput) { t.Errorf("text %q: expected ErrInvalidInput, got %v", text, err) } } } func TestMessageService_Send_TooLongText(t *testing.T) { f := newMessageFixture(t) ctx := context.Background() text := strings.Repeat("я", 4001) _, err := f.messages.Send(ctx, f.aliceID, f.chatID, text) if !errors.Is(err, ErrInvalidInput) { t.Fatalf("expected ErrInvalidInput for 4001 code points, got %v", err) } text = strings.Repeat("я", 4000) if _, err := f.messages.Send(ctx, f.aliceID, f.chatID, text); err != nil { t.Fatalf("expected OK for 4000 code points, got %v", err) } } func TestMessageService_List_OK(t *testing.T) { f := newMessageFixture(t) ctx := context.Background() _, _ = f.messages.Send(ctx, f.aliceID, f.chatID, "one") _, _ = f.messages.Send(ctx, f.bobID, f.chatID, "two") items, next, err := f.messages.List(ctx, f.aliceID, f.chatID, "", 10) if err != nil { t.Fatalf("List failed: %v", err) } if len(items) != 2 { t.Fatalf("expected 2 messages, got %d", len(items)) } if next != "" { t.Errorf("expected empty cursor, got %q", next) } } func TestMessageService_List_Stranger(t *testing.T) { f := newMessageFixture(t) ctx := context.Background() _, _, err := f.messages.List(ctx, f.carolID, f.chatID, "", 10) if !errors.Is(err, ErrNotFound) { t.Fatalf("expected ErrNotFound for stranger, got %v", err) } } func TestMessageService_List_InvalidLimit(t *testing.T) { f := newMessageFixture(t) ctx := context.Background() for _, limit := range []int{0, -1, 101} { _, _, err := f.messages.List(ctx, f.aliceID, f.chatID, "", limit) if !errors.Is(err, ErrInvalidInput) { t.Errorf("limit %d: expected ErrInvalidInput, got %v", limit, err) } } } func TestMessageService_List_CursorFromAnotherChat(t *testing.T) { f := newMessageFixture(t) ctx := context.Background() chat2, err := f.chats.Create(ctx, f.aliceID, "Other", []string{f.aliceID, f.carolID}) if err != nil { t.Fatalf("create second chat: %v", err) } for i := 0; i < 5; i++ { _, _ = f.messages.Send(ctx, f.aliceID, f.chatID, "in chat1") _, _ = f.messages.Send(ctx, f.aliceID, chat2.ID, "in chat2") } // Получаем курсор из первого чата. _, cursor, err := f.messages.List(ctx, f.aliceID, f.chatID, "", 2) if err != nil { t.Fatalf("List chat1 failed: %v", err) } if cursor == "" { t.Fatal("expected non-empty cursor") } // Пытаемся использовать его во втором чате. _, _, err = f.messages.List(ctx, f.aliceID, chat2.ID, cursor, 2) if !errors.Is(err, ErrInvalidInput) { t.Fatalf("expected ErrInvalidInput for cursor from another chat, got %v", err) } } func TestMessageService_Pagination_AllMessages(t *testing.T) { f := newMessageFixture(t) ctx := context.Background() const total = 10 for i := 0; i < total; i++ { if _, err := f.messages.Send(ctx, f.aliceID, f.chatID, "msg"); err != nil { t.Fatalf("send %d failed: %v", i, err) } } seen := make(map[string]bool) cursor := "" pages := 0 for { items, next, err := f.messages.List(ctx, f.aliceID, f.chatID, cursor, 3) if err != nil { t.Fatalf("List failed: %v", err) } for _, m := range items { if seen[m.ID] { t.Errorf("duplicate message: %q", m.ID) } seen[m.ID] = true } pages++ if next == "" { break } cursor = next if pages > 10 { t.Fatal("too many pages") } } if len(seen) != total { t.Errorf("expected %d unique messages, got %d", total, len(seen)) } }