Files
miti99bot/internal/modules/misc/misc_test.go
T
tiennm99 f3b9891a54 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

84 lines
2.4 KiB
Go

package misc
import (
"context"
"testing"
"time"
"github.com/tiennm99/miti99bot/internal/modules"
"github.com/tiennm99/miti99bot/internal/storage"
)
// We test the per-command KV behaviour directly — the bot/Telegram side is
// thin (single SendMessage) and exercising it would require a fake bot HTTP
// server. The KV interaction is the part with logic worth locking down.
func TestNew_RegistersExpectedCommands(t *testing.T) {
deps := modules.Deps{KV: storage.NewMemoryKVStore()}
mod := New(deps)
want := map[string]modules.Visibility{
"ping": modules.VisibilityPublic,
"mstats": modules.VisibilityProtected,
"fortytwo": modules.VisibilityPrivate,
}
if len(mod.Commands) != len(want) {
t.Fatalf("commands count = %d, want %d", len(mod.Commands), len(want))
}
for _, c := range mod.Commands {
v, ok := want[c.Name]
if !ok {
t.Errorf("unexpected command %q", c.Name)
continue
}
if c.Visibility != v {
t.Errorf("command %q visibility = %d, want %d", c.Name, c.Visibility, v)
}
if c.Handler == nil {
t.Errorf("command %q has nil handler", c.Name)
}
}
}
func TestPing_WritesLastPingKV(t *testing.T) {
ctx := context.Background()
kv := storage.NewMemoryKVStore()
// Drive the KV side directly: lock the wire format (ms-epoch number, not
// RFC3339 string). A JS-written {at: 1700000000000} must round-trip into
// the Go struct without a custom decoder.
if err := kv.PutJSON(ctx, lastPingKey, lastPing{At: time.Now().UTC().UnixMilli()}); err != nil {
t.Fatalf("PutJSON: %v", err)
}
var got lastPing
if err := kv.GetJSON(ctx, lastPingKey, &got); err != nil {
t.Fatalf("GetJSON: %v", err)
}
if got.At <= 0 {
t.Errorf("read-back lastPing.At = %d, want positive ms-epoch", got.At)
}
// Also verify a value with the JS-shape decodes correctly.
if err := kv.Put(ctx, lastPingKey, []byte(`{"at":1700000000000}`)); err != nil {
t.Fatal(err)
}
got = lastPing{}
if err := kv.GetJSON(ctx, lastPingKey, &got); err != nil {
t.Fatalf("GetJSON js-shape: %v", err)
}
if got.At != 1700000000000 {
t.Errorf("js-shape round-trip: At = %d, want 1700000000000", got.At)
}
}
func TestMstats_MissingKVReturnsErrNotFound(t *testing.T) {
ctx := context.Background()
kv := storage.NewMemoryKVStore()
var dst lastPing
if err := kv.GetJSON(ctx, lastPingKey, &dst); err != storage.ErrNotFound {
t.Errorf("GetJSON missing = %v, want ErrNotFound", err)
}
}