Stage 8: UI social/account/history surfaces
Wire the deferred Stage 7 surfaces end-to-end (UI -> gateway transcode -> backend REST -> existing domain services): friends (incl. one-time friend codes), per-user blocks, friend-game invitations, profile editing + email binding, the statistics screen, and the in-game history + GCG export. Friends gain two add paths (interview decision, a deliberate plan change): one-time 6-digit codes (friend_codes table, 12h TTL, single-use, rate-limited redeem); and play-gated requests (shared game required) where an explicit decline is permanent, an ignored request lapses after 30 days, and a code bypasses a decline. Migration 00006 widens friendships_status_chk and adds friend_codes. Lobby notification badge is poll + push: a new generic `notify` event drives it live; the client polls on open/focus. Language stays a single Settings control that writes through to the durable account's preferred_language. GCG export is finished-only (game.ErrGameActive) and shares/downloads the .gcg file. Tests: backend unit + inttest (friend gate/decline/code, ListInvitations, GetStats, GCG gate), gateway transcode round-trips + notify constructor, UI vitest (codecs, win-rate, share choice) + Playwright social specs. Docs: PLAN (Stage 8 done + refinements + TODO-5), ARCHITECTURE, FUNCTIONAL(+ru), UI_DESIGN, TESTING, module READMEs.
This commit is contained in:
@@ -76,6 +76,21 @@ func TestAccountProvisionByIdentity(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetStatsZeroForFreshAccount checks that an account with no finished games
|
||||
// reads back the zero statistics rather than an error (the Stage 8 stats screen).
|
||||
func TestGetStatsZeroForFreshAccount(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store := account.NewStore(testDB)
|
||||
id := provisionAccount(t)
|
||||
st, err := store.GetStats(ctx, id)
|
||||
if err != nil {
|
||||
t.Fatalf("get stats: %v", err)
|
||||
}
|
||||
if (st != account.Stats{}) {
|
||||
t.Fatalf("fresh stats = %+v, want zero", st)
|
||||
}
|
||||
}
|
||||
|
||||
// identityConfirmed reads the confirmed flag for one identity directly.
|
||||
func identityConfirmed(t *testing.T, kind, externalID string) bool {
|
||||
t.Helper()
|
||||
|
||||
@@ -555,3 +555,13 @@ func equalStrings(a, b []string) bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// TestExportGCGRefusesActiveGame checks the Stage 8 finished-only gate: a GCG export
|
||||
// is allowed only once the game is over, so an active game leaks nothing mid-play.
|
||||
func TestExportGCGRefusesActiveGame(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
gameID, _ := newGameWithSeats(t, 2)
|
||||
if _, err := newGameService().ExportGCG(ctx, gameID); !errors.Is(err, game.ErrGameActive) {
|
||||
t.Fatalf("export of active game = %v, want ErrGameActive", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,3 +165,31 @@ func TestInvitationCancelByInviter(t *testing.T) {
|
||||
t.Fatalf("respond after cancel = %v, want ErrInvitationNotPending", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListInvitations(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
svc := newInvitationService()
|
||||
inviter := provisionAccount(t)
|
||||
invitee := provisionAccount(t)
|
||||
inv, err := svc.CreateInvitation(ctx, inviter, []uuid.UUID{invitee}, englishInvite())
|
||||
if err != nil {
|
||||
t.Fatalf("create: %v", err)
|
||||
}
|
||||
// An open invitation appears for both the inviter and the invitee.
|
||||
for _, who := range []uuid.UUID{inviter, invitee} {
|
||||
list, err := svc.ListInvitations(ctx, who)
|
||||
if err != nil {
|
||||
t.Fatalf("list for %s: %v", who, err)
|
||||
}
|
||||
if len(list) != 1 || list[0].ID != inv.ID {
|
||||
t.Fatalf("invitations for %s = %+v, want [%s]", who, list, inv.ID)
|
||||
}
|
||||
}
|
||||
// Once accepted (the game starts), it is no longer an open invitation.
|
||||
if _, err := svc.RespondInvitation(ctx, inv.ID, invitee, true); err != nil {
|
||||
t.Fatalf("accept: %v", err)
|
||||
}
|
||||
if list, _ := svc.ListInvitations(ctx, inviter); len(list) != 0 {
|
||||
t.Fatalf("started invitation still listed: %+v", list)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,9 @@ func newGameWithSeats(t *testing.T, n int) (uuid.UUID, []uuid.UUID) {
|
||||
func TestFriendRequestLifecycle(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
svc := newSocialService()
|
||||
a, b := provisionAccount(t), provisionAccount(t)
|
||||
// A request is only allowed between players who share a game.
|
||||
_, seats := newGameWithSeats(t, 2)
|
||||
a, b := seats[0], seats[1]
|
||||
|
||||
if err := svc.SendFriendRequest(ctx, a, b); err != nil {
|
||||
t.Fatalf("send: %v", err)
|
||||
@@ -102,7 +104,8 @@ func TestFriendRequestRefusedByToggleAndBlock(t *testing.T) {
|
||||
func TestBlockSeversFriendship(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
svc := newSocialService()
|
||||
a, b := provisionAccount(t), provisionAccount(t)
|
||||
_, seats := newGameWithSeats(t, 2)
|
||||
a, b := seats[0], seats[1]
|
||||
if err := svc.SendFriendRequest(ctx, a, b); err != nil {
|
||||
t.Fatalf("send: %v", err)
|
||||
}
|
||||
@@ -117,6 +120,113 @@ func TestBlockSeversFriendship(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFriendRequestRequiresSharedGame(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
svc := newSocialService()
|
||||
a, b := provisionAccount(t), provisionAccount(t) // never played together
|
||||
if err := svc.SendFriendRequest(ctx, a, b); !errors.Is(err, social.ErrNoSharedGame) {
|
||||
t.Fatalf("send without shared game = %v, want ErrNoSharedGame", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFriendDeclineIsPermanentUntilCode(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("send: %v", err)
|
||||
}
|
||||
if err := svc.RespondFriendRequest(ctx, b, a, false); err != nil { // b declines a
|
||||
t.Fatalf("decline: %v", err)
|
||||
}
|
||||
// An explicit decline is remembered: a cannot re-send.
|
||||
if err := svc.SendFriendRequest(ctx, a, b); !errors.Is(err, social.ErrRequestDeclined) {
|
||||
t.Fatalf("resend after decline = %v, want ErrRequestDeclined", err)
|
||||
}
|
||||
// But a one-time code from b bypasses the decline.
|
||||
code, err := svc.IssueFriendCode(ctx, b)
|
||||
if err != nil {
|
||||
t.Fatalf("issue code: %v", err)
|
||||
}
|
||||
issuer, err := svc.RedeemFriendCode(ctx, a, code.Code)
|
||||
if err != nil {
|
||||
t.Fatalf("redeem: %v", err)
|
||||
}
|
||||
if issuer != b {
|
||||
t.Fatalf("redeem issuer = %s, want b", issuer)
|
||||
}
|
||||
if friends, _ := svc.ListFriends(ctx, a); len(friends) != 1 || friends[0] != b {
|
||||
t.Fatalf("friends of a after code = %v, want [b]", friends)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFriendRequestResendAfterExpiry(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("send: %v", err)
|
||||
}
|
||||
// A request older than the 30-day window lazily expires: it leaves the incoming
|
||||
// list and may be re-sent.
|
||||
if _, err := testDB.ExecContext(ctx,
|
||||
`UPDATE backend.friendships SET created_at = now() - interval '31 days' WHERE requester_id = $1 AND addressee_id = $2`, a, b); err != nil {
|
||||
t.Fatalf("backdate: %v", err)
|
||||
}
|
||||
if got, _ := svc.ListIncomingRequests(ctx, b); len(got) != 0 {
|
||||
t.Fatalf("expired request still incoming: %v", got)
|
||||
}
|
||||
if err := svc.SendFriendRequest(ctx, a, b); err != nil {
|
||||
t.Fatalf("resend after expiry: %v", err)
|
||||
}
|
||||
if got, _ := svc.ListIncomingRequests(ctx, b); len(got) != 1 || got[0] != a {
|
||||
t.Fatalf("re-sent request not incoming: %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFriendCodeSelfAndSingleUse(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
svc := newSocialService()
|
||||
a := provisionAccount(t)
|
||||
code, err := svc.IssueFriendCode(ctx, a)
|
||||
if err != nil {
|
||||
t.Fatalf("issue: %v", err)
|
||||
}
|
||||
if _, err := svc.RedeemFriendCode(ctx, a, code.Code); !errors.Is(err, social.ErrSelfRelation) {
|
||||
t.Fatalf("self redeem = %v, want ErrSelfRelation", err)
|
||||
}
|
||||
b := provisionAccount(t)
|
||||
if _, err := svc.RedeemFriendCode(ctx, b, code.Code); err != nil {
|
||||
t.Fatalf("redeem: %v", err)
|
||||
}
|
||||
// Single-use: redeeming the same code again fails.
|
||||
if _, err := svc.RedeemFriendCode(ctx, provisionAccount(t), code.Code); !errors.Is(err, social.ErrFriendCodeInvalid) {
|
||||
t.Fatalf("reused code = %v, want ErrFriendCodeInvalid", err)
|
||||
}
|
||||
if friends, _ := svc.ListFriends(ctx, b); len(friends) != 1 || friends[0] != a {
|
||||
t.Fatalf("friends of b = %v, want [a]", friends)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListBlocks(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
svc := newSocialService()
|
||||
a, b := provisionAccount(t), provisionAccount(t)
|
||||
if err := svc.Block(ctx, a, b); err != nil {
|
||||
t.Fatalf("block: %v", err)
|
||||
}
|
||||
blocked, err := svc.ListBlocks(ctx, a)
|
||||
if err != nil {
|
||||
t.Fatalf("list blocks: %v", err)
|
||||
}
|
||||
if len(blocked) != 1 || blocked[0] != b {
|
||||
t.Fatalf("blocks = %v, want [b]", blocked)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatPostListAndBlocks(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
svc := newSocialService()
|
||||
|
||||
Reference in New Issue
Block a user