Phase 2c — testing infrastructure (43 tests, CI gating, docker-compose)
Some checks failed
Docker Build / build-and-push-image (push) Has been cancelled

Brings the project to a TDD/BDD-friendly state — apologies for shipping
Phase 1.5 + Phase 2 code-first, that violated feedback_tdd_first_bdd_required.

What's added :

- helpers_test.go : FakeTelegram (httptest server that records sendMessage /
  deleteMessage / setWebhook / etc.), miniredis bootstrap, MakeUpdate /
  PostWebhook helpers. The same harness simulates 'a user DMing the bot'
  end-to-end without hitting Telegram cloud — answer to the user question.
- 43 tests covering : allowlist parsing, telegram type helpers (UserID /
  ChatID / Text / messageID), secret_token constant-time compare, Backoff
  schedule, Auth (login wrong/right/logout/TTL/nil-receiver), EchoHandler,
  HTTPHandler (forward / timeout / non-2xx / empty body), AuthHandler
  (start / auth / whoami / logout / replay defense delete), Server (bad
  secret 401, unknown bot 404, allowlist drop, gated bot prompt,
  full /auth → echo → /logout flow, healthz/readyz).
- All tests pass with -race in 1.6s, no external deps (miniredis +
  httptest in-process).

Infra :

- Updated .gitea/workflows/dockerimage.yaml : new 'test' job
  (go vet + go test -race) gates the build-and-push-image job. CI now
  also runs on pull_request.
- docker-compose.yml : redis + postgres for full local stack.
- Makefile : test-race, compose-up/down targets.
- README updated with test + local-dev sections.

Refs ~/.claude/plans/pour-les-notifications-on-inherited-seal.md § Phase 2.
This commit is contained in:
2026-05-09 15:18:29 +02:00
parent 4f246ccc1d
commit d63f195b3d
16 changed files with 1100 additions and 9 deletions

59
handler_echo_test.go Normal file
View File

@@ -0,0 +1,59 @@
package main
import (
"strings"
"testing"
)
func TestEchoHandler_PlainText(t *testing.T) {
ft := NewFakeTelegram(t)
h := &EchoHandler{tg: ft.Client()}
bot := Bot{Slug: "echo", Token: "t1", Secret: "s"}
if err := h.Handle(bgCtx(), MakeUpdate(1, 100, 42, 1, "salut"), bot); err != nil {
t.Fatalf("handle: %v", err)
}
sent := ft.Sent()
if len(sent) != 1 || sent[0].ChatID != 42 || sent[0].Text != "salut" {
t.Fatalf("expected echo of 'salut' to chat 42, got %+v", sent)
}
}
func TestEchoHandler_SlashEcho_StripsCommand(t *testing.T) {
ft := NewFakeTelegram(t)
h := &EchoHandler{tg: ft.Client()}
bot := Bot{Slug: "echo", Token: "t1", Secret: "s"}
if err := h.Handle(bgCtx(), MakeUpdate(2, 100, 42, 1, "/echo coucou monde"), bot); err != nil {
t.Fatalf("handle: %v", err)
}
sent := ft.Sent()
if len(sent) != 1 || sent[0].Text != "coucou monde" {
t.Fatalf("expected reply 'coucou monde', got %q", sent[0].Text)
}
}
func TestEchoHandler_EmptyTextNoSend(t *testing.T) {
ft := NewFakeTelegram(t)
h := &EchoHandler{tg: ft.Client()}
bot := Bot{Slug: "echo", Token: "t1", Secret: "s"}
if err := h.Handle(bgCtx(), MakeUpdate(3, 100, 42, 1, " "), bot); err != nil {
t.Fatalf("handle: %v", err)
}
if len(ft.Sent()) != 0 {
t.Fatalf("empty text should send nothing, got %d", len(ft.Sent()))
}
}
func TestEchoHandler_SlashEchoBare_DefaultMessage(t *testing.T) {
ft := NewFakeTelegram(t)
h := &EchoHandler{tg: ft.Client()}
bot := Bot{Slug: "echo", Token: "t1", Secret: "s"}
_ = h.Handle(bgCtx(), MakeUpdate(4, 100, 42, 1, "/echo"), bot)
sent := ft.Sent()
if len(sent) != 1 || !strings.Contains(sent[0].Text, "echo bot online") {
t.Fatalf("expected default greeting, got %+v", sent)
}
}