2026-04-24 14:56:07 +08:00

260 lines
8.3 KiB
Go

package baishun
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
"chatapp3-golang/internal/model"
)
func TestResolveAdminShowcase(t *testing.T) {
t.Run("create defaults to enabled", func(t *testing.T) {
if got := resolveAdminShowcase(nil, false, true); !got {
t.Fatalf("resolveAdminShowcase(nil, false, true) = %v, want true", got)
}
})
t.Run("update keeps current when omitted", func(t *testing.T) {
if got := resolveAdminShowcase(nil, false, false); got {
t.Fatalf("resolveAdminShowcase(nil, false, false) = %v, want false", got)
}
})
t.Run("explicit false remains false", func(t *testing.T) {
showcase := false
if got := resolveAdminShowcase(&showcase, true, true); got {
t.Fatalf("resolveAdminShowcase(false, true, true) = %v, want false", got)
}
})
}
func TestFetchPlatformGamesUsesConfiguredGameListURL(t *testing.T) {
service, _ := newTestBaishunService(t)
ctx := context.Background()
var gotPath string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"code":0,"message":"ok","data":[{"game_id":1021,"name":"LuckyGift","download_url":"https://cdn.example.com/lucky/index.html","preview_url":"https://cdn.example.com/lucky.png","game_version":"1.0.0","game_mode":[1],"game_orientation":1,"safe_height":0,"venue_level":[1]}]}`))
}))
defer server.Close()
service.httpClient = server.Client()
games, err := service.fetchPlatformGames(ctx, baishunRuntimeConfig{
PlatformBaseURL: "https://unused.example.com",
GameListURL: server.URL + "/custom/game-list",
AppID: 1001,
AppChannel: "test",
AppKey: "test-key",
})
if err != nil {
t.Fatalf("fetchPlatformGames() error = %v", err)
}
if gotPath != "/custom/game-list" {
t.Fatalf("request path = %q, want configured game list path", gotPath)
}
if len(games) != 1 || games[0].GameID != 1021 {
t.Fatalf("games = %+v, want fetched game 1021", games)
}
}
func TestImportAndListRoomGamesUseActiveProfile(t *testing.T) {
service, db := newTestBaishunService(t)
ctx := context.Background()
now := time.Now()
if err := db.Create(&[]model.BaishunProviderConfig{
{
ID: 1001,
SysOrigin: "LIKEI",
Profile: baishunProfileProd,
Active: true,
PlatformBaseURL: "https://prod.example.com",
AppID: 111,
AppChannel: "prod",
AppKey: "prod-key",
GSP: 101,
CreateTime: now,
UpdateTime: now,
},
{
ID: 1002,
SysOrigin: "LIKEI",
Profile: baishunProfileTest,
Active: false,
PlatformBaseURL: "https://test.example.com",
AppID: 222,
AppChannel: "test",
AppKey: "test-key",
GSP: 102,
CreateTime: now,
UpdateTime: now,
},
}).Error; err != nil {
t.Fatalf("create provider configs: %v", err)
}
if err := db.Create(&[]model.BaishunGameCatalog{
{
ID: 2001,
SysOrigin: "LIKEI",
Profile: baishunProfileProd,
InternalGameID: "bs_9001",
VendorGameID: 9001,
Name: "Prod Game",
Cover: "https://cdn.example.com/prod.png",
DownloadURL: "https://cdn.example.com/prod/index.html",
Status: baishunCatalogEnabled,
CreateTime: now,
UpdateTime: now,
},
{
ID: 2002,
SysOrigin: "LIKEI",
Profile: baishunProfileTest,
InternalGameID: "bs_9001",
VendorGameID: 9001,
Name: "Test Game",
Cover: "https://cdn.example.com/test.png",
DownloadURL: "https://cdn.example.com/test/index.html",
Status: baishunCatalogEnabled,
CreateTime: now,
UpdateTime: now,
},
}).Error; err != nil {
t.Fatalf("create catalogs: %v", err)
}
if resp, err := service.ImportCatalogGames(ctx, "LIKEI", baishunProfileProd, []int{9001}); err != nil {
t.Fatalf("import prod catalog: %v", err)
} else if resp.Imported != 1 {
t.Fatalf("prod imported = %d, want 1", resp.Imported)
}
var prodConfig model.SysGameListConfig
if err := db.Where("sys_origin = ? AND game_origin = ? AND game_id = ?", "LIKEI", baishunVendorType, "bs_9001").First(&prodConfig).Error; err != nil {
t.Fatalf("load imported prod config: %v", err)
}
if prodConfig.Category != baishunRoomCategory {
t.Fatalf("imported prod category = %q, want %q", prodConfig.Category, baishunRoomCategory)
}
if resp, err := service.ImportCatalogGames(ctx, "LIKEI", baishunProfileTest, []int{9001}); err != nil {
t.Fatalf("import test catalog: %v", err)
} else if resp.Imported != 1 {
t.Fatalf("test imported = %d, want 1", resp.Imported)
}
prodList, err := service.ListRoomGames(ctx, AuthUser{SysOrigin: "LIKEI"}, "room-1", "")
if err != nil {
t.Fatalf("list prod games: %v", err)
}
if len(prodList.Items) != 1 || prodList.Items[0].Name != "Prod Game" {
t.Fatalf("prod list = %+v, want only Prod Game", prodList.Items)
}
if _, err := service.ActivateProviderProfile(ctx, ActivateBaishunProviderProfileRequest{
SysOrigin: "LIKEI",
Profile: baishunProfileTest,
}); err != nil {
t.Fatalf("activate test profile: %v", err)
}
testList, err := service.ListRoomGames(ctx, AuthUser{SysOrigin: "LIKEI"}, "room-1", "")
if err != nil {
t.Fatalf("list test games: %v", err)
}
if len(testList.Items) != 1 || testList.Items[0].Name != "Test Game" {
t.Fatalf("test list = %+v, want only Test Game", testList.Items)
}
}
func TestCatalogAddedRequiresExistingGameConfigAndImportRepairsDanglingExt(t *testing.T) {
service, db := newTestBaishunService(t)
ctx := context.Background()
now := time.Now()
if err := db.Create(&model.BaishunGameCatalog{
ID: 3001,
SysOrigin: "LIKEI",
Profile: baishunProfileProd,
InternalGameID: "bs_1021",
VendorGameID: 1021,
Name: "LuckyGift",
Cover: "https://cdn.example.com/lucky.png",
DownloadURL: "https://cdn.example.com/lucky/index.html",
Status: baishunCatalogEnabled,
CreateTime: now,
UpdateTime: now,
}).Error; err != nil {
t.Fatalf("create catalog: %v", err)
}
if err := db.Create(&model.SysGameListVendorExt{
ID: 4001,
GameListConfigID: 999999,
SysOrigin: "LIKEI",
Profile: baishunProfileProd,
VendorType: baishunVendorType,
VendorGameID: "1021",
Enabled: true,
CreateTime: now,
UpdateTime: now,
}).Error; err != nil {
t.Fatalf("create dangling ext: %v", err)
}
if catalogAddedForVendor(t, service, ctx, "LIKEI", baishunProfileProd, 1021) {
t.Fatalf("catalog added before import = true, want false for dangling ext")
}
importResp, err := service.ImportCatalogGames(ctx, "LIKEI", baishunProfileProd, []int{1021})
if err != nil {
t.Fatalf("import catalog with dangling ext: %v", err)
}
if importResp.Imported != 1 || importResp.Existing != 0 {
t.Fatalf("import response = %+v, want imported 1 existing 0", importResp)
}
var ext model.SysGameListVendorExt
if err := db.Where("id = ?", int64(4001)).First(&ext).Error; err != nil {
t.Fatalf("load repaired ext: %v", err)
}
if ext.GameListConfigID == 999999 || ext.GameListConfigID == 0 {
t.Fatalf("ext game_list_config_id = %d, want repaired config id", ext.GameListConfigID)
}
var cfg model.SysGameListConfig
if err := db.Where("id = ? AND sys_origin = ? AND game_origin = ?", ext.GameListConfigID, "LIKEI", baishunVendorType).First(&cfg).Error; err != nil {
t.Fatalf("load repaired config: %v", err)
}
if cfg.Name != "LuckyGift" {
t.Fatalf("repaired config name = %q, want LuckyGift", cfg.Name)
}
if !catalogAddedForVendor(t, service, ctx, "LIKEI", baishunProfileProd, 1021) {
t.Fatalf("catalog added after import = false, want true")
}
}
func catalogAddedForVendor(t *testing.T, service *BaishunService, ctx context.Context, sysOrigin string, profile string, vendorGameID int) bool {
t.Helper()
var rows []struct {
AddedConfigID *int64
}
err := service.buildAdminCatalogQuery(ctx, sysOrigin, profile, "").
Where("cat.vendor_game_id = ?", vendorGameID).
Select("cfg.id AS added_config_id").
Scan(&rows).Error
if err != nil {
t.Fatalf("query catalog added flag: %v", err)
}
if len(rows) != 1 {
t.Fatalf("catalog rows = %d, want 1", len(rows))
}
return rows[0].AddedConfigID != nil && *rows[0].AddedConfigID > 0
}