Files
scrabble-game/gateway/internal/transcode/transcode_draft_test.go
T
Ilia Denisov 8881214213 R6(a): de-stage code, docs, READMEs; split stage6_test
Mechanical, behaviour-preserving removal of Stage N / TODO-N / phase (RN)
references from comments, doc-comments, service READMEs, the current-state docs
(ARCHITECTURE, FUNCTIONAL+_ru, TESTING, UI_DESIGN), config-file comments, and the
.fbs/.proto schema comments. PLAN.md / PRERELEASE.md / CLAUDE.md keep the stage
history.

- Rename the only stage-named identifiers: registerStage8 -> registerSocialOps,
  registerStage11 -> registerLinkOps (gateway transcode).
- Split stage6_test.go: TestEmailLoginFlow -> email_test.go,
  TestGuestAutoMatchLeavesNoStats (+ provisionGuest) -> account_test.go.
- Regenerated proto bindings (push.pb.go, telegram_grpc.pb.go) from the de-staged
  .proto comments; FB Go/TS bindings unchanged (flatc strips schema comments).

go build/vet/gofmt clean across modules; integration typecheck and pnpm check green.
2026-06-10 16:56:03 +02:00

83 lines
2.6 KiB
Go

package transcode_test
import (
"context"
"io"
"net/http"
"testing"
flatbuffers "github.com/google/flatbuffers/go"
"scrabble/gateway/internal/transcode"
fb "scrabble/pkg/fbs/scrabblefb"
)
// TestDraftSaveForwardsRawJSON checks the save handler forwards the client's composition JSON
// to the backend verbatim (the "no double-encode" contract) with the user header.
func TestDraftSaveForwardsRawJSON(t *testing.T) {
const body = `{"rack_order":"1,0","board_tiles":[{"row":7,"col":7,"letter":"Q","blank":false}]}`
backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut || r.URL.Path != "/api/v1/user/games/g-1/draft" {
t.Errorf("unexpected %s %s", r.Method, r.URL.Path)
}
if got := r.Header.Get("X-User-ID"); got != "u-3" {
t.Errorf("X-User-ID = %q, want u-3", got)
}
raw, _ := io.ReadAll(r.Body)
if string(raw) != body {
t.Errorf("forwarded body = %q, want %q (verbatim)", raw, body)
}
_, _ = w.Write([]byte(`{"ok":true}`))
})
defer cleanup()
reg := transcode.NewRegistry(backend, nil)
op, ok := reg.Lookup(transcode.MsgDraftSave)
if !ok {
t.Fatal("draft.save not registered")
}
b := flatbuffers.NewBuilder(64)
gid := b.CreateString("g-1")
j := b.CreateString(body)
fb.DraftRequestStart(b)
fb.DraftRequestAddGameId(b, gid)
fb.DraftRequestAddJson(b, j)
b.Finish(fb.DraftRequestEnd(b))
if _, err := op.Handler(context.Background(), transcode.Request{Payload: b.FinishedBytes(), UserID: "u-3"}); err != nil {
t.Fatalf("handler: %v", err)
}
}
// TestDraftGetWrapsBackendJSON checks the get handler wraps the backend's stored draft JSON in
// a DraftView verbatim (the gateway never interprets the shape).
func TestDraftGetWrapsBackendJSON(t *testing.T) {
const stored = `{"rack_order":"2,0,1","board_tiles":[]}`
backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet || r.URL.Path != "/api/v1/user/games/g-2/draft" {
t.Errorf("unexpected %s %s", r.Method, r.URL.Path)
}
_, _ = w.Write([]byte(stored))
})
defer cleanup()
reg := transcode.NewRegistry(backend, nil)
op, _ := reg.Lookup(transcode.MsgDraftGet)
b := flatbuffers.NewBuilder(32)
gid := b.CreateString("g-2")
fb.GameActionRequestStart(b)
fb.GameActionRequestAddGameId(b, gid)
b.Finish(fb.GameActionRequestEnd(b))
payload, err := op.Handler(context.Background(), transcode.Request{Payload: b.FinishedBytes(), UserID: "u-4"})
if err != nil {
t.Fatalf("handler: %v", err)
}
v := fb.GetRootAsDraftView(payload, 0)
if string(v.Json()) != stored {
t.Fatalf("DraftView.json = %q, want %q (verbatim)", v.Json(), stored)
}
}