tgbot_add_client_expiry_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package tgbot
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/mymmrac/telego"
  6. "github.com/nicksnyder/go-i18n/v2/i18n"
  7. )
  8. // Pins the decision the dead accumulate branch hid: a preset tap chooses the term
  9. // instead of adding to it, and 0 is Unlimited. The custom keypad shares this case.
  10. func TestAddClientExpiryPresetReplacesTheTerm(t *testing.T) {
  11. const chatID = int64(7505)
  12. draftLocalizer(t,
  13. &i18n.Message{ID: "tgbot.days", Other: "Days"},
  14. &i18n.Message{ID: "tgbot.unlimited", Other: "Unlimited"},
  15. )
  16. url, textsFor := draftTexts(t)
  17. swapTestBot(t, url)
  18. // A fresh draft attaches no inbound, so the card never looks one up by remark.
  19. draft := addClientDrafts.forChat(chatID)
  20. origRunning := isRunning
  21. t.Cleanup(func() {
  22. addClientDrafts.reset(chatID)
  23. isRunning = origRunning
  24. })
  25. isRunning = true
  26. tb := &Tgbot{}
  27. for _, tc := range []struct {
  28. days string
  29. want string
  30. }{
  31. {"30", "Expire: 30 Days"}, // not 37: a second tap replaces the first
  32. {"90", "Expire: 90 Days"}, // not 97, which accumulating would show
  33. {"0", "Expire: Unlimited"}, // the Unlimited button clears the term
  34. } {
  35. t.Run(tc.days, func(t *testing.T) {
  36. // A term left by an earlier preset; every row has to fail on its own
  37. // under the accumulate semantics this change rejected.
  38. draft.expiryTime = -7 * 86400000
  39. tb.answerCallback(&telego.CallbackQuery{
  40. ID: "q1",
  41. From: telego.User{ID: 1},
  42. Data: "add_client_reset_exp_c " + tc.days,
  43. Message: &telego.Message{MessageID: 7, Chat: telego.Chat{ID: chatID}},
  44. }, true) // admin
  45. sent := textsFor(chatID)
  46. if len(sent) == 0 {
  47. t.Fatalf("add_client_reset_exp_c %s rendered no card", tc.days)
  48. }
  49. if got := sent[len(sent)-1]; !strings.Contains(got, tc.want) {
  50. t.Errorf("card after add_client_reset_exp_c %s = %q, want it to contain %q", tc.days, got, tc.want)
  51. }
  52. })
  53. }
  54. }