mirror of
https://github.com/tiennm99/miti99bot.git
synced 2026-09-16 12:21:51 +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.
146 lines
4.1 KiB
Go
146 lines
4.1 KiB
Go
package loldle
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/tiennm99/miti99bot/internal/storage"
|
|
)
|
|
|
|
func TestGameState_StartedAtNullByDefault(t *testing.T) {
|
|
g := gameState{Target: "Aatrox", Guesses: []string{}}
|
|
b, err := json.Marshal(g)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := `{"target":"Aatrox","guesses":[],"startedAt":null}`
|
|
if string(b) != want {
|
|
t.Errorf("marshal:\ngot %s\nwant %s", b, want)
|
|
}
|
|
}
|
|
|
|
func TestGameState_StartedAtAsNumber(t *testing.T) {
|
|
at := int64(1700000000000)
|
|
g := gameState{Target: "Aatrox", Guesses: []string{"Ahri"}, StartedAt: &at}
|
|
b, _ := json.Marshal(g)
|
|
want := `{"target":"Aatrox","guesses":["Ahri"],"startedAt":1700000000000}`
|
|
if string(b) != want {
|
|
t.Errorf("marshal:\ngot %s\nwant %s", b, want)
|
|
}
|
|
}
|
|
|
|
func TestStats_NoLastResultAtField(t *testing.T) {
|
|
// JS loldle stats schema differs from wordle: no lastResultAt. Lock that.
|
|
b, _ := json.Marshal(stats{})
|
|
want := `{"played":0,"wins":0,"streak":0,"bestStreak":0}`
|
|
if string(b) != want {
|
|
t.Errorf("marshal:\ngot %s\nwant %s", b, want)
|
|
}
|
|
}
|
|
|
|
func TestRecordResult_WinAndLossSequence(t *testing.T) {
|
|
ctx := context.Background()
|
|
kv := storage.NewMemoryKVStore()
|
|
|
|
s, err := recordResult(ctx, kv, "u1", true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s.Played != 1 || s.Wins != 1 || s.Streak != 1 || s.BestStreak != 1 {
|
|
t.Errorf("first win: %+v", s)
|
|
}
|
|
s, _ = recordResult(ctx, kv, "u1", true)
|
|
if s.Streak != 2 || s.BestStreak != 2 {
|
|
t.Errorf("two wins: %+v", s)
|
|
}
|
|
s, _ = recordResult(ctx, kv, "u1", false)
|
|
if s.Streak != 0 {
|
|
t.Errorf("loss should reset streak, got %d", s.Streak)
|
|
}
|
|
if s.BestStreak != 2 {
|
|
t.Errorf("best streak should persist, got %d", s.BestStreak)
|
|
}
|
|
}
|
|
|
|
func TestGetMaxGuesses_DefaultsAndOverrides(t *testing.T) {
|
|
ctx := context.Background()
|
|
kv := storage.NewMemoryKVStore()
|
|
|
|
if n, _ := getMaxGuesses(ctx, kv, "u1"); n != MaxGuesses {
|
|
t.Errorf("missing config → %d, want %d", n, MaxGuesses)
|
|
}
|
|
|
|
if err := setMaxGuesses(ctx, kv, "u1", 5); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n, _ := getMaxGuesses(ctx, kv, "u1"); n != 5 {
|
|
t.Errorf("after setMax(5): %d, want 5", n)
|
|
}
|
|
}
|
|
|
|
func TestSetMaxGuesses_ValidatesRange(t *testing.T) {
|
|
ctx := context.Background()
|
|
kv := storage.NewMemoryKVStore()
|
|
for _, n := range []int{0, -1, MaxGuessesCap + 1, 100} {
|
|
if err := setMaxGuesses(ctx, kv, "u1", n); err == nil {
|
|
t.Errorf("setMaxGuesses(%d) should error", n)
|
|
}
|
|
}
|
|
if err := setMaxGuesses(ctx, kv, "u1", 1); err != nil {
|
|
t.Errorf("setMaxGuesses(1) should succeed: %v", err)
|
|
}
|
|
if err := setMaxGuesses(ctx, kv, "u1", MaxGuessesCap); err != nil {
|
|
t.Errorf("setMaxGuesses(cap) should succeed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestGetMaxGuesses_OutOfRangeIgnored(t *testing.T) {
|
|
ctx := context.Background()
|
|
kv := storage.NewMemoryKVStore()
|
|
// Inject a corrupt config to simulate manual KV tampering or a stale
|
|
// schema; getMaxGuesses must fall back to the default rather than
|
|
// returning a wild value to handlers.
|
|
if err := kv.PutJSON(ctx, configKey("u1"), roundConfig{MaxGuesses: 100}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n, _ := getMaxGuesses(ctx, kv, "u1"); n != MaxGuesses {
|
|
t.Errorf("out-of-range config → %d, want %d (default)", n, MaxGuesses)
|
|
}
|
|
}
|
|
|
|
func TestLoadGame_MissingReturnsNil(t *testing.T) {
|
|
g, err := loadGame(context.Background(), storage.NewMemoryKVStore(), "nobody")
|
|
if err != nil {
|
|
t.Errorf("missing should not error: %v", err)
|
|
}
|
|
if g != nil {
|
|
t.Errorf("expected nil, got %+v", g)
|
|
}
|
|
}
|
|
|
|
func TestSaveLoadClearGame_RoundTrip(t *testing.T) {
|
|
ctx := context.Background()
|
|
kv := storage.NewMemoryKVStore()
|
|
at := int64(42)
|
|
want := &gameState{Target: "Aatrox", Guesses: []string{"Ahri"}, StartedAt: &at}
|
|
if err := saveGame(ctx, kv, "u1", want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := loadGame(ctx, kv, "u1")
|
|
if err != nil || got == nil {
|
|
t.Fatalf("loadGame: %v, %v", got, err)
|
|
}
|
|
if got.Target != "Aatrox" || len(got.Guesses) != 1 || got.StartedAt == nil || *got.StartedAt != 42 {
|
|
t.Errorf("round-trip mismatch: %+v", got)
|
|
}
|
|
|
|
if err := clearGame(ctx, kv, "u1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _ = loadGame(ctx, kv, "u1")
|
|
if got != nil {
|
|
t.Errorf("after clearGame, expected nil, got %+v", got)
|
|
}
|
|
}
|