subService_userinfo_test.go 1.4 KB

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