小店商品列表shujuok

This commit is contained in:
yml2213
2026-08-08 17:40:16 +08:00
parent 22dc50196f
commit 379a9d58d3
12 changed files with 2733 additions and 27 deletions
+80
View File
@@ -1,8 +1,10 @@
package client
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"sync/atomic"
"testing"
@@ -126,6 +128,84 @@ func TestNetworkDiagnosticIncludesActivityAndFieldNamesOnly(t *testing.T) {
}
}
func TestProductsAutomaticallyResolvesXiaodianRoleAndPaginates(t *testing.T) {
var goodsOffsets []string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/embed":
if !strings.Contains(r.Header.Get("Cookie"), "acf_uid=uid") {
t.Error("授权请求未携带斗鱼 Cookie")
}
if got := r.URL.Query().Get("token"); got != "uid_1_stk_0_ltkid" {
t.Errorf("token = %q", got)
}
_, _ = w.Write([]byte(`{"error":0,"data":{"txurlH5":"https://app.daoju.qq.com/?gameId=cjm&actId=18882&appId=bp_cf&livePlatId=douyu&code=code&timestamp=1&v=2.0&sig=sig"}}`))
case "/role":
if got := r.URL.Query().Get("code"); got != "code" {
t.Errorf("code = %q", got)
}
if got := r.URL.Query().Get("_act_id"); got != "46195" {
t.Errorf("_act_id = %q", got)
}
_, _ = w.Write([]byte(`var info = {"gameOpenId":"openid","roleId":1192083858,"type":"qq","area":1};`))
case "/goods":
offset := r.URL.Query().Get("page_begin")
goodsOffsets = append(goodsOffsets, offset)
if got := r.URL.Query().Get("areaid"); got != "2" {
t.Errorf("areaid = %q", got)
}
if got := r.URL.Query().Get("page_size"); got != "2" {
t.Errorf("page_size = %q", got)
}
items := []map[string]string{}
switch offset {
case "0":
items = []map[string]string{{"iGoodsId": "g1", "sGoodsName": "商品一", "iPrice": "10", "iGoodsLeft": "-1"}, {"iGoodsId": "g2", "sGoodsName": "商品二", "iPrice": "20", "iGoodsLeft": "3"}}
case "2":
items = []map[string]string{{"iGoodsId": "g2", "sGoodsName": "重复商品", "iPrice": "20", "iGoodsLeft": "3"}, {"iGoodsId": "g3", "sGoodsName": "商品三", "iPrice": "30", "iGoodsLeft": "0"}}
}
body, _ := json.Marshal(map[string]any{"data": map[string]any{"client_data": map[string]any{"itemsdetail": items}}})
_, _ = w.Write(append([]byte("var recommend="), append(body, ';')...))
default:
http.NotFound(w, r)
}
}))
defer server.Close()
act := &activity.Activity{
ID: "xiaodian",
DisplayName: "和平小店",
RoomID: "9263298",
ActAlias: "20260623KDQFH",
ActID: "46195",
BaseURL: server.URL,
ShopReferer: server.URL,
RoleParamURL: server.URL + "/embed",
RoleURL: server.URL + "/role",
StoreHomeURL: server.URL + "/goods",
IsXiaodian: true,
}
cli := New(act, "", &trace.Recorder{})
cli.HTTPClient = server.Client()
products, err := cli.Products("acf_uid=uid; acf_stk=stk; acf_ltkid=ltkid", types.XiaodianProductListOptions{PageSize: 2})
if err != nil {
t.Fatalf("Products() error = %v", err)
}
if len(products) != 3 {
t.Fatalf("商品数量 = %d,期望 3", len(products))
}
if products[0].CommodityID != "g1" || products[2].CommodityID != "g3" {
t.Fatalf("商品顺序 = %#v", products)
}
if products[1].Count != 3 || products[2].Score != 30 {
t.Fatalf("商品字段 = %#v", products)
}
if got := strings.Join(goodsOffsets, ","); got != strings.Join([]string{"0", strconv.Itoa(2), strconv.Itoa(4)}, ",") {
t.Fatalf("分页偏移 = %s", got)
}
}
func testCTNClient(t *testing.T, ctnURL string) *DouyuClient {
t.Helper()
act := &activity.Activity{