package gameopen import ( "chatapp3-golang/internal/config" "chatapp3-golang/internal/integration" "context" "testing" ) type stubGateway struct { credential integration.UserCredential token string profile integration.UserProfile balance int64 exists bool cmd integration.GoldReceiptCommand } func (s *stubGateway) AuthenticateToken(context.Context, string) (integration.UserCredential, error) { return s.credential, nil } func (s *stubGateway) CreateIfAbsentUserCredentialToken(context.Context, int64) (string, error) { return s.token, nil } func (s *stubGateway) GetUserProfile(context.Context, int64) (integration.UserProfile, error) { return s.profile, nil } func (s *stubGateway) MapGoldBalance(context.Context, []int64) (map[int64]int64, error) { return map[int64]int64{int64(s.credential.UserID): s.balance}, nil } func (s *stubGateway) ExistsGoldEvent(context.Context, string) (bool, error) { return s.exists, nil } func (s *stubGateway) ChangeGoldBalance(_ context.Context, cmd integration.GoldReceiptCommand) error { s.cmd = cmd return nil } func TestHandleQueryUser(t *testing.T) { gateway := &stubGateway{ credential: integration.UserCredential{UserID: 1234567, SysOrigin: "LIKEI"}, profile: integration.UserProfile{ ID: 1234567, UserNickname: "tester", UserAvatar: "https://example.com/avatar.png", }, balance: 88, } service := NewGameOpenService(config.Config{ GameOpen: config.GameOpenConfig{AppKey: "game-open-test-key"}, }, nil, gateway) req := QueryUserRequest{ GameID: "101", UID: "1234567", Token: "token", RoomID: "room-1", } req.Sign = buildQueryUserSign(req, "game-open-test-key") resp := service.HandleQueryUser(context.Background(), req, "{}") if resp.ErrorCode != 0 { t.Fatalf("HandleQueryUser() error = %+v", resp) } data, ok := resp.Data.(map[string]any) if !ok { t.Fatalf("HandleQueryUser() data type = %T", resp.Data) } if got := data["coin"]; got != int64(88) { t.Fatalf("HandleQueryUser() coin = %v, want 88", got) } } func TestHandleUpdateCoin(t *testing.T) { gateway := &stubGateway{ credential: integration.UserCredential{UserID: 1234567, SysOrigin: "LIKEI"}, balance: 99, } service := NewGameOpenService(config.Config{ GameOpen: config.GameOpenConfig{AppKey: "game-open-test-key"}, }, nil, gateway) req := UpdateCoinRequest{ OrderID: "order-1", GameID: "101", RoundID: "round-1", UID: "1234567", Coin: 15, Type: 2, RewardType: 2, Token: "token", WinID: "win-1", RoomID: "room-1", } req.Sign = buildUpdateCoinSign(req, "game-open-test-key") resp := service.HandleUpdateCoin(context.Background(), req, "{}") if resp.ErrorCode != 0 { t.Fatalf("HandleUpdateCoin() error = %+v", resp) } if gateway.cmd.EventID != "GAME_OPEN:order-1" { t.Fatalf("HandleUpdateCoin() event id = %q", gateway.cmd.EventID) } if gateway.cmd.ReceiptType != "INCOME" { t.Fatalf("HandleUpdateCoin() receipt type = %q", gateway.cmd.ReceiptType) } } func TestGetIntegrationInfo(t *testing.T) { gateway := &stubGateway{token: "token-123"} service := NewGameOpenService(config.Config{ GameOpen: config.GameOpenConfig{ AppKey: "game-open-test-key", TestUserID: 1234567, TestAccount: "1234567", TestPassword: "1234567", }, }, nil, gateway) info, err := service.GetIntegrationInfo(context.Background(), "https://jvapi.haiyihy.com") if err != nil { t.Fatalf("GetIntegrationInfo() error = %v", err) } if info.QueryUser != "https://jvapi.haiyihy.com/game/open/user-info" { t.Fatalf("GetIntegrationInfo() query url = %q", info.QueryUser) } if info.Token != "token-123" { t.Fatalf("GetIntegrationInfo() token = %q", info.Token) } }