page_data_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package sub
  2. import (
  3. "reflect"
  4. "strings"
  5. "testing"
  6. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  7. )
  8. // A single getSubs entry can hold several links (one per host of an inbound)
  9. // joined by newlines. BuildPageData must split them into one entry per link, with
  10. // the email replicated, so the subpage renders one row per host instead of
  11. // collapsing them onto a single mangled line.
  12. func TestBuildPageData_SplitsMultiHostLinks(t *testing.T) {
  13. s := &SubService{}
  14. subs := []string{
  15. "vless://a@h1:443?type=tcp#DE-john@x\nvless://a@h2:443?type=tcp#DE-john@x\nvless://a@h3:443?type=tcp#DE-john@x",
  16. "vless://b@h:443?type=tcp#FR-alice@x",
  17. }
  18. emails := []string{"john@x", "alice@x"}
  19. page := s.BuildPageData("s1", "", xray.ClientTraffic{}, 0, subs, emails, "", "", "", "/", "", "")
  20. if len(page.Result) != 4 {
  21. t.Fatalf("Result len = %d, want 4 (3 host links + 1 single link)", len(page.Result))
  22. }
  23. for i, link := range page.Result {
  24. if strings.Contains(link, "\n") {
  25. t.Fatalf("Result[%d] still multi-line: %q", i, link)
  26. }
  27. }
  28. wantEmails := []string{"john@x", "john@x", "john@x", "alice@x"}
  29. if !reflect.DeepEqual(page.Emails, wantEmails) {
  30. t.Fatalf("Emails = %v, want %v", page.Emails, wantEmails)
  31. }
  32. }
  33. func TestSubIsOnline(t *testing.T) {
  34. tests := []struct {
  35. name string
  36. sub []string
  37. online []string
  38. want bool
  39. }{
  40. {name: "nobody online", sub: []string{"a@x"}, online: nil, want: false},
  41. {name: "no sub emails", sub: nil, online: []string{"a@x"}, want: false},
  42. {name: "sub client online", sub: []string{"a@x"}, online: []string{"z@x", "a@x"}, want: true},
  43. {name: "only other clients online", sub: []string{"a@x"}, online: []string{"z@x"}, want: false},
  44. {name: "any of several sub entries online", sub: []string{"a@x", "b@x"}, online: []string{"b@x"}, want: true},
  45. }
  46. for _, tt := range tests {
  47. t.Run(tt.name, func(t *testing.T) {
  48. if got := subIsOnline(tt.sub, tt.online); got != tt.want {
  49. t.Fatalf("subIsOnline(%v, %v) = %v, want %v", tt.sub, tt.online, got, tt.want)
  50. }
  51. })
  52. }
  53. }
  54. func TestBuildPageData_IsOnlineFalseWithoutLiveConnections(t *testing.T) {
  55. s := &SubService{}
  56. page := s.BuildPageData("s1", "", xray.ClientTraffic{}, 0, []string{"vless://a@h1:443?type=tcp#DE-john@x"}, []string{"john@x"}, "", "", "", "/", "", "")
  57. if page.IsOnline {
  58. t.Fatal("IsOnline must be false when the subscription's client has no live connection")
  59. }
  60. }