Files
3x-ui/internal/sub/service_dedup_test.go
T
MHSanaei 12d51d7195 perf(tests): copy a migrated template DB instead of migrating per test
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.
2026-09-27 03:04:50 +02:00

109 lines
3.9 KiB
Go

package sub
import (
"fmt"
"path/filepath"
"strings"
"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"
)
// TestGetSubs_DuplicateSettingsClients_Deduped reproduces #5134: multi-node
// sync/import drift can leave the same client twice inside an inbound's
// legacy settings.clients JSON while the normalized client_inbounds table
// stays clean. The subscription output must still contain one profile per
// (inbound, client).
func TestGetSubs_DuplicateSettingsClients_Deduped(t *testing.T) {
dbDir := t.TempDir()
t.Setenv("XUI_DB_FOLDER", dbDir)
dbtest.InitDB(t, filepath.Join(dbDir, "x-ui.db"))
const subId = "sub-dup"
const email = "dup@example.com"
const uuid = "f1b9265f-26a8-4b75-9be2-c64a94b15de1"
db := database.GetDB()
settings := fmt.Sprintf(`{"clients": [
{"id": %q, "email": %q, "subId": %q, "enable": true},
{"id": %q, "email": %q, "subId": %q, "enable": true}
]}`, uuid, email, subId, uuid, email, subId)
ib := &model.Inbound{
UserId: 1,
Tag: "dup-in",
Enable: true,
Port: 42001,
Protocol: model.VLESS,
Settings: settings,
StreamSettings: `{"network": "tcp", "security": "none"}`,
}
if err := db.Create(ib).Error; err != nil {
t.Fatalf("seed inbound: %v", err)
}
client := &model.ClientRecord{Email: email, SubID: subId, UUID: uuid, Enable: true}
if err := db.Create(client).Error; err != nil {
t.Fatalf("seed client: %v", err)
}
if err := db.Create(&model.ClientInbound{ClientId: client.Id, InboundId: ib.Id}).Error; err != nil {
t.Fatalf("seed client_inbound: %v", err)
}
s := NewSubService("")
links, emails, _, _, err := s.GetSubs(subId, "sub.example.com")
if err != nil {
t.Fatalf("GetSubs: %v", err)
}
if len(links) != 1 {
t.Fatalf("links = %d, want 1 (duplicate settings.clients entries must collapse)", len(links))
}
if len(emails) != 1 {
t.Fatalf("emails = %d, want 1, got %v", len(emails), emails)
}
// Identity, not just count: the single surviving link must be for this client.
if !strings.Contains(links[0], uuid) {
t.Fatalf("surviving link must carry the client uuid %q, got %q", uuid, links[0])
}
}
// TestMatchingClients_DedupsCaseInsensitiveEmail pins the dedup KEY, not just the count.
// clients.email is unique but case-sensitively so: two rows differing only by email case
// can coexist (import drift), and dropping strings.ToLower (or keying on another field)
// would emit both. The first row by id must win, matching the old settings-JSON order.
func TestMatchingClients_DedupsCaseInsensitiveEmail(t *testing.T) {
dbDir := t.TempDir()
t.Setenv("XUI_DB_FOLDER", dbDir)
dbtest.InitDB(t, filepath.Join(dbDir, "x-ui.db"))
const subId = "s1"
const uuid = "11111111-2222-4333-8444-555555555555"
db := database.GetDB()
ib := &model.Inbound{Protocol: model.VLESS, Enable: true, Port: 42002, Tag: "dedup-ci", Settings: `{"clients":[]}`}
if err := db.Create(ib).Error; err != nil {
t.Fatalf("seed inbound: %v", err)
}
for _, email := range []string{"Dup@Example.com", "dup@example.com"} {
c := &model.ClientRecord{Email: email, SubID: subId, UUID: uuid, Enable: true}
if err := db.Create(c).Error; err != nil {
t.Fatalf("seed client %q: %v", email, err)
}
if err := db.Create(&model.ClientInbound{ClientId: c.Id, InboundId: ib.Id}).Error; err != nil {
t.Fatalf("seed client_inbound %q: %v", email, err)
}
}
s := &SubService{}
got := s.matchingClients(ib, subId)
if len(got) != 1 {
t.Fatalf("case-differing duplicate emails must dedup to 1 client, got %d", len(got))
}
if got[0].Email != "Dup@Example.com" {
t.Fatalf("first occurrence must be kept, got %q", got[0].Email)
}
// A wrong subId must still be excluded (guards the SQL subId filter).
if other := s.matchingClients(ib, "nope"); len(other) != 0 {
t.Fatalf("non-matching subId must yield 0 clients, got %d", len(other))
}
}