package main import ( "backend/Type" "backend/client" "backend/common" global_config "backend/global" "backend/middleware/alibaba" "backend/middleware/feishu" "backend/model" "backend/msg" "backend/sdk/ship/model/tuyou" "backend/util" "bytes" "context" "encoding/json" "fmt" "os" "path/filepath" "gitea.bywaystudios.com/pet_home/nacos" "sort" "strings" "testing" "time" "unicode" "github.com/joho/godotenv" "github.com/nacos-group/nacos-sdk-go/v2/vo" ) func TestMain(m *testing.M) { loadTestEnv() os.Exit(m.Run()) } func loadTestEnv() { workingDir, err := os.Getwd() if err != nil { return } for dir := workingDir; ; dir = filepath.Dir(dir) { envPath := filepath.Join(dir, ".env") if stat, statErr := os.Stat(envPath); statErr == nil && !stat.IsDir() { _ = godotenv.Load(envPath) return } parent := filepath.Dir(dir) if parent == dir { return } } } func TestGolbalConfig(t *testing.T) { global_config.InitConfig() config := global_config.GetTestChargeUidList() fmt.Printf("config:%+v\n", config) } func TestXxx1(t *testing.T) { //util.Statistics(0) //feishu.SendOperationMsg2(0) a := tuyou.ChargeRequest{ ApiVer: "2", AppID: "20659", AppInfo: `{"appId":1,"serverId":1,"orderId":"order_100100013_202511251228396eCumW","uid":100100013}`, ChargedDiamonds: "20", ChargedRmbs: "1.99", ClientID: "Android_5.00_tyGuest,facebook.googleplay.0-hall20659.googleplay.Meowment", ConsumeCoin: "20", ConsumeID: "d50b32511250000110", OrderID: "-", PlatformOrder: "e50b32511250000549", ProdCount: "1", ProdID: "TY206590059", ProdPrice: "1.99", UserID: "19263", Code: "385188b30e2924042c7ca30e3db818d7", } sign := a.Sign() fmt.Println("sign:", sign) if sign != "385188b30e2924042c7ca30e3db818d7" { fmt.Println("签名错误") } else { fmt.Println("签名正确") } } func TestXxx(t *testing.T) { statistics := model.Statistics{} statistics.AppId = 1 statistics.Emit = []string{"A"} statistics.StatisticsOrder() //controller.FeishuSendInfo2(nil) // util.GetOrderData(3) } func TestFeishu(t *testing.T) { data := struct { Title string Subtitle string Elements string }{ Title: "模拟测试", Subtitle: "模拟测试副标题", Elements: "", } s := util.ParseTmpl("./template/card.tmpl", data) fmt.Println(s) } func TestFeishu2(t *testing.T) { r := Type.NotifyData{} err := feishu.SendNotifyMsg(&r) if err != nil { fmt.Println(err) } } func TestLogin(t *testing.T) { feishu.SendNotifyMsg(&Type.NotifyData{ NotifyMsg: "登录验证失败", Host: "途游", EventName: "invalid token", Severity: "High", AlarmTime: time.Unix(time.Now().Unix(), 0).Format("2006-01-02 15:04:05"), }) // Port, Host, ServerId := util.GetUserInfo(1, 156, "test", "test") // fmt.Printf("ServerId:%d, Host:%s, Port:%d\n", ServerId, Host, Port) } func TestEncrypt(t *testing.T) { str := "3L1Ogjy4J8&EErK" enc, _ := util.Encrypt(str) fmt.Println(enc) } func TestDecrypt(t *testing.T) { str := "guositeng123" enc, _ := util.Encrypt(str) dec, _ := util.Decrypt("mz-AJj2wjQ5F6MY5hbxiqsfqcwjRj8P85w==") fmt.Printf("enc:%s\ndec:%s\n", enc, dec) } func TestAlibaba(t *testing.T) { //err := alibaba.CreateSpace() version := alibaba.DownloadFile() fmt.Println("version:", version) } func TestAlibabaRobot(t *testing.T) { title := "【meowment】订单发货成功" content := ` - 玩家账号:test_user_001 - 玩家等级:25 - 订单号:e50b32601160423c7f - 金额 :1.99 - 发货时间:2026-01-15 18:22:12 - 充值总数:10.99 元 [鼓掌][鼓掌][鼓掌] ` err := alibaba.SendStandardMsg(title, content, "green") if err != nil { fmt.Println("err:", err) } else { fmt.Println("消息发送成功") } } func TestAlibabaCard(t *testing.T) { r := Type.OrderData{ UID: 106622, OrderId: "e50b32601160423c7f", Product: "0.99", ProductName: "100钻石", EventRecovery: "2026-01-15 18:22:12", EventAge: "2026-01-15 18:22:15", } err := alibaba.SendOrderMsg(&r) if err != nil { fmt.Println("err:", err) } else { fmt.Println("卡片发送成功") } } func TestServerUpdateApp(t *testing.T) { log := model.Log{ AppId: 0, Uid: 105372, CurrentPage: 1, PageSize: 20, } r, e := log.Order() if e != nil { fmt.Print(e) } fmt.Print(r) } func TestXxxx(t *testing.T) { ctx := context.Background() client, _ := util.GetEsClient() var buf bytes.Buffer query := map[string]interface{}{ "query": map[string]interface{}{ "wildcard": map[string]interface{}{ "log_message": map[string]interface{}{ "value": "*set kv key 100*", }, }, }, "size": 10000, } if err := json.NewEncoder(&buf).Encode(query); err != nil { return } res, err := client.Search( client.Search.WithContext(ctx), client.Search.WithIndex(".ds-game-node-log*"), client.Search.WithBody(&buf), ) if err != nil { fmt.Print(err) } defer res.Body.Close() var result map[string]interface{} if err := json.NewDecoder(res.Body).Decode(&result); err != nil { return } // player 106910 set kv key 48 value hits := result["hits"].(map[string]interface{})["hits"].([]interface{}) tmap := make(map[string]int) for _, hit := range hits { source := hit.(map[string]interface{})["_source"] log_message := source.(map[string]interface{})["log_message"] fmt.Println(log_message) a := strings.Split(log_message.(string), " ")[1] tmap[a]++ } // 将 map 转换为切片进行排序 type kv struct { Key string Value int } var sorted []kv for k, v := range tmap { sorted = append(sorted, kv{k, v}) } // 按值降序排序 sort.Slice(sorted, func(i, j int) bool { return sorted[i].Value > sorted[j].Value }) for _, item := range sorted { if item.Value < 100 { //continue } fmt.Printf("msg type:%s, 次数:%d\n", item.Key, item.Value) } } func TestEs2(t *testing.T) { s := &model.Server{AppId: 2, ServerId: 1} _, err := s.StartServer() if err != nil { fmt.Println("err:", err) } else { fmt.Println("服务器启动成功") } } func TestIP(t *testing.T) { country, city, err := util.GetGeoInfo("58.23.48.244") if err != nil { fmt.Println("获取地理位置信息失败:", err) } else { fmt.Printf("国家代码: %s, 城市: %s\n", country, city) } } func TestRegister(t *testing.T) { ctx := context.Background() client, _ := util.GetEsClient() var buf bytes.Buffer query := map[string]interface{}{ "query": map[string]interface{}{ "term": map[string]interface{}{ "game.#event_name": map[string]interface{}{ "value": "register", }, }, }, "size": 10000, } if err := json.NewEncoder(&buf).Encode(query); err != nil { return } res, err := client.Search( client.Search.WithContext(ctx), client.Search.WithIndex(".ds-game-node-log*"), client.Search.WithBody(&buf), client.Search.WithTrackTotalHits(true), ) if err != nil { fmt.Print(err) } defer res.Body.Close() } func TestMonitor(t *testing.T) { curCount, yCount, err := util.CountDistinctUidLastHour() if err != nil { fmt.Println("Error counting distinct UIDs:", err) return } fmt.Printf("curCount:%d, yCount:%d", curCount, yCount) } func TestDsSql(t *testing.T) { ctx := context.Background() client, _ := util.GetEsClient() var buf bytes.Buffer query := map[string]interface{}{ "query": map[string]interface{}{ "bool": map[string]interface{}{ "must": []map[string]interface{}{ { "term": map[string]interface{}{ "fields.region.keyword": "us-newyork", }, }, { "term": map[string]interface{}{ "game.#event_name": "func_exec_error", }, }, { "term": map[string]interface{}{ "game.properties.error_info.keyword": "棋子数据不一致", }, }, { "range": map[string]interface{}{ "game.#timestamp": map[string]interface{}{ "gte": 1774454400, "lte": 1774627200, }, }, }, }, }, }, "size": 10000, } if err := json.NewEncoder(&buf).Encode(query); err != nil { return } res, err := client.Search( client.Search.WithContext(ctx), client.Search.WithIndex("game-user-log*"), client.Search.WithBody(&buf), ) if err != nil { fmt.Print(err) } defer res.Body.Close() var result map[string]interface{} if err := json.NewDecoder(res.Body).Decode(&result); err != nil { return } // player 106910 set kv key 48 value hits := result["hits"].(map[string]interface{})["hits"].([]interface{}) uids := map[string]struct{}{} for _, hit := range hits { source := hit.(map[string]interface{})["_source"] game := source.(map[string]interface{})["game"].(map[string]interface{}) uid := game["#distinct_id"].(string) uids[uid] = struct{}{} timestamp := int64(game["#timestamp"].(float64)) fmt.Printf("uid:%s timestamp:%d------\n", uid, timestamp) query := map[string]interface{}{ "query": map[string]interface{}{ "bool": map[string]interface{}{ "must": []map[string]interface{}{ { "term": map[string]interface{}{ "fields.region.keyword": "us-newyork", }, }, { "term": map[string]interface{}{ "game.#distinct_id": uid, }, }, { "range": map[string]interface{}{ "game.#timestamp": map[string]interface{}{ "gte": timestamp - 1, "lte": timestamp + 1, }, }, }, }, }, }, "size": 10000, } if err := json.NewEncoder(&buf).Encode(query); err != nil { return } res, err := client.Search( client.Search.WithContext(ctx), client.Search.WithIndex("game-user-log*"), client.Search.WithBody(&buf), ) if err != nil { fmt.Print(err) } var result map[string]interface{} if err := json.NewDecoder(res.Body).Decode(&result); err != nil { return } hits2 := result["hits"].(map[string]interface{})["hits"].([]interface{}) for _, hit2 := range hits2 { source2 := hit2.(map[string]interface{})["_source"] times2 := source2.(map[string]interface{})["@timestamp"].(string) game2 := source2.(map[string]interface{})["game"].(map[string]interface{}) log_message := game2["properties_summary"] event_name := game2["#event_name"] fmt.Println(times2, event_name, log_message) } } for uid := range uids { fmt.Println("distinct_id:", uid) } } func CountDisplayLen(s string) int { total := 0 for _, r := range s { if unicode.In(r, unicode.Han) { total += 2 continue } total += 1 } return total } func TestWordLen(t *testing.T) { str := "Marshmallow Roasting" fmt.Printf("字节长度: %d\n", len(str)) fmt.Printf("显示长度(中文2,非中文1): %d\n", CountDisplayLen(str)) } func TestNacos(t *testing.T) { client, err := nacos.NewClient(nacos.Options{ Servers: []nacos.Server{ {IPAddr: "ga-sdk.bywaystudios.com", Port: 8848}, }, Username: "nacos", Password: "wC:7Coqh*IDs;2wi", NamespaceID: "meowment", TimeoutMs: 5000, LogDir: "./log/nacos", CacheDir: "./runtime/nacos", LogLevel: "info", NotLoadCacheAtStart: true, DefaultGroup: "server", }) if err != nil { fmt.Println("Error creating Nacos client:", err) return } res, err := client.ListConfigs(vo.SearchConfigParam{ Search: "blur", Group: "server", }) if err != nil { fmt.Println("Error listing configs from Nacos:", err) return } fmt.Printf("Config data: %+v\n", res) data := make(map[string]interface{}) err = client.GetYAML("mysql", "server", &data) if err != nil { fmt.Println("Error getting YAML config from Nacos:", err) return } fmt.Printf("MySQL Config: %+v\n", data) } func TestServerInfo(t *testing.T) { start := time.Now() resp, err := client.GetServerInfo(1, 0, &msg.ReqServerInfo{}) if err != nil { fmt.Printf("获取服务器信息失败: %v\n", err) return } latency := time.Since(start).Milliseconds() fmt.Printf("服务器响应时间: %d ms\n", latency) fmt.Printf("服务器信息: %+v\n", resp) } func TestLoginCountByMonth(t *testing.T) { log := model.Log{ AppId: 0, Uid: 132889, CurrentPage: 1, PageSize: 20, Month: "2026-04", } r, e := log.LoginCountByMonth() if e != nil { fmt.Print(e) } fmt.Print(r) } func TestAlibabaOss(t *testing.T) { err := common.Init() if err != nil { fmt.Println("Error initializing common:", err) return } content := ` { "schedules": [ { "id": 10001, "scheduleType": 0, "deltaTime": { "ticks": 1728000000000 }, "repeats": false, "fireTime": { "ticks": 639086752456801235 }, "repeatInterval": 0, "cancelType": 1, "cancelValue": 3 }, { "id": 10002, "scheduleType": 1, "deltaTime": { "ticks": 0 }, "repeats": false, "fireTime": { "ticks": 639337539300000000 }, "repeatInterval": 0, "cancelType": 0, "cancelValue": 0 }, { "id": 10000, "scheduleType": 0, "deltaTime": { "ticks": 1200000000 }, "repeats": true, "fireTime": { "ticks": 639086752456846041 }, "repeatInterval": 0, "cancelType": 1, "cancelValue": 0 } ], "presentations": [ { "id": 90001, "titles": [ { "language": 10, "locText": "Merry Christmas", "param": 0 }, { "language": 40, "locText": "圣诞快乐", "param": 0 } ], "infos": [ { "language": 10, "locText": "Merry Christmas!", "param": 0 }, { "language": 40, "locText": "圣诞快乐!", "param": 0 } ] }, { "id": 90002, "titles": [ { "language": 10, "locText": "{0} is waiting for you to come home", "param": 1 }, { "language": 40, "locText": "{0}正在等你回家", "param": 1 } ], "infos": [ { "language": 10, "locText": "{0} is hungry and misses you - come back soon!", "param": 1 }, { "language": 40, "locText": "{0}肚子饿了,很想你,快回来看看吧", "param": 1 } ] }, { "id": 90000, "titles": [ { "language": 10, "locText": "Test", "param": 0 }, { "language": 40, "locText": "测试", "param": 0 } ], "infos": [ { "language": 10, "locText": "UserName: {0} is testing", "param": 2 }, { "language": 40, "locText": "用户{0}测试中", "param": 2 } ] } ], "activeNotis": [ { "id": 60000000, "scheduleId": 10000, "conditionId": 40001, "presentationId": 90000 } ], "inactiveNotis": [ { "id": 60000001, "scheduleId": 10001, "conditionId": 40002, "presentationId": 90002 }, { "id": 60000002, "scheduleId": 10002, "conditionId": 40001, "presentationId": 90001 } ] } ` alibaba.Osscreate(content) }