client_effective_flow_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/model"
  7. )
  8. // EffectiveFlowsByEmails resolves intended flow for many clients in one batched
  9. // query, taking the flow_override of the lowest inbound_id and skipping emails
  10. // with no non-empty flow anywhere.
  11. func TestEffectiveFlowsByEmails(t *testing.T) {
  12. dbDir := t.TempDir()
  13. t.Setenv("XUI_DB_FOLDER", dbDir)
  14. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  15. t.Fatalf("InitDB: %v", err)
  16. }
  17. t.Cleanup(func() { _ = database.CloseDB() })
  18. db := database.GetDB()
  19. const vision = "xtls-rprx-vision"
  20. // vis@x: attached to inbound 20 (empty flow) and 10 (Vision) — lowest
  21. // inbound_id (10) wins, so the empty override on 20 must not mask it.
  22. // plain@x: only an empty flow_override anywhere — absent from the result.
  23. mkClient := func(id int, email string) {
  24. if err := db.Create(&model.ClientRecord{Id: id, Email: email, Enable: true}).Error; err != nil {
  25. t.Fatalf("create client %s: %v", email, err)
  26. }
  27. }
  28. mkLink := func(clientID, inboundID int, flow string) {
  29. if err := db.Create(&model.ClientInbound{ClientId: clientID, InboundId: inboundID, FlowOverride: flow}).Error; err != nil {
  30. t.Fatalf("link %d/%d: %v", clientID, inboundID, err)
  31. }
  32. }
  33. mkClient(1, "vis@x")
  34. mkClient(2, "plain@x")
  35. mkLink(1, 20, "") // higher inbound_id, empty
  36. mkLink(1, 10, vision) // lower inbound_id, Vision
  37. mkLink(2, 30, "") // only empty override
  38. cs := &ClientService{}
  39. got, err := cs.EffectiveFlowsByEmails(nil, []string{"vis@x", "plain@x", "missing@x"})
  40. if err != nil {
  41. t.Fatalf("EffectiveFlowsByEmails: %v", err)
  42. }
  43. if got["vis@x"] != vision {
  44. t.Errorf("vis@x = %q, want %q (lowest inbound_id flow_override)", got["vis@x"], vision)
  45. }
  46. if v, ok := got["plain@x"]; ok {
  47. t.Errorf("plain@x present (%q); want absent (no non-empty flow anywhere)", v)
  48. }
  49. if v, ok := got["missing@x"]; ok {
  50. t.Errorf("missing@x present (%q); want absent (unknown client)", v)
  51. }
  52. // Empty input is a no-op (no query).
  53. if m, err := cs.EffectiveFlowsByEmails(nil, nil); err != nil || len(m) != 0 {
  54. t.Errorf("empty input: got %v err %v, want empty map", m, err)
  55. }
  56. }