266 lines
9.6 KiB
Go
266 lines
9.6 KiB
Go
package store
|
|
|
|
import (
|
|
"aeternalis/server/content"
|
|
"aeternalis/server/internal/game"
|
|
p "aeternalis/server/internal/protocol"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestHousingLifecycle(t *testing.T) {
|
|
testHousingLifecycle(t, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "housing.db")))
|
|
}
|
|
func TestPostgresHousingLifecycle(t *testing.T) {
|
|
dsn := os.Getenv("TEST_DATABASE_URL")
|
|
if dsn == "" {
|
|
t.Skip("TEST_DATABASE_URL not configured")
|
|
}
|
|
testHousingLifecycle(t, dsn)
|
|
}
|
|
func testHousingLifecycle(t *testing.T, dsn string) {
|
|
ctx := context.Background()
|
|
db, e := Open(ctx, dsn)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
defer db.Close()
|
|
b, _ := content.Load()
|
|
now := time.Now().UTC()
|
|
w := game.New("housing-"+game.ID(), 1, now, b)
|
|
l, _, e := db.Acquire(ctx, w)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
join := func(name string) *game.Character {
|
|
c, e := w.Join(name+game.ID(), name)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = w.Connect(c.View.Entity.ID); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
return c
|
|
}
|
|
owner, guest, coowner := join("Owner"), join("Guest"), join("Co-owner")
|
|
// Seed arrivals before making this fixture a hub; travel itself has separate coverage.
|
|
w.Kind = "hub"
|
|
w.Phase = "hub"
|
|
for _, c := range []*game.Character{owner, guest, coowner} {
|
|
c.View.Entity.Position = w.HousingEntrancePosition()
|
|
}
|
|
save := func() {
|
|
t.Helper()
|
|
if e := l.Save(ctx, w, ""); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
save()
|
|
act := func(c *game.Character, r HouseRequest) {
|
|
t.Helper()
|
|
if r.RequestID == "" {
|
|
r.RequestID = game.ID()
|
|
}
|
|
if e := l.HouseAction(ctx, w, c.Account, r); e != nil {
|
|
t.Fatal(r.Op, e)
|
|
}
|
|
}
|
|
reject := func(c *game.Character, r HouseRequest) {
|
|
t.Helper()
|
|
r.RequestID = game.ID()
|
|
before, _ := json.Marshal(w)
|
|
e := l.HouseAction(ctx, w, c.Account, r)
|
|
if !errors.Is(e, ErrHousing) {
|
|
t.Fatal("expected housing rejection", r.Op, e)
|
|
}
|
|
after, _ := json.Marshal(w)
|
|
if string(before) != string(after) {
|
|
t.Fatal("rejected action mutated world", r.Op)
|
|
}
|
|
}
|
|
view := func(c *game.Character) p.HouseState {
|
|
t.Helper()
|
|
s, e := db.HouseState(ctx, w, c.Account)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
return s
|
|
}
|
|
tick := func(seconds float64) {
|
|
now = now.Add(time.Duration(seconds * float64(time.Second)))
|
|
w.Advance(now, seconds)
|
|
save()
|
|
}
|
|
reject(owner, HouseRequest{Op: "create", Name: "Home"})
|
|
owner.WorldsSurvived = 1
|
|
guest.WorldsSurvived = 1
|
|
coowner.WorldsSurvived = 1
|
|
save()
|
|
create := HouseRequest{RequestID: game.ID(), Op: "create", Name: "Home"}
|
|
act(owner, create)
|
|
act(owner, create)
|
|
s := view(owner)
|
|
if len(s.Houses) != 1 || s.Houses[0].Keys != 1 {
|
|
t.Fatal("create replay duplicated house or key", s)
|
|
}
|
|
house := s.Houses[0].ID
|
|
key := owner.View.Items[len(owner.View.Items)-1].ID
|
|
reject(owner, HouseRequest{Op: "create", Name: "Duplicate"})
|
|
reject(guest, HouseRequest{Op: "enter", HouseID: house})
|
|
owner.View.Entity.Position.X += 100
|
|
reject(owner, HouseRequest{Op: "enter", HouseID: house, Item: key})
|
|
owner.View.Entity.Position = w.HousingEntrancePosition()
|
|
reject(owner, HouseRequest{Op: "enter", HouseID: house, Item: "unowned-key"})
|
|
entry := HouseRequest{RequestID: game.ID(), Op: "enter", HouseID: house, Item: key}
|
|
act(owner, entry)
|
|
act(owner, entry) // Replaying an entry from inside the room must not enter twice.
|
|
if e := w.Intent(owner.View.Entity.ID, p.Intent{Seq: 1, Op: "attack"}); e == nil {
|
|
t.Fatal("world action inside house")
|
|
}
|
|
if _, e := w.AttemptCraft(owner, p.CraftRequest{Profession: "carver", Ingredients: map[string]int64{"wood": 1}}); e == nil {
|
|
t.Fatal("house craft accepted")
|
|
}
|
|
stale := *l
|
|
stale.Revision--
|
|
if e := stale.HouseAction(ctx, w, owner.Account, HouseRequest{RequestID: game.ID(), Op: "issue-key", HouseID: house}); !errors.Is(e, ErrOwnership) {
|
|
t.Fatal("stale owner mutation", e)
|
|
}
|
|
if view(owner).Houses[0].Keys != 1 {
|
|
t.Fatal("stale lease issued key")
|
|
}
|
|
act(owner, HouseRequest{Op: "invite", HouseID: house, Target: guest.View.Entity.ID, Role: "guest"})
|
|
act(owner, HouseRequest{Op: "invite", HouseID: house, Target: coowner.View.Entity.ID, Role: "co-owner"})
|
|
act(guest, HouseRequest{Op: "enter", HouseID: house})
|
|
act(coowner, HouseRequest{Op: "enter", HouseID: house})
|
|
reject(guest, HouseRequest{Op: "permissions", HouseID: house, Permissions: map[string]bool{"pickupItems": true}})
|
|
reject(coowner, HouseRequest{Op: "issue-key", HouseID: house})
|
|
act(coowner, HouseRequest{Op: "permissions", HouseID: house, Permissions: map[string]bool{"moveFurniture": true, "openFurniture": true, "depositItems": true}})
|
|
act(coowner, HouseRequest{Op: "permissions", HouseID: house, Target: guest.View.Entity.ID, Permissions: map[string]bool{"moveFurniture": false}})
|
|
if v := view(guest); v.Effective["moveFurniture"] || !v.Effective["depositItems"] {
|
|
t.Fatal("baseline/override not enforced")
|
|
}
|
|
if e = w.AdminItems(owner, "grant-item", "storage_chest:1"); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
chest := owner.View.Items[len(owner.View.Items)-1].ID
|
|
owner.View.Inventory["wood"] = 20
|
|
save()
|
|
act(owner, HouseRequest{Op: "place", HouseID: house, Item: chest, X: 0, Z: 0})
|
|
reject(owner, HouseRequest{Op: "move", HouseID: house, Target: chest, X: 100, Z: 0})
|
|
reject(guest, HouseRequest{Op: "move", HouseID: house, Target: chest, X: 3, Z: 0})
|
|
act(owner, HouseRequest{Op: "deposit", HouseID: house, Target: chest, Item: "wood", Quantity: 10})
|
|
reject(guest, HouseRequest{Op: "withdraw", HouseID: house, Target: chest, Item: "wood", Quantity: 1})
|
|
reject(owner, HouseRequest{Op: "pickup-furniture", HouseID: house, Target: chest})
|
|
reject(owner, HouseRequest{Op: "deposit", HouseID: house, Target: chest, Item: key, Quantity: 1})
|
|
act(coowner, HouseRequest{Op: "permissions", HouseID: house, Target: guest.View.Entity.ID, Permissions: map[string]bool{"pickupItems": true, "openFurniture": false}})
|
|
if len(view(guest).Room.Furniture[chest].Contents.Inventory) != 0 {
|
|
t.Fatal("closed container contents leaked")
|
|
}
|
|
reject(guest, HouseRequest{Op: "withdraw", HouseID: house, Target: chest, Item: "wood", Quantity: 1})
|
|
act(coowner, HouseRequest{Op: "permissions", HouseID: house, Target: guest.View.Entity.ID, Permissions: map[string]bool{"pickupItems": true}})
|
|
act(guest, HouseRequest{Op: "withdraw", HouseID: house, Target: chest, Item: "wood", Quantity: 2})
|
|
if view(owner).Room.Furniture[chest].Contents.Inventory["wood"] != 8 || guest.View.Inventory["wood"] != 2 {
|
|
t.Fatal("stored inventory transfer")
|
|
}
|
|
// Furniture movement preserves exact instance and contents.
|
|
act(guest, HouseRequest{Op: "move", HouseID: house, Target: chest, X: 3, Z: 0})
|
|
if f := view(owner).Room.Furniture[chest]; f.Item.ID != chest || f.Contents.Inventory["wood"] != 8 {
|
|
t.Fatal("move lost furniture or contents")
|
|
}
|
|
act(owner, HouseRequest{Op: "issue-key", HouseID: house})
|
|
if view(owner).Houses[0].Keys != 2 {
|
|
t.Fatal("extra key missing")
|
|
}
|
|
act(owner, HouseRequest{Op: "destroy-key", Item: key})
|
|
tick(2.9)
|
|
if view(owner).Houses[0].Keys != 2 || owner.View.DestroyKey == nil {
|
|
t.Fatal("key destroyed early")
|
|
}
|
|
tick(.11)
|
|
if view(owner).Houses[0].Keys != 1 || view(owner).Houses[0].State != "active" {
|
|
t.Fatal("non-last key archived house")
|
|
}
|
|
key = owner.View.Items[len(owner.View.Items)-1].ID
|
|
act(owner, HouseRequest{Op: "leave"})
|
|
act(owner, HouseRequest{Op: "destroy-key", Item: key})
|
|
w.Disconnect(owner.View.Entity.ID)
|
|
save()
|
|
tick(4)
|
|
if view(owner).Houses[0].Keys != 1 || owner.View.DestroyKey != nil {
|
|
t.Fatal("disconnect failed to cancel key action")
|
|
}
|
|
_ = w.Connect(owner.View.Entity.ID)
|
|
act(owner, HouseRequest{Op: "destroy-key", Item: key})
|
|
if e = w.Intent(owner.View.Entity.ID, p.Intent{Seq: 2, Op: "drop", Target: key}); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if owner.View.DestroyKey != nil {
|
|
t.Fatal("drop failed to cancel")
|
|
}
|
|
save()
|
|
var pile string
|
|
for id, c := range w.Corpses {
|
|
for _, i := range c.Items {
|
|
if i.ID == key {
|
|
pile = id
|
|
}
|
|
}
|
|
}
|
|
if pile == "" {
|
|
t.Fatal("key not dropped")
|
|
}
|
|
act(guest, HouseRequest{Op: "leave"})
|
|
guest.View.Entity.Position = owner.View.Entity.Position
|
|
if e = w.Intent(guest.View.Entity.ID, p.Intent{Seq: 1, Op: "loot", Target: pile}); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
save()
|
|
act(guest, HouseRequest{Op: "enter", HouseID: house})
|
|
if view(guest).Role != "owner" {
|
|
t.Fatal("looted key did not grant ownership")
|
|
}
|
|
act(coowner, HouseRequest{Op: "invite", HouseID: house, Target: guest.View.Entity.ID, Role: "remove"})
|
|
if view(guest).Role != "owner" {
|
|
t.Fatal("invitation removal stripped key owner")
|
|
}
|
|
// Last-key destruction archives contents immediately on completion; no invented purge deadline.
|
|
act(guest, HouseRequest{Op: "destroy-key", Item: key})
|
|
tick(3.01)
|
|
if v := view(owner); v.Houses[0].State != "archived" || v.Houses[0].Keys != 0 {
|
|
t.Fatal("last-key archive", v)
|
|
}
|
|
tx, _ := db.sql.BeginTx(ctx, nil)
|
|
stored, e := db.readHouse(ctx, tx, house, false)
|
|
tx.Rollback()
|
|
if e != nil || stored.View.Furniture[chest].Contents.Inventory["wood"] != 8 {
|
|
t.Fatal("archive lost recoverable contents", e)
|
|
}
|
|
reject(owner, HouseRequest{Op: "recover", HouseID: house})
|
|
reject(coowner, HouseRequest{Op: "enter", HouseID: house})
|
|
// Creator death establishes the recovery world. World loss finally purges archived data.
|
|
owner.View.Entity.HP = 0
|
|
tick(.1)
|
|
if owner.DiedAt == nil {
|
|
t.Fatal("creator death absent")
|
|
}
|
|
tx, _ = db.sql.BeginTx(ctx, nil)
|
|
stored, e = db.readHouse(ctx, tx, house, false)
|
|
tx.Rollback()
|
|
if e != nil || stored.DeathWorld != w.ID {
|
|
t.Fatal("recovery world not captured", e)
|
|
}
|
|
w.Phase = "lost"
|
|
save()
|
|
tx, _ = db.sql.BeginTx(ctx, nil)
|
|
stored, e = db.readHouse(ctx, tx, house, false)
|
|
tx.Rollback()
|
|
if e != nil || stored.View.State != "purged" || len(stored.View.Furniture) != 0 {
|
|
t.Fatal("world loss did not purge archived house", e)
|
|
}
|
|
}
|