package service import ( "testing" "github.com/mhsanaei/3x-ui/v3/internal/database/model" "github.com/mhsanaei/3x-ui/v3/internal/web/runtime" "github.com/mhsanaei/3x-ui/v3/internal/xray" ) // The node list counts only the clients this panel syncs as online; clients on the // node's unsynced inbounds must still reach it as a number, never as emails. func TestNodeTreeCountsEveryClientOnlineOnTheNode(t *testing.T) { db := initTrafficTestDB(t) useOnlineTestProcess(t) svc := &InboundService{} seedNodeRow(t, db, &model.Node{Id: 1, Name: "n1", Address: "127.0.0.1", Port: 2096, ApiToken: "tok", Enable: true, Guid: "node-guid"}) createNodeInboundWithClient(t, db, 1, "keep", 41411, "kept@x") settings := `{"clients":[{"email":"kept@x","enable":true}]}` sync := func(t *testing.T, report func(*runtime.TrafficSnapshot)) { t.Helper() snap := snapshotWithClients(t, "keep", settings, xray.ClientTraffic{Email: "kept@x", Enable: true}) report(snap) if _, err := svc.setRemoteTrafficLocked(1, snap, false, false); err != nil { t.Fatalf("sync: %v", err) } } sync(t, func(*runtime.TrafficSnapshot) {}) steps := []struct { name string report func(*runtime.TrafficSnapshot) subNode bool wantSynced int wantTotal int }{ { name: "own GUID entry counted once per client, other GUIDs left out", report: func(snap *runtime.TrafficSnapshot) { snap.OnlineTree = map[string][]string{ "node-guid": {"kept@x", "local-a@x", "local-a@x", "local-b@x"}, "someone-else": {"s1@x", "s2@x", "s3@x", "s4@x", "s5@x"}, } }, wantSynced: 1, wantTotal: 3, }, { name: "survives the per-GUID recount a sub-node triggers", report: func(snap *runtime.TrafficSnapshot) { snap.OnlineTree = map[string][]string{"node-guid": {"kept@x", "local-a@x"}} }, subNode: true, wantSynced: 1, wantTotal: 2, }, { name: "flat list of a node without the per-GUID endpoint", report: func(snap *runtime.TrafficSnapshot) { snap.OnlineEmails = []string{"local-a@x", "local-b@x", "local-c@x", "kept@x"} }, wantSynced: 1, wantTotal: 4, }, { name: "an empty report replaces the previous total", report: func(*runtime.TrafficSnapshot) {}, }, } for _, step := range steps { t.Run(step.name, func(t *testing.T) { if step.subNode { nodeDescendantsMu.Lock() nodeDescendantsCache[1] = []model.NodeSummary{{Guid: "sub-guid", ParentGuid: "node-guid", Name: "sub"}} nodeDescendantsMu.Unlock() t.Cleanup(func() { nodeDescendantsMu.Lock() nodeDescendantsCache = map[int][]model.NodeSummary{} nodeDescendantsMu.Unlock() }) } sync(t, step.report) tree, err := (&NodeService{}).GetNodeTree() if err != nil { t.Fatalf("GetNodeTree: %v", err) } for _, n := range tree { if n.Id != 1 { if n.OnlineTotalCount != 0 { t.Errorf("sub-node %q total online = %d, want 0: nothing reports it", n.Guid, n.OnlineTotalCount) } continue } if n.OnlineCount != step.wantSynced || n.OnlineTotalCount != step.wantTotal { t.Errorf("online = %d synced / %d total, want %d / %d", n.OnlineCount, n.OnlineTotalCount, step.wantSynced, step.wantTotal) } } if step.subNode && len(tree) != 2 { t.Fatalf("tree has %d nodes, want the node and its sub-node", len(tree)) } }) } } // A node the sync no longer reaches keeps no online clients, so it keeps no total either. func TestNodeOnlineTotalIsDroppedWithTheNodesOnlineClients(t *testing.T) { tests := []struct { name string drop func(svc *InboundService) }{ {"failed probe", func(svc *InboundService) { svc.ClearNodeOnlineClients(1) }}, {"node disabled", func(svc *InboundService) { svc.RetainSyncedNodeOnlineClients([]*model.Node{{Id: 1, Enable: false, Status: "online"}}) }}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { db := initTrafficTestDB(t) useOnlineTestProcess(t) svc := &InboundService{} seedNodeRow(t, db, &model.Node{Id: 1, Name: "n1", Address: "127.0.0.1", Port: 2096, ApiToken: "tok", Enable: true, Guid: "node-guid"}) // No synced client is online, so only the total holds the node's entry. snap := &runtime.TrafficSnapshot{OnlineTree: map[string][]string{"node-guid": {"local-a@x", "local-b@x"}}} if _, err := svc.setRemoteTrafficLocked(1, snap, false, false); err != nil { t.Fatalf("sync: %v", err) } if got := svc.GetNodeOnlineTotals()[1]; got != 2 { t.Fatalf("total online before the drop = %d, want 2", got) } tt.drop(svc) if got := svc.GetNodeOnlineTotals(); len(got) != 0 { t.Errorf("total online after the drop = %v, want none", got) } }) } }