3x-ui/util/common/multi_error_test.go
MHSanaei 106adca414
test: cover crypto, random, netsafe, sub helpers, xray equals, websocket hub, node service
Adds ~110 unit tests across previously untested packages. Focus on
pure-logic and concurrency surfaces where regressions would silently
affect users:

- util/crypto, util/random: password hashing round-trip, ss2022 key
  generation, alphabet/length invariants.
- util/netsafe: IsBlockedIP edge cases, NormalizeHost validation,
  SSRF guard with AllowPrivate context bypass.
- util/common, util/json_util: traffic formatter, Combine nil-skip,
  RawMessage empty-as-null and copy-on-unmarshal.
- sub: splitLinkLines, searchKey/searchHost, kcp share fields,
  finalmask normalization, buildVmessLink round-trip.
- xray: Config.Equals and InboundConfig.Equals field-by-field,
  getRequiredUserString/getOptionalUserString type checks.
- web/websocket: hub registration, throttling, slow-client eviction,
  nil-receiver safety, concurrent register/unregister.
- web/service: NodeService.normalize validation, normalizeBasePath,
  HeartbeatPatch.ToUI mapping.
- web/job: atomicBool concurrent set/takeAndReset semantics.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-18 10:00:09 +02:00

44 lines
1.1 KiB
Go

package common
import (
"errors"
"strings"
"testing"
)
func TestCombine_AllNilReturnsNil(t *testing.T) {
if err := Combine(); err != nil {
t.Fatalf("Combine() with no args = %v, want nil", err)
}
if err := Combine(nil, nil, nil); err != nil {
t.Fatalf("Combine(nil, nil, nil) = %v, want nil", err)
}
}
func TestCombine_SkipsNilErrors(t *testing.T) {
e1 := errors.New("boom one")
e2 := errors.New("boom two")
err := Combine(nil, e1, nil, e2, nil)
if err == nil {
t.Fatal("expected non-nil combined error")
}
msg := err.Error()
if !strings.Contains(msg, "boom one") || !strings.Contains(msg, "boom two") {
t.Fatalf("combined error %q does not contain both underlying messages", msg)
}
if !strings.HasPrefix(msg, "multierr: ") {
t.Fatalf("combined error %q missing %q prefix", msg, "multierr: ")
}
}
func TestCombine_SingleErrorStillWrapped(t *testing.T) {
e := errors.New("only one")
err := Combine(e)
if err == nil {
t.Fatal("expected non-nil error")
}
if !strings.Contains(err.Error(), "only one") {
t.Fatalf("combined error %q missing underlying message", err.Error())
}
}