318 lines
9.8 KiB
Go
318 lines
9.8 KiB
Go
package game
|
|
|
|
import (
|
|
"encoding/json"
|
|
"math"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"aeternalis/server/content"
|
|
p "aeternalis/server/internal/protocol"
|
|
)
|
|
|
|
func fixture(t *testing.T) (*World, *Character) {
|
|
t.Helper()
|
|
b, err := content.Load()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w := New("test", 123, time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC), b)
|
|
c, err := w.Join("account", "Traveler")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = w.Connect(c.View.Entity.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return w, c
|
|
}
|
|
func TestTerrainDeterministicAndSeamless(t *testing.T) {
|
|
b, _ := content.Load()
|
|
a := NewTerrain(123, b)
|
|
other := NewTerrain(456, b)
|
|
if !reflect.DeepEqual(a.Chunk(-1, 0), NewTerrain(123, b).Chunk(-1, 0)) {
|
|
t.Fatal("same seed changed")
|
|
}
|
|
if reflect.DeepEqual(a.Chunk(0, 0), other.Chunk(0, 0)) {
|
|
t.Fatal("different seeds identical")
|
|
}
|
|
left, right := a.Chunk(-1, 0), a.Chunk(0, 0)
|
|
n := b.TerrainResolution + 1
|
|
for z := 0; z < n; z++ {
|
|
if left.Heights[z*n+n-1] != right.Heights[z*n] {
|
|
t.Fatal("chunk seam")
|
|
}
|
|
}
|
|
if ChunkCoord(-0.1, 128) != -1 || ChunkCoord(-128, 128) != -1 {
|
|
t.Fatal("negative coordinates")
|
|
}
|
|
for _, r := range left.Resources {
|
|
if math.Abs(r.Position.Y-a.Height(r.Position.X, r.Position.Z)) > 1e-8 {
|
|
t.Fatal("floating resource")
|
|
}
|
|
}
|
|
}
|
|
func TestMovementAuthorityAndInterest(t *testing.T) {
|
|
w, c := fixture(t)
|
|
id := c.View.Entity.ID
|
|
w.now = w.now.Add(time.Second)
|
|
for _, intent := range []p.Intent{{Op: "move", Seq: 1, X: 2}, {Op: "move", Seq: 1, X: math.NaN()}, {Op: "move", Seq: 1, Z: math.Inf(1)}} {
|
|
if w.Intent(id, intent) == nil {
|
|
t.Fatal("invalid movement accepted")
|
|
}
|
|
}
|
|
if err := w.Intent(id, p.Intent{Op: "move", Seq: 1, X: 1, Z: 1}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before := c.View.Entity.Position
|
|
w.Advance(w.now.Add(50*time.Millisecond), 0.05)
|
|
if got := distance(before, c.View.Entity.Position); math.Abs(got-w.Balance.PlayerSpeed*0.05) > 1e-8 {
|
|
t.Fatalf("diagonal speed: %v", got)
|
|
}
|
|
if w.Intent(id, p.Intent{Op: "move", Seq: 1}) == nil {
|
|
t.Fatal("replayed sequence")
|
|
}
|
|
s := w.Snapshot(id, map[string]bool{})
|
|
if len(s.Chunks) != 9 || len(s.ChunkKeys) != 9 {
|
|
t.Fatal("interest area not 3x3")
|
|
}
|
|
known := map[string]bool{}
|
|
for _, key := range s.ChunkKeys {
|
|
known[key] = true
|
|
}
|
|
if len(w.Snapshot(id, known).Chunks) != 0 {
|
|
t.Fatal("resent known terrain")
|
|
}
|
|
far, _ := w.Join("far", "Far")
|
|
far.View.Entity.Position = w.terrain.Position(700, 700)
|
|
far.Online = true
|
|
for _, e := range w.Snapshot(id, known).Entities {
|
|
if e.ID == far.View.Entity.ID {
|
|
t.Fatal("remote entity leaked outside interest")
|
|
}
|
|
}
|
|
w.Advance(w.now.Add(time.Second), 0.05)
|
|
if c.MoveX != 0 || c.MoveZ != 0 {
|
|
t.Fatal("stale input did not stop")
|
|
}
|
|
}
|
|
func TestGatherCraftAndPermanentDeath(t *testing.T) {
|
|
w, c := fixture(t)
|
|
id := c.View.Entity.ID
|
|
w.now = w.now.Add(2 * time.Second)
|
|
if w.Intent(id, p.Intent{Op: "gather", Seq: 1, Target: "arrival:ore"}) == nil {
|
|
t.Fatal("remote gather accepted")
|
|
}
|
|
c.View.Entity.Position = w.terrain.Position(17, 12)
|
|
if err := w.Intent(id, p.Intent{Op: "gather", Seq: 2, Target: "arrival:ore"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.View.Inventory["ore"] != 1 {
|
|
t.Fatal("gather failed")
|
|
}
|
|
if w.Intent(id, p.Intent{Op: "gather", Seq: 3, Target: "arrival:ore"}) == nil {
|
|
t.Fatal("double gather")
|
|
}
|
|
if w.Intent(id, p.Intent{Op: "craft", Seq: 4, Target: "spear"}) == nil {
|
|
t.Fatal("free craft")
|
|
}
|
|
c.View.Inventory["wood"] = 3
|
|
c.View.Inventory["ore"] = 2
|
|
if err := w.craft(c, "spear"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if gearCount(c.View.Items, "spear") != 1 || c.View.Inventory["wood"] != 0 || c.View.Inventory["ore"] != 0 {
|
|
t.Fatal("recipe not atomic")
|
|
}
|
|
w.kill(c, "test death")
|
|
w.Disconnect(id)
|
|
if w.Connect(id) == nil {
|
|
t.Fatal("dead character reconnected")
|
|
}
|
|
same, err := w.Join(c.Account, "Replacement")
|
|
if err != nil || same.View.Entity.ID == id || same.View.Entity.State != "alive" || c.View.Entity.State != "dead" {
|
|
t.Fatal("replacement must be a new life, not a resurrection")
|
|
}
|
|
if same.DailySeconds != 0 || same.View.Inventory["spear"] != 0 || w.LivingCount() != 1 {
|
|
t.Fatal("new life inherited old progress or incorrect capacity")
|
|
}
|
|
if again, _ := w.Join(c.Account, "Duplicate"); again.View.Entity.ID != same.View.Entity.ID {
|
|
t.Fatal("second living character")
|
|
}
|
|
if c.DiedAt == nil || c.DeathReason != "test death" {
|
|
t.Fatal("missing durable death record")
|
|
}
|
|
if len(c.View.Inventory) != 0 || gearCount(w.Corpses[c.RevivalCorpse].Items, "spear") != 1 {
|
|
t.Fatal("corpse inventory")
|
|
}
|
|
raw, _ := json.Marshal(w)
|
|
var restored World
|
|
if err = json.Unmarshal(raw, &restored); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = restored.Restore(w.now); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if restored.Connect(id) == nil {
|
|
t.Fatal("restart resurrected character")
|
|
}
|
|
}
|
|
|
|
func TestStaticResourcesAreNotReplicatedEverySnapshot(t *testing.T) {
|
|
w, c := fixture(t)
|
|
w.Depleted["arrival:ore"] = w.Age() + 60
|
|
s := w.Snapshot(c.View.Entity.ID, map[string]bool{})
|
|
if len(s.UnavailableResources) != 1 || s.UnavailableResources[0] != "arrival:ore" {
|
|
t.Fatal("missing authoritative resource availability")
|
|
}
|
|
for _, e := range s.Entities {
|
|
if e.Kind == "tree" || e.Kind == "ore" || e.Kind == "herb" {
|
|
t.Fatal("static resource repeated in dynamic entity snapshot")
|
|
}
|
|
}
|
|
resources := 0
|
|
for _, ch := range s.Chunks {
|
|
resources += len(ch.Resources)
|
|
}
|
|
if resources == 0 {
|
|
t.Fatal("initial chunk does not carry resources")
|
|
}
|
|
}
|
|
func TestDailyLimitAndKeystone(t *testing.T) {
|
|
w, c := fixture(t)
|
|
w.Balance.DailyPlaySeconds = 1
|
|
c.DailySeconds = .9
|
|
c.View.Entity.HP = 20
|
|
c.View.Entity.Position = w.terrain.Position(40, 40)
|
|
w.Advance(w.now.Add(200*time.Millisecond), .2)
|
|
if c.Online || c.ReturnPosition == nil || !c.View.HasKeystone || c.View.Entity.HP != 50 || distance(c.View.Entity.Position, p.Vec3{}) != 0 {
|
|
t.Fatalf("cap not enforced: %+v", c)
|
|
}
|
|
if w.Connect(c.View.Entity.ID) == nil {
|
|
t.Fatal("same-day cap bypass")
|
|
}
|
|
w.now = w.now.Add(24 * time.Hour)
|
|
if err := w.Connect(c.View.Entity.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := w.Intent(c.View.Entity.ID, p.Intent{Op: "return", Seq: 1}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.View.Entity.Position.X != 40 || c.ReturnPosition != nil || c.View.HasKeystone {
|
|
t.Fatal("keystone failed")
|
|
}
|
|
}
|
|
|
|
func TestDailyBudgetUsesWallTimeDuringLag(t *testing.T) {
|
|
w, c := fixture(t)
|
|
w.Balance.DailyPlaySeconds = 2
|
|
w.Advance(w.now.Add(3*time.Second), 3)
|
|
if c.Online || c.View.RemainingSeconds != 0 {
|
|
t.Fatal("simulation lag bypassed wall-clock play cap")
|
|
}
|
|
}
|
|
|
|
func TestKeystoneCannotGrantRoamingInvulnerability(t *testing.T) {
|
|
w, c := fixture(t)
|
|
c.Sheltered = true
|
|
c.ReturnPosition = &p.Vec3{X: 40, Z: 40}
|
|
if err := w.Intent(c.View.Entity.ID, p.Intent{Op: "move", Seq: 1, X: 1}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.Sheltered || c.ReturnPosition == nil {
|
|
t.Fatal("leaving sanctuary must remove protection while preserving keystone")
|
|
}
|
|
}
|
|
func TestBossVictoryAndCollapse(t *testing.T) {
|
|
t.Run("victory includes offline survivors and excludes dead", func(t *testing.T) {
|
|
w, c := fixture(t)
|
|
dead, _ := w.Join("dead", "Dead")
|
|
w.kill(dead, "test")
|
|
offline, _ := w.Join("offline", "Offline")
|
|
w.now = w.now.Add(time.Second)
|
|
_ = w.TriggerBoss()
|
|
w.BossHP = 1
|
|
if err := w.Intent(c.View.Entity.ID, p.Intent{Op: "attack", Seq: 1, Target: "boss"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w.Phase != "won" || c.View.Entity.State != "eligible" || offline.View.Entity.State != "eligible" || dead.View.Entity.State != "dead" {
|
|
t.Fatal("incorrect survivor selection")
|
|
}
|
|
radius := w.SafeRadius()
|
|
w.Advance(w.now.Add(20*24*time.Hour), .05)
|
|
if w.SafeRadius() != radius {
|
|
t.Fatal("world kept collapsing after boss death")
|
|
}
|
|
if c.View.Entity.State != "eligible" {
|
|
t.Fatal("victory not terminal")
|
|
}
|
|
if w.Intent(c.View.Entity.ID, p.Intent{Op: "move", Seq: 2, X: 1}) != nil {
|
|
t.Fatal("survivor cannot walk to the victory portal")
|
|
}
|
|
if w.Intent(c.View.Entity.ID, p.Intent{Op: "attack", Seq: 3, Target: "boss"}) == nil {
|
|
t.Fatal("combat continued after victory")
|
|
}
|
|
})
|
|
t.Run("collapse kills offline and capped characters", func(t *testing.T) {
|
|
w, c := fixture(t)
|
|
c.Online = false
|
|
c.ReturnPosition = &p.Vec3{X: 40}
|
|
w.Advance(w.CreatedAt.Add(time.Duration((w.Balance.SeasonSeconds+w.Balance.CollapseSeconds+1)*float64(time.Second))), .05)
|
|
if w.Phase != "lost" || c.View.Entity.State != "dead" {
|
|
t.Fatal("world destruction bypass")
|
|
}
|
|
})
|
|
t.Run("boss schedule survives downtime", func(t *testing.T) {
|
|
w, _ := fixture(t)
|
|
w.Advance(w.CreatedAt.Add(7*24*time.Hour+5*time.Minute), .05)
|
|
if w.Phase != "cataclysm" || w.SafeRadius() >= w.Balance.WorldRadius {
|
|
t.Fatal("deadline did not advance")
|
|
}
|
|
})
|
|
}
|
|
func TestSpawnAndTransferPreserveBudget(t *testing.T) {
|
|
w, c := fixture(t)
|
|
other, _ := w.Join("other", "Other")
|
|
if distance(c.View.Entity.Position, other.View.Entity.Position) < w.Balance.SpawnSeparation {
|
|
t.Fatal("spawns overlap")
|
|
}
|
|
c.View.Entity.State = "eligible"
|
|
c.DailySeconds = 123
|
|
c.View.PlayedSeconds = 456
|
|
c.View.Skills["combat"] = 9
|
|
c.View.Inventory["spear"] = 1
|
|
next := New("next", 999, w.now, w.Balance)
|
|
if err := next.Import(c); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.DailySeconds != 123 || c.View.PlayedSeconds != 456 || c.View.Skills["combat"] != 9 || gearCount(c.View.Items, "spear") != 1 {
|
|
t.Fatal("transfer changed progress or budget")
|
|
}
|
|
if err := next.Import(c); err == nil {
|
|
t.Fatal("duplicate import")
|
|
}
|
|
}
|
|
func BenchmarkSnapshot100Players(b *testing.B) {
|
|
balance, _ := content.Load()
|
|
w := New("bench", 123, time.Now(), balance)
|
|
ids := []string{}
|
|
for i := 0; i < 100; i++ {
|
|
c, err := w.Join(ID(), "Traveler")
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
c.Online = true
|
|
ids = append(ids, c.View.Entity.ID)
|
|
}
|
|
w.Advance(time.Now(), .05)
|
|
known := map[string]bool{}
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
s := w.Snapshot(ids[i%len(ids)], known)
|
|
if _, err := json.Marshal(s); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
}
|