inbound_subsort_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package service
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. )
  9. func TestSetInboundSubSortIndexLeavesSettingsUntouched(t *testing.T) {
  10. dbtest.InitDB(t, filepath.Join(t.TempDir(), "x-ui.db"))
  11. const settings = `{"clients":[{"email":"[email protected]","id":"11111111-1111-1111-1111-111111111111"}]}`
  12. ib := &model.Inbound{UserId: 1, Remark: "r", Port: 21001, Protocol: model.VLESS, Settings: settings, SubSortIndex: 1, Enable: true}
  13. if err := database.GetDB().Create(ib).Error; err != nil {
  14. t.Fatalf("seed: %v", err)
  15. }
  16. svc := InboundService{}
  17. if err := svc.SetInboundSubSortIndex(ib.Id, 7); err != nil {
  18. t.Fatalf("set: %v", err)
  19. }
  20. var got model.Inbound
  21. if err := database.GetDB().First(&got, ib.Id).Error; err != nil {
  22. t.Fatalf("reload: %v", err)
  23. }
  24. if got.SubSortIndex != 7 {
  25. t.Fatalf("subSortIndex = %d, want 7", got.SubSortIndex)
  26. }
  27. if got.Settings != settings {
  28. t.Fatalf("settings were rewritten:\n got %s\nwant %s", got.Settings, settings)
  29. }
  30. }
  31. func TestSetInboundSubSortIndexUsesNarrowNodeUpdate(t *testing.T) {
  32. setupBulkDB(t)
  33. nodeID, fake := setupNodeRuntime(t)
  34. ib := nodeInbound(t, nodeID, 21002, []model.Client{{Email: "[email protected]", ID: "11111111-1111-1111-1111-111111111111"}})
  35. ib.SubSortIndex = 1
  36. if err := database.GetDB().Model(ib).Update("sub_sort_index", 1).Error; err != nil {
  37. t.Fatal(err)
  38. }
  39. if err := (&InboundService{}).SetInboundSubSortIndex(ib.Id, 7); err != nil {
  40. t.Fatalf("set: %v", err)
  41. }
  42. if got := fake.updateSubSort.Load(); got != 1 {
  43. t.Fatalf("narrow node updates = %d, want 1", got)
  44. }
  45. if got := fake.updateInbound.Load(); got != 0 {
  46. t.Fatalf("full snapshot node updates = %d, want 0", got)
  47. }
  48. }
  49. func TestSetInboundSubSortIndexPreservesNegative(t *testing.T) {
  50. dbtest.InitDB(t, filepath.Join(t.TempDir(), "x-ui.db"))
  51. ib := &model.Inbound{UserId: 1, Remark: "r", Port: 21003, Protocol: model.VLESS, Settings: `{"clients":[]}`, SubSortIndex: 1, Enable: true}
  52. if err := database.GetDB().Create(ib).Error; err != nil {
  53. t.Fatalf("seed: %v", err)
  54. }
  55. if err := (&InboundService{}).SetInboundSubSortIndex(ib.Id, -2); err != nil {
  56. t.Fatalf("set: %v", err)
  57. }
  58. var got model.Inbound
  59. if err := database.GetDB().First(&got, ib.Id).Error; err != nil {
  60. t.Fatalf("reload: %v", err)
  61. }
  62. if got.SubSortIndex != -2 {
  63. t.Fatalf("subSortIndex = %d, want -2", got.SubSortIndex)
  64. }
  65. }