[PR #6503] [MERGED] refactor(tgbot): make the add-client expiry presets say what they do #6314

Closed
opened 2026-11-02 04:07:52 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6503
Author: @BlindMaster24
Created: 9/13/2026
Status: ✅ Merged
Merged: 9/13/2026
Merged by: @MHSanaei

Base: main ← Head: refactor/tgbot-add-client-expiry-preset


📝 Commits (3)

  • c315481 refactor(tgbot): make the add-client expiry presets say what they do
  • 9bd4999 refactor(tgbot): fold in the review of the expiry-preset change
  • cba7f56 chore(tgbot): merge main into the expiry-preset refactor

📊 Changes

3 files changed (+83 additions, -64 deletions)

View changed files

➕ internal/web/service/tgbot/tgbot_add_client_expiry_test.go (+61 -0)
📝 internal/web/service/tgbot/tgbot_draft_render_test.go (+9 -44)
📝 internal/web/service/tgbot/tgbot_router.go (+13 -20)

📄 Description

Summary

Removes the dead accumulate branch in the add-client wizard's expiry presets, makes the preset labels say what they do, and pins the intent with a test.

Why

The automated review of #6499 raised this twice as a LOW pre-existing: draft.expiryTime = 0 runs unconditionally at the top of add_client_reset_exp_c, so the if draft.expiryTime > 0 below it tests a value set to zero one line earlier. The branch is unreachable and only date = 0 - days ever runs.

I said in that PR I would not fix it there, because reviving the branch is a behaviour change rather than a drive-by, and I did not want to guess which semantics you want. Here is the decision, with the reasoning, so you can disagree with something concrete.

The wizard sets the term; it does not add to it.

  • A wizard draft is a new client, so there is no expiry to add to — add_client_to starts every field at its default, expiry at 0.
  • The presets and the custom keypad share this one case: add_client_reset_exp_in <n> types a number and confirms through add_client_reset_exp_c <n>. With add semantics, correcting 30 days to 90 would silently produce 120.
  • Every other wizard step sets: traffic, IP limit, email, comment, TG user. A preset that accumulated would be the only exception.

0 stays the Unlimited button — it clears the term, which is what the button says.

So the whole reachable behaviour collapses to one assignment, and the code now reads as the decision instead of as an accident. The renewal handler (reset_exp_c, the same preset row on an existing client) is the opposite case and is untouched: there the client does have remaining time, so it adds to the current expiry, and its if days > 0 guard is what keeps 0 meaning Unlimited.

The labels follow. The wizard's preset buttons read Add 7 Days, Add 1 Month, … while assigning rather than adding, so the operator-facing half contradicted the code. They now read 7 Days, 1 Month, 3 Months, with the term the presets actually set. The renewal keyboard keeps its Add — there the word is true. No translation key was added or removed: the wizard rows compose "7 " + tgbot.days instead of tgbot.add + " 7 " + tgbot.days, and tgbot.add stays in use by the renewal row.

Honest framing

This is a refactor with no behaviour change, so the test passes on the base commit too — I am not going to pretend otherwise. What it pins is the decision, and after the review pass it pins it per row: each row starts from a term a preset could have left (client_ExpiryTime = -7 * 86400000), so add_client_reset_exp_c 30, 90 and 0 each fail on their own if the accumulate branch is revived — the card would read 37 Days, 97 Days and 7 Days instead of 30 Days, 90 Days and Unlimited. Before that, only the 90 row discriminated, and only because the 30 row had run first.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

How was this tested?

TestAddClientExpiryPresetReplacesTheTerm drives the real answerCallback path with a mocked Telegram API, so the assertion is on the card text that would actually be sent: 30 Days / 90 Days / Unlimited. It installs a go-i18n bundle for the two labels the card prints, because I18n returns the bare key with no localizer, and it clears receiver_inbound_IDs so the card's attached-inbounds line cannot reach the database from whatever test ran before it — both the expiry global and that slice are saved and restored around the test.

The mocked-Telegram helpers are the package's shared ones (draftLocalizer, draftTexts), which now register only the messages a test passes in and record editMessageText alongside sendMessage. They replaced a second, near-identical copy this PR had added, as the review asked.

Behaviour is otherwise untouched: the renewal preset and the custom keypad keep their existing paths.

Verification

This machine has no C compiler, so the local run substitutes a pure-Go SQLite driver (modernc.org/sqlite) behind a go build -overlay whose only entries are internal/database's three driver call sites. No source file this PR touches is overlaid, so the handler and the test below are the ones in the diff.

Under that rig:

  • go test ./internal/web/service/tgbot/ — the whole package passes, including the prompt-escaping test that shares the helpers the review asked me to consolidate.
  • The test fails on the semantics this PR rejects. With the accumulate branch revived and the zeroing line gone, all three rows go red on their own: the card reads 37 Days, 97 Days and 7 Days instead of 30 Days, 90 Days and Unlimited.
  • With upstream main's handler verbatim, the same test passes — the "no behaviour change" claim above, checked rather than asserted.

gofmt and go vet are clean on all three files. CI still runs the full gate against the real CGo driver.

Screenshots / recordings

N/A — Telegram message text, covered by the test above.

Breaking changes

None. Existing drafts and clients keep the expiry the wizard already gave them.

Checklist

  • I tested the change locally and confirmed the described behavior. (pure-Go driver behind an overlay — see Verification)
  • I added or updated tests for the new behavior (when applicable). (pins existing behaviour by design — see above)
  • go build ./... and the test suite pass locally. (as-is they need the CGo driver; CI covers that)
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass. (no frontend change in this PR)
  • I updated the Wiki / README / API docs if user-facing behavior changed. (no docs describe this wizard)
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6503 **Author:** [@BlindMaster24](https://github.com/BlindMaster24) **Created:** 9/13/2026 **Status:** ✅ Merged **Merged:** 9/13/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `refactor/tgbot-add-client-expiry-preset` --- ### 📝 Commits (3) - [`c315481`](https://github.com/MHSanaei/3x-ui/commit/c315481bcbdc46cb1b7b0ebb0f1875dd8bfdde78) refactor(tgbot): make the add-client expiry presets say what they do - [`9bd4999`](https://github.com/MHSanaei/3x-ui/commit/9bd4999a286d9ac672c088d3ddbfcb64c5b163aa) refactor(tgbot): fold in the review of the expiry-preset change - [`cba7f56`](https://github.com/MHSanaei/3x-ui/commit/cba7f56936f53e0f526a671d246bf378185bd68c) chore(tgbot): merge main into the expiry-preset refactor ### 📊 Changes **3 files changed** (+83 additions, -64 deletions) <details> <summary>View changed files</summary> ➕ `internal/web/service/tgbot/tgbot_add_client_expiry_test.go` (+61 -0) 📝 `internal/web/service/tgbot/tgbot_draft_render_test.go` (+9 -44) 📝 `internal/web/service/tgbot/tgbot_router.go` (+13 -20) </details> ### 📄 Description ## Summary Removes the dead accumulate branch in the add-client wizard's expiry presets, makes the preset labels say what they do, and pins the intent with a test. ## Why The automated review of #6499 raised this twice as a **LOW pre-existing**: `draft.expiryTime = 0` runs unconditionally at the top of `add_client_reset_exp_c`, so the `if draft.expiryTime > 0` below it tests a value set to zero one line earlier. The branch is unreachable and only `date = 0 - days` ever runs. I said in that PR I would not fix it there, because reviving the branch is a behaviour change rather than a drive-by, and I did not want to guess which semantics you want. Here is the decision, with the reasoning, so you can disagree with something concrete. **The wizard sets the term; it does not add to it.** - A wizard draft is a *new* client, so there is no expiry to add to — `add_client_to` starts every field at its default, expiry at 0. - The presets and the **custom keypad share this one case**: `add_client_reset_exp_in <n>` types a number and confirms through `add_client_reset_exp_c <n>`. With add semantics, correcting 30 days to 90 would silently produce 120. - Every other wizard step sets: traffic, IP limit, email, comment, TG user. A preset that accumulated would be the only exception. **`0` stays the Unlimited button** — it clears the term, which is what the button says. So the whole reachable behaviour collapses to one assignment, and the code now reads as the decision instead of as an accident. The **renewal** handler (`reset_exp_c`, the same preset row on an existing client) is the opposite case and is untouched: there the client *does* have remaining time, so it adds to the current expiry, and its `if days > 0` guard is what keeps `0` meaning Unlimited. **The labels follow.** The wizard's preset buttons read **Add 7 Days**, **Add 1 Month**, … while assigning rather than adding, so the operator-facing half contradicted the code. They now read **7 Days**, **1 Month**, **3 Months**, with the term the presets actually set. The renewal keyboard keeps its **Add** — there the word is true. No translation key was added or removed: the wizard rows compose `"7 " + tgbot.days` instead of `tgbot.add + " 7 " + tgbot.days`, and `tgbot.add` stays in use by the renewal row. ### Honest framing This is a **refactor with no behaviour change**, so the test passes on the base commit too — I am not going to pretend otherwise. What it pins is the decision, and after the review pass it pins it per row: each row starts from a term a preset could have left (`client_ExpiryTime = -7 * 86400000`), so `add_client_reset_exp_c 30`, `90` and `0` each fail on their own if the accumulate branch is revived — the card would read `37 Days`, `97 Days` and `7 Days` instead of `30 Days`, `90 Days` and `Unlimited`. Before that, only the `90` row discriminated, and only because the `30` row had run first. ## Type of change - [ ] Bug fix - [ ] New feature - [x] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [ ] Frontend (UI / panel pages) - [ ] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [x] Telegram bot ## How was this tested? `TestAddClientExpiryPresetReplacesTheTerm` drives the real `answerCallback` path with a mocked Telegram API, so the assertion is on the card text that would actually be sent: `30 Days` / `90 Days` / `Unlimited`. It installs a go-i18n bundle for the two labels the card prints, because `I18n` returns the bare key with no localizer, and it clears `receiver_inbound_IDs` so the card's attached-inbounds line cannot reach the database from whatever test ran before it — both the expiry global and that slice are saved and restored around the test. The mocked-Telegram helpers are the package's shared ones (`draftLocalizer`, `draftTexts`), which now register only the messages a test passes in and record `editMessageText` alongside `sendMessage`. They replaced a second, near-identical copy this PR had added, as the review asked. Behaviour is otherwise untouched: the renewal preset and the custom keypad keep their existing paths. ### Verification This machine has no C compiler, so the local run substitutes a pure-Go SQLite driver (`modernc.org/sqlite`) behind a `go build -overlay` whose only entries are `internal/database`'s three driver call sites. No source file this PR touches is overlaid, so the handler and the test below are the ones in the diff. Under that rig: - `go test ./internal/web/service/tgbot/` — the whole package passes, including the prompt-escaping test that shares the helpers the review asked me to consolidate. - The test fails on the semantics this PR rejects. With the accumulate branch revived and the zeroing line gone, all three rows go red on their own: the card reads `37 Days`, `97 Days` and `7 Days` instead of `30 Days`, `90 Days` and `Unlimited`. - With upstream `main`'s handler verbatim, the same test passes — the "no behaviour change" claim above, checked rather than asserted. `gofmt` and `go vet` are clean on all three files. CI still runs the full gate against the real CGo driver. ## Screenshots / recordings N/A — Telegram message text, covered by the test above. ## Breaking changes None. Existing drafts and clients keep the expiry the wizard already gave them. ## Checklist - [x] I tested the change locally and confirmed the described behavior. (pure-Go driver behind an overlay — see Verification) - [x] I added or updated tests for the new behavior (when applicable). (pins existing behaviour by design — see above) - [ ] `go build ./...` and the test suite pass locally. (as-is they need the CGo driver; CI covers that) - [ ] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. (no frontend change in this PR) - [x] I updated the Wiki / README / API docs if user-facing behavior changed. (no docs describe this wizard) - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:07:52 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6314
No description provided.