Files
scrabble-game/backend/internal/robot/namevariety_test.go
T
Ilia Denisov 183e08ec80
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 9s
CI / integration (pull_request) Successful in 15s
CI / ui (pull_request) Successful in 48s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m10s
feat(robot): per-game display names from a wide name corpus
Decouple the displayed opponent name from the small pool of durable robot
accounts: the disguised auto-match robot now gets a freshly composed name each
game, stamped on a new game_players.display_name seat snapshot. The snapshot
also captures humans' names, freezing what an opponent sees for the life of a
game (a later rename no longer rewrites past games); readers fall back to the
account's current name for pre-migration rows.

Names come from a wide composed corpus (internal/robot/namevariety.go): Western
locales (EN/DE/ES/IT/FR/PT), native Japanese/Chinese names, a gender-agreed
Russian pool, and human-style handles. Routing keeps Pick's spirit -- a Russian
game draws Cyrillic + <=20% Latin and never a CJK script; an English game the
full corpus -- via robot.PickNamed.

Loosen account.ValidateDisplayName (and the UI mirror) to admit a trailing run
of up to five digits, so "Player2007"-style handles are valid for humans too
and the disguised robot stays indistinguishable.

Migration 00007 adds game_players.display_name (additive, NOT NULL DEFAULT '');
jet regenerated. Docs (ARCHITECTURE 7, FUNCTIONAL + _ru, PLAN, README) updated.
2026-06-16 12:28:04 +02:00

115 lines
3.5 KiB
Go

package robot
import (
"testing"
"unicode"
"scrabble/backend/internal/account"
"scrabble/backend/internal/engine"
)
// anyRune reports whether any rune of s satisfies f.
func anyRune(s string, f func(rune) bool) bool {
for _, r := range s {
if f(r) {
return true
}
}
return false
}
func isCyrillic(r rune) bool { return unicode.Is(unicode.Cyrillic, r) }
func isLatin(r rune) bool { return unicode.Is(unicode.Latin, r) }
func isCJK(r rune) bool {
return unicode.Is(unicode.Han, r) || unicode.Is(unicode.Hiragana, r) || unicode.Is(unicode.Katakana, r)
}
// TestComposeRandomNameValid is the disguise invariant: every per-game name a robot can
// receive, in every variant, must be a name a human could also pick — i.e. it must pass
// account.ValidateDisplayName — so a disguised robot stays indistinguishable.
func TestComposeRandomNameValid(t *testing.T) {
t.Parallel()
for _, v := range []engine.Variant{engine.VariantEnglish, engine.VariantRussianScrabble, engine.VariantErudit} {
for i := 0; i < 5000; i++ {
name := composeRandomName(v)
if _, err := account.ValidateDisplayName(name); err != nil {
t.Fatalf("variant %s produced an invalid display name %q: %v", v, name, err)
}
}
}
}
// TestRussianVariantScriptMix checks a Russian game stays Cyrillic-dominant with a small
// Latin minority and never a CJK script, so a Russian-speaking audience is not paired
// with visibly foreign-scripted opponents.
func TestRussianVariantScriptMix(t *testing.T) {
t.Parallel()
const n = 6000
var cyr, lat, cjk int
for i := 0; i < n; i++ {
name := composeRandomName(engine.VariantRussianScrabble)
switch {
case anyRune(name, isCyrillic):
cyr++
case anyRune(name, isCJK):
cjk++
case anyRune(name, isLatin):
lat++
}
}
if cjk != 0 {
t.Errorf("a Russian game must never draw a CJK name, got %d of %d", cjk, n)
}
if lat == 0 {
t.Errorf("a Russian game should show some Latin names, got 0 of %d", n)
}
if cyr <= lat {
t.Errorf("Cyrillic names should dominate a Russian game: cyrillic=%d latin=%d", cyr, lat)
}
// The Latin share is governed by latinShareInRussian (~20%); allow a wide band.
if share := lat * 100 / n; share < 8 || share > 32 {
t.Errorf("Latin share %d%% out of the expected ~%d%% band", share, latinShareInRussian)
}
}
// TestEnglishVariantScriptMix checks an English game draws an international crowd: mostly
// Western Latin names, a visible Japanese/Chinese minority, and never Cyrillic.
func TestEnglishVariantScriptMix(t *testing.T) {
t.Parallel()
const n = 6000
var lat, cjk, cyr int
for i := 0; i < n; i++ {
name := composeRandomName(engine.VariantEnglish)
switch {
case anyRune(name, isCJK):
cjk++
case anyRune(name, isCyrillic):
cyr++
case anyRune(name, isLatin):
lat++
}
}
if cyr != 0 {
t.Errorf("an English game must not draw Cyrillic names, got %d of %d", cyr, n)
}
if cjk == 0 {
t.Errorf("an English game should show some CJK names, got 0 of %d", n)
}
if lat <= cjk {
t.Errorf("Latin names should dominate an English game: latin=%d cjk=%d", lat, cjk)
}
}
// TestComposeRandomNameVariety checks the corpus yields a large spread of distinct names
// (the point of the feature), so the same opponent rarely recurs.
func TestComposeRandomNameVariety(t *testing.T) {
t.Parallel()
seen := make(map[string]struct{})
for i := 0; i < 2000; i++ {
seen[composeRandomName(engine.VariantEnglish)] = struct{}{}
}
if len(seen) < 1000 {
t.Errorf("expected a wide spread of names, got only %d distinct of 2000", len(seen))
}
}