Files
mirror-3x-ui/internal/web/service/node_tree_test.go
T
Hamed 4954b032f1 feat(nodes): show live network throughput (#7249)
Comparing current traffic across nodes meant opening each node's history chart. The heartbeat already carries every node's upload and download rate.

Show both rates in a stacked column of the Nodes table, between Memory and Xray Version, and in the mobile detail dialog, which now follows heartbeat updates. A measured idle link reads 0 B/s; a disabled, offline or transitive node, a peer too old to report throughput, and a sample older than 30 seconds read as a dash. The sample's age is computed on the master and anchored to when the browser received it, so a skewed browser clock does not blank the column.

The probe now records a missing or invalid direction as unavailable where it used to store zero, so such peers also stop adding zero points to the history chart.

Fixes #7238
2026-11-29 17:44:53 +03:30

110 lines
3.7 KiB
Go

package service
import (
"testing"
"time"
"github.com/mhsanaei/3x-ui/v3/internal/database"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
)
// #4983: a transitive sub-node learned from a direct node must surface as its
// own read-only entry nested under its parent, and per-GUID counts must split a
// direct node's own inbounds from its sub-nodes'.
func TestGetNodeTree_SurfacesTransitiveNodeNestedUnderParent(t *testing.T) {
setupConflictDB(t)
db := database.GetDB()
svc := NodeService{}
selfGuid, _ := (&SettingService{}).GetPanelGuid()
if err := db.Create(&model.Node{
Id: 1, Name: "Node2", Address: "10.0.0.2", Port: 2053,
ApiToken: "t", Guid: "node2-guid", Status: "online",
}).Error; err != nil {
t.Fatalf("create node: %v", err)
}
// Node2's own inbound and a transitive inbound physically on Node3
// (managed through Node2, so node_id = Node2 but origin = Node3).
nid := 1
if err := db.Create(&model.Inbound{Tag: "n1-own", Enable: true, Port: 443, Protocol: model.VLESS, Settings: `{"clients":[]}`, NodeID: &nid, OriginNodeGuid: "node2-guid"}).Error; err != nil {
t.Fatalf("create own inbound: %v", err)
}
if err := db.Create(&model.Inbound{Tag: "n1-via", Enable: true, Port: 8443, Protocol: model.VLESS, Settings: `{"clients":[]}`, NodeID: &nid, OriginNodeGuid: "node3-guid"}).Error; err != nil {
t.Fatalf("create transitive inbound: %v", err)
}
// The heartbeat learned that Node2 manages Node3.
nodeDescendantsMu.Lock()
nodeDescendantsCache[1] = []model.NodeSummary{{
Guid: "node3-guid", ParentGuid: "node2-guid", Name: "Node3", Address: "10.0.0.3", Status: "online",
}}
nodeDescendantsMu.Unlock()
t.Cleanup(func() {
nodeDescendantsMu.Lock()
nodeDescendantsCache = map[int][]model.NodeSummary{}
nodeDescendantsMu.Unlock()
})
tree, err := svc.GetNodeTree()
if err != nil {
t.Fatalf("GetNodeTree: %v", err)
}
var node2, node3 *model.Node
for _, n := range tree {
switch n.Guid {
case "node2-guid":
node2 = n
case "node3-guid":
node3 = n
}
}
if node2 == nil || node3 == nil {
t.Fatalf("expected Node2 + transitive Node3, got %d nodes", len(tree))
return
}
if node2.ParentGuid != selfGuid {
t.Errorf("Node2 parent = %q, want this panel's GUID %q", node2.ParentGuid, selfGuid)
}
if !node3.Transitive || node3.ParentGuid != "node2-guid" {
t.Errorf("Node3 should be transitive under node2-guid, got transitive=%v parent=%q", node3.Transitive, node3.ParentGuid)
}
if node3.Id != 0 {
t.Errorf("transitive node must be a read-only projection (Id 0), got Id=%d", node3.Id)
}
if node2.InboundCount != 1 {
t.Errorf("Node2 should host only its own inbound, got InboundCount=%d", node2.InboundCount)
}
if node3.InboundCount != 1 {
t.Errorf("transitive Node3 should host its 1 inbound, got %d", node3.InboundCount)
}
}
func TestNodeTreeViewReportsMasterRelativeHeartbeatAge(t *testing.T) {
setupConflictDB(t)
now := time.Now()
for _, node := range []model.Node{
{Name: "sampled", Address: "10.0.0.2", Port: 2053, LastHeartbeat: now.Add(-5 * time.Second).Unix()},
{Name: "unprobed", Address: "10.0.0.3", Port: 2053},
} {
if err := database.GetDB().Create(&node).Error; err != nil {
t.Fatalf("create node: %v", err)
}
}
views, err := (&NodeService{}).GetNodeTreeView()
if err != nil || len(views) != 2 {
t.Fatalf("get node views: len=%d err=%v", len(views), err)
}
sampled, unprobed := views[0], views[1]
minAge := now.UnixMilli() - sampled.LastHeartbeat*1000
maxAge := time.Now().UnixMilli() - sampled.LastHeartbeat*1000
if age := sampled.HeartbeatAgeMillis; age == nil || *age < minAge || *age > maxAge {
t.Fatalf("sampled heartbeat age = %v, want a value in [%d, %d]", age, minAge, maxAge)
}
if unprobed.HeartbeatAgeMillis != nil {
t.Fatal("unprobed node has a heartbeat age")
}
}