feat: game lobby service
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
// Package declineinvite implements the `lobby.invite.decline` message type.
|
||||
// It transitions the invite from created to declined when the actor is the
|
||||
// invitee. Per `lobby/README.md` §Invite Lifecycle, no notification is
|
||||
// published in v1.
|
||||
package declineinvite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"galaxy/lobby/internal/domain/common"
|
||||
"galaxy/lobby/internal/domain/invite"
|
||||
"galaxy/lobby/internal/logging"
|
||||
"galaxy/lobby/internal/ports"
|
||||
"galaxy/lobby/internal/service/shared"
|
||||
"galaxy/lobby/internal/telemetry"
|
||||
)
|
||||
|
||||
// Service executes the private-game invite decline use case.
|
||||
type Service struct {
|
||||
invites ports.InviteStore
|
||||
clock func() time.Time
|
||||
logger *slog.Logger
|
||||
telemetry *telemetry.Runtime
|
||||
}
|
||||
|
||||
// Dependencies groups the collaborators used by Service.
|
||||
type Dependencies struct {
|
||||
// Invites loads the target invite and applies the
|
||||
// created → declined transition.
|
||||
Invites ports.InviteStore
|
||||
|
||||
// Clock supplies the wall-clock used for DecidedAt.
|
||||
Clock func() time.Time
|
||||
|
||||
// Logger records structured service-level events.
|
||||
Logger *slog.Logger
|
||||
|
||||
// Telemetry records the `lobby.invite.outcomes` counter on each
|
||||
// successful decline. Optional; nil disables metric emission.
|
||||
Telemetry *telemetry.Runtime
|
||||
}
|
||||
|
||||
// NewService constructs one Service with deps.
|
||||
func NewService(deps Dependencies) (*Service, error) {
|
||||
if deps.Invites == nil {
|
||||
return nil, errors.New("new decline invite service: nil invite store")
|
||||
}
|
||||
clock := deps.Clock
|
||||
if clock == nil {
|
||||
clock = time.Now
|
||||
}
|
||||
logger := deps.Logger
|
||||
if logger == nil {
|
||||
logger = slog.Default()
|
||||
}
|
||||
return &Service{
|
||||
invites: deps.Invites,
|
||||
clock: clock,
|
||||
logger: logger.With("service", "lobby.declineinvite"),
|
||||
telemetry: deps.Telemetry,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Input stores the arguments required to decline one invite.
|
||||
type Input struct {
|
||||
// Actor identifies the caller. Must be the invitee.
|
||||
Actor shared.Actor
|
||||
|
||||
// GameID identifies the game referenced by the request path; it must
|
||||
// match the loaded invite's GameID.
|
||||
GameID common.GameID
|
||||
|
||||
// InviteID identifies the target invite.
|
||||
InviteID common.InviteID
|
||||
}
|
||||
|
||||
// Handle authorizes the invitee, validates the invite state, transitions the
|
||||
// invite to declined, and returns the updated record.
|
||||
func (service *Service) Handle(ctx context.Context, input Input) (invite.Invite, error) {
|
||||
if service == nil {
|
||||
return invite.Invite{}, errors.New("decline invite: nil service")
|
||||
}
|
||||
if ctx == nil {
|
||||
return invite.Invite{}, errors.New("decline invite: nil context")
|
||||
}
|
||||
if err := input.Actor.Validate(); err != nil {
|
||||
return invite.Invite{}, fmt.Errorf("decline invite: actor: %w", err)
|
||||
}
|
||||
if !input.Actor.IsUser() {
|
||||
return invite.Invite{}, fmt.Errorf(
|
||||
"%w: only the invited user may decline an invite",
|
||||
shared.ErrForbidden,
|
||||
)
|
||||
}
|
||||
if err := input.GameID.Validate(); err != nil {
|
||||
return invite.Invite{}, fmt.Errorf("decline invite: %w", err)
|
||||
}
|
||||
if err := input.InviteID.Validate(); err != nil {
|
||||
return invite.Invite{}, fmt.Errorf("decline invite: %w", err)
|
||||
}
|
||||
|
||||
inv, err := service.invites.Get(ctx, input.InviteID)
|
||||
if err != nil {
|
||||
return invite.Invite{}, fmt.Errorf("decline invite: %w", err)
|
||||
}
|
||||
if inv.GameID != input.GameID {
|
||||
return invite.Invite{}, fmt.Errorf(
|
||||
"decline invite: invite %q does not belong to game %q: %w",
|
||||
inv.InviteID, input.GameID, invite.ErrNotFound,
|
||||
)
|
||||
}
|
||||
if inv.InviteeUserID != input.Actor.UserID {
|
||||
return invite.Invite{}, fmt.Errorf(
|
||||
"%w: invite is addressed to a different user",
|
||||
shared.ErrForbidden,
|
||||
)
|
||||
}
|
||||
if inv.Status != invite.StatusCreated {
|
||||
return invite.Invite{}, fmt.Errorf(
|
||||
"decline invite: status %q is not %q: %w",
|
||||
inv.Status, invite.StatusCreated, invite.ErrConflict,
|
||||
)
|
||||
}
|
||||
|
||||
now := service.clock().UTC()
|
||||
if err := service.invites.UpdateStatus(ctx, ports.UpdateInviteStatusInput{
|
||||
InviteID: inv.InviteID,
|
||||
ExpectedFrom: invite.StatusCreated,
|
||||
To: invite.StatusDeclined,
|
||||
At: now,
|
||||
}); err != nil {
|
||||
return invite.Invite{}, fmt.Errorf("decline invite: %w", err)
|
||||
}
|
||||
|
||||
declined, err := service.invites.Get(ctx, inv.InviteID)
|
||||
if err != nil {
|
||||
return invite.Invite{}, fmt.Errorf("decline invite: %w", err)
|
||||
}
|
||||
|
||||
service.telemetry.RecordInviteOutcome(ctx, "declined")
|
||||
|
||||
logArgs := []any{
|
||||
"game_id", declined.GameID.String(),
|
||||
"invite_id", declined.InviteID.String(),
|
||||
"user_id", declined.InviteeUserID,
|
||||
}
|
||||
logArgs = append(logArgs, logging.ContextAttrs(ctx)...)
|
||||
service.logger.InfoContext(ctx, "invite declined", logArgs...)
|
||||
return declined, nil
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
package declineinvite_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"galaxy/lobby/internal/adapters/invitestub"
|
||||
"galaxy/lobby/internal/domain/common"
|
||||
"galaxy/lobby/internal/domain/invite"
|
||||
"galaxy/lobby/internal/ports"
|
||||
"galaxy/lobby/internal/service/declineinvite"
|
||||
"galaxy/lobby/internal/service/shared"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const (
|
||||
gameID = common.GameID("game-private")
|
||||
ownerUserID = "user-owner"
|
||||
inviteeUserID = "user-invitee"
|
||||
)
|
||||
|
||||
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 fixture struct {
|
||||
now time.Time
|
||||
invites *invitestub.Store
|
||||
}
|
||||
|
||||
func newFixture(t *testing.T) *fixture {
|
||||
t.Helper()
|
||||
return &fixture{
|
||||
now: time.Date(2026, 4, 25, 10, 0, 0, 0, time.UTC),
|
||||
invites: invitestub.NewStore(),
|
||||
}
|
||||
}
|
||||
|
||||
func newService(t *testing.T, f *fixture) *declineinvite.Service {
|
||||
t.Helper()
|
||||
svc, err := declineinvite.NewService(declineinvite.Dependencies{
|
||||
Invites: f.invites,
|
||||
Clock: fixedClock(f.now),
|
||||
Logger: silentLogger(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
return svc
|
||||
}
|
||||
|
||||
func seedCreatedInvite(t *testing.T, f *fixture, inviteID common.InviteID, invitee string) invite.Invite {
|
||||
t.Helper()
|
||||
inv, err := invite.New(invite.NewInviteInput{
|
||||
InviteID: inviteID,
|
||||
GameID: gameID,
|
||||
InviterUserID: ownerUserID,
|
||||
InviteeUserID: invitee,
|
||||
Now: f.now,
|
||||
ExpiresAt: f.now.Add(24 * time.Hour),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, f.invites.Save(context.Background(), inv))
|
||||
return inv
|
||||
}
|
||||
|
||||
func TestDeclineHappyPath(t *testing.T) {
|
||||
t.Parallel()
|
||||
f := newFixture(t)
|
||||
inv := seedCreatedInvite(t, f, "invite-1", inviteeUserID)
|
||||
svc := newService(t, f)
|
||||
|
||||
got, err := svc.Handle(context.Background(), declineinvite.Input{
|
||||
Actor: shared.NewUserActor(inviteeUserID),
|
||||
GameID: gameID,
|
||||
InviteID: inv.InviteID,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, invite.StatusDeclined, got.Status)
|
||||
require.NotNil(t, got.DecidedAt)
|
||||
assert.Equal(t, f.now, *got.DecidedAt)
|
||||
}
|
||||
|
||||
func TestDeclineAdminActorForbidden(t *testing.T) {
|
||||
t.Parallel()
|
||||
f := newFixture(t)
|
||||
inv := seedCreatedInvite(t, f, "invite-1", inviteeUserID)
|
||||
svc := newService(t, f)
|
||||
|
||||
_, err := svc.Handle(context.Background(), declineinvite.Input{
|
||||
Actor: shared.NewAdminActor(),
|
||||
GameID: gameID,
|
||||
InviteID: inv.InviteID,
|
||||
})
|
||||
require.ErrorIs(t, err, shared.ErrForbidden)
|
||||
}
|
||||
|
||||
func TestDeclineNonInviteeForbidden(t *testing.T) {
|
||||
t.Parallel()
|
||||
f := newFixture(t)
|
||||
inv := seedCreatedInvite(t, f, "invite-1", inviteeUserID)
|
||||
svc := newService(t, f)
|
||||
|
||||
_, err := svc.Handle(context.Background(), declineinvite.Input{
|
||||
Actor: shared.NewUserActor("user-impostor"),
|
||||
GameID: gameID,
|
||||
InviteID: inv.InviteID,
|
||||
})
|
||||
require.ErrorIs(t, err, shared.ErrForbidden)
|
||||
}
|
||||
|
||||
func TestDeclineNotFound(t *testing.T) {
|
||||
t.Parallel()
|
||||
f := newFixture(t)
|
||||
svc := newService(t, f)
|
||||
|
||||
_, err := svc.Handle(context.Background(), declineinvite.Input{
|
||||
Actor: shared.NewUserActor(inviteeUserID),
|
||||
GameID: gameID,
|
||||
InviteID: "invite-missing",
|
||||
})
|
||||
require.ErrorIs(t, err, invite.ErrNotFound)
|
||||
}
|
||||
|
||||
func TestDeclineCrossGameNotFound(t *testing.T) {
|
||||
t.Parallel()
|
||||
f := newFixture(t)
|
||||
inv := seedCreatedInvite(t, f, "invite-1", inviteeUserID)
|
||||
svc := newService(t, f)
|
||||
|
||||
_, err := svc.Handle(context.Background(), declineinvite.Input{
|
||||
Actor: shared.NewUserActor(inviteeUserID),
|
||||
GameID: "game-other",
|
||||
InviteID: inv.InviteID,
|
||||
})
|
||||
require.ErrorIs(t, err, invite.ErrNotFound)
|
||||
}
|
||||
|
||||
func TestDeclineWrongStatusConflict(t *testing.T) {
|
||||
t.Parallel()
|
||||
f := newFixture(t)
|
||||
inv := seedCreatedInvite(t, f, "invite-1", inviteeUserID)
|
||||
require.NoError(t, f.invites.UpdateStatus(context.Background(), ports.UpdateInviteStatusInput{
|
||||
InviteID: inv.InviteID,
|
||||
ExpectedFrom: invite.StatusCreated,
|
||||
To: invite.StatusRevoked,
|
||||
At: f.now,
|
||||
}))
|
||||
svc := newService(t, f)
|
||||
|
||||
_, err := svc.Handle(context.Background(), declineinvite.Input{
|
||||
Actor: shared.NewUserActor(inviteeUserID),
|
||||
GameID: gameID,
|
||||
InviteID: inv.InviteID,
|
||||
})
|
||||
require.ErrorIs(t, err, invite.ErrConflict)
|
||||
}
|
||||
Reference in New Issue
Block a user