Files
miti99bot/internal/modules/loldle/handlers_test.go
T
tiennm99 642fccb7b7 refactor: rename module to miti99bot, canonicalize AWS deploy path
Rename:
- Go module github.com/tiennm99/miti99bot-go → github.com/tiennm99/miti99bot
- CloudFormation stack miti99bot-aws-port → miti99bot
- Drop "port", "Cloud Run", "GCP", "cutover", "Phase NN" framing from
  active code and docs — project reads as canonical AWS-Lambda from now on.

AWS deploy guide + flow fix:
- New docs/deploy-aws-free-tier-guide.md — Ubuntu 24.04 ARM64 onboarding
  with project-local venv (pip awscli + sam-cli), SSM secrets via read -s,
  idempotent OIDC provider + role creation, $1 budget alarm.
- Drop sam build from the pipeline — provided.al2023 + makefile builder
  expects a Makefile in CodeUri (build/lambda/, the output dir), so the
  step always fails. sam deploy --template-file template.yaml now reads
  the raw template and zips build/lambda/ directly.
- Rollback section rewritten — use continue-update-rollback /
  cancel-update-stack / git-SHA redeploy. Drop the broken
  --use-previous-template recipe.
- DynamoDB free-tier row corrected (on-demand is 2.5M read / 1M write
  request units, not 25 RCU/WCU).

Updated:
- README.md fully rewritten (drops port/legacy framing, lists modules,
  points new users at the free-tier guide).
- aws/README.md retitled "AWS account setup", phase numbers stripped.
- Makefile / .github/workflows/deploy.yml — sam deploy flow.
- samconfig.toml — stack_name = "miti99bot".
- Go comments — Cloud Run → Lambda, Cloud Scheduler → EventBridge
  Scheduler, Cloud Logging → CloudWatch Logs.
- Struct field GCPProject → FirestoreProject (env GOOGLE_CLOUD_PROJECT
  unchanged).

Plus advisory reports under plans/reports/ from the code-reviewer +
researcher passes that informed the fixes.

Verified: go vet ./..., go build ./..., go test ./... all green.
2026-05-13 22:05:38 +07:00

154 lines
4.9 KiB
Go

package loldle
import (
"context"
"strings"
"testing"
"github.com/tiennm99/miti99bot/internal/modules"
"github.com/tiennm99/miti99bot/internal/storage"
"github.com/tiennm99/miti99bot/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)
}
}