195 lines
6.2 KiB
Go
195 lines
6.2 KiB
Go
package applicationstore_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"galaxy/lobby/internal/adapters/postgres/applicationstore"
|
|
"galaxy/lobby/internal/adapters/postgres/gamestore"
|
|
"galaxy/lobby/internal/adapters/postgres/internal/pgtest"
|
|
"galaxy/lobby/internal/domain/application"
|
|
"galaxy/lobby/internal/domain/common"
|
|
"galaxy/lobby/internal/domain/game"
|
|
"galaxy/lobby/internal/ports"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestMain(m *testing.M) { pgtest.RunMain(m) }
|
|
|
|
func newStores(t *testing.T) (*gamestore.Store, *applicationstore.Store) {
|
|
t.Helper()
|
|
pgtest.TruncateAll(t)
|
|
gs, err := gamestore.New(gamestore.Config{
|
|
DB: pgtest.Ensure(t).Pool(), OperationTimeout: pgtest.OperationTimeout,
|
|
})
|
|
require.NoError(t, err)
|
|
as, err := applicationstore.New(applicationstore.Config{
|
|
DB: pgtest.Ensure(t).Pool(), OperationTimeout: pgtest.OperationTimeout,
|
|
})
|
|
require.NoError(t, err)
|
|
return gs, as
|
|
}
|
|
|
|
func seedGame(t *testing.T, gs *gamestore.Store, id string) game.Game {
|
|
t.Helper()
|
|
now := time.Date(2026, 4, 23, 12, 0, 0, 0, time.UTC)
|
|
g, err := game.New(game.NewGameInput{
|
|
GameID: common.GameID(id),
|
|
GameName: "Game " + id,
|
|
GameType: game.GameTypePublic,
|
|
MinPlayers: 2,
|
|
MaxPlayers: 8,
|
|
StartGapHours: 12,
|
|
StartGapPlayers: 2,
|
|
EnrollmentEndsAt: now.Add(7 * 24 * time.Hour),
|
|
TurnSchedule: "0 18 * * *",
|
|
TargetEngineVersion: "v1.0.0",
|
|
Now: now,
|
|
})
|
|
require.NoError(t, err)
|
|
require.NoError(t, gs.Save(context.Background(), g))
|
|
return g
|
|
}
|
|
|
|
func newApplication(t *testing.T, id, gameID, userID string) application.Application {
|
|
t.Helper()
|
|
a, err := application.New(application.NewApplicationInput{
|
|
ApplicationID: common.ApplicationID(id),
|
|
GameID: common.GameID(gameID),
|
|
ApplicantUserID: userID,
|
|
RaceName: "Pilot " + id,
|
|
Now: time.Date(2026, 4, 23, 12, 0, 0, 0, time.UTC),
|
|
})
|
|
require.NoError(t, err)
|
|
return a
|
|
}
|
|
|
|
func TestSaveAndGet(t *testing.T) {
|
|
ctx := context.Background()
|
|
gs, as := newStores(t)
|
|
seedGame(t, gs, "game-001")
|
|
|
|
rec := newApplication(t, "application-001", "game-001", "user-a")
|
|
require.NoError(t, as.Save(ctx, rec))
|
|
|
|
got, err := as.Get(ctx, rec.ApplicationID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, rec.ApplicationID, got.ApplicationID)
|
|
assert.Equal(t, application.StatusSubmitted, got.Status)
|
|
assert.Equal(t, "user-a", got.ApplicantUserID)
|
|
assert.Nil(t, got.DecidedAt)
|
|
}
|
|
|
|
func TestSaveRejectsNonSubmittedRecord(t *testing.T) {
|
|
ctx := context.Background()
|
|
gs, as := newStores(t)
|
|
seedGame(t, gs, "game-001")
|
|
|
|
rec := newApplication(t, "application-001", "game-001", "user-a")
|
|
rec.Status = application.StatusApproved
|
|
require.Error(t, as.Save(ctx, rec))
|
|
}
|
|
|
|
func TestSavePartialUniqueRejectsSecondActiveForSameUserGame(t *testing.T) {
|
|
ctx := context.Background()
|
|
gs, as := newStores(t)
|
|
seedGame(t, gs, "game-001")
|
|
|
|
a1 := newApplication(t, "application-001", "game-001", "user-a")
|
|
require.NoError(t, as.Save(ctx, a1))
|
|
|
|
// second submission by the same user against the same game must fail.
|
|
a2 := newApplication(t, "application-002", "game-001", "user-a")
|
|
err := as.Save(ctx, a2)
|
|
require.ErrorIs(t, err, application.ErrConflict)
|
|
}
|
|
|
|
func TestSavePartialUniqueAllowsResubmitAfterRejection(t *testing.T) {
|
|
ctx := context.Background()
|
|
gs, as := newStores(t)
|
|
seedGame(t, gs, "game-001")
|
|
|
|
a1 := newApplication(t, "application-001", "game-001", "user-a")
|
|
require.NoError(t, as.Save(ctx, a1))
|
|
|
|
require.NoError(t, as.UpdateStatus(ctx, ports.UpdateApplicationStatusInput{
|
|
ApplicationID: a1.ApplicationID,
|
|
ExpectedFrom: application.StatusSubmitted,
|
|
To: application.StatusRejected,
|
|
At: a1.CreatedAt.Add(time.Minute),
|
|
}))
|
|
|
|
// after rejection a new submission for the same (user, game) is allowed.
|
|
a2 := newApplication(t, "application-002", "game-001", "user-a")
|
|
require.NoError(t, as.Save(ctx, a2))
|
|
}
|
|
|
|
func TestUpdateStatusReturnsConflictOnExpectedFromMismatch(t *testing.T) {
|
|
ctx := context.Background()
|
|
gs, as := newStores(t)
|
|
seedGame(t, gs, "game-001")
|
|
|
|
rec := newApplication(t, "application-001", "game-001", "user-a")
|
|
require.NoError(t, as.Save(ctx, rec))
|
|
|
|
// First, transition the row to approved.
|
|
require.NoError(t, as.UpdateStatus(ctx, ports.UpdateApplicationStatusInput{
|
|
ApplicationID: rec.ApplicationID,
|
|
ExpectedFrom: application.StatusSubmitted,
|
|
To: application.StatusApproved,
|
|
At: rec.CreatedAt.Add(time.Minute),
|
|
}))
|
|
|
|
// Second attempt claims status is still submitted: (submitted, rejected)
|
|
// is a valid domain transition, but the row is already approved, so the
|
|
// adapter must surface ErrConflict on the row-level mismatch.
|
|
err := as.UpdateStatus(ctx, ports.UpdateApplicationStatusInput{
|
|
ApplicationID: rec.ApplicationID,
|
|
ExpectedFrom: application.StatusSubmitted,
|
|
To: application.StatusRejected,
|
|
At: rec.CreatedAt.Add(2 * time.Minute),
|
|
})
|
|
require.ErrorIs(t, err, application.ErrConflict)
|
|
}
|
|
|
|
func TestUpdateStatusReturnsNotFoundForMissing(t *testing.T) {
|
|
ctx := context.Background()
|
|
_, as := newStores(t)
|
|
err := as.UpdateStatus(ctx, ports.UpdateApplicationStatusInput{
|
|
ApplicationID: common.ApplicationID("application-missing"),
|
|
ExpectedFrom: application.StatusSubmitted,
|
|
To: application.StatusApproved,
|
|
At: time.Now().UTC(),
|
|
})
|
|
require.ErrorIs(t, err, application.ErrNotFound)
|
|
}
|
|
|
|
func TestGetByGameAndGetByUser(t *testing.T) {
|
|
ctx := context.Background()
|
|
gs, as := newStores(t)
|
|
seedGame(t, gs, "game-001")
|
|
seedGame(t, gs, "game-002")
|
|
|
|
require.NoError(t, as.Save(ctx, newApplication(t, "application-001", "game-001", "user-a")))
|
|
require.NoError(t, as.Save(ctx, newApplication(t, "application-002", "game-001", "user-b")))
|
|
require.NoError(t, as.Save(ctx, newApplication(t, "application-003", "game-002", "user-a")))
|
|
|
|
g1, err := as.GetByGame(ctx, common.GameID("game-001"))
|
|
require.NoError(t, err)
|
|
assert.Len(t, g1, 2)
|
|
|
|
userA, err := as.GetByUser(ctx, "user-a")
|
|
require.NoError(t, err)
|
|
assert.Len(t, userA, 2)
|
|
}
|
|
|
|
func TestGetMissingReturnsNotFound(t *testing.T) {
|
|
ctx := context.Background()
|
|
_, as := newStores(t)
|
|
_, err := as.Get(ctx, common.ApplicationID("application-missing"))
|
|
require.ErrorIs(t, err, application.ErrNotFound)
|
|
}
|