81b9e1529e
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 9s
CI / integration (pull_request) Successful in 15s
CI / ui (pull_request) Successful in 51s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m19s
Make a per-user block one-directional and non-destructive: the blocker stops
receiving everything from the blocked user (chat, nudge, friend requests,
invitations) and the matchmaker never pairs them, while the blocked user
notices nothing — their sends still persist by the normal rules but are never
delivered or surfaced (born-read). A block no longer deletes the friendship
(an unblock cleanly restores it) and instant-reads any unread the blocked user
had left for the blocker.
- backend: a directional blockExists guard across chat/nudge/friends/invitations
(store-but-hide for the blocked->blocker direction, refuse blocker->blocked);
the matchmaker excludes a block-related pair (both directions) from auto-match;
user_blocked/user_unblocked notifications to the blocker only (in-app only).
- ui: the opponent score card gains a block ✖️ control mirroring add-friend
(red "Block?" confirm, mutual-hide while confirming, struck name, hidden chat
composer when blocked); optimistic apply + event confirm + rollback for both.
- admin: the user card gains cross-linked blocks / blocked-by / friends lists.
- docs: FUNCTIONAL(+ru), ARCHITECTURE §10 + decision record, UI_DESIGN, PRERELEASE.
271 lines
10 KiB
Go
271 lines
10 KiB
Go
//go:build integration
|
|
|
|
package inttest
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"scrabble/backend/internal/engine"
|
|
"scrabble/backend/internal/notify"
|
|
"scrabble/backend/internal/social"
|
|
)
|
|
|
|
// TestBlockInstantReadsExistingUnread checks that blocking marks read any chat the blocked
|
|
// user had left unread for the blocker in their shared games, so no stale unread badge
|
|
// lingers for someone the blocker no longer sees.
|
|
func TestBlockInstantReadsExistingUnread(t *testing.T) {
|
|
ctx := context.Background()
|
|
svc := newSocialService()
|
|
gameID, seats := newGameWithSeats(t, 2)
|
|
sender, viewer := seats[0], seats[1] // seat 0 moves first, so it may chat
|
|
|
|
if _, err := svc.PostMessage(ctx, gameID, sender, "good luck", ""); err != nil {
|
|
t.Fatalf("post: %v", err)
|
|
}
|
|
if unread, _ := svc.HasUnread(ctx, gameID, viewer); !unread {
|
|
t.Fatal("viewer should have the message unread before blocking")
|
|
}
|
|
if err := svc.Block(ctx, viewer, sender); err != nil {
|
|
t.Fatalf("block: %v", err)
|
|
}
|
|
if unread, _ := svc.HasUnread(ctx, gameID, viewer); unread {
|
|
t.Error("blocking should instant-read the blocked sender's prior unread message")
|
|
}
|
|
}
|
|
|
|
// TestChatFromBlockedIsBornReadAndNotDelivered checks the store-but-hide path for chat: a
|
|
// message the blocked user sends is stored and visible to themselves, but is born read for
|
|
// the blocker, never delivered to them, and hidden from their view.
|
|
func TestChatFromBlockedIsBornReadAndNotDelivered(t *testing.T) {
|
|
ctx := context.Background()
|
|
svc := newSocialService()
|
|
pub := &capturePublisher{}
|
|
svc.SetNotifier(pub)
|
|
gameID, seats := newGameWithSeats(t, 2)
|
|
blocked, blocker := seats[0], seats[1] // seat 0 moves first, so the blocked user may chat
|
|
|
|
if err := svc.Block(ctx, blocker, blocked); err != nil {
|
|
t.Fatalf("block: %v", err)
|
|
}
|
|
if _, err := svc.PostMessage(ctx, gameID, blocked, "hello there", ""); err != nil {
|
|
t.Fatalf("blocked user's post = %v, want nil (it must look ordinary to them)", err)
|
|
}
|
|
if unread, _ := svc.HasUnread(ctx, gameID, blocker); unread {
|
|
t.Error("the blocked sender's message must be born read for the blocker")
|
|
}
|
|
if pub.delivered(blocker, notify.KindChatMessage) {
|
|
t.Error("the blocked sender's message must not be delivered to the blocker")
|
|
}
|
|
if msgs, _ := svc.Messages(ctx, gameID, blocker); len(msgs) != 0 {
|
|
t.Errorf("blocker still sees the blocked sender's message: %+v", msgs)
|
|
}
|
|
if msgs, _ := svc.Messages(ctx, gameID, blocked); len(msgs) != 1 {
|
|
t.Errorf("the blocked sender should see their own message, got %+v", msgs)
|
|
}
|
|
}
|
|
|
|
// TestChatToBlockedIsRejected checks the blocker-side guard: a player cannot post when their
|
|
// only opponent is someone they have blocked (the composer is hidden client-side too).
|
|
func TestChatToBlockedIsRejected(t *testing.T) {
|
|
ctx := context.Background()
|
|
svc := newSocialService()
|
|
gameID, seats := newGameWithSeats(t, 2)
|
|
blocker, blocked := seats[0], seats[1] // blocker moves first, so it is the one trying to chat
|
|
|
|
if err := svc.Block(ctx, blocker, blocked); err != nil {
|
|
t.Fatalf("block: %v", err)
|
|
}
|
|
if _, err := svc.PostMessage(ctx, gameID, blocker, "hi", ""); !errors.Is(err, social.ErrRecipientBlocked) {
|
|
t.Fatalf("chat to blocked opponent = %v, want ErrRecipientBlocked", err)
|
|
}
|
|
}
|
|
|
|
// TestNudgeFromBlockedIsBornReadAndNotDelivered checks the store-but-hide path for nudge: a
|
|
// nudge the blocked user sends is recorded (so their once-per-hour cooldown applies, looking
|
|
// ordinary to them) but is born read and never delivered to the blocker.
|
|
func TestNudgeFromBlockedIsBornReadAndNotDelivered(t *testing.T) {
|
|
ctx := context.Background()
|
|
svc := newSocialService()
|
|
pub := &capturePublisher{}
|
|
svc.SetNotifier(pub)
|
|
gameID, seats := newGameWithSeats(t, 2)
|
|
blocker, blocked := seats[0], seats[1] // seat 0 awaited; the blocked seat-1 player nudges it
|
|
|
|
if err := svc.Block(ctx, blocker, blocked); err != nil {
|
|
t.Fatalf("block: %v", err)
|
|
}
|
|
if _, err := svc.Nudge(ctx, gameID, blocked); err != nil {
|
|
t.Fatalf("blocked user's nudge = %v, want nil (it must look ordinary to them)", err)
|
|
}
|
|
if unread, _ := svc.HasUnread(ctx, gameID, blocker); unread {
|
|
t.Error("the blocked sender's nudge must be born read for the blocker")
|
|
}
|
|
if pub.delivered(blocker, notify.KindNudge) {
|
|
t.Error("the blocked sender's nudge must not be delivered to the blocker")
|
|
}
|
|
// The nudge is still recorded, so the cooldown applies (the blocked user notices nothing).
|
|
if _, ok, _ := svc.LastNudgeAt(ctx, gameID, blocked); !ok {
|
|
t.Error("the suppressed nudge should still be recorded")
|
|
}
|
|
}
|
|
|
|
// TestNudgeToBlockedIsRejected checks the blocker-side guard: a player cannot nudge the
|
|
// awaited opponent when they have blocked them.
|
|
func TestNudgeToBlockedIsRejected(t *testing.T) {
|
|
ctx := context.Background()
|
|
svc := newSocialService()
|
|
gameID, seats := newGameWithSeats(t, 2)
|
|
blocked, blocker := seats[0], seats[1] // seat 0 awaited; the blocker (seat 1) tries to nudge it
|
|
|
|
if err := svc.Block(ctx, blocker, blocked); err != nil {
|
|
t.Fatalf("block: %v", err)
|
|
}
|
|
if _, err := svc.Nudge(ctx, gameID, blocker); !errors.Is(err, social.ErrRecipientBlocked) {
|
|
t.Fatalf("nudge to blocked opponent = %v, want ErrRecipientBlocked", err)
|
|
}
|
|
}
|
|
|
|
// TestBlockPublishesToBlockerOnly checks that block and unblock confirm to the blocker (so
|
|
// their other sessions update in place) and never reach the blocked user.
|
|
func TestBlockPublishesToBlockerOnly(t *testing.T) {
|
|
ctx := context.Background()
|
|
svc := newSocialService()
|
|
pub := &capturePublisher{}
|
|
svc.SetNotifier(pub)
|
|
blocker, blocked := provisionAccount(t), provisionAccount(t)
|
|
|
|
if err := svc.Block(ctx, blocker, blocked); err != nil {
|
|
t.Fatalf("block: %v", err)
|
|
}
|
|
if !pub.notified(blocker, notify.NotifyUserBlocked) {
|
|
t.Error("the blocker should receive a user_blocked event")
|
|
}
|
|
if pub.notified(blocked, notify.NotifyUserBlocked) {
|
|
t.Error("the blocked user must never receive a user_blocked event")
|
|
}
|
|
if err := svc.Unblock(ctx, blocker, blocked); err != nil {
|
|
t.Fatalf("unblock: %v", err)
|
|
}
|
|
if !pub.notified(blocker, notify.NotifyUserUnblocked) {
|
|
t.Error("the blocker should receive a user_unblocked event")
|
|
}
|
|
if pub.notified(blocked, notify.NotifyUserUnblocked) {
|
|
t.Error("the blocked user must never receive a user_unblocked event")
|
|
}
|
|
}
|
|
|
|
// TestMatchmakingExcludesBlockedPlayers checks auto-match never pairs two players with a
|
|
// block between them (either direction), while an unblocked third player still joins.
|
|
func TestMatchmakingExcludesBlockedPlayers(t *testing.T) {
|
|
ctx := context.Background()
|
|
clearOpenGames(t)
|
|
mm := newMatchmaker(t, newRobotService(t, newGameService()), 90*time.Second, 90*time.Second)
|
|
soc := newSocialService()
|
|
mm.SetBlocker(soc)
|
|
a, b, c := provisionAccount(t), provisionAccount(t), provisionAccount(t)
|
|
|
|
if err := soc.Block(ctx, a, b); err != nil {
|
|
t.Fatalf("block: %v", err)
|
|
}
|
|
// The block is seen from both sides — the exclusion set unions both directions.
|
|
if !contains(blockedWith(t, soc, a), b) || !contains(blockedWith(t, soc, b), a) {
|
|
t.Fatal("a block must appear in BlockedWith for both the blocker and the blocked")
|
|
}
|
|
|
|
// b opens a game awaiting an opponent.
|
|
r1, err := mm.Enqueue(ctx, b, engine.VariantEnglish, true)
|
|
if err != nil {
|
|
t.Fatalf("enqueue b: %v", err)
|
|
}
|
|
if r1.Matched {
|
|
t.Fatal("first enqueue must open a game, not match")
|
|
}
|
|
// a must not join b's game (a blocked b): it opens its own instead.
|
|
r2, err := mm.Enqueue(ctx, a, engine.VariantEnglish, true)
|
|
if err != nil {
|
|
t.Fatalf("enqueue a: %v", err)
|
|
}
|
|
if r2.Matched || r2.Game.ID == r1.Game.ID {
|
|
t.Fatalf("a joined the blocked player's game %s (matched %v), want a fresh game", r1.Game.ID, r2.Matched)
|
|
}
|
|
// A third, unblocked player joins b's still-open game (the oldest), proving the exclusion
|
|
// is specific to the blocked pair, not a blanket refusal.
|
|
r3, err := mm.Enqueue(ctx, c, engine.VariantEnglish, true)
|
|
if err != nil {
|
|
t.Fatalf("enqueue c: %v", err)
|
|
}
|
|
if !r3.Matched || r3.Game.ID != r1.Game.ID {
|
|
t.Fatalf("unblocked c = (game %s, matched %v), want it to join b's open game %s", r3.Game.ID, r3.Matched, r1.Game.ID)
|
|
}
|
|
}
|
|
|
|
// TestAdminSocialLists checks the admin user card's blocks / blocked-by / friends queries
|
|
// return the full truth in both directions, including a friendship that a block overrides but
|
|
// does not delete.
|
|
func TestAdminSocialLists(t *testing.T) {
|
|
ctx := context.Background()
|
|
svc := newSocialService()
|
|
_, seats := newGameWithSeats(t, 2)
|
|
a, b := seats[0], seats[1]
|
|
if err := svc.SendFriendRequest(ctx, a, b); err != nil {
|
|
t.Fatalf("request: %v", err)
|
|
}
|
|
if err := svc.RespondFriendRequest(ctx, b, a, true); err != nil {
|
|
t.Fatalf("accept: %v", err)
|
|
}
|
|
if err := svc.Block(ctx, a, b); err != nil {
|
|
t.Fatalf("block: %v", err)
|
|
}
|
|
|
|
blocks, err := svc.AdminBlocksBy(ctx, a)
|
|
if err != nil {
|
|
t.Fatalf("admin blocks by: %v", err)
|
|
}
|
|
if len(blocks) != 1 || blocks[0].AccountID != b || blocks[0].At.IsZero() {
|
|
t.Errorf("a's blocks = %v, want [b] with a date", blocks)
|
|
}
|
|
blockedBy, err := svc.AdminBlockedBy(ctx, b)
|
|
if err != nil {
|
|
t.Fatalf("admin blocked by: %v", err)
|
|
}
|
|
if len(blockedBy) != 1 || blockedBy[0].AccountID != a {
|
|
t.Errorf("b's blocked-by = %v, want [a]", blockedBy)
|
|
}
|
|
// The friendship survives the block, so it still shows on both admin friend lists.
|
|
friendsA, err := svc.AdminFriends(ctx, a)
|
|
if err != nil {
|
|
t.Fatalf("admin friends: %v", err)
|
|
}
|
|
if len(friendsA) != 1 || friendsA[0].AccountID != b || friendsA[0].At.IsZero() {
|
|
t.Errorf("a's admin friends = %v, want [b] with a date", friendsA)
|
|
}
|
|
if friendsB, err := svc.AdminFriends(ctx, b); err != nil || len(friendsB) != 1 || friendsB[0].AccountID != a {
|
|
t.Errorf("b's admin friends = %v (err %v), want [a]", friendsB, err)
|
|
}
|
|
}
|
|
|
|
// blockedWith reads the both-direction block set for id.
|
|
func blockedWith(t *testing.T, soc *social.Service, id uuid.UUID) []uuid.UUID {
|
|
t.Helper()
|
|
ids, err := soc.BlockedWith(context.Background(), id)
|
|
if err != nil {
|
|
t.Fatalf("blocked with: %v", err)
|
|
}
|
|
return ids
|
|
}
|
|
|
|
// contains reports whether ids includes want.
|
|
func contains(ids []uuid.UUID, want uuid.UUID) bool {
|
|
for _, id := range ids {
|
|
if id == want {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|