mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-10-20 12:55:00 +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.
56 lines
1.9 KiB
Go
56 lines
1.9 KiB
Go
package tgbot
|
|
|
|
import (
|
|
"encoding/json"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/web/locale"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/xray"
|
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
|
"golang.org/x/text/language"
|
|
)
|
|
|
|
// clientInfoLocalizer renders the lines clientInfoMsg prints with the templates
|
|
// the translation files carry; without it I18n returns the bare keys.
|
|
func clientInfoLocalizer(t *testing.T) {
|
|
t.Helper()
|
|
bundle := i18n.NewBundle(language.MustParse("en-US"))
|
|
bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
|
|
_ = bundle.AddMessages(language.MustParse("en-US"),
|
|
&i18n.Message{ID: "tgbot.messages.email", Other: "Email: {{ .Email }}\r\n"},
|
|
&i18n.Message{ID: "tgbot.days", Other: "Days"},
|
|
&i18n.Message{ID: "tgbot.messages.expireIn", Other: "Expire In: {{ .Time }}\r\n"},
|
|
&i18n.Message{ID: "tgbot.messages.expire", Other: "Expire Date: {{ .Time }}\r\n"},
|
|
&i18n.Message{ID: "tgbot.wentWrong", Other: "went wrong"},
|
|
)
|
|
orig := locale.LocalizerBot
|
|
t.Cleanup(func() { locale.LocalizerBot = orig })
|
|
locale.LocalizerBot = i18n.NewLocalizer(bundle, "en-US")
|
|
}
|
|
|
|
// Regression test: a start-after-first-use client is stored as a negative duration,
|
|
// and a disabled one rendered it as a 1969 date.
|
|
func TestClientInfoShowsStartAfterFirstUseWhenDisabled(t *testing.T) {
|
|
dbtest.InitDB(t, filepath.Join(t.TempDir(), "x-ui.db"))
|
|
clientInfoLocalizer(t)
|
|
|
|
traffic := &xray.ClientTraffic{
|
|
Email: "trial@example.com",
|
|
Enable: false,
|
|
ExpiryTime: -30 * 24 * 60 * 60000,
|
|
}
|
|
|
|
out := (&Tgbot{}).clientInfoMsg(traffic, false, false, false, true, false, false)
|
|
|
|
if strings.Contains(out, "1969") {
|
|
t.Errorf("client info = %q, want the days left, not a 1969 date", out)
|
|
}
|
|
if !strings.Contains(out, "Expire In: 30 Days") {
|
|
t.Errorf("client info = %q, want it to contain %q", out, "Expire In: 30 Days")
|
|
}
|
|
}
|