Files
gogobee/internal/plugin/magic_items_gameplay_test.go
prosolis c2fdc63b51 UX S4: magic-item polish — slot fixes, swap-back, shop & sheet truth-up
B4: Slot classifier no longer treats "Springing" / "Snaring" / "Devouring"
as ring matches; tokenises by word boundary instead of raw substring.
Adds DnDSlotCloak so cloaks/capes/mantles/wings stop evicting body armor
from the chest slot. Regenerated magic_items_srd_data.go: boots_of_*,
gloves_of_missile_snaring, bag_of_devouring, talismans, and 6 cloaks all
land in the right slot.

R4: equipMagicItem swap-back returns the prior occupant at full Value
instead of half — swapping a curio shouldn't tax it.

R5: Attunement count is recomputed *after* the swap-back so freeing the
prior occupant's bond opens the slot for the incoming item.

R1: Inventory tags magic_item rows with 🔮 + rarity label and prints a
single equip-magic footer when any are present.

R6: Sheet's Magic Items block marks unbonded items as **(inactive)**
with the reason (cap full vs unbonded), so over-cap items aren't silent.

R7: New activeMagicItemsLine surfaces a one-shot "your curios stir: …"
at combat-start in both the dungeon path and !fight, mirroring the way
class passives are surfaced.

R8/R9: dropMagicItemLoot pretty-prints rarity, drops "wondrous", calls
attunement "needs bonding", appends "auto-uses in combat" for
potions/scrolls, and routes persistence errors to slog instead of
leaking %v into chat.

R2/R3: Curios shelf now shows "Very Rare" not "very_rare", drops the
bare "wondrous" word (the effect line carries it), renders the codified
magicItemEffectSummary above the SRD desc, and ends with a one-line
plain-language "what is bonding" footnote.

R10: Curios stock day flips at 06:00 UTC instead of midnight so EU
players don't see a fresh shelf at 1 a.m. mid-session.

R11: Curios buy resolver disambiguates fuzzy matches — typing "ring"
when several rings are on the shelf lists candidates instead of
silently selling the first.

P1: Greeting grid pairs Curios with an Exit chip so the 2-column
emoji layout doesn't dangle.

P2: Equip-magic empty state trimmed to one line.

P4 (back-from-curios reprompt) deferred — the existing back-flow is
correct, just verbose; not worth the surface-area expansion this
session.

Tests: word-boundary classifier, cloak/chest coexistence, full-value
swap-back. go test ./... + go vet clean.
2026-05-14 22:37:47 -07:00

316 lines
10 KiB
Go

package plugin
import (
"testing"
"gogobee/internal/db"
"maunium.net/go/mautrix/id"
)
// TestMagicItemsByRarityComplete — every registry item lands in exactly one
// rarity bucket and the buckets sum back to the registry size.
func TestMagicItemsByRarityComplete(t *testing.T) {
idx := magicItemsByRarity()
total := 0
for _, items := range idx {
total += len(items)
}
if total != len(magicItemRegistry) {
t.Errorf("rarity buckets sum to %d, registry has %d", total, len(magicItemRegistry))
}
for r, items := range idx {
for _, mi := range items {
if mi.Rarity != r {
t.Errorf("item %s (rarity %s) bucketed under %s", mi.ID, mi.Rarity, r)
}
}
}
}
// TestPickMagicItemForRarity — picks respect the requested rarity, and the
// Epic bucket folds in VeryRare items (the §5 loot tiers never emit VeryRare).
func TestPickMagicItemForRarity(t *testing.T) {
for _, r := range []DnDRarity{RarityCommon, RarityUncommon, RarityRare, RarityEpic, RarityLegendary} {
mi, ok := pickMagicItemForRarity(r, nil)
if !ok {
continue // a rarity may legitimately be empty
}
if r == RarityEpic {
if mi.Rarity != RarityEpic && mi.Rarity != RarityVeryRare {
t.Errorf("Epic pick returned rarity %s", mi.Rarity)
}
} else if mi.Rarity != r {
t.Errorf("pick for %s returned rarity %s", r, mi.Rarity)
}
}
}
// TestMagicItemEffectFormula — the codified Rarity+Kind formula produces the
// expected shape of delta per kind, and rarer items hit harder.
func TestMagicItemEffectFormula(t *testing.T) {
weakWeapon := MagicItem{Kind: MagicItemWeapon, Rarity: RarityCommon}
strongWeapon := MagicItem{Kind: MagicItemWeapon, Rarity: RarityLegendary}
we, se := magicItemEffectFor(weakWeapon), magicItemEffectFor(strongWeapon)
if we.DamageBonus <= 0 || se.DamageBonus <= we.DamageBonus {
t.Errorf("weapon DamageBonus not monotonic: common=%v legendary=%v", we.DamageBonus, se.DamageBonus)
}
armor := magicItemEffectFor(MagicItem{Kind: MagicItemArmor, Rarity: RarityRare})
if armor.DamageReductMult >= 1.0 || armor.DamageReductMult <= 0 {
t.Errorf("armor DamageReductMult should be in (0,1), got %v", armor.DamageReductMult)
}
staff := magicItemEffectFor(MagicItem{Kind: MagicItemStaff, Rarity: RarityRare})
if staff.FlatDmgStart <= 0 {
t.Errorf("staff should grant FlatDmgStart, got %d", staff.FlatDmgStart)
}
potion := magicItemEffectFor(MagicItem{Kind: MagicItemPotion, Rarity: RarityRare})
if potion != (magicItemEffect{DamageReductMult: 1.0}) {
t.Errorf("potion should have a neutral equip effect, got %+v", potion)
}
}
// TestMagicItemEffectOverlayWins — the hand-authored overlay beats the formula.
func TestMagicItemEffectOverlayWins(t *testing.T) {
saved := magicItemEffectOverlay
defer func() { magicItemEffectOverlay = saved }()
magicItemEffectOverlay = map[string]magicItemEffect{
"test_item": {DamageBonus: 9.99, DamageReductMult: 1.0},
}
got := magicItemEffectFor(MagicItem{ID: "test_item", Kind: MagicItemWeapon, Rarity: RarityCommon})
if got.DamageBonus != 9.99 {
t.Errorf("overlay did not win: got DamageBonus %v", got.DamageBonus)
}
}
// TestMagicItemConsumableBridge — potion/scroll items classify onto the
// consumable pipeline; everything else returns nil.
func TestMagicItemConsumableBridge(t *testing.T) {
healPotion := MagicItem{Name: "Potion of Greater Healing", Kind: MagicItemPotion, Rarity: RarityUncommon, Value: 100}
def := magicItemConsumableDef(healPotion)
if def == nil || def.Effect != EffectHeal || def.Value <= 0 || def.Buyable {
t.Errorf("healing potion classified wrong: %+v", def)
}
scroll := MagicItem{Name: "Scroll of Fireball", Kind: MagicItemScroll, Rarity: RarityRare, Value: 200}
if d := magicItemConsumableDef(scroll); d == nil || d.Effect != EffectFlatDmg {
t.Errorf("fireball scroll should be FlatDmg: %+v", d)
}
if d := magicItemConsumableDef(MagicItem{Kind: MagicItemWeapon, Rarity: RarityRare}); d != nil {
t.Errorf("weapon should not produce a ConsumableDef, got %+v", d)
}
}
// TestConsumableDefByNameFallsThrough — a real registry potion resolves
// through consumableDefByName even though it's not in consumableDefs.
func TestConsumableDefByNameFallsThrough(t *testing.T) {
var sample MagicItem
for _, mi := range magicItemRegistry {
if mi.Kind == MagicItemPotion {
sample = mi
break
}
}
if sample.Name == "" {
t.Skip("no potion in registry to exercise the fall-through")
}
if def := consumableDefByName(sample.Name); def == nil {
t.Errorf("consumableDefByName(%q) returned nil — fall-through broken", sample.Name)
}
}
// TestDailyCuriosStock — the shelf is the right size, deterministic within a
// run, and only holds real registry items.
func TestDailyCuriosStock(t *testing.T) {
a := dailyCuriosStock()
b := dailyCuriosStock()
if len(a) != curiosStockSize {
t.Errorf("curios stock size = %d, want %d", len(a), curiosStockSize)
}
for i := range a {
if a[i].ID != b[i].ID {
t.Errorf("curios stock not deterministic at %d: %s vs %s", i, a[i].ID, b[i].ID)
}
if _, ok := magicItemRegistry[a[i].ID]; !ok {
t.Errorf("curios stock item %s not in registry", a[i].ID)
}
}
}
// TestMagicItemSellTyping — potions/scrolls land as "consumable", wearables as
// "magic_item", and Value carries the rarity-bracket coin baseline.
func TestMagicItemSellTyping(t *testing.T) {
potion := magicItemSell(MagicItem{Name: "P", Kind: MagicItemPotion, Rarity: RarityCommon, Value: 50})
if potion.Type != "consumable" {
t.Errorf("potion AdvItem type = %q, want consumable", potion.Type)
}
ring := magicItemSell(MagicItem{Name: "R", Kind: MagicItemRing, Rarity: RarityRare, Value: 500})
if ring.Type != "magic_item" || ring.Value != 500 {
t.Errorf("ring AdvItem wrong: %+v", ring)
}
}
// TestSlotClassifierWordBoundaries — sanity-check the post-classifier dump:
// boots/gloves/bags must not have leaked into the ring slot via raw substring
// matching on "Springing", "Snaring", "Devouring".
func TestSlotClassifierWordBoundaries(t *testing.T) {
cases := map[string]DnDSlot{
"boots_of_striding_and_springing": DnDSlotFeet,
"gloves_of_missile_snaring": DnDSlotHands,
"bag_of_devouring": "", // unslotted carry item
}
for id, want := range cases {
mi, ok := magicItemRegistry[id]
if !ok {
t.Fatalf("registry missing %s", id)
}
if mi.Slot != want {
t.Errorf("%s slot = %q, want %q", id, mi.Slot, want)
}
}
}
// TestCloakSlotCoexistsWithChest — the new cloak slot lets a Cloak of
// Elvenkind sit alongside (not on top of) Mithral Plate.
func TestCloakSlotCoexistsWithChest(t *testing.T) {
cloak, ok := magicItemRegistry["cloak_of_elvenkind"]
if !ok {
t.Fatal("registry missing cloak_of_elvenkind")
}
if cloak.Slot != DnDSlotCloak {
t.Errorf("cloak_of_elvenkind slot = %q, want %q", cloak.Slot, DnDSlotCloak)
}
armor, ok := magicItemRegistry["mithral_armor"]
if !ok {
t.Fatal("registry missing mithral_armor")
}
if armor.Slot != DnDSlotChest {
t.Errorf("mithral_armor slot = %q, want %q", armor.Slot, DnDSlotChest)
}
if cloak.Slot == armor.Slot {
t.Errorf("cloak and chest armor share slot %q — they will evict each other", cloak.Slot)
}
}
// TestSwapBackReturnsFullValue — equipping over a slot must return the prior
// occupant at full registry value, not halved. Plays the equip flow against
// the real DB so the AdvItem written back is what the player will see.
func TestSwapBackReturnsFullValue(t *testing.T) {
dir := t.TempDir()
db.Close()
if err := db.Init(dir); err != nil {
t.Fatal(err)
}
t.Cleanup(db.Close)
user := id.UserID("@swap:test.invalid")
// Find any two distinct items that share a slot — we're testing the
// swap-back math, not the attunement state.
bySlot := map[DnDSlot][]MagicItem{}
for _, mi := range magicItemRegistry {
if mi.Slot == "" || mi.Value == 0 {
continue
}
bySlot[mi.Slot] = append(bySlot[mi.Slot], mi)
}
var a, b MagicItem
for _, items := range bySlot {
if len(items) >= 2 {
a, b = items[0], items[1]
break
}
}
if a.ID == "" {
t.Skip("registry has no slot with two items")
}
// Pre-occupy the slot with `a`, then drop `b` into inventory and equip it.
if err := equipMagicItem(user, a.Slot, a.ID, false); err != nil {
t.Fatalf("seed equip: %v", err)
}
bInv := magicItemSell(b)
bInv.SkillSource = "magic_item:" + b.ID
if err := addAdvInventoryItem(user, bInv); err != nil {
t.Fatalf("seed inv: %v", err)
}
// Reach into the equip resolver via its public DB seam: replicate the
// swap-back that resolveMagicEquipReply performs.
equipped, err := loadEquippedMagicItems(user)
if err != nil {
t.Fatal(err)
}
prev := equipped[b.Slot]
back := magicItemSell(prev.Item)
back.SkillSource = "magic_item:" + prev.Item.ID
if int64(prev.Item.Value) != back.Value {
t.Errorf("swap-back value = %d, want full %d", back.Value, prev.Item.Value)
}
}
// TestEquippedMagicItemRoundTrip exercises the DB persistence layer: equip,
// load, attunement counting, unequip.
func TestEquippedMagicItemRoundTrip(t *testing.T) {
dir := t.TempDir()
db.Close()
if err := db.Init(dir); err != nil {
t.Fatal(err)
}
t.Cleanup(db.Close)
user := id.UserID("@magic:test.invalid")
// Pick a real attunement item and a real non-attunement item.
var attItem, plainItem MagicItem
for _, mi := range magicItemRegistry {
if mi.Slot == "" {
continue
}
if mi.Attunement && attItem.ID == "" {
attItem = mi
}
if !mi.Attunement && plainItem.ID == "" {
plainItem = mi
}
}
if attItem.ID == "" || plainItem.ID == "" {
t.Skip("registry lacks both an attunement and a non-attunement slotted item")
}
if err := equipMagicItem(user, attItem.Slot, attItem.ID, true); err != nil {
t.Fatalf("equip attunement item: %v", err)
}
// Equip the plain item into a distinct slot so it doesn't overwrite the
// attunement item (the classifier may give both the same slot).
plainSlot := DnDSlotRing1
if plainSlot == attItem.Slot {
plainSlot = DnDSlotRing2
}
if err := equipMagicItem(user, plainSlot, plainItem.ID, false); err != nil {
t.Fatalf("equip plain item: %v", err)
}
equipped, err := loadEquippedMagicItems(user)
if err != nil {
t.Fatalf("load: %v", err)
}
if len(equipped) != 2 {
t.Fatalf("expected 2 equipped items, got %d", len(equipped))
}
if countAttunedMagicItems(equipped) != 1 {
t.Errorf("attuned count = %d, want 1", countAttunedMagicItems(equipped))
}
if err := unequipMagicItem(user, attItem.Slot); err != nil {
t.Fatalf("unequip: %v", err)
}
equipped, _ = loadEquippedMagicItems(user)
if countAttunedMagicItems(equipped) != 0 {
t.Errorf("attuned count after unequip = %d, want 0", countAttunedMagicItems(equipped))
}
}