41a642ef97
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 9s
CI / integration (pull_request) Successful in 13s
CI / ui (pull_request) Successful in 37s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m8s
Enrich the in-app live stream into a delta channel so the UI renders a move from the event without a follow-up game.state, and make the matchmaking poll a stream-down fallback. - pkg/fbs: trailing fields on opponent_moved (move+game+bag_len), your_turn (move_count), match_found (state), game_over (game), notify (account/invitation/state), MoveResult (rack+bag_len); regenerate Go + TS. - backend: notify owns the FB encoding (encode.go + payload.go input structs); game/lobby/social map their domain types in. emitMove builds the move delta; game.Service.InitialState feeds match_found/game_started the recipient's initial StateView; friends/invitations notify carry their account/invitation. The move-commit response (submit_play/pass/exchange/resign) returns the actor's refilled rack + bag size. - gateway: MoveResult transcode carries rack+bag_len. - ui: pure lib/gamedelta.ts reducer advances the per-game cache keyed on move_count (idempotent + gap-safe); app.svelte seeds the cache on match_found/game_started; Game.svelte applies the delta (commit/pass/exchange/resign drop their load()); NewGame polls only while app.streamAlive is false. - docs: ARCHITECTURE §10, FUNCTIONAL(+ru), backend/gateway/ui READMEs; PRERELEASE R4 marked done + Refinements.
290 lines
9.1 KiB
Go
290 lines
9.1 KiB
Go
package lobby
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"go.uber.org/zap"
|
|
|
|
"scrabble/backend/internal/engine"
|
|
"scrabble/backend/internal/game"
|
|
"scrabble/backend/internal/notify"
|
|
)
|
|
|
|
// fakeCreator records the games a matchmaker asks it to start.
|
|
type fakeCreator struct {
|
|
created []game.CreateParams
|
|
err error
|
|
}
|
|
|
|
func (f *fakeCreator) Create(_ context.Context, p game.CreateParams) (game.Game, error) {
|
|
if f.err != nil {
|
|
return game.Game{}, f.err
|
|
}
|
|
f.created = append(f.created, p)
|
|
return game.Game{ID: uuid.New(), Players: len(p.Seats)}, nil
|
|
}
|
|
|
|
// InitialState satisfies GameCreator; the matchmaker reads it to enrich match_found. The pairing
|
|
// tests assert on matching behaviour, not the payload, so an empty state is enough.
|
|
func (f *fakeCreator) InitialState(_ context.Context, _, _ uuid.UUID) (notify.PlayerState, error) {
|
|
return notify.PlayerState{}, nil
|
|
}
|
|
|
|
// fakeRobots is a RobotProvider returning a fixed robot id, or an error to model
|
|
// an empty pool. It records the variant of the last substitution request.
|
|
type fakeRobots struct {
|
|
id uuid.UUID
|
|
err error
|
|
lastVariant engine.Variant
|
|
}
|
|
|
|
func (f *fakeRobots) Pick(variant engine.Variant) (uuid.UUID, error) {
|
|
f.lastVariant = variant
|
|
if f.err != nil {
|
|
return uuid.Nil, f.err
|
|
}
|
|
return f.id, nil
|
|
}
|
|
|
|
// testWaitDelay is long enough that the reaper never fires in the pairing tests
|
|
// (which do not run it); the substitution tests drive reap directly.
|
|
const testWaitDelay = 10 * time.Second
|
|
|
|
func newTestMatchmaker(creator GameCreator, robotID uuid.UUID) *Matchmaker {
|
|
return NewMatchmaker(creator, &fakeRobots{id: robotID}, testWaitDelay, zap.NewNop())
|
|
}
|
|
|
|
func seatsContain(seats []uuid.UUID, want ...uuid.UUID) bool {
|
|
for _, w := range want {
|
|
found := false
|
|
for _, s := range seats {
|
|
if s == w {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func TestMatchmakerPairsTwoHumans(t *testing.T) {
|
|
creator := &fakeCreator{}
|
|
mm := newTestMatchmaker(creator, uuid.New())
|
|
ctx := context.Background()
|
|
a, b := uuid.New(), uuid.New()
|
|
|
|
r1, err := mm.Enqueue(ctx, a, engine.VariantEnglish)
|
|
if err != nil {
|
|
t.Fatalf("enqueue a: %v", err)
|
|
}
|
|
if r1.Matched {
|
|
t.Fatal("first enqueue must wait, not match")
|
|
}
|
|
if mm.QueueLen(engine.VariantEnglish) != 1 {
|
|
t.Fatalf("queue len = %d, want 1", mm.QueueLen(engine.VariantEnglish))
|
|
}
|
|
|
|
r2, err := mm.Enqueue(ctx, b, engine.VariantEnglish)
|
|
if err != nil {
|
|
t.Fatalf("enqueue b: %v", err)
|
|
}
|
|
if !r2.Matched {
|
|
t.Fatal("second enqueue must match")
|
|
}
|
|
if mm.QueueLen(engine.VariantEnglish) != 0 {
|
|
t.Fatalf("queue len = %d, want 0 after match", mm.QueueLen(engine.VariantEnglish))
|
|
}
|
|
if len(creator.created) != 1 {
|
|
t.Fatalf("created %d games, want 1", len(creator.created))
|
|
}
|
|
p := creator.created[0]
|
|
if len(p.Seats) != 2 || !seatsContain(p.Seats, a, b) {
|
|
t.Errorf("seats = %v, want both %s and %s", p.Seats, a, b)
|
|
}
|
|
if p.TurnTimeout != game.DefaultTurnTimeout || !p.HintsAllowed || p.HintsPerPlayer != autoMatchHintsPerPlayer {
|
|
t.Errorf("auto-match defaults not applied: %+v", p)
|
|
}
|
|
|
|
// The waiting opponent learns of the match through Poll, exactly once.
|
|
got, err := mm.Poll(ctx, a)
|
|
if err != nil {
|
|
t.Fatalf("poll a: %v", err)
|
|
}
|
|
if !got.Matched || got.Game.ID != r2.Game.ID {
|
|
t.Errorf("poll a = %+v, want the matched game %s", got, r2.Game.ID)
|
|
}
|
|
if again, _ := mm.Poll(ctx, a); again.Matched {
|
|
t.Error("poll result must drain after the first read")
|
|
}
|
|
}
|
|
|
|
func TestMatchmakerAlreadyQueued(t *testing.T) {
|
|
mm := newTestMatchmaker(&fakeCreator{}, uuid.New())
|
|
ctx := context.Background()
|
|
a := uuid.New()
|
|
if _, err := mm.Enqueue(ctx, a, engine.VariantEnglish); err != nil {
|
|
t.Fatalf("enqueue: %v", err)
|
|
}
|
|
if _, err := mm.Enqueue(ctx, a, engine.VariantEnglish); !errors.Is(err, ErrAlreadyQueued) {
|
|
t.Fatalf("second enqueue err = %v, want ErrAlreadyQueued", err)
|
|
}
|
|
}
|
|
|
|
func TestMatchmakerCancel(t *testing.T) {
|
|
mm := newTestMatchmaker(&fakeCreator{}, uuid.New())
|
|
ctx := context.Background()
|
|
a := uuid.New()
|
|
if _, err := mm.Enqueue(ctx, a, engine.VariantEnglish); err != nil {
|
|
t.Fatalf("enqueue: %v", err)
|
|
}
|
|
if !mm.Cancel(ctx, a) {
|
|
t.Fatal("cancel of a queued account must report true")
|
|
}
|
|
if mm.QueueLen(engine.VariantEnglish) != 0 {
|
|
t.Fatalf("queue len = %d, want 0 after cancel", mm.QueueLen(engine.VariantEnglish))
|
|
}
|
|
if mm.Cancel(ctx, a) {
|
|
t.Fatal("cancel of an unqueued account must report false")
|
|
}
|
|
}
|
|
|
|
func TestMatchmakerVariantsAreSeparate(t *testing.T) {
|
|
creator := &fakeCreator{}
|
|
mm := newTestMatchmaker(creator, uuid.New())
|
|
ctx := context.Background()
|
|
if _, err := mm.Enqueue(ctx, uuid.New(), engine.VariantEnglish); err != nil {
|
|
t.Fatalf("enqueue en: %v", err)
|
|
}
|
|
if _, err := mm.Enqueue(ctx, uuid.New(), engine.VariantRussianScrabble); err != nil {
|
|
t.Fatalf("enqueue ru: %v", err)
|
|
}
|
|
if len(creator.created) != 0 {
|
|
t.Fatalf("different variants must not match; created %d", len(creator.created))
|
|
}
|
|
if mm.QueueLen(engine.VariantEnglish) != 1 || mm.QueueLen(engine.VariantRussianScrabble) != 1 {
|
|
t.Errorf("each variant pool should hold one waiter")
|
|
}
|
|
}
|
|
|
|
func TestMatchmakerFIFO(t *testing.T) {
|
|
creator := &fakeCreator{}
|
|
mm := newTestMatchmaker(creator, uuid.New())
|
|
ctx := context.Background()
|
|
a, b, c := uuid.New(), uuid.New(), uuid.New()
|
|
for _, id := range []uuid.UUID{a, b, c} {
|
|
if _, err := mm.Enqueue(ctx, id, engine.VariantEnglish); err != nil {
|
|
t.Fatalf("enqueue %s: %v", id, err)
|
|
}
|
|
}
|
|
// a waited, b matched a (oldest), c waits.
|
|
if len(creator.created) != 1 {
|
|
t.Fatalf("created %d games, want 1", len(creator.created))
|
|
}
|
|
if !seatsContain(creator.created[0].Seats, a, b) {
|
|
t.Errorf("FIFO match should pair a and b, got %v", creator.created[0].Seats)
|
|
}
|
|
if mm.QueueLen(engine.VariantEnglish) != 1 {
|
|
t.Errorf("c should remain queued; len = %d", mm.QueueLen(engine.VariantEnglish))
|
|
}
|
|
}
|
|
|
|
func TestMatchmakerReaperSubstitutesRobot(t *testing.T) {
|
|
creator := &fakeCreator{}
|
|
robotID := uuid.New()
|
|
mm := newTestMatchmaker(creator, robotID)
|
|
base := time.Now()
|
|
mm.clock = func() time.Time { return base }
|
|
ctx := context.Background()
|
|
a := uuid.New()
|
|
|
|
if _, err := mm.Enqueue(ctx, a, engine.VariantEnglish); err != nil {
|
|
t.Fatalf("enqueue: %v", err)
|
|
}
|
|
|
|
mm.Reap(ctx, base.Add(5*time.Second)) // before the wait window
|
|
if len(creator.created) != 0 || mm.QueueLen(engine.VariantEnglish) != 1 {
|
|
t.Fatalf("must not substitute before the wait: created=%d queued=%d", len(creator.created), mm.QueueLen(engine.VariantEnglish))
|
|
}
|
|
|
|
mm.Reap(ctx, base.Add(testWaitDelay+time.Second)) // past the wait window
|
|
if len(creator.created) != 1 {
|
|
t.Fatalf("created %d games, want 1 after substitution", len(creator.created))
|
|
}
|
|
if !seatsContain(creator.created[0].Seats, a, robotID) {
|
|
t.Errorf("substituted game seats = %v, want human %s and robot %s", creator.created[0].Seats, a, robotID)
|
|
}
|
|
if mm.QueueLen(engine.VariantEnglish) != 0 {
|
|
t.Errorf("waiter should be dequeued after substitution")
|
|
}
|
|
got, err := mm.Poll(ctx, a)
|
|
if err != nil || !got.Matched {
|
|
t.Errorf("poll after substitution = %+v err=%v, want matched", got, err)
|
|
}
|
|
}
|
|
|
|
func TestMatchmakerReaperSkipsCancelled(t *testing.T) {
|
|
creator := &fakeCreator{}
|
|
mm := newTestMatchmaker(creator, uuid.New())
|
|
base := time.Now()
|
|
mm.clock = func() time.Time { return base }
|
|
ctx := context.Background()
|
|
a := uuid.New()
|
|
|
|
if _, err := mm.Enqueue(ctx, a, engine.VariantEnglish); err != nil {
|
|
t.Fatalf("enqueue: %v", err)
|
|
}
|
|
mm.Cancel(ctx, a)
|
|
mm.Reap(ctx, base.Add(testWaitDelay+time.Second))
|
|
if len(creator.created) != 0 {
|
|
t.Errorf("a cancelled waiter must not be substituted; created %d", len(creator.created))
|
|
}
|
|
}
|
|
|
|
// TestMatchmakerCancelClearsPendingResult covers the race where the reaper substitutes a
|
|
// robot just before the player cancels: Cancel must drop the pending result so the
|
|
// abandoned game never surfaces through Poll (Stage 17).
|
|
func TestMatchmakerCancelClearsPendingResult(t *testing.T) {
|
|
creator := &fakeCreator{}
|
|
mm := newTestMatchmaker(creator, uuid.New())
|
|
base := time.Now()
|
|
mm.clock = func() time.Time { return base }
|
|
ctx := context.Background()
|
|
a := uuid.New()
|
|
|
|
if _, err := mm.Enqueue(ctx, a, engine.VariantEnglish); err != nil {
|
|
t.Fatalf("enqueue: %v", err)
|
|
}
|
|
mm.Reap(ctx, base.Add(testWaitDelay+time.Second)) // substitution stores a pending result
|
|
mm.Cancel(ctx, a) // ... then the player cancels
|
|
if got, _ := mm.Poll(ctx, a); got.Matched {
|
|
t.Error("cancel must drop the pending substituted game; Poll still matched")
|
|
}
|
|
}
|
|
|
|
func TestMatchmakerReaperDefersWithoutRobot(t *testing.T) {
|
|
creator := &fakeCreator{}
|
|
mm := NewMatchmaker(creator, &fakeRobots{err: errors.New("empty pool")}, testWaitDelay, zap.NewNop())
|
|
base := time.Now()
|
|
mm.clock = func() time.Time { return base }
|
|
ctx := context.Background()
|
|
a := uuid.New()
|
|
|
|
if _, err := mm.Enqueue(ctx, a, engine.VariantEnglish); err != nil {
|
|
t.Fatalf("enqueue: %v", err)
|
|
}
|
|
mm.Reap(ctx, base.Add(testWaitDelay+time.Second))
|
|
if len(creator.created) != 0 {
|
|
t.Errorf("no robot available: must not create a game; created %d", len(creator.created))
|
|
}
|
|
if mm.QueueLen(engine.VariantEnglish) != 1 {
|
|
t.Errorf("waiter must stay queued when substitution is deferred; len %d", mm.QueueLen(engine.VariantEnglish))
|
|
}
|
|
}
|