Files
miti99bot/internal/modules/loldle/handlers_test.go
T
tiennm99 1e7b1a9bca test(handlers): integration tests + recording bot + emulator on CI
Phase 5 of the 2026-05-09 review remediation plan. Closes the
handler-layer test gap (5 modules at 0% coverage in the audit) and
ends the storage-package's CI t.Skip on Firestore emulator tests.

- internal/testutil: Update fixture builders (NewPrivateMessage,
  NewGroupMessage, NewSupergroupMessage, NewChannelMessage) plus a
  RecordingBot that wraps the real go-telegram/bot.Bot with an
  httptest server. The bot library hits the test server instead of
  Telegram; multipart form fields are captured per call. Tests assert
  on Sent() / LastSent() / AssertSentText().
- Handler tests added: misc (4), util (7), wordle (10), loldle (9),
  loldleemoji (8). Cover happy paths, error paths, auth gates,
  group-vs-private subject keying, KV side effects.
- Coverage 44.7% -> 69.8% (verified via -coverprofile). All packages
  now report coverage in CI output.
- CI: ci.yml installs cloud-firestore-emulator beta component and
  starts it on localhost:8090 before go test. Sets
  FIRESTORE_EMULATOR_HOST + GOOGLE_CLOUD_PROJECT env so the storage
  package's emulator-gated tests execute instead of skipping.

go test -race -count=1 ./... clean across all 15 packages locally.
2026-05-09 16:19:38 +07:00

154 lines
4.9 KiB
Go

package loldle
import (
"context"
"strings"
"testing"
"github.com/tiennm99/miti99bot-go/internal/modules"
"github.com/tiennm99/miti99bot-go/internal/storage"
"github.com/tiennm99/miti99bot-go/internal/testutil"
)
// installLoldle wires the loldle module + auth (owner gates /loldle_setmax,
// which is private). seedTarget pre-seeds a game so guess outcomes are
// deterministic without hooking math/rand.
func installLoldle(t *testing.T, ownerID int64, seedSubject, seedTarget string) (*testutil.RecordingBot, storage.KVStore) {
t.Helper()
rb := testutil.NewRecordingBot(t)
provider := storage.NewMemoryProvider()
kv := provider.For("loldle")
mod := New(modules.Deps{KV: kv})
reg := &modules.Registry{
Modules: []modules.Module{{Name: "loldle", Commands: mod.Commands}},
AllCommands: map[string]modules.Command{},
}
for _, c := range mod.Commands {
reg.AllCommands[c.Name] = c
}
modules.Install(rb.Bot, reg, modules.Auth{BotOwnerID: ownerID})
if seedTarget != "" {
g := &gameState{Target: seedTarget, Guesses: []string{}}
if err := saveGame(context.Background(), kv, seedSubject, g); err != nil {
t.Fatalf("seed game: %v", err)
}
}
return rb, kv
}
func TestLoldle_NoArgShowsBoard(t *testing.T) {
rb, _ := installLoldle(t, 0, "1", "Aatrox")
rb.Bot.ProcessUpdate(context.Background(), testutil.NewPrivateMessage(1, "/loldle"))
got := rb.LastSent().Text()
if !strings.Contains(got, "Guess 0/") {
t.Errorf("/loldle no-arg reply missing 'Guess 0/...': %q", got)
}
}
func TestLoldle_Win(t *testing.T) {
rb, _ := installLoldle(t, 0, "1", "Aatrox")
rb.Bot.ProcessUpdate(context.Background(), testutil.NewPrivateMessage(1, "/loldle aatrox"))
got := rb.LastSent().Text()
if !strings.Contains(strings.ToLower(got), "aatrox") {
t.Errorf("win reply missing 'Aatrox': %q", got)
}
if !strings.Contains(got, "🎉") {
t.Errorf("win reply missing celebration emoji: %q", got)
}
}
func TestLoldle_UnknownChampion(t *testing.T) {
rb, _ := installLoldle(t, 0, "1", "Aatrox")
rb.Bot.ProcessUpdate(context.Background(), testutil.NewPrivateMessage(1, "/loldle ZilbeanZ"))
got := rb.LastSent().Text()
if !strings.Contains(got, "Champion not found") {
t.Errorf("unknown champion reject: %q", got)
}
}
func TestLoldle_DuplicateGuessRejected(t *testing.T) {
rb, _ := installLoldle(t, 0, "1", "Aatrox")
// First guess: Ahri (not the target — round continues).
rb.Bot.ProcessUpdate(context.Background(), testutil.NewPrivateMessage(1, "/loldle ahri"))
rb.Reset()
// Second guess: Ahri again — duplicate path.
rb.Bot.ProcessUpdate(context.Background(), testutil.NewPrivateMessage(1, "/loldle ahri"))
got := rb.LastSent().Text()
if !strings.Contains(got, "already guessed") {
t.Errorf("duplicate-guess reply: %q", got)
}
}
func TestLoldleGiveup_RevealsAnswer(t *testing.T) {
rb, _ := installLoldle(t, 0, "1", "Aatrox")
rb.Bot.ProcessUpdate(context.Background(), testutil.NewPrivateMessage(1, "/loldle_giveup"))
got := rb.LastSent().Text()
if !strings.Contains(got, "Aatrox") {
t.Errorf("/loldle_giveup should reveal Aatrox: %q", got)
}
}
func TestLoldleGiveup_NoActiveRound(t *testing.T) {
rb, _ := installLoldle(t, 0, "", "")
rb.Bot.ProcessUpdate(context.Background(), testutil.NewPrivateMessage(1, "/loldle_giveup"))
got := rb.LastSent().Text()
if !strings.Contains(got, "No active round") {
t.Errorf("/loldle_giveup with no round: %q", got)
}
}
func TestLoldleStats_Empty(t *testing.T) {
rb, _ := installLoldle(t, 0, "", "")
rb.Bot.ProcessUpdate(context.Background(), testutil.NewPrivateMessage(1, "/loldle_stats"))
got := rb.LastSent().Text()
for _, want := range []string{"Played: 0", "Wins: 0 (0%)", "Best streak: 0"} {
if !strings.Contains(got, want) {
t.Errorf("/loldle_stats empty missing %q; got %q", want, got)
}
}
}
func TestLoldleSetMax_OwnerSucceeds(t *testing.T) {
rb, kv := installLoldle(t, 999, "", "")
rb.Bot.ProcessUpdate(context.Background(), testutil.NewPrivateMessage(999, "/loldle_setmax 5"))
got := rb.LastSent().Text()
if !strings.Contains(got, "max guesses set to 5") {
t.Errorf("/loldle_setmax reply: %q", got)
}
var cfg roundConfig
if err := kv.GetJSON(context.Background(), configKey("999"), &cfg); err != nil {
t.Fatalf("expected config persisted: %v", err)
}
if cfg.MaxGuesses != 5 {
t.Errorf("MaxGuesses persisted = %d, want 5", cfg.MaxGuesses)
}
}
func TestLoldleSetMax_DeniedToNonOwner(t *testing.T) {
rb, _ := installLoldle(t, 999, "", "")
rb.Bot.ProcessUpdate(context.Background(), testutil.NewPrivateMessage(7, "/loldle_setmax 5"))
if calls := rb.Sent(); len(calls) != 0 {
t.Errorf("non-owner /loldle_setmax replied: %+v", calls)
}
}
func TestLoldleSetMax_RejectsOutOfRange(t *testing.T) {
rb, _ := installLoldle(t, 999, "", "")
rb.Bot.ProcessUpdate(context.Background(), testutil.NewPrivateMessage(999, "/loldle_setmax 99"))
got := rb.LastSent().Text()
if !strings.Contains(got, "Usage:") {
t.Errorf("out-of-range setmax should show usage; got %q", got)
}
}