Files
dingoandClaude Sonnet 5 5df2f1b681 Merge control portal work from RPGTheory/aeternalis-portal
RPGTheory/aeternalis-portal had diverged from this repo (the git/CI/CD
copy) with an in-progress control portal feature that was never synced
back here, so none of it had gone through CI or been deployed:

- New site/game/control routing split: index.html becomes a world
  directory landing page (site.ts), game.html is the renamed former
  index.html (main.ts), control.html is a new admin panel
  (control.ts, portal-shared.ts, portal.css).
- Server: new internal/store/portal.go (character/world registry,
  command queue, admin grants) and internal/transport/control.go
  (portal API routes), plus a new internal/gitops package that lets
  the portal provision new worlds via a GitOps repo commit.
- game.World gains BornAt/DiedAt/DeathReason and an operator Control()
  path (close/open admissions, awaken boss, end world, per-character
  rename/heal/unstuck/retire); death no longer reuses the same
  character id on rejoin, matching the "new life" permadeath model in
  Plan.md rather than the old bypass-prevention test's expectations.
- deploy/: new control-egress network policy and portal/control
  values (admin role, GitOps repo/branch/values file/CIDRs).
- New e2e coverage: tests/portal.spec.ts, tools/capture-world.mjs.

Verified this repo's own independent commits (800ms movement-staleness
window, 20s boss-attack poll, SRS/TLS deploy values, ubuntu-22.04
runner pin, DOCKER_HOST fix) were already present in the portal copy
before merging, so nothing here regresses them. `tsc --noEmit`, the
vite client build, and the protocol contract check all pass; no go.mod
changes. Could not run `go build`/`go test` locally (no Go toolchain
on PATH, Docker daemon not running) — every new/changed Go symbol was
manually cross-referenced and resolves, but this should still be built
and tested in CI before merging to master.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-06 11:08:44 -04:00

215 lines
5.9 KiB
Go

package store
import (
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"sync"
"testing"
"time"
"aeternalis/server/content"
"aeternalis/server/internal/game"
)
func TestGlobalLivingCharacter(t *testing.T) {
runGlobalLives(t, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "lives.db")))
}
func TestPostgresGlobalLivingCharacter(t *testing.T) {
dsn := os.Getenv("TEST_DATABASE_URL")
if dsn == "" {
t.Skip("TEST_DATABASE_URL not configured")
}
runGlobalLives(t, dsn)
}
func runGlobalLives(t *testing.T, dsn string) {
ctx := context.Background()
db, err := Open(ctx, dsn)
if err != nil {
t.Fatal(err)
}
defer db.Close()
b, _ := content.Load()
prefix := game.ID()
account := "lives:" + prefix
worlds := []*game.World{game.New(prefix+"-a", 1, time.Now(), b), game.New(prefix+"-b", 2, time.Now(), b)}
leases := make([]*Lease, 2)
for i, w := range worlds {
leases[i], _, err = db.Acquire(ctx, w)
if err != nil {
t.Fatal(err)
}
if _, err = w.Join(account, "Traveler"); err != nil {
t.Fatal(err)
}
}
var wg sync.WaitGroup
results := make([]error, 2)
for i := range worlds {
wg.Add(1)
go func(i int) { defer wg.Done(); results[i] = leases[i].Save(ctx, worlds[i], "") }(i)
}
wg.Wait()
winner := -1
loser := -1
for i, err := range results {
if err == nil {
if winner != -1 {
t.Fatal("two living characters committed")
}
winner = i
} else {
if !errors.Is(err, ErrLivingCharacter) {
t.Fatal(err)
}
loser = i
}
}
if winner < 0 || loser < 0 {
t.Fatal("expected exactly one committed life")
}
var raw string
if err = db.sql.QueryRowContext(ctx, db.q(`SELECT snapshot FROM worlds WHERE id=?`), worlds[loser].ID).Scan(&raw); err != nil {
t.Fatal(err)
}
var durable game.World
_ = json.Unmarshal([]byte(raw), &durable)
if len(durable.Characters) != 0 {
t.Fatal("failed join left a durable duplicate")
}
old := worlds[winner].ForAccount(account)
if err = worlds[winner].Control("retire", old.View.Entity.ID, "", "test retirement"); err != nil {
t.Fatal(err)
}
if err = leases[winner].Save(ctx, worlds[winner], ""); err != nil {
t.Fatal(err)
}
if err = leases[loser].Save(ctx, worlds[loser], ""); err != nil {
t.Fatal(err)
}
active, err := db.ActiveCharacter(ctx, account)
if err != nil || active == nil || active.WorldID != worlds[loser].ID {
t.Fatal("death did not release the living-character reservation")
}
if err = leases[winner].Save(ctx, worlds[winner], ""); err != nil {
t.Fatal("old dead world interfered with replacement", err)
}
history, err := db.Characters(ctx, account, "")
if err != nil || len(history) != 2 {
t.Fatal("death history missing", err)
}
for _, l := range leases {
_ = l.Release(ctx)
}
}
func TestCommandAuditAtomicity(t *testing.T) {
ctx := context.Background()
db, err := Open(ctx, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "audit.db")))
if err != nil {
t.Fatal(err)
}
defer db.Close()
b, _ := content.Load()
w := game.New("audit", 1, time.Now(), b)
l, _, _ := db.Acquire(ctx, w)
c := Command{ID: game.ID(), WorldID: w.ID, Actor: "admin", Op: "close-admissions", Reason: "maintenance test"}
if err = db.Enqueue(ctx, c); err != nil {
t.Fatal(err)
}
if err = db.Enqueue(ctx, c); err != nil {
t.Fatal("retry not idempotent", err)
}
if err = w.Control(c.Op, "", "", c.Reason); err != nil {
t.Fatal(err)
}
if err = l.SaveCommand(ctx, w, "", &c, ""); err != nil {
t.Fatal(err)
}
commands, _ := db.Commands(ctx, w.ID, false)
if len(commands) != 1 || commands[0].Status != "completed" {
t.Fatal("audit not completed")
}
if err = l.SaveCommand(ctx, w, "", &c, ""); err == nil {
t.Fatal("command completed twice")
}
c.Actor = "different"
if err = db.Enqueue(ctx, c); err == nil {
t.Fatal("request ID could be reused")
}
}
func TestOldSurvivorSnapshotCannotReclaimReplacement(t *testing.T) {
ctx := context.Background()
db, err := Open(ctx, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "old-source.db")))
if err != nil {
t.Fatal(err)
}
defer db.Close()
b, _ := content.Load()
source := game.New("source", 1, time.Now(), b)
sl, _, _ := db.Acquire(ctx, source)
c, _ := source.Join("account", "Survivor")
c.View.Entity.State = "eligible"
source.Phase = "won"
if err = sl.Save(ctx, source, ""); err != nil {
t.Fatal(err)
}
target := game.New("target", 2, time.Now(), b)
tl, _, _ := db.Acquire(ctx, target)
copy, _ := db.ReadPassage(ctx, c.View.Entity.ID, c.Account)
if err = target.Import(copy); err != nil {
t.Fatal(err)
}
if err = tl.Save(ctx, target, c.View.Entity.ID); err != nil {
t.Fatal(err)
}
if err = target.Control("retire", c.View.Entity.ID, "", "test death"); err != nil {
t.Fatal(err)
}
if err = tl.Save(ctx, target, ""); err != nil {
t.Fatal(err)
}
replacement, _ := target.Join(c.Account, "Replacement")
if err = tl.Save(ctx, target, ""); err != nil {
t.Fatal(err)
}
if err = sl.Save(ctx, source, ""); err != nil {
t.Fatal("old source blocked new life", err)
}
active, _ := db.ActiveCharacter(ctx, c.Account)
if active == nil || active.ID != replacement.View.Entity.ID {
t.Fatal("old survivor reclaimed character slot")
}
}
func TestLegacySnapshotBackfill(t *testing.T) {
ctx := context.Background()
dsn := "file:" + filepath.ToSlash(filepath.Join(t.TempDir(), "legacy.db"))
db, err := Open(ctx, dsn)
if err != nil {
t.Fatal(err)
}
b, _ := content.Load()
w := game.New("legacy", 1, time.Now(), b)
l, _, _ := db.Acquire(ctx, w)
c, _ := w.Join("account", "Legacy")
if err = l.Save(ctx, w, ""); err != nil {
t.Fatal(err)
}
_ = l.Release(ctx)
_, _ = db.sql.ExecContext(ctx, `DELETE FROM character_records`)
_, _ = db.sql.ExecContext(ctx, `DELETE FROM schema_migrations`)
_ = db.Close()
db, err = Open(ctx, dsn)
if err != nil {
t.Fatal(err)
}
defer db.Close()
active, err := db.ActiveCharacter(ctx, c.Account)
if err != nil || active == nil || active.ID != c.View.Entity.ID {
t.Fatal("legacy living character not backfilled", err)
}
}