420 lines
12 KiB
Go
420 lines
12 KiB
Go
package submitapplication_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"log/slog"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"galaxy/lobby/internal/adapters/applicationinmem"
|
|
"galaxy/lobby/internal/adapters/gameinmem"
|
|
"galaxy/lobby/internal/adapters/membershipinmem"
|
|
"galaxy/lobby/internal/adapters/mocks"
|
|
"galaxy/lobby/internal/adapters/racenameinmem"
|
|
"galaxy/lobby/internal/domain/application"
|
|
"galaxy/lobby/internal/domain/common"
|
|
"galaxy/lobby/internal/domain/game"
|
|
"galaxy/lobby/internal/domain/membership"
|
|
"galaxy/lobby/internal/ports"
|
|
"galaxy/lobby/internal/service/shared"
|
|
"galaxy/lobby/internal/service/submitapplication"
|
|
"galaxy/notificationintent"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"go.uber.org/mock/gomock"
|
|
)
|
|
|
|
type intentRec struct {
|
|
mu sync.Mutex
|
|
published []notificationintent.Intent
|
|
err error
|
|
}
|
|
|
|
func (r *intentRec) record(_ context.Context, intent notificationintent.Intent) (string, error) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
if r.err != nil {
|
|
return "", r.err
|
|
}
|
|
r.published = append(r.published, intent)
|
|
return "1", nil
|
|
}
|
|
|
|
func (r *intentRec) snapshot() []notificationintent.Intent {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
return append([]notificationintent.Intent(nil), r.published...)
|
|
}
|
|
|
|
func (r *intentRec) setErr(err error) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
r.err = err
|
|
}
|
|
|
|
type userRec struct {
|
|
mu sync.Mutex
|
|
elig map[string]ports.Eligibility
|
|
failures map[string]error
|
|
}
|
|
|
|
func (r *userRec) record(_ context.Context, userID string) (ports.Eligibility, error) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
if err, ok := r.failures[userID]; ok {
|
|
return ports.Eligibility{}, err
|
|
}
|
|
if e, ok := r.elig[userID]; ok {
|
|
return e, nil
|
|
}
|
|
return ports.Eligibility{Exists: false}, nil
|
|
}
|
|
|
|
func (r *userRec) setEligibility(userID string, e ports.Eligibility) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
if r.elig == nil {
|
|
r.elig = make(map[string]ports.Eligibility)
|
|
}
|
|
r.elig[userID] = e
|
|
}
|
|
|
|
func (r *userRec) setFailure(userID string, err error) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
if r.failures == nil {
|
|
r.failures = make(map[string]error)
|
|
}
|
|
r.failures[userID] = err
|
|
}
|
|
|
|
func newIntentMock(t *testing.T, rec *intentRec) *mocks.MockIntentPublisher {
|
|
t.Helper()
|
|
m := mocks.NewMockIntentPublisher(gomock.NewController(t))
|
|
m.EXPECT().Publish(gomock.Any(), gomock.Any()).DoAndReturn(rec.record).AnyTimes()
|
|
return m
|
|
}
|
|
|
|
func newUserMock(t *testing.T, rec *userRec) *mocks.MockUserService {
|
|
t.Helper()
|
|
m := mocks.NewMockUserService(gomock.NewController(t))
|
|
m.EXPECT().GetEligibility(gomock.Any(), gomock.Any()).DoAndReturn(rec.record).AnyTimes()
|
|
return m
|
|
}
|
|
|
|
const (
|
|
defaultRaceName = "SolarPilot"
|
|
otherRaceName = "VoidRunner"
|
|
)
|
|
|
|
func silentLogger() *slog.Logger {
|
|
return slog.New(slog.NewTextHandler(io.Discard, nil))
|
|
}
|
|
|
|
func fixedClock(at time.Time) func() time.Time {
|
|
return func() time.Time { return at }
|
|
}
|
|
|
|
type fixedIDs struct {
|
|
applicationID common.ApplicationID
|
|
membershipID common.MembershipID
|
|
}
|
|
|
|
func (f fixedIDs) NewGameID() (common.GameID, error) { return "", errors.New("not used") }
|
|
func (f fixedIDs) NewApplicationID() (common.ApplicationID, error) {
|
|
return f.applicationID, nil
|
|
}
|
|
func (f fixedIDs) NewInviteID() (common.InviteID, error) {
|
|
return "", errors.New("not used")
|
|
}
|
|
func (f fixedIDs) NewMembershipID() (common.MembershipID, error) {
|
|
return f.membershipID, nil
|
|
}
|
|
|
|
type fixture struct {
|
|
now time.Time
|
|
games *gameinmem.Store
|
|
memberships *membershipinmem.Store
|
|
applications *applicationinmem.Store
|
|
directory *racenameinmem.Directory
|
|
users *userRec
|
|
usersMock *mocks.MockUserService
|
|
intents *intentRec
|
|
intentsMock *mocks.MockIntentPublisher
|
|
ids fixedIDs
|
|
openPublicGameID common.GameID
|
|
defaultUserID string
|
|
}
|
|
|
|
func newFixture(t *testing.T) *fixture {
|
|
t.Helper()
|
|
now := time.Date(2026, 4, 25, 10, 0, 0, 0, time.UTC)
|
|
dir, err := racenameinmem.NewDirectory(racenameinmem.WithClock(fixedClock(now)))
|
|
require.NoError(t, err)
|
|
users := &userRec{}
|
|
users.setEligibility("user-1", ports.Eligibility{Exists: true, CanLogin: true, CanJoinGame: true})
|
|
games := gameinmem.NewStore()
|
|
memberships := membershipinmem.NewStore()
|
|
applications := applicationinmem.NewStore()
|
|
|
|
gameRecord, err := game.New(game.NewGameInput{
|
|
GameID: "game-public",
|
|
GameName: "Galactic Open",
|
|
GameType: game.GameTypePublic,
|
|
MinPlayers: 2,
|
|
MaxPlayers: 4,
|
|
StartGapHours: 2,
|
|
StartGapPlayers: 1,
|
|
EnrollmentEndsAt: now.Add(24 * time.Hour),
|
|
TurnSchedule: "0 */6 * * *",
|
|
TargetEngineVersion: "1.0.0",
|
|
Now: now,
|
|
})
|
|
require.NoError(t, err)
|
|
gameRecord.Status = game.StatusEnrollmentOpen
|
|
require.NoError(t, games.Save(context.Background(), gameRecord))
|
|
|
|
intents := &intentRec{}
|
|
return &fixture{
|
|
now: now,
|
|
games: games,
|
|
memberships: memberships,
|
|
applications: applications,
|
|
directory: dir,
|
|
users: users,
|
|
usersMock: newUserMock(t, users),
|
|
intents: intents,
|
|
intentsMock: newIntentMock(t, intents),
|
|
ids: fixedIDs{applicationID: "application-fixed", membershipID: "membership-fixed"},
|
|
openPublicGameID: gameRecord.GameID,
|
|
defaultUserID: "user-1",
|
|
}
|
|
}
|
|
|
|
func newService(t *testing.T, f *fixture) *submitapplication.Service {
|
|
t.Helper()
|
|
svc, err := submitapplication.NewService(submitapplication.Dependencies{
|
|
Games: f.games,
|
|
Memberships: f.memberships,
|
|
Applications: f.applications,
|
|
Users: f.usersMock,
|
|
Directory: f.directory,
|
|
Intents: f.intentsMock,
|
|
IDs: f.ids,
|
|
Clock: fixedClock(f.now),
|
|
Logger: silentLogger(),
|
|
})
|
|
require.NoError(t, err)
|
|
return svc
|
|
}
|
|
|
|
func defaultInput(f *fixture) submitapplication.Input {
|
|
return submitapplication.Input{
|
|
Actor: shared.NewUserActor(f.defaultUserID),
|
|
GameID: f.openPublicGameID,
|
|
RaceName: defaultRaceName,
|
|
}
|
|
}
|
|
|
|
func TestHandleHappyPath(t *testing.T) {
|
|
t.Parallel()
|
|
f := newFixture(t)
|
|
svc := newService(t, f)
|
|
|
|
got, err := svc.Handle(context.Background(), defaultInput(f))
|
|
require.NoError(t, err)
|
|
assert.Equal(t, application.StatusSubmitted, got.Status)
|
|
assert.Equal(t, common.ApplicationID("application-fixed"), got.ApplicationID)
|
|
assert.Equal(t, defaultRaceName, got.RaceName)
|
|
|
|
intents := f.intents.snapshot()
|
|
require.Len(t, intents, 1)
|
|
assert.Equal(t, notificationintent.NotificationTypeLobbyApplicationSubmitted, intents[0].NotificationType)
|
|
assert.Equal(t, notificationintent.AudienceKindAdminEmail, intents[0].AudienceKind)
|
|
}
|
|
|
|
func TestHandleAdminActorForbidden(t *testing.T) {
|
|
t.Parallel()
|
|
f := newFixture(t)
|
|
svc := newService(t, f)
|
|
|
|
input := defaultInput(f)
|
|
input.Actor = shared.NewAdminActor()
|
|
|
|
_, err := svc.Handle(context.Background(), input)
|
|
require.ErrorIs(t, err, shared.ErrForbidden)
|
|
}
|
|
|
|
func TestHandleGameNotFound(t *testing.T) {
|
|
t.Parallel()
|
|
f := newFixture(t)
|
|
svc := newService(t, f)
|
|
|
|
input := defaultInput(f)
|
|
input.GameID = "game-missing"
|
|
|
|
_, err := svc.Handle(context.Background(), input)
|
|
require.ErrorIs(t, err, game.ErrNotFound)
|
|
}
|
|
|
|
func TestHandleNonPublicGameConflict(t *testing.T) {
|
|
t.Parallel()
|
|
f := newFixture(t)
|
|
now := f.now
|
|
private, err := game.New(game.NewGameInput{
|
|
GameID: "game-private",
|
|
GameName: "Private Match",
|
|
GameType: game.GameTypePrivate,
|
|
OwnerUserID: "user-9",
|
|
MinPlayers: 2,
|
|
MaxPlayers: 4,
|
|
StartGapHours: 2,
|
|
StartGapPlayers: 1,
|
|
EnrollmentEndsAt: now.Add(24 * time.Hour),
|
|
TurnSchedule: "0 */6 * * *",
|
|
TargetEngineVersion: "1.0.0",
|
|
Now: now,
|
|
})
|
|
require.NoError(t, err)
|
|
private.Status = game.StatusEnrollmentOpen
|
|
require.NoError(t, f.games.Save(context.Background(), private))
|
|
|
|
svc := newService(t, f)
|
|
input := defaultInput(f)
|
|
input.GameID = private.GameID
|
|
|
|
_, err = svc.Handle(context.Background(), input)
|
|
require.ErrorIs(t, err, game.ErrConflict)
|
|
}
|
|
|
|
func TestHandleWrongStatusConflict(t *testing.T) {
|
|
t.Parallel()
|
|
f := newFixture(t)
|
|
// Force status back to draft.
|
|
rec, err := f.games.Get(context.Background(), f.openPublicGameID)
|
|
require.NoError(t, err)
|
|
rec.Status = game.StatusDraft
|
|
require.NoError(t, f.games.Save(context.Background(), rec))
|
|
|
|
svc := newService(t, f)
|
|
_, err = svc.Handle(context.Background(), defaultInput(f))
|
|
require.ErrorIs(t, err, game.ErrConflict)
|
|
}
|
|
|
|
func TestHandleUserMissingEligibilityDenied(t *testing.T) {
|
|
t.Parallel()
|
|
f := newFixture(t)
|
|
// Default-missing returns Exists=false.
|
|
svc := newService(t, f)
|
|
input := defaultInput(f)
|
|
input.Actor = shared.NewUserActor("user-unknown")
|
|
|
|
_, err := svc.Handle(context.Background(), input)
|
|
require.ErrorIs(t, err, shared.ErrEligibilityDenied)
|
|
}
|
|
|
|
func TestHandleCanJoinGameFalseEligibilityDenied(t *testing.T) {
|
|
t.Parallel()
|
|
f := newFixture(t)
|
|
f.users.setEligibility("user-blocked", ports.Eligibility{Exists: true, CanLogin: true, CanJoinGame: false})
|
|
svc := newService(t, f)
|
|
input := defaultInput(f)
|
|
input.Actor = shared.NewUserActor("user-blocked")
|
|
|
|
_, err := svc.Handle(context.Background(), input)
|
|
require.ErrorIs(t, err, shared.ErrEligibilityDenied)
|
|
}
|
|
|
|
func TestHandleUserServiceUnavailable(t *testing.T) {
|
|
t.Parallel()
|
|
f := newFixture(t)
|
|
f.users.setFailure(f.defaultUserID, ports.ErrUserServiceUnavailable)
|
|
svc := newService(t, f)
|
|
|
|
_, err := svc.Handle(context.Background(), defaultInput(f))
|
|
require.ErrorIs(t, err, shared.ErrServiceUnavailable)
|
|
require.ErrorIs(t, err, ports.ErrUserServiceUnavailable)
|
|
}
|
|
|
|
func TestHandleRosterFullConflict(t *testing.T) {
|
|
t.Parallel()
|
|
f := newFixture(t)
|
|
// Capacity = max_players(4) + start_gap_players(1) = 5; seed 5 active members.
|
|
for i := range 5 {
|
|
member, err := membership.New(membership.NewMembershipInput{
|
|
MembershipID: common.MembershipID("membership-seed-" + string(rune('a'+i))),
|
|
GameID: f.openPublicGameID,
|
|
UserID: "seed-user-" + string(rune('a'+i)),
|
|
RaceName: "Seed " + string(rune('A'+i)),
|
|
CanonicalKey: "seed" + string(rune('a'+i)),
|
|
Now: f.now,
|
|
})
|
|
require.NoError(t, err)
|
|
require.NoError(t, f.memberships.Save(context.Background(), member))
|
|
}
|
|
|
|
svc := newService(t, f)
|
|
_, err := svc.Handle(context.Background(), defaultInput(f))
|
|
require.ErrorIs(t, err, game.ErrConflict)
|
|
}
|
|
|
|
func TestHandleRaceNameTakenByOtherUser(t *testing.T) {
|
|
t.Parallel()
|
|
f := newFixture(t)
|
|
require.NoError(t, f.directory.Reserve(context.Background(), "game-other", "user-2", defaultRaceName))
|
|
|
|
svc := newService(t, f)
|
|
_, err := svc.Handle(context.Background(), defaultInput(f))
|
|
require.ErrorIs(t, err, ports.ErrNameTaken)
|
|
}
|
|
|
|
func TestHandleInvalidRaceName(t *testing.T) {
|
|
t.Parallel()
|
|
f := newFixture(t)
|
|
svc := newService(t, f)
|
|
input := defaultInput(f)
|
|
input.RaceName = " " // empty after trim — ports.ErrInvalidName
|
|
|
|
_, err := svc.Handle(context.Background(), input)
|
|
require.Error(t, err)
|
|
require.True(t, errors.Is(err, ports.ErrInvalidName))
|
|
}
|
|
|
|
func TestHandleDuplicateActiveApplicationConflict(t *testing.T) {
|
|
t.Parallel()
|
|
f := newFixture(t)
|
|
// Pre-existing submitted application by the same user for the same game.
|
|
existing, err := application.New(application.NewApplicationInput{
|
|
ApplicationID: "application-existing",
|
|
GameID: f.openPublicGameID,
|
|
ApplicantUserID: f.defaultUserID,
|
|
RaceName: otherRaceName,
|
|
Now: f.now,
|
|
})
|
|
require.NoError(t, err)
|
|
require.NoError(t, f.applications.Save(context.Background(), existing))
|
|
|
|
svc := newService(t, f)
|
|
_, err = svc.Handle(context.Background(), defaultInput(f))
|
|
require.ErrorIs(t, err, application.ErrConflict)
|
|
}
|
|
|
|
func TestHandlePublishFailureDoesNotRollback(t *testing.T) {
|
|
t.Parallel()
|
|
f := newFixture(t)
|
|
f.intents.setErr(errors.New("publish failed"))
|
|
svc := newService(t, f)
|
|
|
|
got, err := svc.Handle(context.Background(), defaultInput(f))
|
|
require.NoError(t, err)
|
|
assert.Equal(t, application.StatusSubmitted, got.Status)
|
|
|
|
stored, err := f.applications.Get(context.Background(), got.ApplicationID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, application.StatusSubmitted, stored.Status)
|
|
}
|