182 lines
6.8 KiB
Go
182 lines
6.8 KiB
Go
package integration
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"chatapp3-golang/internal/config"
|
|
)
|
|
|
|
func TestUserTotalRechargeAcceptsNumericTimestamps(t *testing.T) {
|
|
var item UserTotalRecharge
|
|
raw := []byte(`{
|
|
"id":"2049097310624870401",
|
|
"userId":"2042274349343506434",
|
|
"type":"SHIPPING_AGENT",
|
|
"amount":1.60,
|
|
"createTime":1777377844000,
|
|
"updateTime":1777556451000
|
|
}`)
|
|
|
|
if err := json.Unmarshal(raw, &item); err != nil {
|
|
t.Fatalf("json.Unmarshal() error = %v", err)
|
|
}
|
|
if item.CreateTime != "1777377844000" {
|
|
t.Fatalf("CreateTime = %q, want numeric timestamp string", item.CreateTime)
|
|
}
|
|
if item.UpdateTime != "1777556451000" {
|
|
t.Fatalf("UpdateTime = %q, want numeric timestamp string", item.UpdateTime)
|
|
}
|
|
if item.Amount != "1.60" {
|
|
t.Fatalf("Amount = %q, want 1.60", item.Amount)
|
|
}
|
|
}
|
|
|
|
func TestResolveRegionCodeByCountryCodeUsesRegionConfig(t *testing.T) {
|
|
calls := 0
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
calls++
|
|
if r.Method != http.MethodPost || r.URL.Path != "/region-config/client/listByCondition" {
|
|
t.Fatalf("request = %s %s, want POST /region-config/client/listByCondition", r.Method, r.URL.Path)
|
|
}
|
|
var payload map[string]string
|
|
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
|
t.Fatalf("decode request: %v", err)
|
|
}
|
|
if payload["sysOrigin"] != "LIKEI" {
|
|
t.Fatalf("sysOrigin = %q, want LIKEI", payload["sysOrigin"])
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"body":[{"regionCode":"中东区","sysOrigin":"LIKEI","countryCodes":"JO, EG, KSA","del":false},{"regionCode":"US","sysOrigin":"LIKEI","countryCodes":"US","del":true}]}`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := New(config.Config{
|
|
HTTP: config.HTTPConfig{Timeout: time.Second},
|
|
Java: config.JavaConfig{OtherBaseURL: server.URL},
|
|
})
|
|
got, err := client.ResolveRegionCodeByCountryCode(context.Background(), " likei ", " ksa ")
|
|
if err != nil {
|
|
t.Fatalf("ResolveRegionCodeByCountryCode() error = %v", err)
|
|
}
|
|
if got != "中东区" {
|
|
t.Fatalf("region = %q, want 中东区", got)
|
|
}
|
|
got, err = client.ResolveRegionCodeByCountryCode(context.Background(), "LIKEI", "eg")
|
|
if err != nil {
|
|
t.Fatalf("ResolveRegionCodeByCountryCode() cached error = %v", err)
|
|
}
|
|
if got != "中东区" {
|
|
t.Fatalf("cached region = %q, want 中东区", got)
|
|
}
|
|
if calls != 1 {
|
|
t.Fatalf("region config calls = %d, want cached 1", calls)
|
|
}
|
|
}
|
|
|
|
func TestGetRewardGroupDetailAcceptsDataWrapper(t *testing.T) {
|
|
groupID := int64(2056981527522242600)
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet || r.URL.Path != "/props-activity/reward/group/client/getByGroupId" {
|
|
t.Fatalf("request = %s %s, want GET /props-activity/reward/group/client/getByGroupId", r.Method, r.URL.Path)
|
|
}
|
|
if r.URL.Query().Get("id") != "2056981527522242600" {
|
|
t.Fatalf("id query = %q", r.URL.Query().Get("id"))
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"data":{"id":"2056981527522242600","name":"累计充值10美金","rewardConfigList":[{"id":"1","type":"PROPS","detailType":"AVATAR_FRAME","name":"Frame","quantity":"1","sourceUrl":"https://example.com/frame.svga"}]}}`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := New(config.Config{
|
|
HTTP: config.HTTPConfig{Timeout: time.Second},
|
|
Java: config.JavaConfig{OtherBaseURL: server.URL},
|
|
})
|
|
got, err := client.GetRewardGroupDetail(context.Background(), groupID)
|
|
if err != nil {
|
|
t.Fatalf("GetRewardGroupDetail() error = %v", err)
|
|
}
|
|
if int64(got.ID) != groupID || len(got.RewardConfigList) != 1 {
|
|
t.Fatalf("detail = %+v, want group with one reward item", got)
|
|
}
|
|
}
|
|
|
|
func TestGetRewardGroupDetailFallsBackToConsoleEndpoint(t *testing.T) {
|
|
groupID := int64(2056981527522242600)
|
|
var innerCalls, consoleCalls int
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/props-activity/reward/group/client/getByGroupId", func(w http.ResponseWriter, r *http.Request) {
|
|
innerCalls++
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"body":{"id":"2056981527522242600","name":"累计充值10美金","rewardConfigList":[]}}`))
|
|
})
|
|
mux.HandleFunc("/console/props/activity/reward/group/2056981527522242600", func(w http.ResponseWriter, r *http.Request) {
|
|
consoleCalls++
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"id":"2056981527522242600","name":"累计充值10美金","rewardConfigList":[{"id":"1","type":"GIFT","name":"Gift","quantity":"1","cover":"https://example.com/gift.png"}]}`))
|
|
})
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
client := New(config.Config{
|
|
HTTP: config.HTTPConfig{Timeout: time.Second},
|
|
Java: config.JavaConfig{
|
|
OtherBaseURL: server.URL,
|
|
ConsoleBaseURL: server.URL + "/console",
|
|
},
|
|
})
|
|
got, err := client.GetRewardGroupDetail(context.Background(), groupID)
|
|
if err != nil {
|
|
t.Fatalf("GetRewardGroupDetail() error = %v", err)
|
|
}
|
|
if innerCalls != 1 || consoleCalls != 1 {
|
|
t.Fatalf("calls inner=%d console=%d, want 1/1", innerCalls, consoleCalls)
|
|
}
|
|
if len(got.RewardConfigList) != 1 || got.RewardConfigList[0].Name != "Gift" {
|
|
t.Fatalf("detail = %+v, want fallback reward item", got)
|
|
}
|
|
}
|
|
|
|
func TestFindRewardGroupDetailHydratesPageRecord(t *testing.T) {
|
|
groupID := int64(2056980937278816399)
|
|
var pageCalls, detailCalls int
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/props-activity/reward/group/client/page", func(w http.ResponseWriter, r *http.Request) {
|
|
pageCalls++
|
|
if r.Method != http.MethodPost {
|
|
t.Fatalf("method = %s, want POST", r.Method)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"body":{"records":[{"id":"2056980937278816399","name":"累充活动$100","rewardConfigList":[]}]}}`))
|
|
})
|
|
mux.HandleFunc("/props-activity/reward/group/client/getByGroupId", func(w http.ResponseWriter, r *http.Request) {
|
|
detailCalls++
|
|
if r.URL.Query().Get("id") != "2056980937278816399" {
|
|
t.Fatalf("id query = %q", r.URL.Query().Get("id"))
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"body":{"id":"2056980937278816399","name":"累充活动$100","rewardConfigList":[{"id":"1","type":"BADGE","name":"Badge","quantity":"30","cover":"https://example.com/badge.png"}]}}`))
|
|
})
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
client := New(config.Config{
|
|
HTTP: config.HTTPConfig{Timeout: time.Second},
|
|
Java: config.JavaConfig{OtherBaseURL: server.URL},
|
|
})
|
|
got, err := client.FindRewardGroupDetail(context.Background(), "LIKEI", "累充活动$100")
|
|
if err != nil {
|
|
t.Fatalf("FindRewardGroupDetail() error = %v", err)
|
|
}
|
|
if pageCalls != 1 || detailCalls != 1 {
|
|
t.Fatalf("calls page=%d detail=%d, want 1/1", pageCalls, detailCalls)
|
|
}
|
|
if int64(got.ID) != groupID || len(got.RewardConfigList) != 1 {
|
|
t.Fatalf("detail = %+v, want hydrated reward group", got)
|
|
}
|
|
}
|