mirror of
https://github.com/tiennm99/miti99bot.git
synced 2026-09-20 04:23:43 +00:00
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.
109 lines
3.2 KiB
Go
109 lines
3.2 KiB
Go
package twentyq
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/tiennm99/miti99bot/internal/ai"
|
|
"github.com/tiennm99/miti99bot/internal/modules"
|
|
"github.com/tiennm99/miti99bot/internal/storage"
|
|
"github.com/tiennm99/miti99bot/internal/testutil"
|
|
)
|
|
|
|
// scriptedChatter returns canned responses by call index. Tests script the
|
|
// exact sequence the handler will see (round-start then judge per turn).
|
|
type scriptedChatter struct {
|
|
responses []string
|
|
err error
|
|
calls int
|
|
}
|
|
|
|
func (s *scriptedChatter) Generate(_ context.Context, _, _ string) (string, error) {
|
|
if s.err != nil {
|
|
return "", s.err
|
|
}
|
|
if s.calls >= len(s.responses) {
|
|
return "{}", nil
|
|
}
|
|
r := s.responses[s.calls]
|
|
s.calls++
|
|
return r, nil
|
|
}
|
|
|
|
func install(t *testing.T, c ai.Chatter) *testutil.RecordingBot {
|
|
t.Helper()
|
|
rb := testutil.NewRecordingBot(t)
|
|
reg, err := modules.Build([]string{"twentyq"},
|
|
map[string]modules.Factory{"twentyq": New},
|
|
storage.NewMemoryProvider(),
|
|
modules.BuildOptions{Chatter: c})
|
|
if err != nil {
|
|
t.Fatalf("Build: %v", err)
|
|
}
|
|
modules.Install(rb.Bot, reg, modules.Auth{})
|
|
return rb
|
|
}
|
|
|
|
func TestTwentyq_NoChatterRefuses(t *testing.T) {
|
|
rb := install(t, nil)
|
|
rb.Bot.ProcessUpdate(context.Background(), testutil.NewPrivateMessage(42, "/twentyq"))
|
|
last := rb.LastSent().Text()
|
|
if !strings.Contains(last, "GEMINI_API_KEY") {
|
|
t.Errorf("missing-key warning: got %q", last)
|
|
}
|
|
}
|
|
|
|
func TestTwentyq_FreshRoundShowsIntro(t *testing.T) {
|
|
c := &scriptedChatter{responses: []string{
|
|
`{"category":"animal","initialHint":"a cryptic clue"}`,
|
|
}}
|
|
rb := install(t, c)
|
|
rb.Bot.ProcessUpdate(context.Background(), testutil.NewPrivateMessage(42, "/twentyq"))
|
|
last := rb.LastSent().Text()
|
|
if !strings.Contains(last, "animal") || !strings.Contains(last, "cryptic clue") {
|
|
t.Errorf("intro: got %q", last)
|
|
}
|
|
}
|
|
|
|
func TestTwentyq_RoundStartFallback(t *testing.T) {
|
|
c := &scriptedChatter{responses: []string{"unparseable garbage"}}
|
|
rb := install(t, c)
|
|
rb.Bot.ProcessUpdate(context.Background(), testutil.NewPrivateMessage(42, "/twentyq"))
|
|
last := rb.LastSent().Text()
|
|
// Fallback category=object, initialHint=everyday-life.
|
|
if !strings.Contains(last, "object") || !strings.Contains(last, "everyday life") {
|
|
t.Errorf("fallback intro: got %q", last)
|
|
}
|
|
}
|
|
|
|
func TestTwentyq_GiveupNoActiveRound(t *testing.T) {
|
|
c := &scriptedChatter{}
|
|
rb := install(t, c)
|
|
rb.Bot.ProcessUpdate(context.Background(), testutil.NewPrivateMessage(42, "/twentyq_giveup"))
|
|
last := rb.LastSent().Text()
|
|
if !strings.Contains(last, "No active round") {
|
|
t.Errorf("giveup-no-round: got %q", last)
|
|
}
|
|
}
|
|
|
|
func TestTwentyq_StatsEmpty(t *testing.T) {
|
|
c := &scriptedChatter{}
|
|
rb := install(t, c)
|
|
rb.Bot.ProcessUpdate(context.Background(), testutil.NewPrivateMessage(42, "/twentyq_stats"))
|
|
last := rb.LastSent().Text()
|
|
if !strings.Contains(last, "No twentyq games") {
|
|
t.Errorf("empty stats: got %q", last)
|
|
}
|
|
}
|
|
|
|
func TestTwentyq_RateLimitedRoundStart(t *testing.T) {
|
|
c := &scriptedChatter{err: ai.ErrRateLimited}
|
|
rb := install(t, c)
|
|
rb.Bot.ProcessUpdate(context.Background(), testutil.NewPrivateMessage(42, "/twentyq"))
|
|
last := rb.LastSent().Text()
|
|
if !strings.Contains(last, "rate-limited") {
|
|
t.Errorf("rate-limited reply: got %q", last)
|
|
}
|
|
}
|