150 lines
5.3 KiB
Go
150 lines
5.3 KiB
Go
package luckygift
|
|
|
|
import (
|
|
"chatapp3-golang/internal/config"
|
|
"testing"
|
|
)
|
|
|
|
// TestBuildLuckyGiftSignature 校验签名生成结果稳定且符合预期。
|
|
func TestBuildLuckyGiftSignature(t *testing.T) {
|
|
payload := map[string]any{
|
|
"app_channel": "android",
|
|
"app_id": "1001",
|
|
"gift_id": int64(111),
|
|
"gift_is_free": 0,
|
|
"gift_num": int64(17),
|
|
"gift_price": int64(150),
|
|
"order_id": []string{"LG_90001", "LG_90002"},
|
|
"room_id": int64(12345),
|
|
"timestamp": int64(1713270000),
|
|
"user_id": int64(67890),
|
|
}
|
|
|
|
signature, err := buildLuckyGiftSignature(payload, "app-secret")
|
|
if err != nil {
|
|
t.Fatalf("buildLuckyGiftSignature returned error: %v", err)
|
|
}
|
|
if signature != "70d22e13f3665c3d1ac784f1008e2241" {
|
|
t.Fatalf("unexpected signature: %s", signature)
|
|
}
|
|
}
|
|
|
|
// TestParseLuckyGiftProviderResponseSuccess 校验三方正常回包解析逻辑。
|
|
func TestParseLuckyGiftProviderResponseSuccess(t *testing.T) {
|
|
raw := `{"code":200,"message":"ok","data":{"order_list":[{"order_id":"LG_90001","is_win":0,"reward_num":0},{"order_id":"LG_90002","is_win":1,"reward_num":300}]}}`
|
|
orders := []LuckyGiftDrawOrderInput{
|
|
{ID: "90001", AcceptUserID: 2001},
|
|
{ID: "90002", AcceptUserID: 2002},
|
|
}
|
|
|
|
code, results, rewardTotal, err := parseLuckyGiftProviderResponse(raw, orders)
|
|
if err != nil {
|
|
t.Fatalf("parseLuckyGiftProviderResponse returned error: %v", err)
|
|
}
|
|
if code != 200 {
|
|
t.Fatalf("unexpected provider code: %d", code)
|
|
}
|
|
if rewardTotal != 300 {
|
|
t.Fatalf("unexpected rewardTotal: %d", rewardTotal)
|
|
}
|
|
if len(results) != 2 || results[1].RewardNum != 300 || !results[1].IsWin {
|
|
t.Fatalf("unexpected results: %+v", results)
|
|
}
|
|
}
|
|
|
|
// TestParseLuckyGiftProviderResponseSupportsUppercaseOrderList 校验兼容大写字段名的回包。
|
|
func TestParseLuckyGiftProviderResponseSupportsUppercaseOrderList(t *testing.T) {
|
|
raw := `{"code":200,"docs":"成功","data":{"OrderList":[{"order_id":"LG_90001","is_win":0,"reward_num":0}]}}`
|
|
orders := []LuckyGiftDrawOrderInput{
|
|
{ID: "90001", AcceptUserID: 2001},
|
|
}
|
|
|
|
code, results, rewardTotal, err := parseLuckyGiftProviderResponse(raw, orders)
|
|
if err != nil {
|
|
t.Fatalf("parseLuckyGiftProviderResponse returned error: %v", err)
|
|
}
|
|
if code != 200 || rewardTotal != 0 {
|
|
t.Fatalf("unexpected result: code=%d rewardTotal=%d", code, rewardTotal)
|
|
}
|
|
if len(results) != 1 || results[0].OrderID != "LG_90001" {
|
|
t.Fatalf("unexpected results: %+v", results)
|
|
}
|
|
}
|
|
|
|
// TestParseLuckyGiftProviderResponseFuse 校验三方熔断码会被解释为全未中奖结果。
|
|
func TestParseLuckyGiftProviderResponseFuse(t *testing.T) {
|
|
raw := `{"code":501,"message":"fused","data":{"order_list":[]}}`
|
|
orders := []LuckyGiftDrawOrderInput{
|
|
{ID: "90001", AcceptUserID: 2001},
|
|
{ID: "90002", AcceptUserID: 2002},
|
|
}
|
|
|
|
code, results, rewardTotal, err := parseLuckyGiftProviderResponse(raw, orders)
|
|
if err != nil {
|
|
t.Fatalf("parseLuckyGiftProviderResponse returned error: %v", err)
|
|
}
|
|
if code != 501 || rewardTotal != 0 {
|
|
t.Fatalf("unexpected fuse result: code=%d rewardTotal=%d", code, rewardTotal)
|
|
}
|
|
if len(results) != 2 || results[0].RewardNum != 0 || results[1].RewardNum != 0 {
|
|
t.Fatalf("unexpected results: %+v", results)
|
|
}
|
|
}
|
|
|
|
// TestParseLuckyGiftProviderResponseOrderCountMismatch 校验订单数量不一致时报错。
|
|
func TestParseLuckyGiftProviderResponseOrderCountMismatch(t *testing.T) {
|
|
raw := `{"code":200,"message":"ok","data":{"order_list":[{"order_id":"LG_90001","is_win":1,"reward_num":100}]}}`
|
|
orders := []LuckyGiftDrawOrderInput{
|
|
{ID: "90001", AcceptUserID: 2001},
|
|
{ID: "90002", AcceptUserID: 2002},
|
|
}
|
|
|
|
if _, _, _, err := parseLuckyGiftProviderResponse(raw, orders); err == nil {
|
|
t.Fatalf("expected order count mismatch error")
|
|
}
|
|
}
|
|
|
|
// TestBuildLuckyGiftProviderPayloadUsesStringRoomAndUserIDs 校验三方请求体里的房间和用户 ID 使用字符串格式。
|
|
func TestBuildLuckyGiftProviderPayloadUsesStringRoomAndUserIDs(t *testing.T) {
|
|
payload, err := buildLuckyGiftProviderPayload(config.LuckyGiftProviderConfig{
|
|
AppID: "9291",
|
|
AppChannel: "likei",
|
|
AppKey: "shared-key",
|
|
}, LuckyGiftDrawRequest{
|
|
RoomID: 2043938895665598465,
|
|
SendUserID: 4569421795454615552,
|
|
GiftID: 2044387508480962561,
|
|
GiftPrice: 100,
|
|
GiftNum: 1,
|
|
Orders: []LuckyGiftDrawOrderInput{
|
|
{ID: "1900012345678901101", AcceptUserID: 4569421795454615552},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("buildLuckyGiftProviderPayload returned error: %v", err)
|
|
}
|
|
if _, ok := payload["room_id"].(string); !ok {
|
|
t.Fatalf("expected room_id to be string, got %T", payload["room_id"])
|
|
}
|
|
if _, ok := payload["user_id"].(string); !ok {
|
|
t.Fatalf("expected user_id to be string, got %T", payload["user_id"])
|
|
}
|
|
}
|
|
|
|
// TestFindLuckyGiftConfigFallback 校验档位未命中时会回退到默认配置。
|
|
func TestFindLuckyGiftConfigFallback(t *testing.T) {
|
|
configs := []config.LuckyGiftProviderConfig{
|
|
{StandardID: 0, URL: "https://default.example", AppID: "1", AppChannel: "android", AppKey: "k1"},
|
|
{StandardID: 18816, URL: "https://18816.example", AppID: "2", AppChannel: "ios", AppKey: "k2"},
|
|
}
|
|
|
|
cfg, ok := findLuckyGiftConfig(configs, 18816)
|
|
if !ok || cfg.URL != "https://18816.example" {
|
|
t.Fatalf("expected exact match, got ok=%v cfg=%+v", ok, cfg)
|
|
}
|
|
cfg, ok = findLuckyGiftConfig(configs, 999)
|
|
if !ok || cfg.URL != "https://default.example" {
|
|
t.Fatalf("expected fallback match, got ok=%v cfg=%+v", ok, cfg)
|
|
}
|
|
}
|