Files
gogobee/internal/plugin/adventure_pet_combat_xp_test.go
prosolis ca2d1a8ea3 pets: earn XP from combat wins again
petGrantXP has been dead code since R1 deleted the legacy daily activity
loop it used to ride. Nothing replaced the call, so for the whole life of
Adventure 2.0 the only pet XP in the game came from a paid babysitter.
Prod bears it out: the one player who never subscribed has a pet sitting
at level 1 with 0 XP after months of play.

That is not cosmetic. DerivePlayerStats scales PetAttackProc,
PetDeflectProc and PetAttackDmg off pet level, so a frozen pet is a
permanently dead combat slot that the player has no way to revive.

Wire it into postCombatBookkeeping — the one seam all four combat
close-outs already meet, so a pet cannot level differently depending on
whether the fight auto-resolved or was played a round at a time. Both
slots earn on the same win, matching the babysit trickle: combat only
reads the two pets' averaged procs, so leveling both is not a spike.

Writes go through the narrow per-slot pet upserts rather than
saveAdvCharacter, because this runs on a path that does not hold the
per-user lock and a full-row write could clobber a concurrent save.

Verified against the sim: a level-3 pet finishes one L10 expedition at
level 4 with carryover, where before it finished exactly where it started.
2026-07-20 23:51:49 -07:00

119 lines
3.2 KiB
Go

package plugin
import (
"testing"
"gogobee/internal/db"
"maunium.net/go/mautrix/id"
)
func newPetXPTestDB(t *testing.T) {
t.Helper()
dir := t.TempDir()
db.Close()
if err := db.Init(dir); err != nil {
t.Fatal(err)
}
t.Cleanup(db.Close)
}
// TestGrantPetCombatXPPersists is the regression guard for the bug this fixes:
// petGrantXP existed but nothing called it, so an un-babysat pet sat at its
// adoption level forever. A win must move XP on disk.
func TestGrantPetCombatXPPersists(t *testing.T) {
newPetXPTestDB(t)
uid := id.UserID("@petxp:test")
pet := PetState{Type: "dog", Name: "Rex", Arrived: true, Level: 1, XP: 0}
if err := upsertPlayerMetaPetState(uid, pet); err != nil {
t.Fatal(err)
}
if leveled := grantPetCombatXP(uid); len(leveled) != 0 {
t.Errorf("one win should not level a fresh pet, got %v", leveled)
}
got, err := loadPetState(uid)
if err != nil {
t.Fatal(err)
}
if got.XP != int(petXPPerAction*100) {
t.Errorf("XP = %d, want %d", got.XP, int(petXPPerAction*100))
}
if got.Level != 1 {
t.Errorf("Level = %d, want 1", got.Level)
}
}
// TestGrantPetCombatXPLevelsBothSlots checks the second pet earns off the same
// win, matching the babysit trickle — combat only reads the two pets' averaged
// procs, so leveling both is not a power spike.
func TestGrantPetCombatXPLevelsBothSlots(t *testing.T) {
newPetXPTestDB(t)
uid := id.UserID("@petxp2:test")
// Both one grant short of level 2 (needs 10 XP = 1000 centi-XP).
short := 1000 - int(petXPPerAction*100)
if err := upsertPlayerMetaPetState(uid,
PetState{Type: "dog", Name: "Rex", Arrived: true, Level: 1, XP: short}); err != nil {
t.Fatal(err)
}
if err := upsertPlayerMetaPet2State(uid,
PetState{Type: "cat", Name: "Whiskers", Arrived: true, Level: 1, XP: short}); err != nil {
t.Fatal(err)
}
leveled := grantPetCombatXP(uid)
if len(leveled) != 2 {
t.Fatalf("expected both pets to level, got %v", leveled)
}
p1, _ := loadPetState(uid)
p2, _ := loadPet2State(uid)
if p1.Level != 2 || p2.Level != 2 {
t.Errorf("levels = %d/%d, want 2/2", p1.Level, p2.Level)
}
}
// TestGrantPetCombatXPIgnoresChasedAway — a pet that isn't with you doesn't
// fight, so it doesn't earn.
func TestGrantPetCombatXPIgnoresChasedAway(t *testing.T) {
newPetXPTestDB(t)
uid := id.UserID("@petxp3:test")
if err := upsertPlayerMetaPetState(uid, PetState{
Type: "dog", Name: "Rex", Arrived: true, ChasedAway: true, Level: 3, XP: 100,
}); err != nil {
t.Fatal(err)
}
grantPetCombatXP(uid)
got, _ := loadPetState(uid)
if got.XP != 100 {
t.Errorf("chased-away pet gained XP: %d, want 100", got.XP)
}
}
// TestGrantPetCombatXPCapsAtTen — a maxed pet stops earning rather than
// accumulating dead XP.
func TestGrantPetCombatXPCapsAtTen(t *testing.T) {
newPetXPTestDB(t)
uid := id.UserID("@petxp4:test")
if err := upsertPlayerMetaPetState(uid, PetState{
Type: "dog", Name: "Rex", Arrived: true, Level: 10, XP: 0,
}); err != nil {
t.Fatal(err)
}
if leveled := grantPetCombatXP(uid); len(leveled) != 0 {
t.Errorf("L10 pet should not level, got %v", leveled)
}
got, _ := loadPetState(uid)
if got.XP != 0 || got.Level != 10 {
t.Errorf("L10 pet moved: level %d xp %d", got.Level, got.XP)
}
}