inbound_sub_sort_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package service
  2. import (
  3. "testing"
  4. "github.com/mhsanaei/3x-ui/v3/internal/database"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. )
  7. func makeInboundWithSubSortIndex(tag string, port int, subSortIndex int) *model.Inbound {
  8. return &model.Inbound{
  9. UserId: 1,
  10. Tag: tag,
  11. Enable: true,
  12. Listen: "0.0.0.0",
  13. Port: port,
  14. Protocol: model.VLESS,
  15. StreamSettings: `{"network":"tcp"}`,
  16. Settings: `{"clients":[]}`,
  17. SubSortIndex: subSortIndex,
  18. }
  19. }
  20. // TestUpdateInbound_PersistsSubSortIndex verifies that UpdateInbound copies
  21. // SubSortIndex from the incoming update payload to the persisted row.
  22. func TestUpdateInbound_PersistsSubSortIndex(t *testing.T) {
  23. setupConflictDB(t)
  24. ib := makeInboundWithSubSortIndex("in-7001-tcp", 7001, 1)
  25. if err := database.GetDB().Create(ib).Error; err != nil {
  26. t.Fatalf("create inbound: %v", err)
  27. }
  28. update := *ib
  29. update.SubSortIndex = 7
  30. svc := &InboundService{}
  31. got, _, err := svc.UpdateInbound(&update)
  32. if err != nil {
  33. t.Fatalf("UpdateInbound: %v", err)
  34. }
  35. if got.SubSortIndex != 7 {
  36. t.Fatalf("returned SubSortIndex = %d, want 7", got.SubSortIndex)
  37. }
  38. var reloaded model.Inbound
  39. if err := database.GetDB().First(&reloaded, ib.Id).Error; err != nil {
  40. t.Fatalf("reload: %v", err)
  41. }
  42. if reloaded.SubSortIndex != 7 {
  43. t.Fatalf("persisted SubSortIndex = %d, want 7", reloaded.SubSortIndex)
  44. }
  45. }
  46. // TestUpdateInbound_SubSortIndexClampedToMinimum verifies that values below
  47. // the 1-based minimum (0 from clients that predate the field, or negatives)
  48. // are clamped to 1 instead of being stored.
  49. func TestUpdateInbound_SubSortIndexClampedToMinimum(t *testing.T) {
  50. setupConflictDB(t)
  51. ib := makeInboundWithSubSortIndex("in-7002-tcp", 7002, 5)
  52. if err := database.GetDB().Create(ib).Error; err != nil {
  53. t.Fatalf("create inbound: %v", err)
  54. }
  55. svc := &InboundService{}
  56. for _, below := range []int{0, -3} {
  57. update := *ib
  58. update.SubSortIndex = below
  59. got, _, err := svc.UpdateInbound(&update)
  60. if err != nil {
  61. t.Fatalf("UpdateInbound(%d): %v", below, err)
  62. }
  63. if got.SubSortIndex != 1 {
  64. t.Fatalf("returned SubSortIndex = %d for input %d, want 1", got.SubSortIndex, below)
  65. }
  66. var reloaded model.Inbound
  67. if err := database.GetDB().First(&reloaded, ib.Id).Error; err != nil {
  68. t.Fatalf("reload: %v", err)
  69. }
  70. if reloaded.SubSortIndex != 1 {
  71. t.Fatalf("persisted SubSortIndex = %d for input %d, want 1", reloaded.SubSortIndex, below)
  72. }
  73. }
  74. }
  75. // TestAddInbound_SubSortIndexClampedToMinimum verifies the same clamping on
  76. // the create path (an omitted form field binds to 0).
  77. func TestAddInbound_SubSortIndexClampedToMinimum(t *testing.T) {
  78. setupConflictDB(t)
  79. svc := &InboundService{}
  80. ib := makeInboundWithSubSortIndex("in-7003-tcp", 7003, 0)
  81. got, _, err := svc.AddInbound(ib)
  82. if err != nil {
  83. t.Fatalf("AddInbound: %v", err)
  84. }
  85. if got.SubSortIndex != 1 {
  86. t.Fatalf("returned SubSortIndex = %d, want 1", got.SubSortIndex)
  87. }
  88. var reloaded model.Inbound
  89. if err := database.GetDB().First(&reloaded, got.Id).Error; err != nil {
  90. t.Fatalf("reload: %v", err)
  91. }
  92. if reloaded.SubSortIndex != 1 {
  93. t.Fatalf("persisted SubSortIndex = %d, want 1", reloaded.SubSortIndex)
  94. }
  95. }