route available-spells-new

This commit is contained in:
2025-07-31 22:49:46 +02:00
parent 6157f926ce
commit f1f2b01248
6 changed files with 344 additions and 4 deletions
+206
View File
@@ -416,3 +416,209 @@ func TestGetAvailableSkillsNew(t *testing.T) {
}
}
}
func TestGetAvailableSpellsNew(t *testing.T) {
database.SetupTestDB(true) // Setup test database
// Initialize a Gin router
r := gin.Default()
router.SetupGin(r)
// Routes
protected := router.BaseRouterGrp(r)
character.RegisterRoutes(protected)
gsmaster.RegisterRoutes(protected)
protected.GET("/test", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "Test OK"})
})
u := user.User{}
u.First("testuser")
// Create request body for available spells
spellRequest := gsmaster.LernCostRequest{
CharId: 20,
Name: "Angst", // Use a valid spell name for validation
CurrentLevel: 0,
Type: "spell",
Action: "learn",
TargetLevel: 1,
UsePP: 0,
UseGold: 0,
Reward: &[]string{"default"}[0],
}
jsonData, _ := json.Marshal(spellRequest)
// Create a test HTTP request
req, _ := http.NewRequest("POST", "/api/characters/available-spells-new", bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
token := user.GenerateToken(&u)
req.Header.Set("Authorization", "Bearer "+token)
// Create a response recorder to capture the handler's response
respRecorder := httptest.NewRecorder()
// Perform the test request
r.ServeHTTP(respRecorder, req)
// Assert the response status code
assert.Equal(t, http.StatusOK, respRecorder.Code)
// Parse the response to verify it contains spells by school
var response map[string]interface{}
err := json.Unmarshal(respRecorder.Body.Bytes(), &response)
assert.NoError(t, err, "Response should be valid JSON")
// Check that the response contains spells_by_school
spellsBySchool, exists := response["spells_by_school"]
assert.True(t, exists, "Response should contain spells_by_school")
assert.NotNil(t, spellsBySchool, "spells_by_school should not be nil")
// Convert to map for easier access
spellsMap, ok := spellsBySchool.(map[string]interface{})
assert.True(t, ok, "spells_by_school should be a map")
assert.Greater(t, len(spellsMap), 0, "Should return at least one school of spells")
// Verify that each spell has the expected structure and check for fallback values
fallbackSpells := []string{}
totalSpells := 0
for schoolName, schoolSpellsInterface := range spellsMap {
schoolSpells, ok := schoolSpellsInterface.([]interface{})
assert.True(t, ok, "School %s should contain an array of spells", schoolName)
for _, spellInterface := range schoolSpells {
spell, ok := spellInterface.(map[string]interface{})
assert.True(t, ok, "Each spell should be a map")
totalSpells++
// Check required fields
name, hasName := spell["name"]
level, hasLevel := spell["level"]
epCost, hasEpCost := spell["epCost"]
goldCost, hasGoldCost := spell["goldCost"]
assert.True(t, hasName, "Spell should have name field")
assert.True(t, hasLevel, "Spell should have level field")
assert.True(t, hasEpCost, "Spell should have epCost field")
assert.True(t, hasGoldCost, "Spell should have goldCost field")
// Check for fallback values (10000 EP, 50000 GS)
if epCostFloat, ok := epCost.(float64); ok && epCostFloat == 10000 {
if goldCostFloat, ok := goldCost.(float64); ok && goldCostFloat == 50000 {
fallbackSpells = append(fallbackSpells, name.(string))
t.Logf("FALLBACK VALUES DETECTED: Spell '%s' (Level %v) - EP: %.0f, Gold: %.0f",
name, level, epCostFloat, goldCostFloat)
}
}
// Log first few spells for debugging
if totalSpells <= 5 {
t.Logf("Spell '%s' (Level %v) - EP: %v, Gold: %v", name, level, epCost, goldCost)
}
}
}
// Assert that no spells have fallback values
if len(fallbackSpells) > 0 {
t.Errorf("Found %d spells with fallback values (10000 EP, 50000 GS): %v",
len(fallbackSpells), fallbackSpells)
}
t.Logf("Total spells checked: %d, spells with fallback values: %d", totalSpells, len(fallbackSpells))
}
func TestFallbackValueDetection(t *testing.T) {
database.SetupTestDB(true) // Setup test database
// Initialize a Gin router
r := gin.Default()
router.SetupGin(r)
// Routes
protected := router.BaseRouterGrp(r)
character.RegisterRoutes(protected)
gsmaster.RegisterRoutes(protected)
u := user.User{}
u.First("testuser")
// Test both skills and spells for fallback values
testCases := []struct {
endpoint string
itemType string
testName string
}{
{"/api/characters/available-skills-new", "skill", "Schwimmen"},
{"/api/characters/available-spells-new", "spell", "Angst"},
}
for _, tc := range testCases {
t.Run(tc.itemType, func(t *testing.T) {
request := gsmaster.LernCostRequest{
CharId: 20,
Name: tc.testName,
CurrentLevel: 0,
Type: tc.itemType,
Action: "learn",
TargetLevel: 1,
UsePP: 0,
UseGold: 0,
Reward: &[]string{"default"}[0],
}
jsonData, _ := json.Marshal(request)
req, _ := http.NewRequest("POST", tc.endpoint, bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
token := user.GenerateToken(&u)
req.Header.Set("Authorization", "Bearer "+token)
respRecorder := httptest.NewRecorder()
r.ServeHTTP(respRecorder, req)
assert.Equal(t, http.StatusOK, respRecorder.Code)
var response map[string]interface{}
err := json.Unmarshal(respRecorder.Body.Bytes(), &response)
assert.NoError(t, err)
fallbackCount := 0
totalItems := 0
// Check for fallback values in response
var dataKey string
if tc.itemType == "skill" {
dataKey = "skills_by_category"
} else {
dataKey = "spells_by_school"
}
if data, exists := response[dataKey]; exists {
if dataMap, ok := data.(map[string]interface{}); ok {
for _, itemsInterface := range dataMap {
if items, ok := itemsInterface.([]interface{}); ok {
for _, itemInterface := range items {
if item, ok := itemInterface.(map[string]interface{}); ok {
totalItems++
if epCost, hasEP := item["epCost"]; hasEP {
if goldCost, hasGold := item["goldCost"]; hasGold {
if epFloat, ok := epCost.(float64); ok && epFloat == 10000 {
if goldFloat, ok := goldCost.(float64); ok && goldFloat == 50000 {
fallbackCount++
t.Logf("FALLBACK DETECTED in %s '%s': EP=%v, Gold=%v",
tc.itemType, item["name"], epCost, goldCost)
}
}
}
}
}
}
}
}
}
}
t.Logf("%s test: Total items=%d, Fallback values=%d", tc.itemType, totalItems, fallbackCount)
assert.Equal(t, 0, fallbackCount, "No %s should have fallback values (10000 EP, 50000 GS)", tc.itemType)
})
}
}