Files
aeternalis/server/internal/transport/catalog_integration_test.go
T

45 lines
3.2 KiB
Go

package transport
import (
"context"
"encoding/json"
"path/filepath"
"testing"
"time"
"aeternalis/server/content"
"aeternalis/server/internal/game"
"aeternalis/server/internal/store"
)
func TestPublishedTitlesAndStatsReachDurableCharacterViews(t *testing.T) {
ctx:=context.Background()
db,err:=store.Open(ctx,"file:"+filepath.ToSlash(filepath.Join(t.TempDir(),"catalog-owner.db")));if err!=nil{t.Fatal(err)};defer db.Close()
b,err:=content.Load();if err!=nil{t.Fatal(err)}
w:=game.New("catalog-owner",23,time.Now(),b)
lease,_,err:=db.Acquire(ctx,w);if err!=nil{t.Fatal(err)}
svc:=NewService(w,lease)
initial,err:=db.Catalog(ctx);if err!=nil{t.Fatal(err)}
title:=content.Title{ID:"published_strength",Name:"Published Strength",Scope:"personal",Requirements:[]content.TitleRequirement{{Metric:"level",AtLeast:1}},Effects:content.TitleEffects{Modifiers:[]content.StatModifier{{Stat:"strength",Flat:4,Percent:50}}}}
result,err:=db.PublishCatalog(ctx,"operator",store.CatalogChange{ID:game.ID(),ExpectedVersion:initial.Version,Op:"create-title",Reason:"Integration title",Title:&title});if err!=nil{t.Fatal(err)}
view,err:=svc.join(ctx,"catalog-player","Catalog Player","");if err!=nil{t.Fatal(err)}
if len(view.Titles)!=1||view.Titles[0].ID!=title.ID||view.Stats["strength"].Final!=6||view.Stats["meleeDamage"].Final!=24{t.Fatal("published title missing from committed join",view)}
c:=w.Characters[view.Entity.ID];c.View.Entity.HP=20;c.DailySeconds=321
rules:=initial.Stats;rules.Base["healthPoints"]=150
result,err=db.PublishCatalog(ctx,"operator",store.CatalogChange{ID:game.ID(),ExpectedVersion:result.Version,Op:"update-stats",Reason:"Integration formula",Stats:&rules});if err!=nil{t.Fatal(err)}
if err=svc.controls(ctx);err!=nil{t.Fatal(err)}
record,err:=db.ActiveCharacter(ctx,c.Account);if err!=nil{t.Fatal(err)}
if record.Character.View.Entity.MaxHP!=150||record.Character.View.Entity.HP!=20||record.Character.DailySeconds!=321||record.Character.View.Titles[0].Effects.Modifiers[0].Flat!=4{t.Fatal("catalog changed health, budget or earned title")}
worlds,err:=db.Worlds(ctx);if err!=nil||worlds[0].CatalogVersion!=result.Version{t.Fatal("applied version not checkpointed",err)}
if err=lease.Release(ctx);err!=nil{t.Fatal(err)}
nextLease,raw,err:=db.Acquire(ctx,game.New(w.ID,23,time.Now(),b));if err!=nil{t.Fatal(err)};defer nextLease.Release(ctx)
var restored game.World;if err=json.Unmarshal(raw,&restored);err!=nil{t.Fatal(err)}
if err=restored.Restore(time.Now());err!=nil{t.Fatal(err)}
if restored.CatalogVersion!=result.Version||restored.Characters[c.View.Entity.ID].View.Stats["healthPoints"].Final!=150{t.Fatal("restart lost published stats")}
// Old ownership may no longer expose a speculative formula revision.
rules.Base["healthPoints"]=200
if _,err=db.PublishCatalog(ctx,"operator",store.CatalogChange{ID:game.ID(),ExpectedVersion:result.Version,Op:"update-stats",Reason:"Stale owner test",Stats:&rules});err!=nil{t.Fatal(err)}
if err=svc.refreshCatalog(ctx);err==nil||svc.ready.Load(){t.Fatal("stale owner continued after failed catalog checkpoint")}
record,err=db.ActiveCharacter(ctx,c.Account);if err!=nil{t.Fatal(err)}
if record.Character.View.Entity.MaxHP!=150{t.Fatal("failed checkpoint published speculative stats")}
}