panel_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package panel
  2. import (
  3. "testing"
  4. "github.com/mhsanaei/3x-ui/v3/internal/web/service"
  5. )
  6. func TestIsNewerVersion(t *testing.T) {
  7. cases := []struct {
  8. latest string
  9. current string
  10. want bool
  11. }{
  12. {"v2.9.4", "2.9.3", true},
  13. {"v2.10.0", "2.9.9", true},
  14. {"v2.9.3", "2.9.3", false},
  15. {"v2.9.2", "2.9.3", false},
  16. {"v3.0.0", "2.9.3", true},
  17. }
  18. for _, tc := range cases {
  19. if got := isNewerVersion(tc.latest, tc.current); got != tc.want {
  20. t.Fatalf("isNewerVersion(%q, %q) = %v, want %v", tc.latest, tc.current, got, tc.want)
  21. }
  22. }
  23. }
  24. func TestCompareVersionStringsRejectsUnexpectedFormats(t *testing.T) {
  25. if _, ok := compareVersionStrings("latest", "2.9.3"); ok {
  26. t.Fatal("expected non-semver latest tag to be rejected")
  27. }
  28. if _, ok := compareVersionStrings("v2.9", "2.9.3"); ok {
  29. t.Fatal("expected short version to be rejected")
  30. }
  31. }
  32. func TestShellQuote(t *testing.T) {
  33. if got := shellQuote("/usr/bin/curl"); got != "'/usr/bin/curl'" {
  34. t.Fatalf("unexpected quote result: %s", got)
  35. }
  36. if got := shellQuote("/tmp/a'b"); got != "'/tmp/a'\\''b'" {
  37. t.Fatalf("unexpected quote result with single quote: %s", got)
  38. }
  39. }
  40. func TestExtractReleaseCommit(t *testing.T) {
  41. full := "1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b"
  42. cases := []struct {
  43. name string
  44. release service.Release
  45. want string
  46. }{
  47. {
  48. name: "from body marker",
  49. release: service.Release{Body: "Rolling build\n\ncommit=" + full + "\nbuilt=2026-06-24T00:00:00Z"},
  50. want: full,
  51. },
  52. {
  53. name: "body marker is case-insensitive and wins over target",
  54. release: service.Release{Body: "COMMIT=" + full, TargetCommitish: "deadbeef"},
  55. want: full,
  56. },
  57. {
  58. name: "fallback to target commit sha",
  59. release: service.Release{Body: "no marker here", TargetCommitish: full},
  60. want: full,
  61. },
  62. {
  63. name: "branch target is not a commit",
  64. release: service.Release{Body: "no marker", TargetCommitish: "main"},
  65. want: "",
  66. },
  67. }
  68. for _, tc := range cases {
  69. if got := extractReleaseCommit(&tc.release); got != tc.want {
  70. t.Fatalf("%s: extractReleaseCommit = %q, want %q", tc.name, got, tc.want)
  71. }
  72. }
  73. }
  74. func TestCommitsEqual(t *testing.T) {
  75. full := "1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b"
  76. cases := []struct {
  77. a, b string
  78. want bool
  79. }{
  80. {"1a2b3c4d", full, true}, // injected 8-char prefix matches full release sha
  81. {full, "1a2b3c4d", true}, // order independent
  82. {"1A2B3C4D", full, true}, // case insensitive
  83. {"deadbeef", full, false}, // different commit
  84. {"", full, false}, // empty current never matches
  85. {"1a2b3c4d", "", false}, // empty latest never matches
  86. }
  87. for _, tc := range cases {
  88. if got := commitsEqual(tc.a, tc.b); got != tc.want {
  89. t.Fatalf("commitsEqual(%q, %q) = %v, want %v", tc.a, tc.b, got, tc.want)
  90. }
  91. }
  92. }
  93. func TestShortCommit(t *testing.T) {
  94. if got := shortCommit("1a2b3c4d5e6f7a8b"); got != "1a2b3c4d" {
  95. t.Fatalf("shortCommit truncation = %q, want %q", got, "1a2b3c4d")
  96. }
  97. if got := shortCommit("abc"); got != "abc" {
  98. t.Fatalf("shortCommit short input = %q, want %q", got, "abc")
  99. }
  100. }