Files
mirror-3x-ui/internal/web/runtime/remote_reset_test.go
T
Hamed 0afa11ddff fix(nodes): share lightweight remote inbound catalog refreshes (#7313)
Replace full inbound/client-list downloads on cold remote ID lookups with an owner-scoped six-field catalog projected directly from the database. Coalesce concurrent catalog refreshes per remote while preserving independent caller cancellation and rejecting stale ID mappings after topology mutations.

Seed reconciliation from the catalog, create missing inbounds directly, preserve prefixed/adopted aliases and invalidate pushed state when remote IDs change. Avoid invalidating catalog generations on unchanged-ID settings pushes. Retain client-membership-aware reconciliation state and legacy full-list fallback only on HTTP 404/405.

Cover permissions, query projection, concurrent refresh/cancellation, deletion/recreation, reconciliation request counts and mixed-version node compatibility. Integrated CI and Docs CI passed, including race, node E2E and PostgreSQL catalog projection. Synthetic response-size comparison is not a production latency benchmark.

Fixes #7303.
2026-12-03 03:26:44 +03:30

84 lines
3.0 KiB
Go

package runtime
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"slices"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
)
// The master replays a node's reset backlog through the node's bulk endpoint.
func TestRemoteResetClientTrafficsPostsEmailsToBulkEndpoint(t *testing.T) {
var path string
var body struct {
Emails []string `json:"emails"`
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path = r.URL.Path
_ = json.NewDecoder(r.Body).Decode(&body)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"success":true}`))
}))
t.Cleanup(srv.Close)
r := NewRemote(nodeForPlainServer(t, srv, "verify", "tok"), nil)
if err := r.ResetClientTraffics(context.Background(), []string{"a@x", "b@x"}); err != nil {
t.Fatalf("ResetClientTraffics: %v", err)
}
if path != "/panel/api/clients/bulkResetTraffic" || !slices.Equal(body.Emails, []string{"a@x", "b@x"}) {
t.Fatalf("node got %s %v, want /panel/api/clients/bulkResetTraffic [a@x b@x]", path, body.Emails)
}
}
// A central inbound id need not match the node's id, so the reset must target
// the node-side id resolved from the tag, never ib.Id.
func TestRemoteResetInboundTrafficUsesNodeInboundID(t *testing.T) {
var method, path string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
method, path = req.Method, req.URL.Path
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"success":true,"msg":"ok"}`))
}))
t.Cleanup(srv.Close)
r := NewRemote(nodeForPlainServer(t, srv, "verify", "tok"), nil)
r.cacheSet("n1-in-443", 7)
ib := &model.Inbound{Id: 42, Tag: "n1-in-443"}
if err := r.ResetInboundTraffic(context.Background(), ib); err != nil {
t.Fatalf("ResetInboundTraffic: %v", err)
}
if method != http.MethodPost || path != "/panel/api/inbounds/7/resetTraffic" {
t.Fatalf("node got %s %s, want POST /panel/api/inbounds/7/resetTraffic", method, path)
}
}
// An unresolvable tag must fail before posting, so a reset never lands on an
// unrelated node inbound that happens to share the central id.
func TestRemoteResetInboundTrafficUnknownTagErrors(t *testing.T) {
var resetPosted bool
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
if req.URL.Path == "/panel/api/inbounds/catalog" {
_, _ = w.Write([]byte(`{"success":true,"msg":"ok","obj":[]}`))
return
}
resetPosted = true
_, _ = w.Write([]byte(`{"success":true,"msg":"ok"}`))
}))
t.Cleanup(srv.Close)
r := NewRemote(nodeForPlainServer(t, srv, "verify", "tok"), nil)
ib := &model.Inbound{Id: 42, Tag: "n1-in-443"}
if err := r.ResetInboundTraffic(context.Background(), ib); !errors.Is(err, errRemoteInboundNotFound) {
t.Fatalf("ResetInboundTraffic error = %v, want unknown-tag error", err)
}
if resetPosted {
t.Fatal("reset request posted to node despite an unresolved tag")
}
}