Files
scrabble-game/backend/internal/notify/notify_test.go
T
Ilia Denisov f166ff30fe
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 8s
CI / integration (pull_request) Successful in 12s
CI / ui (pull_request) Successful in 34s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m20s
Stage 17 #4: enrich the out-of-app your-turn push + add game-over
The Telegram 'your turn' notification now names the opponent and recaps their last
move (voiced as the opponent: «{name}: my move — «WORD». Score 120:95» for a scoring
play; a short 'swapped / passed, your turn' otherwise), and a new game-over
notification reports the result + final score when a game ends by any path (closing
play, all-pass, resign, timeout). Scores are recipient-first (the reader's score
leads), 2-4 players (120:95:80).

- schema: YourTurnEvent gains opponent_name/last_action/last_word/score_line
  (appended, backward-compatible); new GameOverEvent{result, score_line}. Go + UI
  bindings regenerated (flatc 23.5.26 + pnpm codegen).
- backend: notify.YourTurn enriched + notify.GameOver; emitMove resolves the mover's
  name and emits per-recipient (your_turn to the next mover, game_over to every seat),
  with recipient-first score lines built in one place.
- gateway: game_over joins the out-of-app whitelist (routing.go).
- connector: render builds the enriched your_turn + game_over text per language (en/ru).
- tests: notify round-trip (enriched + game_over), emit (enriched fields + game_over to
  all seats / per-seat result), connector render (en/ru), routing; integration replay
  (play → your_turn with real name; resign → game_over) green.
- docs: ARCHITECTURE push catalog + out-of-app set, FUNCTIONAL (+ _ru), PLAN tracker.
2026-06-09 01:15:18 +02:00

130 lines
4.3 KiB
Go

package notify_test
import (
"testing"
"time"
"github.com/google/uuid"
"scrabble/backend/internal/notify"
fb "scrabble/pkg/fbs/scrabblefb"
)
func TestHubDeliversToSubscriber(t *testing.T) {
h := notify.NewHub(4)
ch, cancel := h.Subscribe()
defer cancel()
want := notify.Intent{UserID: uuid.New(), Kind: notify.KindYourTurn, Payload: []byte{1, 2, 3}}
h.Publish(want)
select {
case got := <-ch:
if got.Kind != want.Kind || got.UserID != want.UserID {
t.Fatalf("delivered %+v, want %+v", got, want)
}
case <-time.After(time.Second):
t.Fatal("no delivery within timeout")
}
}
func TestHubDropsWhenSubscriberBufferFull(t *testing.T) {
h := notify.NewHub(1)
ch, cancel := h.Subscribe()
defer cancel()
in := notify.Intent{UserID: uuid.New(), Kind: notify.KindNudge}
// Buffer holds one; the second and third are dropped, and Publish must not block.
h.Publish(in, in, in)
if got := len(ch); got != 1 {
t.Fatalf("buffered %d intents, want 1 (rest dropped)", got)
}
}
func TestHubUnsubscribeClosesChannel(t *testing.T) {
h := notify.NewHub(2)
ch, cancel := h.Subscribe()
cancel()
if _, ok := <-ch; ok {
t.Fatal("channel should be closed after unsubscribe")
}
// Publishing after unsubscribe must be safe (no panic, no delivery).
h.Publish(notify.Intent{Kind: notify.KindMatchFound})
}
func TestNopPublisherDiscards(t *testing.T) {
var p notify.Publisher = notify.Nop{}
p.Publish(notify.Intent{Kind: notify.KindYourTurn}) // must not panic
}
func TestYourTurnPayloadRoundTrips(t *testing.T) {
uid, gid := uuid.New(), uuid.New()
in := notify.YourTurn(uid, gid, time.Unix(1717000000, 0), "Ann", "play", "STOOL", "120:95")
if in.UserID != uid || in.Kind != notify.KindYourTurn || in.EventID == "" {
t.Fatalf("intent metadata wrong: %+v", in)
}
ev := fb.GetRootAsYourTurnEvent(in.Payload, 0)
if got := string(ev.GameId()); got != gid.String() {
t.Fatalf("game id = %q, want %q", got, gid)
}
if got := ev.DeadlineUnix(); got != 1717000000 {
t.Fatalf("deadline = %d, want 1717000000", got)
}
if string(ev.OpponentName()) != "Ann" || string(ev.LastAction()) != "play" ||
string(ev.LastWord()) != "STOOL" || string(ev.ScoreLine()) != "120:95" {
t.Fatalf("enriched fields wrong: name=%q action=%q word=%q score=%q",
ev.OpponentName(), ev.LastAction(), ev.LastWord(), ev.ScoreLine())
}
}
func TestGameOverPayloadRoundTrips(t *testing.T) {
uid, gid := uuid.New(), uuid.New()
in := notify.GameOver(uid, gid, "won", "120:95:80")
if in.UserID != uid || in.Kind != notify.KindGameOver || in.EventID == "" {
t.Fatalf("intent metadata wrong: %+v", in)
}
ev := fb.GetRootAsGameOverEvent(in.Payload, 0)
if string(ev.GameId()) != gid.String() || string(ev.Result()) != "won" || string(ev.ScoreLine()) != "120:95:80" {
t.Fatalf("game_over fields wrong: game=%q result=%q score=%q", ev.GameId(), ev.Result(), ev.ScoreLine())
}
}
func TestOpponentMovedPayloadRoundTrips(t *testing.T) {
uid, gid := uuid.New(), uuid.New()
in := notify.OpponentMoved(uid, gid, 1, "play", 24, 130)
if in.Kind != notify.KindOpponentMoved {
t.Fatalf("kind = %q", in.Kind)
}
ev := fb.GetRootAsOpponentMovedEvent(in.Payload, 0)
if string(ev.GameId()) != gid.String() || ev.Seat() != 1 || string(ev.Action()) != "play" || ev.Score() != 24 || ev.Total() != 130 {
t.Fatalf("decoded wrong: game=%q seat=%d action=%q score=%d total=%d",
ev.GameId(), ev.Seat(), ev.Action(), ev.Score(), ev.Total())
}
}
func TestChatMessagePayloadRoundTrips(t *testing.T) {
uid, gid, sid := uuid.New(), uuid.New(), uuid.New()
in := notify.ChatMessage(uid, gid, sid, "msg-1", "message", "hi", time.Unix(1717000001, 0))
if in.Kind != notify.KindChatMessage {
t.Fatalf("kind = %q", in.Kind)
}
ev := fb.GetRootAsChatMessage(in.Payload, 0)
if string(ev.Id()) != "msg-1" || string(ev.SenderId()) != sid.String() || string(ev.Body()) != "hi" || ev.CreatedAtUnix() != 1717000001 {
t.Fatalf("decoded wrong chat message: %+v", ev)
}
}
func TestNotificationPayloadRoundTrips(t *testing.T) {
uid := uuid.New()
in := notify.Notification(uid, notify.NotifyFriendRequest)
if in.UserID != uid || in.Kind != notify.KindNotification || in.EventID == "" {
t.Fatalf("intent metadata wrong: %+v", in)
}
ev := fb.GetRootAsNotificationEvent(in.Payload, 0)
if got := string(ev.Kind()); got != notify.NotifyFriendRequest {
t.Fatalf("notification sub-kind = %q, want %q", got, notify.NotifyFriendRequest)
}
}