1
0

service_userinfo_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package sub
  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. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  9. )
  10. func TestAggregateTrafficByEmails_FallsBackToClientLimits(t *testing.T) {
  11. dbDir := t.TempDir()
  12. t.Setenv("XUI_DB_FOLDER", dbDir)
  13. dbtest.InitDB(t, filepath.Join(dbDir, "x-ui.db"))
  14. const email = "[email protected]"
  15. const totalBytes = int64(300) * 1024 * 1024 * 1024
  16. const expiry = int64(1893456000000)
  17. db := database.GetDB()
  18. if err := db.Create(&model.ClientRecord{
  19. Email: email,
  20. TotalGB: totalBytes,
  21. ExpiryTime: expiry,
  22. Enable: true,
  23. }).Error; err != nil {
  24. t.Fatalf("seed client record: %v", err)
  25. }
  26. if err := db.Create(&xray.ClientTraffic{
  27. Email: email,
  28. Up: 111,
  29. Down: 222,
  30. Total: 0,
  31. ExpiryTime: 0,
  32. Enable: true,
  33. }).Error; err != nil {
  34. t.Fatalf("seed client traffic: %v", err)
  35. }
  36. var s SubService
  37. agg, _ := s.AggregateTrafficByEmails([]string{email})
  38. if agg.Up != 111 || agg.Down != 222 {
  39. t.Errorf("usage = up %d/down %d, want 111/222", agg.Up, agg.Down)
  40. }
  41. if agg.Total != totalBytes {
  42. t.Errorf("total = %d, want %d (fallback to clients table)", agg.Total, totalBytes)
  43. }
  44. if agg.ExpiryTime != expiry {
  45. t.Errorf("expiry = %d, want %d (fallback to clients table)", agg.ExpiryTime, expiry)
  46. }
  47. }