Stage 3: game domain (lifecycle, journal+cache, hint, word-check, GCG, stats)
internal/game drives the engine over a single match and owns everything the
engine does not: event-sourced persistence (a games row + an append-only decoded
move journal, with the live engine.Game kept warm in a cache and rebuilt by
replay on a miss), the play/pass/exchange/resign transitions with
validate-at-submit scoring, an unlimited score/legality preview, the hint
(per-game allowance + profile wallet), the word-check tool with complaint
capture, per-player game state, history and GCG export (Poslfit dialect), and a
background turn-timeout sweeper that auto-resigns overdue turns honouring each
player's daily away window. Like Stages 1-2 it is a service/store layer with no
HTTP; the gateway surface lands in Stage 6.
Engine: additive decoded domain API (Direction, SubmitPlay/SubmitExchange/
EvaluatePlay/HintView/Hand, MoveRecord.{Dir,MainRow,MainCol}, Registry.Lookup,
ParseVariant) so internal/game never imports scrabble-solver; and a Resign fix
so the resigner keeps their score yet never wins (the other player wins a
two-player game). Timeout reuses Resign.
Persistence: migration 00002 adds games, game_players, game_moves, complaints,
account_stats and extends accounts with away_start/away_end/hint_balance; go-jet
regenerated. account gained SpendHint. Config adds BACKEND_DICT_DIR (required),
BACKEND_DICT_VERSION, BACKEND_GAME_TIMEOUT_SWEEP_INTERVAL, BACKEND_GAME_CACHE_TTL;
main loads the registry at boot (hard dependency) and starts the sweeper.
Tests: engine resign + decoded-API tests; game unit tests (GCG, away-window
boundaries, hint budget, cache, keyed mutex, payload); inttest integration
(lifecycle, replay equivalence, timeout sweep with away grace, resign stats,
hint policy, word-check/complaint, per-game-lock). Docs (PLAN, ARCHITECTURE,
FUNCTIONAL +_ru, TESTING, README) updated.
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -15,6 +17,7 @@ import (
|
||||
tcpostgres "github.com/testcontainers/testcontainers-go/modules/postgres"
|
||||
"github.com/testcontainers/testcontainers-go/wait"
|
||||
|
||||
"scrabble/backend/internal/engine"
|
||||
"scrabble/backend/internal/postgres"
|
||||
)
|
||||
|
||||
@@ -22,6 +25,13 @@ import (
|
||||
// hydrated once by TestMain.
|
||||
var testDB *sql.DB
|
||||
|
||||
// testRegistry holds the committed dictionaries the game service plays over,
|
||||
// loaded once under testDictVersion (the version the game tests pin).
|
||||
var testRegistry *engine.Registry
|
||||
|
||||
// testDictVersion is the single dictionary version the integration suite loads.
|
||||
const testDictVersion = "v1"
|
||||
|
||||
const (
|
||||
pgImage = "postgres:17-alpine"
|
||||
pgDatabase = "scrabble_backend"
|
||||
@@ -89,10 +99,27 @@ func runSuite(m *testing.M) (int, error) {
|
||||
return 0, fmt.Errorf("apply migrations: %w", err)
|
||||
}
|
||||
|
||||
reg, err := engine.Open(dictDir(), testDictVersion)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("load dictionaries: %w", err)
|
||||
}
|
||||
defer func() { _ = reg.Close() }()
|
||||
|
||||
testDB = db
|
||||
testRegistry = reg
|
||||
return m.Run(), nil
|
||||
}
|
||||
|
||||
// dictDir resolves the committed DAWG directory: BACKEND_DICT_DIR when set (CI),
|
||||
// otherwise the sibling scrabble-solver checkout located relative to this file.
|
||||
func dictDir() string {
|
||||
if dir := os.Getenv("BACKEND_DICT_DIR"); dir != "" {
|
||||
return dir
|
||||
}
|
||||
_, file, _, _ := runtime.Caller(0)
|
||||
return filepath.Join(filepath.Dir(file), "..", "..", "..", "..", "scrabble-solver", "dawg")
|
||||
}
|
||||
|
||||
// withSearchPath rewrites dsn so every connection pins search_path to schema.
|
||||
func withSearchPath(dsn, schema string) (string, error) {
|
||||
u, err := url.Parse(dsn)
|
||||
|
||||
Reference in New Issue
Block a user