R6(c): centralize shared integration-test fixtures in helpers.go
Move the cross-file integration fixtures — the service constructors (newGameService/newSocialService/newRobotService/newMatchmaker), the game-assembly helpers (newMirror/newGameWithSeats/newDraftGame), account provisioning (provisionAccount/provisionGuest) and the stats reader — out of the domain test files (newGameService alone was used by 10 files) into a single backend/internal/inttest/helpers.go. Helpers used by a single file stay local. Pure relocation: the helper bodies are unchanged, no test logic changes; the imports the moves left unused are pruned. go vet -tags=integration is clean.
This commit is contained in:
@@ -4,88 +4,17 @@ package inttest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"scrabble/backend/internal/account"
|
||||
"scrabble/backend/internal/engine"
|
||||
"scrabble/backend/internal/game"
|
||||
)
|
||||
|
||||
// newGameService builds a game service over the shared pool and registry.
|
||||
func newGameService() *game.Service {
|
||||
return game.NewService(
|
||||
game.NewStore(testDB),
|
||||
account.NewStore(testDB),
|
||||
testRegistry,
|
||||
game.Config{
|
||||
DictDir: dictDir(),
|
||||
DictVersion: testDictVersion,
|
||||
TimeoutSweepInterval: time.Minute,
|
||||
CacheTTL: time.Hour,
|
||||
},
|
||||
zap.NewNop(),
|
||||
)
|
||||
}
|
||||
|
||||
// provisionAccount creates a fresh durable account and returns its id.
|
||||
func provisionAccount(t *testing.T) uuid.UUID {
|
||||
t.Helper()
|
||||
acc, err := account.NewStore(testDB).ProvisionByIdentity(context.Background(), account.KindTelegram, "tg-"+uuid.NewString())
|
||||
if err != nil {
|
||||
t.Fatalf("provision account: %v", err)
|
||||
}
|
||||
return acc.ID
|
||||
}
|
||||
|
||||
// openingSeed returns a seed whose fresh two-player English opening rack has a
|
||||
// legal move, so a greedy mirror can drive a game.
|
||||
func openingSeed(t *testing.T) int64 {
|
||||
t.Helper()
|
||||
for seed := int64(1); seed <= 200; seed++ {
|
||||
g, err := engine.New(testRegistry, engine.Options{Variant: engine.VariantEnglish, Version: testDictVersion, Players: 2, Seed: seed})
|
||||
if err != nil {
|
||||
t.Fatalf("engine new: %v", err)
|
||||
}
|
||||
if _, ok := g.HintView(); ok {
|
||||
return seed
|
||||
}
|
||||
}
|
||||
t.Fatal("no opening seed found")
|
||||
return 0
|
||||
}
|
||||
|
||||
// newMirror builds a parallel engine game with the same seed, used to compute
|
||||
// legal moves to feed the service under test.
|
||||
func newMirror(t *testing.T, seed int64, players int) *engine.Game {
|
||||
t.Helper()
|
||||
g, err := engine.New(testRegistry, engine.Options{Variant: engine.VariantEnglish, Version: testDictVersion, Players: players, Seed: seed})
|
||||
if err != nil {
|
||||
t.Fatalf("mirror new: %v", err)
|
||||
}
|
||||
return g
|
||||
}
|
||||
|
||||
// readStats reads an account's statistics row.
|
||||
func readStats(t *testing.T, id uuid.UUID) (wins, losses, draws, maxGame, maxWord int, found bool) {
|
||||
t.Helper()
|
||||
row := testDB.QueryRowContext(context.Background(),
|
||||
`SELECT wins, losses, draws, max_game_points, max_word_points FROM backend.account_stats WHERE account_id = $1`, id)
|
||||
if err := row.Scan(&wins, &losses, &draws, &maxGame, &maxWord); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return 0, 0, 0, 0, 0, false
|
||||
}
|
||||
t.Fatalf("read stats: %v", err)
|
||||
}
|
||||
return wins, losses, draws, maxGame, maxWord, true
|
||||
}
|
||||
|
||||
// TestListForAccount checks the lobby "my games" query: it returns exactly the
|
||||
// games the account is seated in (each with its seats), and nothing for an outsider.
|
||||
func TestListForAccount(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user