Files
3x-ui/internal/web/service/tgbot/tgbot_add_client_expiry_test.go
T
BlindMaster24 2fcd28c1bc refactor(tgbot): make the add-client expiry presets say what they do (#6503)
* refactor(tgbot): make the add-client expiry presets say what they do

The wizard's "Add N days" buttons were a copy of the renewal handler, whose
accumulate branch they cleared two lines later: the branch tested a value the
line above had just set to zero, so it was dead and only the "set N days from
first use" path was reachable. That reads as an accident, and the automated
review of #6499 flagged it twice.

The wizard keeps the term it sets, which is now the code: a create flow has no
expiry to add to, the custom keypad lands in this same case, and a corrected
number has to replace the one it follows. 0 stays the Unlimited button. The
renewal handler (reset_exp_c) genuinely adds to the client's remaining time and
is unchanged.

* refactor(tgbot): fold in the review of the expiry-preset change

The test now starts every row from a term a preset could have left, so each row
fails on its own under the accumulate semantics rather than depending on the row
before it, and it reuses the package's draft helpers instead of a second copy.
The wizard presets drop the "Add" verb they never honoured; the renewal
keyboard keeps it, where reset_exp_c really does add to the remaining time.
2026-09-13 19:57:46 +02:00

62 lines
1.8 KiB
Go

package tgbot
import (
"strings"
"testing"
"github.com/mymmrac/telego"
"github.com/nicksnyder/go-i18n/v2/i18n"
)
// Pins the decision the dead accumulate branch hid: a preset tap chooses the term
// instead of adding to it, and 0 is Unlimited. The custom keypad shares this case.
func TestAddClientExpiryPresetReplacesTheTerm(t *testing.T) {
const chatID = int64(7505)
draftLocalizer(t,
&i18n.Message{ID: "tgbot.days", Other: "Days"},
&i18n.Message{ID: "tgbot.unlimited", Other: "Unlimited"},
)
url, textsFor := draftTexts(t)
swapTestBot(t, url)
// A fresh draft attaches no inbound, so the card never looks one up by remark.
draft := addClientDrafts.forChat(chatID)
origRunning := isRunning
t.Cleanup(func() {
addClientDrafts.reset(chatID)
isRunning = origRunning
})
isRunning = true
tb := &Tgbot{}
for _, tc := range []struct {
days string
want string
}{
{"30", "Expire: 30 Days"}, // not 37: a second tap replaces the first
{"90", "Expire: 90 Days"}, // not 97, which accumulating would show
{"0", "Expire: Unlimited"}, // the Unlimited button clears the term
} {
t.Run(tc.days, func(t *testing.T) {
// A term left by an earlier preset; every row has to fail on its own
// under the accumulate semantics this change rejected.
draft.expiryTime = -7 * 86400000
tb.answerCallback(&telego.CallbackQuery{
ID: "q1",
From: telego.User{ID: 1},
Data: "add_client_reset_exp_c " + tc.days,
Message: &telego.Message{MessageID: 7, Chat: telego.Chat{ID: chatID}},
}, true) // admin
sent := textsFor(chatID)
if len(sent) == 0 {
t.Fatalf("add_client_reset_exp_c %s rendered no card", tc.days)
}
if got := sent[len(sent)-1]; !strings.Contains(got, tc.want) {
t.Errorf("card after add_client_reset_exp_c %s = %q, want it to contain %q", tc.days, got, tc.want)
}
})
}
}