116 lines
3.2 KiB
Go
116 lines
3.2 KiB
Go
package store
|
|
|
|
import (
|
|
"aeternalis/server/content"
|
|
"aeternalis/server/internal/game"
|
|
p "aeternalis/server/internal/protocol"
|
|
"context"
|
|
"errors"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestHouseVisitorsAcrossHubOwners(t *testing.T) {
|
|
ctx := context.Background()
|
|
db, e := Open(ctx, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "visitors.db")))
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
defer db.Close()
|
|
b, _ := content.Load()
|
|
now := time.Now()
|
|
makeHub := func(id string) (*game.World, *Lease, *game.Character) {
|
|
t.Helper()
|
|
w := game.New(id, 1, now, b)
|
|
c, e := w.Join(id, id)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
c.WorldsSurvived = 1
|
|
_ = w.Connect(c.View.Entity.ID)
|
|
w.Kind = "hub"
|
|
w.Phase = "hub"
|
|
c.View.Entity.Position = w.HousingEntrancePosition()
|
|
l, _, e := db.Acquire(ctx, w)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = l.Save(ctx, w, ""); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
return w, l, c
|
|
}
|
|
a, al, owner := makeHub("hub-a")
|
|
bworld, bl, guest := makeHub("hub-b")
|
|
act := func(w *game.World, l *Lease, c *game.Character, r HouseRequest) {
|
|
t.Helper()
|
|
r.RequestID = game.ID()
|
|
if e := l.HouseAction(ctx, w, c.Account, r); e != nil {
|
|
t.Fatal(r.Op, e)
|
|
}
|
|
}
|
|
save := func(w *game.World, l *Lease) {
|
|
t.Helper()
|
|
if e := l.Save(ctx, w, ""); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
act(a, al, owner, HouseRequest{Op: "create", Name: "Shared room"})
|
|
directory, e := db.HouseState(ctx, a, owner.Account)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
house := directory.Houses[0].ID
|
|
act(a, al, owner, HouseRequest{Op: "enter", HouseID: house})
|
|
act(a, al, owner, HouseRequest{Op: "invite", HouseID: house, Target: guest.View.Entity.ID, Role: "guest"})
|
|
act(bworld, bl, guest, HouseRequest{Op: "enter", HouseID: house})
|
|
if e := bworld.Intent(guest.View.Entity.ID, p.Intent{Seq: 1, Op: "move", X: 1}); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
bworld.Advance(now.Add(100*time.Millisecond), .1)
|
|
save(bworld, bl)
|
|
save(a, al)
|
|
scene := a.HouseSnapshot(owner)
|
|
if scene == nil || len(scene.People) != 2 {
|
|
t.Fatal("cross-hub visitor missing", scene)
|
|
}
|
|
found := false
|
|
for _, v := range scene.People {
|
|
if v.ID == guest.View.Entity.ID {
|
|
found = true
|
|
if v.Position != *guest.View.HousePosition {
|
|
t.Fatal("visitor used hub coordinates")
|
|
}
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatal("guest missing")
|
|
}
|
|
if _, exists := a.Characters[guest.View.Entity.ID]; exists {
|
|
t.Fatal("replicated visitor into another simulation owner")
|
|
}
|
|
stale := *bl
|
|
stale.Revision--
|
|
if e := stale.HouseAction(ctx, bworld, guest.Account, HouseRequest{RequestID: game.ID(), Op: "leave"}); !errors.Is(e, ErrOwnership) {
|
|
t.Fatal("unfenced visitor change", e)
|
|
}
|
|
if game.HouseID(guest) != house {
|
|
t.Fatal("rollback published eviction")
|
|
}
|
|
act(a, al, owner, HouseRequest{Op: "invite", HouseID: house, Target: guest.View.Entity.ID, Role: "remove"})
|
|
save(bworld, bl)
|
|
save(a, al)
|
|
if guest.View.HousePosition != nil || game.HouseID(guest) != "" || len(a.HouseSnapshot(owner).People) != 1 {
|
|
t.Fatal("revoked guest retained room presence")
|
|
}
|
|
act(a, al, owner, HouseRequest{Op: "leave"})
|
|
if owner.HouseRoom != nil || owner.View.HousePosition != nil {
|
|
t.Fatal("exit retained cached room")
|
|
}
|
|
var n int
|
|
if e = db.sql.QueryRow(`SELECT COUNT(*) FROM house_visitors`).Scan(&n); e != nil || n != 0 {
|
|
t.Fatal("stale presence retained", n, e)
|
|
}
|
|
}
|