package store import ( "aeternalis/server/content" "aeternalis/server/internal/game" p "aeternalis/server/internal/protocol" "context" "encoding/json" "errors" "path/filepath" "testing" "time" ) func TestHouseDeathTravelAndRecovery(t *testing.T) { ctx := context.Background() db, e := Open(ctx, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "recovery.db"))) if e != nil { t.Fatal(e) } defer db.Close() b, _ := content.Load() now := time.Now().UTC() hub := game.New("home", 1, now, b) c, _ := hub.Join("creator", "Creator") c.WorldsSurvived = 1 _ = hub.Connect(c.View.Entity.ID) hub.Kind = "hub" hub.Phase = "hub" c.View.Entity.Position = hub.HousingEntrancePosition() hl, _, e := db.Acquire(ctx, hub) if e != nil { t.Fatal(e) } if e = hl.Save(ctx, hub, ""); e != nil { t.Fatal(e) } act := func(l *Lease, w *game.World, 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) } } state := func(w *game.World, c *game.Character) p.HouseState { t.Helper() v, e := db.HouseState(ctx, w, c.Account) if e != nil { t.Fatal(e) } return v } act(hl, hub, c, HouseRequest{Op: "create", Name: "Recovery home"}) house := state(hub, c).Houses[0].ID key := c.View.Items[len(c.View.Items)-1].ID act(hl, hub, c, HouseRequest{Op: "enter", HouseID: house}) c.View.Inventory["wood"] = 12 act(hl, hub, c, HouseRequest{Op: "drop", HouseID: house, Item: "wood", Quantity: 7}) act(hl, hub, c, HouseRequest{Op: "leave"}) next := game.New("doomed", 2, now, b) nl, _, e := db.Acquire(ctx, next) if e != nil { t.Fatal(e) } // Traveling cancels the action, retains the exact key, and never creates another owner copy. c.View.Entity.Position = hub.PortalPosition() act(hl, hub, c, HouseRequest{Op: "destroy-key", Item: key}) oldWorld := *hub oldWorld.Characters = map[string]*game.Character{c.View.Entity.ID: c} tr, e := hl.PrepareTravel(ctx, hub, c.Account, game.ID(), next.ID, "hub") if e != nil { t.Fatal(e) } _, e = nl.AcceptTravel(ctx, next, c.Account, tr.ID) if e != nil { t.Fatal(e) } c = next.ForAccount(c.Account) _ = next.Connect(c.View.Entity.ID) if c.View.DestroyKey != nil || c.View.Items[len(c.View.Items)-1].ID != key { t.Fatal("travel lost key or retained pending destruction") } // Saving an old source-world copy cannot complete its now-expired key action. oldWorld.Advance(now.Add(4*time.Second), 4) if e = hl.Save(ctx, &oldWorld, ""); e != nil { t.Fatal(e) } var keys int if e = db.sql.QueryRow(`SELECT COUNT(*) FROM house_keys WHERE state='active'`).Scan(&keys); e != nil || keys != 1 { t.Fatal("stale source destroyed traveling key", e) } act(nl, next, c, HouseRequest{Op: "destroy-key", Item: key}) c.View.Entity.HP = 0 next.Advance(now.Add(time.Second), 1) if e = nl.Save(ctx, next, ""); e != nil { t.Fatal(e) } corpse := next.Corpses[c.RevivalCorpse] if corpse == nil || c.View.DestroyKey != nil { t.Fatal("death failed to interrupt") } found := false for _, i := range corpse.Items { if i.ID == key { found = true } } if !found { t.Fatal("interrupted key missing from corpse") } // Replacement inherits no key. Recovery remains gated on that replacement's survival. replacement, e := next.Join(c.Account, "Replacement") if e != nil { t.Fatal(e) } _ = next.Connect(replacement.View.Entity.ID) replacement.View.Inventory["escape_stone"] = 1 if e = nl.Save(ctx, next, ""); e != nil { t.Fatal(e) } // Remove the deliberately stale fixture copy from the hub before arrival. delete(hub.Characters, c.View.Entity.ID) tr, e = nl.PrepareTravel(ctx, next, replacement.Account, game.ID(), hub.ID, "escape") if e != nil { t.Fatal(e) } _, e = hl.AcceptTravel(ctx, hub, replacement.Account, tr.ID) if e != nil { t.Fatal(e) } replacement = hub.ForAccount(replacement.Account) _ = hub.Connect(replacement.View.Entity.ID) if len(replacement.View.Items) != 1 { t.Fatal("replacement unexpectedly inherited items", replacement.View.Items) } // Starting knife only. e = hl.HouseAction(ctx, hub, replacement.Account, HouseRequest{RequestID: game.ID(), Op: "recover", HouseID: house}) if !errors.Is(e, ErrHousing) { t.Fatal("unproven replacement recovered", e) } replacement.WorldsSurvived = 1 act(hl, hub, replacement, HouseRequest{Op: "recover", HouseID: house}) if v := state(hub, replacement); v.Houses[0].Keys != 2 { t.Fatal("creator recovery invalidated corpse key") } replacement.View.Entity.Position = hub.HousingEntrancePosition() act(hl, hub, replacement, HouseRequest{Op: "enter", HouseID: house}) if state(hub, replacement).Room.Floor.Inventory["wood"] != 7 { t.Fatal("recovery recreated or lost contents") } // Restart exits the room and cancels an unfinished destruction, retaining its key and contents. key2 := replacement.View.Items[len(replacement.View.Items)-1].ID act(hl, hub, replacement, HouseRequest{Op: "destroy-key", Item: key2}) raw, _ := json.Marshal(hub) var restored game.World if e = json.Unmarshal(raw, &restored); e != nil { t.Fatal(e) } if e = restored.Restore(now.Add(10 * time.Second)); e != nil { t.Fatal(e) } rc := restored.ForAccount(replacement.Account) if rc == nil || rc.View.DestroyKey != nil || game.HouseID(rc) != "" { t.Fatal("restart preserved pending room/action") } next.Phase = "lost" if e = nl.Save(ctx, next, ""); e != nil { t.Fatal(e) } // An active house remains owned by valid keys when the recovery world is destroyed. if state(hub, replacement).Houses[0].State != "active" { t.Fatal("death-world loss deleted key-owned house") } e = hl.HouseAction(ctx, hub, replacement.Account, HouseRequest{RequestID: game.ID(), Op: "recover", HouseID: house}) if !errors.Is(e, ErrHousing) { t.Fatal("recovery after world destruction", e) } }