From 3cfde577ebb262a0e5e2586fb0b2724369bc3d9c Mon Sep 17 00:00:00 2001 From: xinian Date: Thu, 16 Apr 2026 09:21:39 +0800 Subject: [PATCH] test: add pet fusion transaction coverage --- modules/player/service/pet_fusion_tx.go | 28 +++++++++++ modules/player/service/pet_fusion_tx_test.go | 50 ++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 modules/player/service/pet_fusion_tx_test.go diff --git a/modules/player/service/pet_fusion_tx.go b/modules/player/service/pet_fusion_tx.go index 5abc44dc5..66d0fcc76 100644 --- a/modules/player/service/pet_fusion_tx.go +++ b/modules/player/service/pet_fusion_tx.go @@ -72,6 +72,9 @@ func (s *UserService) PetFusionTx( return nil } + if err := syncPetSnapshotBeforeDeleteTx(tx, userID, currentInfo, masterCatchTime); err != nil { + return err + } if err := deletePetTx(tx, userID, masterCatchTime); err != nil { return err } @@ -82,6 +85,9 @@ func (s *UserService) PetFusionTx( } result.CostItemUsed = used if !used { + if err := syncPetSnapshotBeforeDeleteTx(tx, userID, currentInfo, auxCatchTime); err != nil { + return err + } if err := deletePetTx(tx, userID, auxCatchTime); err != nil { return err } @@ -223,6 +229,28 @@ func updatePetDataTx(tx gdb.TX, userID uint32, catchTime uint32, pet model.PetIn return nil } +func syncPetSnapshotBeforeDeleteTx(tx gdb.TX, userID uint32, info model.PlayerInfo, catchTime uint32) error { + pet, ok := findPetDataSnapshotInPlayerInfo(info, catchTime) + if !ok { + return nil + } + return updatePetDataTx(tx, userID, catchTime, pet) +} + +func findPetDataSnapshotInPlayerInfo(info model.PlayerInfo, catchTime uint32) (model.PetInfo, bool) { + for i := range info.PetList { + if info.PetList[i].CatchTime == catchTime { + return info.PetList[i], true + } + } + for i := range info.BackupPetList { + if info.BackupPetList[i].CatchTime == catchTime { + return info.BackupPetList[i], true + } + } + return model.PetInfo{}, false +} + func deletePetTx(tx gdb.TX, userID uint32, catchTime uint32) error { res, err := tx.Model(model.NewPet()). Where("player_id", userID). diff --git a/modules/player/service/pet_fusion_tx_test.go b/modules/player/service/pet_fusion_tx_test.go new file mode 100644 index 000000000..8eb81386e --- /dev/null +++ b/modules/player/service/pet_fusion_tx_test.go @@ -0,0 +1,50 @@ +package service + +import ( + "blazing/modules/player/model" + "testing" +) + +func TestFindPetDataSnapshotInPlayerInfo(t *testing.T) { + t.Run("pet list", func(t *testing.T) { + want := model.PetInfo{CatchTime: 1001, Level: 55} + info := model.PlayerInfo{ + PetList: []model.PetInfo{want}, + } + + got, ok := findPetDataSnapshotInPlayerInfo(info, want.CatchTime) + if !ok { + t.Fatal("expected pet snapshot in pet list") + } + if got.CatchTime != want.CatchTime || got.Level != want.Level { + t.Fatalf("unexpected pet snapshot: %+v", got) + } + }) + + t.Run("backup pet list", func(t *testing.T) { + want := model.PetInfo{CatchTime: 2002, Level: 66} + info := model.PlayerInfo{ + BackupPetList: []model.PetInfo{want}, + } + + got, ok := findPetDataSnapshotInPlayerInfo(info, want.CatchTime) + if !ok { + t.Fatal("expected pet snapshot in backup pet list") + } + if got.CatchTime != want.CatchTime || got.Level != want.Level { + t.Fatalf("unexpected pet snapshot: %+v", got) + } + }) + + t.Run("not found", func(t *testing.T) { + info := model.PlayerInfo{ + PetList: []model.PetInfo{{CatchTime: 3003}}, + BackupPetList: []model.PetInfo{{CatchTime: 4004}}, + } + + _, ok := findPetDataSnapshotInPlayerInfo(info, 9999) + if ok { + t.Fatal("expected missing pet snapshot") + } + }) +}