254 lines
6.6 KiB
Go
254 lines
6.6 KiB
Go
package functions
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"eq2emu/internal/events"
|
|
"eq2emu/internal/spawn"
|
|
)
|
|
|
|
func TestAllEQ2Functions(t *testing.T) {
|
|
// Create event handler
|
|
handler := events.NewEventHandler()
|
|
|
|
// Register all EQ2 functions
|
|
err := RegisterAllEQ2Functions(handler)
|
|
if err != nil {
|
|
t.Fatalf("Failed to register all EQ2 functions: %v", err)
|
|
}
|
|
|
|
// Verify we have a substantial number of functions registered
|
|
events := handler.ListEvents()
|
|
if len(events) < 100 {
|
|
t.Errorf("Expected at least 100 functions, got %d", len(events))
|
|
}
|
|
|
|
// Test some key functions exist
|
|
requiredFunctions := []string{
|
|
"SetCurrentHP", "GetCurrentHP", "SetMaxHP", "GetMaxHP",
|
|
"SetLevel", "GetLevel", "SetClass", "GetClass",
|
|
"SetPosition", "GetPosition", "GetX", "GetY", "GetZ",
|
|
"Attack", "AddHate", "SpellDamage", "IsInCombat",
|
|
"GetName", "GetID", "IsPlayer", "IsNPC",
|
|
"MakeRandomInt", "ParseInt", "LogMessage",
|
|
}
|
|
|
|
for _, funcName := range requiredFunctions {
|
|
if !handler.HasEvent(funcName) {
|
|
t.Errorf("Required function %s not registered", funcName)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHealthFunctions(t *testing.T) {
|
|
handler := events.NewEventHandler()
|
|
err := RegisterAllEQ2Functions(handler)
|
|
if err != nil {
|
|
t.Fatalf("Failed to register functions: %v", err)
|
|
}
|
|
|
|
// Create test spawn
|
|
testSpawn := &spawn.Spawn{}
|
|
|
|
// Test SetCurrentHP
|
|
ctx := events.NewEventContext(events.EventTypeSpawn, "SetCurrentHP", "test").
|
|
WithSpawn(testSpawn).
|
|
WithParameter("hp", 250.0)
|
|
|
|
err = handler.Execute(ctx)
|
|
if err != nil {
|
|
t.Fatalf("SetCurrentHP failed: %v", err)
|
|
}
|
|
|
|
// Test GetCurrentHP
|
|
ctx2 := events.NewEventContext(events.EventTypeSpawn, "GetCurrentHP", "test").
|
|
WithSpawn(testSpawn)
|
|
|
|
err = handler.Execute(ctx2)
|
|
if err != nil {
|
|
t.Fatalf("GetCurrentHP failed: %v", err)
|
|
}
|
|
|
|
// Verify result
|
|
if hp, exists := ctx2.GetResult("hp"); !exists {
|
|
t.Error("GetCurrentHP should return hp result")
|
|
} else if hp != int32(250) {
|
|
t.Errorf("Expected HP 250, got %v", hp)
|
|
}
|
|
}
|
|
|
|
func TestAttributeFunctions(t *testing.T) {
|
|
handler := events.NewEventHandler()
|
|
err := RegisterAllEQ2Functions(handler)
|
|
if err != nil {
|
|
t.Fatalf("Failed to register functions: %v", err)
|
|
}
|
|
|
|
// Create test spawn
|
|
testSpawn := &spawn.Spawn{}
|
|
|
|
// Test SetLevel
|
|
ctx := events.NewEventContext(events.EventTypeSpawn, "SetLevel", "test").
|
|
WithSpawn(testSpawn).
|
|
WithParameter("level", 50)
|
|
|
|
err = handler.Execute(ctx)
|
|
if err != nil {
|
|
t.Fatalf("SetLevel failed: %v", err)
|
|
}
|
|
|
|
// Test GetLevel
|
|
ctx2 := events.NewEventContext(events.EventTypeSpawn, "GetLevel", "test").
|
|
WithSpawn(testSpawn)
|
|
|
|
err = handler.Execute(ctx2)
|
|
if err != nil {
|
|
t.Fatalf("GetLevel failed: %v", err)
|
|
}
|
|
|
|
// Verify result
|
|
if level, exists := ctx2.GetResult("level"); !exists {
|
|
t.Error("GetLevel should return level result")
|
|
} else if level != int16(50) {
|
|
t.Errorf("Expected level 50, got %v", level)
|
|
}
|
|
}
|
|
|
|
func TestMovementFunctions(t *testing.T) {
|
|
handler := events.NewEventHandler()
|
|
err := RegisterAllEQ2Functions(handler)
|
|
if err != nil {
|
|
t.Fatalf("Failed to register functions: %v", err)
|
|
}
|
|
|
|
// Create test spawn
|
|
testSpawn := &spawn.Spawn{}
|
|
|
|
// Test SetPosition
|
|
ctx := events.NewEventContext(events.EventTypeSpawn, "SetPosition", "test").
|
|
WithSpawn(testSpawn).
|
|
WithParameter("x", 100.0).
|
|
WithParameter("y", 200.0).
|
|
WithParameter("z", 300.0).
|
|
WithParameter("heading", 180.0)
|
|
|
|
err = handler.Execute(ctx)
|
|
if err != nil {
|
|
t.Fatalf("SetPosition failed: %v", err)
|
|
}
|
|
|
|
// Test GetPosition
|
|
ctx2 := events.NewEventContext(events.EventTypeSpawn, "GetPosition", "test").
|
|
WithSpawn(testSpawn)
|
|
|
|
err = handler.Execute(ctx2)
|
|
if err != nil {
|
|
t.Fatalf("GetPosition failed: %v", err)
|
|
}
|
|
|
|
// Verify results
|
|
if x, exists := ctx2.GetResult("x"); !exists || x != float32(100.0) {
|
|
t.Errorf("Expected X=100.0, got %v (exists: %t)", x, exists)
|
|
}
|
|
if y, exists := ctx2.GetResult("y"); !exists || y != float32(200.0) {
|
|
t.Errorf("Expected Y=200.0, got %v (exists: %t)", y, exists)
|
|
}
|
|
if z, exists := ctx2.GetResult("z"); !exists || z != float32(300.0) {
|
|
t.Errorf("Expected Z=300.0, got %v (exists: %t)", z, exists)
|
|
}
|
|
}
|
|
|
|
func TestMiscFunctions(t *testing.T) {
|
|
handler := events.NewEventHandler()
|
|
err := RegisterAllEQ2Functions(handler)
|
|
if err != nil {
|
|
t.Fatalf("Failed to register functions: %v", err)
|
|
}
|
|
|
|
// Create test spawn
|
|
testSpawn := &spawn.Spawn{}
|
|
|
|
// Test GetName
|
|
ctx := events.NewEventContext(events.EventTypeSpawn, "GetName", "test").
|
|
WithSpawn(testSpawn)
|
|
|
|
err = handler.Execute(ctx)
|
|
if err != nil {
|
|
t.Fatalf("GetName failed: %v", err)
|
|
}
|
|
|
|
// Test MakeRandomInt
|
|
ctx2 := events.NewEventContext(events.EventTypeSpawn, "MakeRandomInt", "test").
|
|
WithParameter("min", 10).
|
|
WithParameter("max", 20)
|
|
|
|
err = handler.Execute(ctx2)
|
|
if err != nil {
|
|
t.Fatalf("MakeRandomInt failed: %v", err)
|
|
}
|
|
|
|
if result, exists := ctx2.GetResult("random_int"); !exists {
|
|
t.Error("MakeRandomInt should return random_int result")
|
|
} else if randInt, ok := result.(int); !ok || randInt < 10 || randInt > 20 {
|
|
t.Errorf("Expected random int between 10-20, got %v", result)
|
|
}
|
|
}
|
|
|
|
func TestFunctionsByDomain(t *testing.T) {
|
|
domains := GetFunctionsByDomain()
|
|
|
|
// Verify we have expected domains
|
|
expectedDomains := []string{"health", "attributes", "movement", "combat", "misc"}
|
|
for _, domain := range expectedDomains {
|
|
if functions, exists := domains[domain]; !exists {
|
|
t.Errorf("Domain %s not found", domain)
|
|
} else if len(functions) == 0 {
|
|
t.Errorf("Domain %s has no functions", domain)
|
|
}
|
|
}
|
|
|
|
// Verify health domain has expected functions
|
|
healthFunctions := domains["health"]
|
|
expectedHealthFunctions := []string{"SetCurrentHP", "GetCurrentHP", "SetMaxHP", "GetMaxHP"}
|
|
for _, funcName := range expectedHealthFunctions {
|
|
found := false
|
|
for _, f := range healthFunctions {
|
|
if f == funcName {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Errorf("Health domain missing function %s", funcName)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestErrorHandling(t *testing.T) {
|
|
handler := events.NewEventHandler()
|
|
err := RegisterAllEQ2Functions(handler)
|
|
if err != nil {
|
|
t.Fatalf("Failed to register functions: %v", err)
|
|
}
|
|
|
|
// Test function with no spawn context
|
|
ctx := events.NewEventContext(events.EventTypeSpawn, "SetCurrentHP", "test").
|
|
WithParameter("hp", 100.0)
|
|
// No spawn set
|
|
|
|
err = handler.Execute(ctx)
|
|
if err == nil {
|
|
t.Error("SetCurrentHP should fail without spawn context")
|
|
}
|
|
|
|
// Test function with invalid parameters
|
|
testSpawn := &spawn.Spawn{}
|
|
ctx2 := events.NewEventContext(events.EventTypeSpawn, "SetCurrentHP", "test").
|
|
WithSpawn(testSpawn).
|
|
WithParameter("hp", -50.0) // Negative HP
|
|
|
|
err = handler.Execute(ctx2)
|
|
if err == nil {
|
|
t.Error("SetCurrentHP should fail with negative HP")
|
|
}
|
|
} |