commands_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package telegram
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestParse_Start(t *testing.T) {
  7. cases := []struct {
  8. in string
  9. want string // expected InviteCode; "" means Start is nil
  10. }{
  11. {"/start", ""},
  12. {"/start abc-123", "abc-123"},
  13. {"/start@acme_x_bot abc-123", "abc-123"},
  14. {" /start abc-123 ", "abc-123"},
  15. }
  16. for _, tc := range cases {
  17. got := Parse(tc.in)
  18. if got.Start == nil {
  19. if tc.want != "" {
  20. t.Errorf("%q: Start is nil, want %q", tc.in, tc.want)
  21. }
  22. continue
  23. }
  24. if got.Start.InviteCode != tc.want {
  25. t.Errorf("%q: InviteCode = %q, want %q", tc.in, got.Start.InviteCode, tc.want)
  26. }
  27. }
  28. }
  29. func TestParse_Subscribe(t *testing.T) {
  30. cases := []struct {
  31. in string
  32. source string
  33. minSev string
  34. badInput bool
  35. }{
  36. {"/subscribe prom-prod warning", "prom-prod", "warning", false},
  37. {"/subscribe prom-prod", "prom-prod", "info", false}, // default min
  38. {"/subscribe", "", "", true}, // missing arg
  39. {"/subscribe prom-prod bogus", "", "", true}, // bad severity
  40. {"/SUBSCRIBE prom-prod critical", "prom-prod", "critical", false}, // case-insensitive
  41. }
  42. for _, tc := range cases {
  43. got := Parse(tc.in)
  44. if tc.badInput {
  45. if got.Subscribe != nil {
  46. t.Errorf("%q: expected Unknown, got Subscribe %+v", tc.in, got.Subscribe)
  47. }
  48. continue
  49. }
  50. if got.Subscribe == nil {
  51. t.Errorf("%q: Subscribe is nil", tc.in)
  52. continue
  53. }
  54. if got.Subscribe.SourceID != tc.source || got.Subscribe.MinSeverity != tc.minSev {
  55. t.Errorf("%q: Subscribe = %+v, want source=%q min=%q", tc.in, got.Subscribe, tc.source, tc.minSev)
  56. }
  57. }
  58. }
  59. func TestParse_Unsubscribe(t *testing.T) {
  60. c := Parse("/unsubscribe prom-prod")
  61. if c.Unsubscribe == nil || c.Unsubscribe.SourceID != "prom-prod" {
  62. t.Errorf("unsubscribe: %+v", c)
  63. }
  64. c = Parse("/unsubscribe")
  65. if c.Unsubscribe != nil {
  66. t.Errorf("unsubscribe (no arg) should be nil, got %+v", c.Unsubscribe)
  67. }
  68. }
  69. func TestParse_Preferences(t *testing.T) {
  70. c := Parse("/preferences")
  71. if !c.Preferences {
  72. t.Errorf("expected Preferences=true, got %+v", c)
  73. }
  74. }
  75. func TestParse_Status(t *testing.T) {
  76. c := Parse("/status")
  77. if c.Status == nil || c.Status.Limit != 5 {
  78. t.Errorf("/status default limit: %+v", c)
  79. }
  80. c = Parse("/status 12")
  81. if c.Status == nil || c.Status.Limit != 12 {
  82. t.Errorf("/status 12: %+v", c)
  83. }
  84. c = Parse("/status 0") // 0 should fall back to default
  85. if c.Status == nil || c.Status.Limit != 5 {
  86. t.Errorf("/status 0: %+v", c)
  87. }
  88. }
  89. func TestParse_Mute(t *testing.T) {
  90. c := Parse("/mute 2h")
  91. if c.Mute == nil || c.Mute.Duration != 2*time.Hour {
  92. t.Errorf("/mute 2h: %+v", c)
  93. }
  94. c = Parse("/mute 30m")
  95. if c.Mute == nil || c.Mute.Duration != 30*time.Minute {
  96. t.Errorf("/mute 30m: %+v", c)
  97. }
  98. c = Parse("/mute until 18:00")
  99. if c.Mute == nil || c.Mute.Until.IsZero() {
  100. t.Errorf("/mute until 18:00: %+v", c.Mute)
  101. }
  102. if h := c.Mute.Until.Hour(); h != 18 {
  103. t.Errorf("/mute until 18:00 hour = %d, want 18", h)
  104. }
  105. c = Parse("/mute")
  106. if c.Mute != nil {
  107. t.Errorf("/mute (no arg): %+v", c.Mute)
  108. }
  109. }
  110. func TestParse_Unmute(t *testing.T) {
  111. c := Parse("/unmute")
  112. if !c.Unmute {
  113. t.Errorf("expected Unmute=true, got %+v", c)
  114. }
  115. }
  116. func TestParse_Unknown(t *testing.T) {
  117. c := Parse("hello world")
  118. if c.Unknown == "" {
  119. t.Error("non-command text should be Unknown")
  120. }
  121. c = Parse("/nonsense")
  122. if c.Unknown == "" {
  123. t.Error("unknown command should be Unknown")
  124. }
  125. c = Parse("")
  126. if c.Unknown != "" {
  127. t.Error("empty text should be Unknown=\"\"")
  128. }
  129. }