45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
package service
|
|
|
|
import (
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
)
|
|
|
|
func TestUpdateHeartbeatStoresNetMetrics(t *testing.T) {
|
|
_ = initTrafficTestDB(t)
|
|
s := &NodeService{}
|
|
|
|
n := &model.Node{Name: "netn", Address: "1.2.3.4", Port: 2053, Scheme: "https", ApiToken: "t"}
|
|
if err := database.GetDB().Create(n).Error; err != nil {
|
|
t.Fatalf("create node: %v", err)
|
|
}
|
|
|
|
patch := HeartbeatPatch{Status: "online", LastHeartbeat: time.Now().Unix(), NetUp: 111, NetDown: 222}
|
|
if err := s.UpdateHeartbeat(n.Id, patch); err != nil {
|
|
t.Fatalf("UpdateHeartbeat: %v", err)
|
|
}
|
|
|
|
var got model.Node
|
|
if err := database.GetDB().First(&got, n.Id).Error; err != nil {
|
|
t.Fatalf("reload node: %v", err)
|
|
}
|
|
if got.NetUp != 111 || got.NetDown != 222 {
|
|
t.Fatalf("net columns not persisted: up=%d down=%d", got.NetUp, got.NetDown)
|
|
}
|
|
if len(s.AggregateNodeMetric(n.Id, "netUp", 2, 60)) == 0 {
|
|
t.Fatal("expected netUp history points after an online heartbeat")
|
|
}
|
|
}
|
|
|
|
func TestNodeMetricKeysIncludesNet(t *testing.T) {
|
|
for _, k := range []string{"netUp", "netDown"} {
|
|
if !slices.Contains(NodeMetricKeys, k) {
|
|
t.Fatalf("NodeMetricKeys must include %q so the history endpoint accepts it", k)
|
|
}
|
|
}
|
|
}
|