42 lines
1.3 KiB
Go
42 lines
1.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestFetchGroupDataUsesLatestHeartbeat(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.URL.Path {
|
|
case "/api/status-page/status":
|
|
_, _ = fmt.Fprint(w, `{"publicGroupList":[{"id":1,"name":"hidden","monitorList":[{"id":7,"name":"GPT Mix"}]}]}`)
|
|
case "/api/status-page/heartbeat/status":
|
|
_, _ = fmt.Fprint(w, `{"heartbeatList":{"7":[{"status":0,"time":"2026-09-05 10:00:00","ping":0},{"status":1,"time":"2026-09-05 10:03:00","ping":321}]},"uptimeList":{"7_24":0.95}}`)
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
|
|
result := fetchGroupData(context.Background(), server.Client(), map[string]interface{}{
|
|
"url": server.URL,
|
|
"slug": "status",
|
|
"categoryName": "API",
|
|
})
|
|
|
|
require.Len(t, result.Monitors, 1)
|
|
monitor := result.Monitors[0]
|
|
assert.Equal(t, "GPT Mix", monitor.Name)
|
|
assert.Equal(t, 1, monitor.Status)
|
|
assert.Equal(t, 321, monitor.Ping)
|
|
assert.Equal(t, "2026-09-05 10:03:00", monitor.LastChecked)
|
|
assert.Equal(t, 0.95, monitor.Uptime)
|
|
assert.Len(t, monitor.History, 2)
|
|
}
|