Files
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

103 lines
3.6 KiB
Go

package controller
import (
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"strconv"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
)
func setupSubBalancerRouter(t *testing.T) *gin.Engine {
t.Helper()
t.Setenv("XUI_DB_FOLDER", t.TempDir())
dbtest.InitDB(t, filepath.Join(t.TempDir(), "x-ui.db"))
gin.SetMode(gin.TestMode)
router := gin.New()
NewSubBalancerController(router.Group("/panel/api"))
return router
}
func subBalancerPost(t *testing.T, router *gin.Engine, path, body string) *httptest.ResponseRecorder {
t.Helper()
req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
return resp
}
func responseObj(t *testing.T, body string) map[string]any {
t.Helper()
var m map[string]any
if err := json.Unmarshal([]byte(body), &m); err != nil {
t.Fatalf("unmarshal response %q: %v", body, err)
}
return m
}
// enabled absent on create defaults to true; "false" disables; a non-boolean
// value is rejected so a malformed toggle can't silently flip the row.
func TestSubBalancerController_EnabledParsing(t *testing.T) {
router := setupSubBalancerRouter(t)
base := "remark=auto&strategy=random&sortOrder=1&inboundIds=1"
resp := subBalancerPost(t, router, "/panel/api/sub-balancers", base)
if !strings.Contains(resp.Body.String(), `"success":true`) {
t.Fatalf("create no enabled: %s", resp.Body.String())
}
bal := responseObj(t, resp.Body.String())["obj"].(map[string]any)
if bal["enabled"] != true {
t.Fatalf("absent enabled = %v, want true", bal["enabled"])
}
resp = subBalancerPost(t, router, "/panel/api/sub-balancers", base+"&enabled=false")
bal = responseObj(t, resp.Body.String())["obj"].(map[string]any)
if bal["enabled"] != false {
t.Fatalf("enabled=false -> %v, want false", bal["enabled"])
}
resp = subBalancerPost(t, router, "/panel/api/sub-balancers", base+"&enabled=bogus")
if !strings.Contains(resp.Body.String(), `"success":false`) {
t.Fatalf("enabled=bogus should be rejected: %s", resp.Body.String())
}
}
// An update omitting enabled preserves the stored value instead of resetting it
// to the create default — a partial PATCH must not clobber the toggle.
func TestSubBalancerController_UpdatePreservesEnabledWhenAbsent(t *testing.T) {
router := setupSubBalancerRouter(t)
base := "remark=auto&strategy=random&sortOrder=1&inboundIds=1"
resp := subBalancerPost(t, router, "/panel/api/sub-balancers", base+"&enabled=false")
bal := responseObj(t, resp.Body.String())["obj"].(map[string]any)
id := strconv.Itoa(int(bal["id"].(float64)))
if bal["enabled"] != false {
t.Fatalf("setup: enabled = %v, want false", bal["enabled"])
}
resp = subBalancerPost(t, router, "/panel/api/sub-balancers/"+id, "remark=renamed&strategy=random&sortOrder=1&inboundIds=1")
if !strings.Contains(resp.Body.String(), `"success":true`) {
t.Fatalf("update: %s", resp.Body.String())
}
bal = responseObj(t, resp.Body.String())["obj"].(map[string]any)
if bal["enabled"] != false {
t.Fatalf("update without enabled = %v, want preserved false", bal["enabled"])
}
if bal["remark"] != "renamed" {
t.Fatalf("remark = %v, want renamed", bal["remark"])
}
resp = subBalancerPost(t, router, "/panel/api/sub-balancers/"+id, "remark=renamed&strategy=random&sortOrder=1&inboundIds=1&enabled=true")
bal = responseObj(t, resp.Body.String())["obj"].(map[string]any)
if bal["enabled"] != true {
t.Fatalf("enabled=true -> %v, want true", bal["enabled"])
}
}