veza/veza-backend-api/internal/handlers/system_metrics_test.go
senke 0950fa30aa [T0-006] test(backend): Ajout tests pour frontend_log_handler
- Tests complets pour frontend_log_handler.go (12 tests)
- Tests couvrent NewFrontendLogHandler et ReceiveLog
- Tests pour tous les niveaux de log (DEBUG, INFO, WARN, ERROR)
- Tests pour gestion des erreurs et validation JSON
- Couverture actuelle: 30.6% (objectif: 80%)

Files: veza-backend-api/internal/handlers/frontend_log_handler_test.go
       VEZA_ROADMAP.json
Hours: 16 estimated, 23 actual
2026-01-04 01:44:22 +01:00

94 lines
2.2 KiB
Go

package handlers
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestSystemMetrics_Success(t *testing.T) {
// Setup
gin.SetMode(gin.TestMode)
router := gin.New()
router.GET("/system/metrics", SystemMetrics)
// Execute
req, _ := http.NewRequest("GET", "/system/metrics", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
// Assert
assert.Equal(t, http.StatusOK, w.Code)
var response map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &response)
assert.NoError(t, err)
// Verify structure
assert.Contains(t, response, "timestamp")
assert.Contains(t, response, "memory")
assert.Contains(t, response, "goroutines")
assert.Contains(t, response, "cpu_count")
// Verify memory structure
memory, ok := response["memory"].(map[string]interface{})
assert.True(t, ok)
assert.Contains(t, memory, "alloc_mb")
assert.Contains(t, memory, "total_alloc_mb")
assert.Contains(t, memory, "sys_mb")
assert.Contains(t, memory, "num_gc")
// Verify numeric values
assert.IsType(t, float64(0), response["goroutines"])
assert.IsType(t, float64(0), response["cpu_count"])
}
func TestSystemMetrics_MultipleRequests(t *testing.T) {
// Setup
gin.SetMode(gin.TestMode)
router := gin.New()
router.GET("/system/metrics", SystemMetrics)
// Execute multiple requests
for i := 0; i < 3; i++ {
req, _ := http.NewRequest("GET", "/system/metrics", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
// Assert
assert.Equal(t, http.StatusOK, w.Code)
var response map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &response)
assert.NoError(t, err)
assert.Contains(t, response, "memory")
assert.Contains(t, response, "goroutines")
}
}
func TestBToMb_Conversion(t *testing.T) {
tests := []struct {
name string
input uint64
expected uint64
}{
{"Zero bytes", 0, 0},
{"1 MB", 1024 * 1024, 1},
{"10 MB", 10 * 1024 * 1024, 10},
{"100 MB", 100 * 1024 * 1024, 100},
{"Less than 1 MB", 512 * 1024, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := bToMb(tt.input)
assert.Equal(t, tt.expected, result)
})
}
}