package nodee2e import ( "encoding/json" "fmt" "net/http" "testing" "time" "github.com/google/uuid" ) func TestScopedDetachReconcilesOfflineNode(t *testing.T) { bin := panelBinary(t) master, node := newPanel(t, bin, "detach-master"), newPanel(t, bin, "detach-node") master.start() node.start() port := freePort(t) node.call(http.MethodPost, "/panel/api/inbounds/add", vlessInbound("detach-source", port, nil, vlessClient("detach-me"), vlessClient("keep-me"))) enrollTrafficNode(t, master, node) var source inboundView eventually(t, settleTimeout, "master adopts both clients", func() (bool, string) { var ok bool source, ok = master.inboundOnPort(port) return ok && len(source.emails()) == 2, fmt.Sprint(source.emails()) }) var page struct { Items []struct { ID int `json:"id"` Email string `json:"email"` } } if err := json.Unmarshal(master.call(http.MethodGet, fmt.Sprintf("/panel/api/clients/detachCandidates?inboundId=%d", source.Id), nil), &page); err != nil { t.Fatal(err) } var keep int for _, item := range page.Items { if item.Email == "keep-me" { keep = item.ID } } if keep == 0 || len(page.Items) != 2 { t.Fatalf("source candidates: %+v", page) } var frozen struct{ ScopeToken string } if err := json.Unmarshal(master.call(http.MethodPost, "/panel/api/clients/detachCandidates/freeze", map[string]any{"inboundId": source.Id}), &frozen); err != nil { t.Fatal(err) } node.stop() id := uuid.NewString() master.call(http.MethodPost, "/panel/api/clients/bulkDetach", map[string]any{ "inboundIds": []int{source.Id}, "requestId": id, "selection": map[string]any{"mode": "allMatching", "scopeToken": frozen.ScopeToken, "excludedClientIds": []int{keep}}, }) eventually(t, 10*time.Second, "scoped detachment commits and queues reconciliation", func() (bool, string) { var status struct { Running bool Error string Result struct { Detached, Skipped, Errors int NodePending bool } } raw := master.call(http.MethodGet, "/panel/api/clients/bulkDetach/"+id, nil) if err := json.Unmarshal(raw, &status); err != nil { t.Fatal(err) } return !status.Running && status.Error == "" && status.Result.Detached == 1 && status.Result.Skipped == 0 && status.Result.Errors == 0 && status.Result.NodePending, string(raw) }) node.start() eventually(t, 90*time.Second, "node applies detach and keeps the excluded client", func() (bool, string) { ib, ok := node.inboundOnPort(port) return ok && len(ib.emails()) == 1 && ib.client("keep-me") != nil, fmt.Sprint(ib.emails()) }) }