mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-10-17 11:25:20 +03:00
12d51d7195
Most tests opened a throwaway panel DB with database.InitDB, which runs the full AutoMigrate + seed on an empty file every time: ~230ms, and ~850ms under -race because GORM's reflection-heavy migration is what the detector slows most. internal/web/service does this in ~550 of its 830 tests, so the CI race job spent ~10 of its ~14.6 minutes re-migrating empty databases. internal/database/dbtest.InitDB migrates once per test process, then hands each test its own copy of that file (~130ms under -race) and registers the CloseDB cleanup. The copy then goes through InitDB like a panel restart, so every test still starts from the state a fresh install has. Tests that reopen an existing file, migrate a hand-built legacy DB or target Postgres keep calling database.InitDB. Locally under -race: internal/web/service 626s (last CI run) -> 114s, internal/sub 246s -> 35s.
66 lines
2.2 KiB
Go
66 lines
2.2 KiB
Go
package service
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/xray"
|
|
)
|
|
|
|
// TestGetClientTrafficTgBot_SettingsSerializationStyles guards against the
|
|
// prefilter regressing into a formatting-sensitive string match (#5805): the
|
|
// lookup must find clients whether inbounds.settings stores compact JSON
|
|
// ("tgId":N, as written by node sync/import) or indented JSON ("tgId": N, as
|
|
// written by the panel's MarshalIndent).
|
|
func TestGetClientTrafficTgBot_SettingsSerializationStyles(t *testing.T) {
|
|
dbDir := t.TempDir()
|
|
t.Setenv("XUI_DB_FOLDER", dbDir)
|
|
dbtest.InitDB(t, filepath.Join(dbDir, "x-ui.db"))
|
|
|
|
db := database.GetDB()
|
|
|
|
const tgId int64 = 123456789
|
|
cases := []struct {
|
|
name string
|
|
settings string
|
|
email string
|
|
port int
|
|
}{
|
|
{"compact", `{"clients":[{"id":"u1","email":"compact-user","tgId":123456789}]}`, "compact-user", 41001},
|
|
{"spaced", `{"clients": [{"id": "u2", "email": "spaced-user", "tgId": 123456789}]}`, "spaced-user", 41002},
|
|
}
|
|
for _, c := range cases {
|
|
inbound := &model.Inbound{UserId: 1, Tag: "tg-" + c.name, Enable: true, Port: c.port, Protocol: model.VLESS, Settings: c.settings}
|
|
if err := db.Create(inbound).Error; err != nil {
|
|
t.Fatalf("create %s inbound: %v", c.name, err)
|
|
}
|
|
if err := db.Create(&xray.ClientTraffic{InboundId: inbound.Id, Email: c.email, Enable: true, Up: 10, Down: 20}).Error; err != nil {
|
|
t.Fatalf("create %s client_traffics: %v", c.name, err)
|
|
}
|
|
}
|
|
|
|
svc := InboundService{}
|
|
traffics, err := svc.GetClientTrafficTgBot(tgId)
|
|
if err != nil {
|
|
t.Fatalf("GetClientTrafficTgBot: %v", err)
|
|
}
|
|
got := make(map[string]bool, len(traffics))
|
|
for _, tr := range traffics {
|
|
got[tr.Email] = true
|
|
}
|
|
if len(traffics) != 2 || !got["compact-user"] || !got["spaced-user"] {
|
|
t.Fatalf("expected traffic for compact-user and spaced-user, got %v", got)
|
|
}
|
|
|
|
other, err := svc.GetClientTrafficTgBot(42)
|
|
if err != nil {
|
|
t.Fatalf("GetClientTrafficTgBot(42): %v", err)
|
|
}
|
|
if len(other) != 0 {
|
|
t.Fatalf("expected no traffic for unknown tgId, got %d rows", len(other))
|
|
}
|
|
}
|