game/server/characters/characters_test.go
2026-04-20 16:07:22 +08:00

130 lines
3.2 KiB
Go
Executable File

package characters
import (
"testing"
"wuziqi-server/core"
)
func TestGetInitialHP(t *testing.T) {
// Setup custom config
hpConfig := map[string]int{
"custom_char": 10,
}
manager := NewCharacterManager(hpConfig)
// Test Case 1: Configured Override
hp := manager.GetInitialHP("custom_char", 4)
if hp != 10 {
t.Errorf("Expected configured HP 10, got %d", hp)
}
// Test Case 2: Config Override Default (if key exists)
// If we map "dog" -> 6
manager.HPConfig["dog"] = 6
hp = manager.GetInitialHP("dog", 4)
if hp != 6 {
t.Errorf("Expected configured dog HP 6, got %d", hp)
}
// Test Case 3: Global Default (hardcoded default arg)
// Passing a new manager for cleanliness
manager2 := NewCharacterManager(nil)
hp = manager2.GetInitialHP("unknown", 5)
if hp != 5 {
t.Errorf("Expected default arg HP 5, got %d", hp)
}
// Test Case 4: Character Data Default
// "cat" is 3 in manager.go data
hp = manager2.GetInitialHP("cat", 0)
if hp != 3 {
t.Errorf("Expected cat data HP 3, got %d", hp)
}
// Test Case 5: Final Fallback
hp = manager2.GetInitialHP("unknown_and_no_default", 0)
if hp != 4 {
t.Errorf("Expected final fallback 4, got %d", hp)
}
}
func TestOnDamageTaken(t *testing.T) {
manager := NewCharacterManager(nil)
p := &core.Player{}
// Test Cat: Always takes 1 dmg
p.Character = "cat"
dmg := manager.OnDamageTaken(p, 100, false)
if dmg != 1 {
t.Errorf("Cat should take 1 dmg, got %d", dmg)
}
// Test Other: Takes raw dmg
p.Character = "dog"
dmg = manager.OnDamageTaken(p, 5, false)
if dmg != 5 {
t.Errorf("Dog should take 5 dmg, got %d", dmg)
}
}
func TestShouldResistPoison(t *testing.T) {
manager := NewCharacterManager(nil)
if !manager.ShouldResistPoison("sloth") {
t.Error("Sloth should resist poison")
}
if manager.ShouldResistPoison("dog") {
t.Error("Dog should not resist poison")
}
}
func TestTryTriggerChickenAbility(t *testing.T) {
manager := NewCharacterManager(nil)
p := &core.Player{Character: "chicken", ChickenItemCount: 0}
// This is probabilistic, so hard to deterministic test without mocking rand.
// But we can verify logic constraints.
// If count >= 2, should never trigger
p.ChickenItemCount = 2
item, triggered := manager.TryTriggerChickenAbility(p)
if triggered {
t.Error("Chicken should not trigger if count >= 2")
}
if item != "" {
t.Error("Should return empty item")
}
// Not Chicken
p.Character = "dog"
p.ChickenItemCount = 0
_, triggered = manager.TryTriggerChickenAbility(p)
if triggered {
t.Error("Non-chicken should not trigger")
}
}
func TestTryTriggerHippoResist(t *testing.T) {
manager := NewCharacterManager(nil)
p := &core.Player{Character: "hippo", HippoDeathImmune: false}
// Probabilistic again.
// Test constraint: if already immune, won't trigger again?
// Actually code says: `if !target.HippoDeathImmune` -> set true -> return true.
// So if it returns true, immune flag must be set.
p.HippoDeathImmune = true
triggered := manager.TryTriggerHippoResist(p)
if triggered {
t.Error("Should not trigger if already immune used")
}
// Not Hippo
p.Character = "dog"
p.HippoDeathImmune = false
triggered = manager.TryTriggerHippoResist(p)
if triggered {
t.Error("Non-hippo should not trigger")
}
}