Files
galaxy-game/pkg/transcoder/order_test.go
T
Ilia Denisov 723885e74e
Tests · UI / test (push) Has been cancelled
Tests · Go / test (push) Successful in 2m3s
Tests · Go / test (pull_request) Successful in 2m5s
Tests · Integration / integration (pull_request) Successful in 1m44s
Tests · UI / test (pull_request) Failing after 4m28s
fix(order): surface rejection reason, keep sync green, hydrate verdicts
Three issues surfaced once the per-command rejection from the previous
commit actually reached the UI:

1. Sync banner falsely red. `OrderDraftStore.runSync` flipped
   `syncStatus = "error"` whenever any command was rejected and
   advertised a Retry button. A per-command rejection is a
   player-correctable state — the round trip succeeded, the engine
   just refused that command — so the retry can't help. Keep
   `syncStatus = "synced"` on `success`; the red row highlight is
   the visible cue.

2. Rejection reason missing. Add `cmd_error_message: string` to
   `CommandItem` in `pkg/schema/fbs/order.fbs` (appended last to
   preserve existing slot offsets) and regenerate the Go + TS stubs
   for that one type. Plumb the message through `CommandMeta`,
   `Controller.applyCommand`'s `m.Result(code, message)` call, the
   Go transcoder, the UI decoders in `submit.ts` /  `order-load.ts`,
   and the `OrderDraftStore.errorMessages` map. `order-tab.svelte`
   renders it as an italic danger-coloured line under rejected
   commands, with new CSS for `.error-reason`.

3. Verdict lost on navigation. `order-load.ts.decodeCommand` never
   read `cmdApplied`/`cmdErrorCode`, so `hydrateFromServer` fell
   back to a blanket "applied" status — a previously-rejected
   command came back green after a lobby → game round trip. Extend
   the fetch decoder to populate `statuses`/`errorCodes`/
   `errorMessages` maps and have `hydrateFromServer` use them.
   Engine-side persistence already records the verdict on disk —
   verified against the live `0000/order/<id>.json`.

`flatbuffers@25` elides default-int8/int64 fields on write; the Go
transcoder force-slots `cmd_applied=false` / `cmd_error_code=0`
already, the new test fixtures flip `builder.forceDefaults(true)` to
mirror that behaviour so the round trip survives.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-29 11:42:27 +02:00

270 lines
7.2 KiB
Go

package transcoder
import (
"reflect"
"strconv"
"testing"
model "galaxy/model/order"
"github.com/google/uuid"
)
func TestUserGamesCommandPayloadRoundTrip(t *testing.T) {
t.Parallel()
source := &model.UserGamesCommand{
GameID: uuid.MustParse("11111111-2222-3333-4444-555555555555"),
Commands: []model.DecodableCommand{
&model.CommandRaceVote{CommandMeta: commandMeta("cmd-01", model.CommandTypeRaceVote, nil, nil), Acceptor: "race-a"},
&model.CommandShipGroupSend{CommandMeta: commandMeta("cmd-02", model.CommandTypeShipGroupSend, nil, nil), ID: "group-1", Destination: 7},
},
}
payload, err := UserGamesCommandToPayload(source)
if err != nil {
t.Fatalf("encode user games command: %v", err)
}
decoded, err := PayloadToUserGamesCommand(payload)
if err != nil {
t.Fatalf("decode user games command: %v", err)
}
if !reflect.DeepEqual(source, decoded) {
t.Fatalf("round-trip mismatch\nsource: %#v\ndecoded:%#v", source, decoded)
}
}
func TestUserGamesOrderPayloadRoundTrip(t *testing.T) {
t.Parallel()
source := &model.UserGamesOrder{
GameID: uuid.MustParse("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"),
UpdatedAt: 12345,
Commands: []model.DecodableCommand{
&model.CommandPlanetRename{CommandMeta: commandMeta("cmd-1", model.CommandTypePlanetRename, nil, nil), Number: 5, Name: "alpha"},
},
}
payload, err := UserGamesOrderToPayload(source)
if err != nil {
t.Fatalf("encode user games order: %v", err)
}
decoded, err := PayloadToUserGamesOrder(payload)
if err != nil {
t.Fatalf("decode user games order: %v", err)
}
if !reflect.DeepEqual(source, decoded) {
t.Fatalf("round-trip mismatch\nsource: %#v\ndecoded:%#v", source, decoded)
}
}
func TestUserGamesCommandRejectsNilAndEmpty(t *testing.T) {
t.Parallel()
if _, err := UserGamesCommandToPayload(nil); err == nil {
t.Fatalf("expected error encoding nil user games command")
}
if _, err := PayloadToUserGamesCommand(nil); err == nil {
t.Fatalf("expected error decoding empty user games command")
}
if _, err := UserGamesOrderToPayload(nil); err == nil {
t.Fatalf("expected error encoding nil user games order")
}
if _, err := PayloadToUserGamesOrder(nil); err == nil {
t.Fatalf("expected error decoding empty user games order")
}
if _, err := UserGamesOrderGetToPayload(nil); err == nil {
t.Fatalf("expected error encoding nil user games order get")
}
if _, err := PayloadToUserGamesOrderGet(nil); err == nil {
t.Fatalf("expected error decoding empty user games order get")
}
if _, _, err := PayloadToUserGamesOrderGetResponse(nil); err == nil {
t.Fatalf("expected error decoding empty user games order get response")
}
}
func TestUserGamesOrderResponsePayloadRoundTrip(t *testing.T) {
t.Parallel()
applied := true
rejected := false
errCode := 7
errMsg := "rename failed: planet does not exist"
source := &model.UserGamesOrder{
GameID: uuid.MustParse("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"),
UpdatedAt: 99,
Commands: []model.DecodableCommand{
&model.CommandPlanetRename{
CommandMeta: commandMeta("cmd-1", model.CommandTypePlanetRename, &applied, nil),
Number: 5,
Name: "alpha",
},
&model.CommandPlanetRename{
CommandMeta: commandMetaWithMsg("cmd-2", model.CommandTypePlanetRename, &rejected, &errCode, &errMsg),
Number: 6,
Name: "beta",
},
},
}
payload, err := UserGamesOrderResponseToPayload(source)
if err != nil {
t.Fatalf("encode user games order response: %v", err)
}
decoded, err := PayloadToUserGamesOrderResponse(payload)
if err != nil {
t.Fatalf("decode user games order response: %v", err)
}
if !reflect.DeepEqual(source, decoded) {
t.Fatalf("round-trip mismatch\nsource: %#v\ndecoded:%#v", source, decoded)
}
}
func TestUserGamesOrderResponseEmptyPayload(t *testing.T) {
t.Parallel()
payload, err := UserGamesOrderResponseToPayload(nil)
if err != nil {
t.Fatalf("encode empty user games order response: %v", err)
}
if len(payload) == 0 {
t.Fatal("empty envelope payload must be non-zero length")
}
decoded, err := PayloadToUserGamesOrderResponse(payload)
if err != nil {
t.Fatalf("decode empty user games order response: %v", err)
}
if decoded != nil {
t.Fatalf("empty envelope must decode to nil, got %#v", decoded)
}
}
func TestUserGamesOrderGetPayloadRoundTrip(t *testing.T) {
t.Parallel()
source := &model.UserGamesOrderGet{
GameID: uuid.MustParse("11111111-2222-3333-4444-555555555555"),
Turn: 7,
}
payload, err := UserGamesOrderGetToPayload(source)
if err != nil {
t.Fatalf("encode user games order get: %v", err)
}
decoded, err := PayloadToUserGamesOrderGet(payload)
if err != nil {
t.Fatalf("decode user games order get: %v", err)
}
if !reflect.DeepEqual(source, decoded) {
t.Fatalf("round-trip mismatch\nsource: %#v\ndecoded:%#v", source, decoded)
}
}
func TestUserGamesOrderGetRejectsNegativeTurn(t *testing.T) {
t.Parallel()
if _, err := UserGamesOrderGetToPayload(&model.UserGamesOrderGet{
GameID: uuid.MustParse("11111111-2222-3333-4444-555555555555"),
Turn: -1,
}); err == nil {
t.Fatalf("expected error encoding negative turn")
}
}
func TestUserGamesOrderGetResponseRoundTrip(t *testing.T) {
t.Parallel()
applied := true
stored := &model.UserGamesOrder{
GameID: uuid.MustParse("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"),
UpdatedAt: 1234,
Commands: []model.DecodableCommand{
&model.CommandPlanetRename{
CommandMeta: commandMeta("cmd-1", model.CommandTypePlanetRename, &applied, nil),
Number: 5,
Name: "stored",
},
},
}
payload, err := UserGamesOrderGetResponseToPayload(stored, true)
if err != nil {
t.Fatalf("encode user games order get response: %v", err)
}
decoded, found, err := PayloadToUserGamesOrderGetResponse(payload)
if err != nil {
t.Fatalf("decode user games order get response: %v", err)
}
if !found {
t.Fatal("expected found=true round-trip")
}
if !reflect.DeepEqual(stored, decoded) {
t.Fatalf("round-trip mismatch\nsource: %#v\ndecoded:%#v", stored, decoded)
}
}
func TestUserGamesOrderGetResponseNotFound(t *testing.T) {
t.Parallel()
payload, err := UserGamesOrderGetResponseToPayload(nil, false)
if err != nil {
t.Fatalf("encode not-found response: %v", err)
}
decoded, found, err := PayloadToUserGamesOrderGetResponse(payload)
if err != nil {
t.Fatalf("decode not-found response: %v", err)
}
if found {
t.Fatal("expected found=false")
}
if decoded != nil {
t.Fatalf("expected nil order, got %#v", decoded)
}
}
func TestInt64ToInt(t *testing.T) {
t.Parallel()
value, err := int64ToInt(123, "field")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if value != 123 {
t.Fatalf("unexpected int value: %d", value)
}
if strconv.IntSize == 32 {
maxInt := int(^uint(0) >> 1)
_, err = int64ToInt(int64(maxInt)+1, "field")
if err == nil {
t.Fatal("expected overflow error")
}
}
}
func commandMeta(id string, cmdType model.CommandType, applied *bool, errCode *int) model.CommandMeta {
return commandMetaWithMsg(id, cmdType, applied, errCode, nil)
}
func commandMetaWithMsg(id string, cmdType model.CommandType, applied *bool, errCode *int, errMsg *string) model.CommandMeta {
return model.CommandMeta{
CmdType: cmdType,
CmdID: id,
CmdApplied: applied,
CmdErrCode: errCode,
CmdErrMsg: errMsg,
}
}