336 lines
10 KiB
Go
336 lines
10 KiB
Go
package submitapplication_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"log/slog"
|
|
"testing"
|
|
"time"
|
|
|
|
"galaxy/lobby/internal/adapters/applicationstub"
|
|
"galaxy/lobby/internal/adapters/gamestub"
|
|
"galaxy/lobby/internal/adapters/intentpubstub"
|
|
"galaxy/lobby/internal/adapters/membershipstub"
|
|
"galaxy/lobby/internal/adapters/racenamestub"
|
|
"galaxy/lobby/internal/adapters/userservicestub"
|
|
"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"
|
|
)
|
|
|
|
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 *gamestub.Store
|
|
memberships *membershipstub.Store
|
|
applications *applicationstub.Store
|
|
directory *racenamestub.Directory
|
|
users *userservicestub.Service
|
|
intents *intentpubstub.Publisher
|
|
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 := racenamestub.NewDirectory(racenamestub.WithClock(fixedClock(now)))
|
|
require.NoError(t, err)
|
|
users := userservicestub.NewService()
|
|
users.SetEligibility("user-1", ports.Eligibility{Exists: true, CanLogin: true, CanJoinGame: true})
|
|
games := gamestub.NewStore()
|
|
memberships := membershipstub.NewStore()
|
|
applications := applicationstub.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))
|
|
|
|
return &fixture{
|
|
now: now,
|
|
games: games,
|
|
memberships: memberships,
|
|
applications: applications,
|
|
directory: dir,
|
|
users: users,
|
|
intents: intentpubstub.NewPublisher(),
|
|
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.users,
|
|
Directory: f.directory,
|
|
Intents: f.intents,
|
|
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.Published()
|
|
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.SetError(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)
|
|
}
|