119 lines
3.2 KiB
Go
119 lines
3.2 KiB
Go
package baishun
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"chatapp3-golang/internal/config"
|
|
"chatapp3-golang/internal/integration"
|
|
"chatapp3-golang/internal/model"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type stubGateway struct{}
|
|
|
|
func (stubGateway) GetUserProfile(context.Context, int64) (integration.UserProfile, error) {
|
|
return integration.UserProfile{}, nil
|
|
}
|
|
|
|
func (stubGateway) MapGoldBalance(context.Context, []int64) (map[int64]int64, error) {
|
|
return map[int64]int64{}, nil
|
|
}
|
|
|
|
func (stubGateway) GetUserLanguage(context.Context, int64) (string, error) {
|
|
return "en", nil
|
|
}
|
|
|
|
func (stubGateway) ExistsGoldEvent(context.Context, string) (bool, error) {
|
|
return false, nil
|
|
}
|
|
|
|
func (stubGateway) ChangeGoldBalance(context.Context, integration.GoldReceiptCommand) error {
|
|
return nil
|
|
}
|
|
|
|
func newTestBaishunService(t *testing.T) (*BaishunService, *gorm.DB) {
|
|
t.Helper()
|
|
|
|
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatalf("open sqlite: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(
|
|
&model.SysGameListConfig{},
|
|
&model.SysGameListVendorExt{},
|
|
&model.BaishunProviderConfig{},
|
|
&model.BaishunGameCatalog{},
|
|
&model.BaishunLaunchSession{},
|
|
&model.BaishunRoomState{},
|
|
); err != nil {
|
|
t.Fatalf("auto migrate: %v", err)
|
|
}
|
|
|
|
service := NewBaishunService(config.Config{
|
|
Baishun: config.BaishunConfig{
|
|
PlatformBaseURL: "https://mesh-channels-online.jieyou.shop",
|
|
AppID: 7485318192,
|
|
AppName: "yumiparty_app",
|
|
AppChannel: "yumiparty",
|
|
AppKey: "test-app-key",
|
|
GSP: 111,
|
|
LaunchCodeTTLSeconds: 300,
|
|
SSTokenTTLSeconds: 86400,
|
|
},
|
|
}, db, nil, stubGateway{})
|
|
return service, db
|
|
}
|
|
|
|
func TestLaunchGameFallsBackToCatalogWhenConfigMissing(t *testing.T) {
|
|
service, db := newTestBaishunService(t)
|
|
|
|
if err := db.Create(&model.BaishunGameCatalog{
|
|
ID: 1,
|
|
SysOrigin: "LIKEI",
|
|
InternalGameID: "bs_1146",
|
|
VendorGameID: 1146,
|
|
Name: "LordOfOlympus",
|
|
Cover: "https://cdn.example.com/cover.png",
|
|
PreviewURL: "https://cdn.example.com/preview.png",
|
|
DownloadURL: "https://cdn.example.com/game/index.html",
|
|
PackageVersion: "1.0.0",
|
|
SafeHeight: 24,
|
|
GSP: "222",
|
|
Status: baishunCatalogEnabled,
|
|
}).Error; err != nil {
|
|
t.Fatalf("create catalog: %v", err)
|
|
}
|
|
|
|
resp, err := service.LaunchGame(context.Background(), AuthUser{
|
|
UserID: 1234567,
|
|
SysOrigin: "LIKEI",
|
|
}, BaishunLaunchAppRequest{
|
|
RoomID: "room-1",
|
|
GameID: "bs_1146",
|
|
SceneMode: 1,
|
|
ClientOrigin: "COMMON",
|
|
}, "127.0.0.1")
|
|
if err != nil {
|
|
t.Fatalf("LaunchGame() error = %v", err)
|
|
}
|
|
|
|
if resp.GameID != "bs_1146" {
|
|
t.Fatalf("gameId = %q, want bs_1146", resp.GameID)
|
|
}
|
|
if resp.VendorGameID != 1146 {
|
|
t.Fatalf("vendorGameId = %d, want 1146", resp.VendorGameID)
|
|
}
|
|
if resp.Entry.EntryURL != "https://cdn.example.com/game/index.html" {
|
|
t.Fatalf("entryUrl = %q, want catalog download url", resp.Entry.EntryURL)
|
|
}
|
|
if resp.BridgeConfig.GSP != 222 {
|
|
t.Fatalf("gsp = %d, want 222", resp.BridgeConfig.GSP)
|
|
}
|
|
if resp.RoomState.State != baishunRoomStatePlaying {
|
|
t.Fatalf("roomState.state = %q, want %q", resp.RoomState.State, baishunRoomStatePlaying)
|
|
}
|
|
}
|