Files
galaxy-game/pkg/transcoder/order_test.go
T
2026-05-07 00:58:53 +03:00

110 lines
2.8 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")
}
}
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 model.CommandMeta{
CmdType: cmdType,
CmdID: id,
CmdApplied: applied,
CmdErrCode: errCode,
}
}