117 lines
3.4 KiB
Go
117 lines
3.4 KiB
Go
package hotgame
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"chatapp3-golang/internal/config"
|
|
"chatapp3-golang/internal/model"
|
|
"chatapp3-golang/internal/service/gameprovider"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestSyncImportAndListRoomGames(t *testing.T) {
|
|
db := newHotgameTestDB(t)
|
|
service := NewService(config.Config{}, db, nil)
|
|
|
|
resp, err := service.SyncCatalog(context.Background(), SyncCatalogRequest{
|
|
SysOrigin: "LIKEI",
|
|
Profile: "PROD",
|
|
VendorGameIDs: []string{"1"},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("SyncCatalog() error = %v", err)
|
|
}
|
|
if resp.Inserted != 1 {
|
|
t.Fatalf("SyncCatalog() inserted = %d, want 1", resp.Inserted)
|
|
}
|
|
|
|
importResp, err := service.ImportCatalogGames(context.Background(), "LIKEI", "PROD", []string{"1"}, true)
|
|
if err != nil {
|
|
t.Fatalf("ImportCatalogGames() error = %v", err)
|
|
}
|
|
if importResp.Imported != 1 {
|
|
t.Fatalf("ImportCatalogGames() imported = %d, want 1", importResp.Imported)
|
|
}
|
|
|
|
provider := NewAppProvider(service)
|
|
list, err := provider.ListRoomGames(context.Background(), gameprovider.AuthUser{UserID: 1234567, SysOrigin: "LIKEI"}, "room-1", "")
|
|
if err != nil {
|
|
t.Fatalf("ListRoomGames() error = %v", err)
|
|
}
|
|
if len(list.Items) != 1 {
|
|
t.Fatalf("ListRoomGames() items = %d, want 1", len(list.Items))
|
|
}
|
|
item := list.Items[0]
|
|
if item.Provider != vendorType || item.ProviderGameID != "1" || item.GameID != "hg_1" {
|
|
t.Fatalf("ListRoomGames() item = %+v", item)
|
|
}
|
|
if item.LaunchParams["screenMode"] != "seven" {
|
|
t.Fatalf("ListRoomGames() screenMode = %v, want seven", item.LaunchParams["screenMode"])
|
|
}
|
|
}
|
|
|
|
func TestLaunchGameReplacesHotgameURLParams(t *testing.T) {
|
|
db := newHotgameTestDB(t)
|
|
service := NewService(config.Config{}, db, nil)
|
|
if _, err := service.SyncAdminGames(context.Background(), SyncCatalogRequest{
|
|
SysOrigin: "LIKEI",
|
|
Profile: "PROD",
|
|
VendorGameIDs: []string{"1"},
|
|
}); err != nil {
|
|
t.Fatalf("SyncAdminGames() error = %v", err)
|
|
}
|
|
|
|
provider := NewAppProvider(service)
|
|
resp, err := provider.LaunchGame(
|
|
context.Background(),
|
|
gameprovider.AuthUser{UserID: 1234567, SysOrigin: "LIKEI", Token: "token=="},
|
|
gameprovider.LaunchRequest{RoomID: "room-1", GameID: "hg_1", Params: map[string]any{"lang": "tr-TR"}},
|
|
"127.0.0.1",
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("LaunchGame() error = %v", err)
|
|
}
|
|
if !strings.Contains(resp.Entry.EntryURL, "uid=1234567") {
|
|
t.Fatalf("LaunchGame() url missing uid: %s", resp.Entry.EntryURL)
|
|
}
|
|
if !strings.Contains(resp.Entry.EntryURL, "token=token%3D%3D") {
|
|
t.Fatalf("LaunchGame() url missing escaped token: %s", resp.Entry.EntryURL)
|
|
}
|
|
if !strings.Contains(resp.Entry.EntryURL, "lang=tr") {
|
|
t.Fatalf("LaunchGame() url missing lang: %s", resp.Entry.EntryURL)
|
|
}
|
|
}
|
|
|
|
func newHotgameTestDB(t *testing.T) *gorm.DB {
|
|
t.Helper()
|
|
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatalf("open sqlite: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(
|
|
&model.HotgameProviderConfig{},
|
|
&model.HotgameGameCatalog{},
|
|
&model.SysGameListConfig{},
|
|
&model.SysGameListVendorExt{},
|
|
); err != nil {
|
|
t.Fatalf("migrate sqlite: %v", err)
|
|
}
|
|
if err := db.Create(&model.HotgameProviderConfig{
|
|
ID: 1,
|
|
SysOrigin: "LIKEI",
|
|
Profile: "PROD",
|
|
Active: true,
|
|
AppKey: "hotgame-key",
|
|
CreateTime: time.Now(),
|
|
UpdateTime: time.Now(),
|
|
}).Error; err != nil {
|
|
t.Fatalf("create provider config: %v", err)
|
|
}
|
|
return db
|
|
}
|