inbound_sub_sort_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_SubSortIndexZeroMapsToDefault verifies that 0 from clients
  47. // that predate the field is normalized to 1 instead of being stored.
  48. func TestUpdateInbound_SubSortIndexZeroMapsToDefault(t *testing.T) {
  49. setupConflictDB(t)
  50. ib := makeInboundWithSubSortIndex("in-7002-tcp", 7002, 5)
  51. if err := database.GetDB().Create(ib).Error; err != nil {
  52. t.Fatalf("create inbound: %v", err)
  53. }
  54. svc := &InboundService{}
  55. update := *ib
  56. update.SubSortIndex = 0
  57. got, _, err := svc.UpdateInbound(&update)
  58. if err != nil {
  59. t.Fatalf("UpdateInbound: %v", err)
  60. }
  61. if got.SubSortIndex != 1 {
  62. t.Fatalf("returned SubSortIndex = %d, want 1", got.SubSortIndex)
  63. }
  64. var reloaded model.Inbound
  65. if err := database.GetDB().First(&reloaded, ib.Id).Error; err != nil {
  66. t.Fatalf("reload: %v", err)
  67. }
  68. if reloaded.SubSortIndex != 1 {
  69. t.Fatalf("persisted SubSortIndex = %d, want 1", reloaded.SubSortIndex)
  70. }
  71. }
  72. // TestUpdateInbound_PreservesNegativeSubSortIndex verifies that an explicitly
  73. // set negative index is stored (so it can sort ahead of the default of 1).
  74. func TestUpdateInbound_PreservesNegativeSubSortIndex(t *testing.T) {
  75. setupConflictDB(t)
  76. ib := makeInboundWithSubSortIndex("in-7004-tcp", 7004, 5)
  77. if err := database.GetDB().Create(ib).Error; err != nil {
  78. t.Fatalf("create inbound: %v", err)
  79. }
  80. svc := &InboundService{}
  81. update := *ib
  82. update.SubSortIndex = -1
  83. got, _, err := svc.UpdateInbound(&update)
  84. if err != nil {
  85. t.Fatalf("UpdateInbound: %v", err)
  86. }
  87. if got.SubSortIndex != -1 {
  88. t.Fatalf("returned SubSortIndex = %d, want -1", got.SubSortIndex)
  89. }
  90. var reloaded model.Inbound
  91. if err := database.GetDB().First(&reloaded, ib.Id).Error; err != nil {
  92. t.Fatalf("reload: %v", err)
  93. }
  94. if reloaded.SubSortIndex != -1 {
  95. t.Fatalf("persisted SubSortIndex = %d, want -1", reloaded.SubSortIndex)
  96. }
  97. }
  98. // TestAddInbound_SubSortIndexClampedToMinimum verifies the same clamping on
  99. // the create path (an omitted form field binds to 0).
  100. func TestAddInbound_SubSortIndexClampedToMinimum(t *testing.T) {
  101. setupConflictDB(t)
  102. svc := &InboundService{}
  103. ib := makeInboundWithSubSortIndex("in-7003-tcp", 7003, 0)
  104. got, _, err := svc.AddInbound(ib)
  105. if err != nil {
  106. t.Fatalf("AddInbound: %v", err)
  107. }
  108. if got.SubSortIndex != 1 {
  109. t.Fatalf("returned SubSortIndex = %d, want 1", got.SubSortIndex)
  110. }
  111. var reloaded model.Inbound
  112. if err := database.GetDB().First(&reloaded, got.Id).Error; err != nil {
  113. t.Fatalf("reload: %v", err)
  114. }
  115. if reloaded.SubSortIndex != 1 {
  116. t.Fatalf("persisted SubSortIndex = %d, want 1", reloaded.SubSortIndex)
  117. }
  118. }