Files
Ilia Denisov 85baabe4ba
Tests · Go / test (push) Successful in 6s
Tests · Integration / integration (push) Successful in 10s
Tests · Go / test (pull_request) Successful in 5s
Tests · Integration / integration (pull_request) Successful in 10s
Stage 5: robot opponent (pool, seed-derived strategy, move driver, matchmaker substitution)
- internal/robot: durable kind='robot' account pool (migration 00004); every
  per-game and per-turn choice derived deterministically from the game seed
  (restart-stable FNV mix); a background move driver; margin targeting (band
  1-30, closest-to-band); right-skewed [2,90]min delays (median ~10m);
  opponent-anchored sleep with +/-3h drift; daytime nudge reply + proactive
  12h nudge; friend/chat blocked via profile toggles.
- engine.Candidates (decoded ranked plays); game.Candidates + RobotTurns;
  social.LastNudgeAt.
- matchmaker: 10s wait then robot substitution (reaper) + Poll delivery seam.
- config (BACKEND_ROBOT_DRIVE_INTERVAL, BACKEND_LOBBY_ROBOT_WAIT,
  BACKEND_LOBBY_REAPER_INTERVAL); main wiring + boot-time pool provisioning.
- metrics: robot account_stats (authoritative balance) + robot_games_finished_total
  OTel counter + per-finish log.
- docs: PLAN, ARCHITECTURE, FUNCTIONAL(+ru), TESTING, README; account.go comment.
- tests: robot strategy units, matchmaker reaper/Poll, engine.Candidates; inttest
  robot full-game / substitution / proactive-nudge.
2026-06-02 21:02:20 +02:00

191 lines
6.6 KiB
Go

package robot
import (
"sort"
"testing"
"time"
"scrabble/backend/internal/engine"
)
// TestPlayToWinDistribution checks the once-per-game decision is fixed per seed
// and lands near the 40% target over many games.
func TestPlayToWinDistribution(t *testing.T) {
const n = 20000
wins := 0
for seed := int64(1); seed <= n; seed++ {
if playToWin(seed) {
wins++
}
if playToWin(seed) != playToWin(seed) {
t.Fatalf("playToWin not deterministic for seed %d", seed)
}
}
pct := float64(wins) / float64(n) * 100
if pct < 37 || pct > 43 {
t.Errorf("play-to-win rate = %.1f%%, want ~40%% (37-43)", pct)
}
}
// TestMoveDelayBoundsAndDeterminism checks every sampled delay stays in
// [2min, 90min) and is reproducible for a (seed, moveCount).
func TestMoveDelayBoundsAndDeterminism(t *testing.T) {
for seed := int64(1); seed <= 200; seed++ {
for mc := 0; mc < 50; mc++ {
d := moveDelay(seed, mc)
if d < 2*time.Minute || d >= 90*time.Minute {
t.Fatalf("delay %s out of [2m,90m) for seed=%d mc=%d", d, seed, mc)
}
if moveDelay(seed, mc) != d {
t.Fatalf("delay not deterministic for seed=%d mc=%d", seed, mc)
}
}
}
}
// TestMoveDelaySkew checks the distribution is right-skewed with the intended
// ~10-minute median: most delays are short, the mean sits above the median.
func TestMoveDelaySkew(t *testing.T) {
const n = 20000
mins := make([]float64, 0, n)
var sum float64
for mc := 0; mc < n; mc++ {
m := moveDelay(42, mc).Minutes()
mins = append(mins, m)
sum += m
}
sort.Float64s(mins)
median := mins[n/2]
mean := sum / float64(n)
if median < 7 || median > 13 {
t.Errorf("median delay = %.1f min, want ~10 (7-13)", median)
}
if mean <= median {
t.Errorf("mean %.1f should exceed median %.1f (right skew)", mean, median)
}
}
// TestSelectMovePlayToWinKeepsLeadSmall checks the winning robot prefers an
// in-band move with the smallest resulting lead.
func TestSelectMovePlayToWinKeepsLeadSmall(t *testing.T) {
cands := plays(50, 20, 5, 2) // margins 50,20,5,2 with scores even
d := selectMove(cands, 100, 100, true, marginBand{1, 30}, nil, 0)
if d.kind != decidePlay || d.move.Score != 2 {
t.Errorf("got kind=%d score=%d, want play score=2 (smallest in-band lead)", d.kind, d.move.Score)
}
}
// TestSelectMovePlayToLoseKeepsDeficitSmall checks the losing robot prefers the
// in-band move with the smallest deficit.
func TestSelectMovePlayToLoseKeepsDeficitSmall(t *testing.T) {
cands := plays(50, 20, 15, 5) // myScore 80, opp 100 → margins 30,0,-5,-15
d := selectMove(cands, 80, 100, false, marginBand{1, 30}, nil, 0)
if d.kind != decidePlay || d.move.Score != 15 {
t.Errorf("got kind=%d score=%d, want play score=15 (smallest deficit in band)", d.kind, d.move.Score)
}
}
// TestSelectMoveFallbackBehind checks that when even the best play cannot reach
// the band the winning robot takes the highest-scoring move (best catch-up).
func TestSelectMoveFallbackBehind(t *testing.T) {
cands := plays(10, 5) // myScore 50, opp 100 → margins -40,-45, both below band
d := selectMove(cands, 50, 100, true, marginBand{1, 30}, nil, 0)
if d.move.Score != 10 {
t.Errorf("got score=%d, want 10 (closest to band from below)", d.move.Score)
}
}
// TestSelectMoveFallbackOvershoot checks that when every play overshoots the band
// the winning robot takes the lowest-scoring move (keeps the lead near the cap).
func TestSelectMoveFallbackOvershoot(t *testing.T) {
cands := plays(40, 10) // myScore 100, opp 50 → margins 90,60, both above band
d := selectMove(cands, 100, 50, true, marginBand{1, 30}, nil, 0)
if d.move.Score != 10 {
t.Errorf("got score=%d, want 10 (closest to band from above)", d.move.Score)
}
}
// TestSelectMoveNoPlay checks the exchange-or-pass fallback.
func TestSelectMoveNoPlay(t *testing.T) {
rack := []string{"A", "B", "C"}
if d := selectMove(nil, 0, 0, true, defaultBand, rack, 5); d.kind != decideExchange || len(d.exchange) != 3 {
t.Errorf("with a refillable bag want exchange of 3, got kind=%d n=%d", d.kind, len(d.exchange))
}
if d := selectMove(nil, 0, 0, true, defaultBand, rack, 2); d.kind != decidePass {
t.Errorf("with a short bag want pass, got kind=%d", d.kind)
}
if d := selectMove(nil, 0, 0, true, defaultBand, nil, 9); d.kind != decidePass {
t.Errorf("with an empty rack want pass, got kind=%d", d.kind)
}
}
// TestSleepDriftBounds checks the drift stays within ±3h and is deterministic.
func TestSleepDriftBounds(t *testing.T) {
for seed := int64(1); seed <= 5000; seed++ {
d := sleepDrift(seed)
if d < -3*time.Hour || d > 3*time.Hour {
t.Fatalf("drift %s out of ±3h for seed %d", d, seed)
}
if sleepDrift(seed) != d {
t.Fatalf("drift not deterministic for seed %d", seed)
}
}
}
// TestAsleep covers the window, the drift shift, a real timezone and the
// midnight wrap.
func TestAsleep(t *testing.T) {
at := func(tz string, y int, mo time.Month, d, h int) time.Time {
loc, err := time.LoadLocation(tz)
if err != nil {
t.Fatalf("load %s: %v", tz, err)
}
return time.Date(y, mo, d, h, 0, 0, 0, loc)
}
cases := []struct {
name string
tz string
drift time.Duration
now time.Time
want bool
}{
{"utc night", "UTC", 0, at("UTC", 2024, 1, 1, 3), true},
{"utc day", "UTC", 0, at("UTC", 2024, 1, 1, 12), false},
{"utc edge end", "UTC", 0, at("UTC", 2024, 1, 1, 7), false},
{"drift+3 shifts earlier", "UTC", 3 * time.Hour, at("UTC", 2024, 1, 1, 22), true},
{"drift+3 awake midday", "UTC", 3 * time.Hour, at("UTC", 2024, 1, 1, 5), false},
{"drift-3 shifts later", "UTC", -3 * time.Hour, at("UTC", 2024, 1, 1, 9), true},
{"tokyo asleep", "Asia/Tokyo", 0, at("UTC", 2024, 1, 1, 18), true}, // 03:00 JST
{"tokyo awake", "Asia/Tokyo", 0, at("UTC", 2024, 1, 1, 0), false}, // 09:00 JST
{"bad tz falls back to utc", "Nowhere/Bad", 0, at("UTC", 2024, 1, 1, 3), true},
}
for _, c := range cases {
if got := asleep(c.tz, c.drift, c.now); got != c.want {
t.Errorf("%s: asleep = %v, want %v", c.name, got, c.want)
}
}
}
// TestMixDeterministic checks the mixer is stable (across calls, and so across
// restarts) and salt-sensitive.
func TestMixDeterministic(t *testing.T) {
if mix(7, "win") != mix(7, "win") {
t.Error("mix not stable for the same inputs")
}
if mix(7, "win") == mix(7, "delay") {
t.Error("mix should differ by salt")
}
if mix(7, "delay", 1) == mix(7, "delay", 2) {
t.Error("mix should differ by move index")
}
}
// plays builds candidate plays carrying only the given scores (ranked as passed).
func plays(scores ...int) []engine.MoveRecord {
out := make([]engine.MoveRecord, len(scores))
for i, s := range scores {
out[i] = engine.MoveRecord{Action: engine.ActionPlay, Score: s}
}
return out
}