功能:接入乐玩开放平台双渠道路由,并修复渠道锁定与 Webhook 可靠性
在保持 DLC 默认零破坏的前提下,支持 priority/仅乐玩/仅 DLC 履约策略与后台 GUI 配置;同步补齐 Prepare 渠道落库、乐玩订单取消、Webhook 验签与失败重试。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"affiliate_dash/internal/model"
|
||||
)
|
||||
|
||||
func TestParseProductRoutingConfig(t *testing.T) {
|
||||
// Case 1: 存量老商品未配置,根据 SKU 默认走 DLC
|
||||
cfg1 := ParseProductRoutingConfig("", "suit_pink_sheep")
|
||||
if cfg1.RoutingMode != RoutingModeDLC {
|
||||
t.Fatalf("expected RoutingModeDLC, got %s", cfg1.RoutingMode)
|
||||
}
|
||||
if cfg1.DLCGoodID != "682ef3c5a8f40c4234c59f4d" {
|
||||
t.Fatalf("expected builtin goodID 682ef3c5a8f40c4234c59f4d, got %s", cfg1.DLCGoodID)
|
||||
}
|
||||
|
||||
// Case 2: 仅配置乐玩
|
||||
cfg2JSON := `{"routing_mode":"lewan","lewan_product_id":"uuid-001","delivery_type":"LINK_BINDING"}`
|
||||
cfg2 := ParseProductRoutingConfig(cfg2JSON, "any_sku")
|
||||
if cfg2.RoutingMode != RoutingModeLewan {
|
||||
t.Fatalf("expected RoutingModeLewan, got %s", cfg2.RoutingMode)
|
||||
}
|
||||
if cfg2.LewanProductID != "uuid-001" {
|
||||
t.Fatalf("expected lewan_product_id uuid-001, got %s", cfg2.LewanProductID)
|
||||
}
|
||||
|
||||
// Case 3: 双平台优先级 (乐玩优先)
|
||||
cfg3JSON := `{"routing_mode":"priority","priority":["lewan","dlc"],"dlc_good_id":"dlc-123","lewan_product_id":"uuid-123"}`
|
||||
cfg3 := ParseProductRoutingConfig(cfg3JSON, "any_sku")
|
||||
if cfg3.RoutingMode != RoutingModePriority {
|
||||
t.Fatalf("expected RoutingModePriority, got %s", cfg3.RoutingMode)
|
||||
}
|
||||
if len(cfg3.Priority) != 2 || cfg3.Priority[0] != ProviderLewan || cfg3.Priority[1] != ProviderDLC {
|
||||
t.Fatalf("expected priority [lewan, dlc], got %+v", cfg3.Priority)
|
||||
}
|
||||
if cfg3.DLCGoodID != "dlc-123" || cfg3.LewanProductID != "uuid-123" {
|
||||
t.Fatalf("mismatched IDs: dlc=%s lewan=%s", cfg3.DLCGoodID, cfg3.LewanProductID)
|
||||
}
|
||||
|
||||
// Case 4: 兼容历史 legacy 单 provider 字段
|
||||
cfg4JSON := `{"provider":"lewan","lewan_product_id":"uuid-999"}`
|
||||
cfg4 := ParseProductRoutingConfig(cfg4JSON, "any_sku")
|
||||
if cfg4.RoutingMode != RoutingModeLewan || cfg4.LewanProductID != "uuid-999" {
|
||||
t.Fatalf("legacy lewan format failed: %+v", cfg4)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFulfillmentRouterResolution(t *testing.T) {
|
||||
dlc := NewDLCProvider("https://bff.example", "dlc", nil)
|
||||
lewan := NewLewanProvider(nil)
|
||||
router := NewFulfillmentRouter(dlc, lewan)
|
||||
|
||||
// Case 1: 存量 DLC 商品
|
||||
orderDLC := &model.FulfillmentOrder{
|
||||
ProductSKU: "suit_alan_walker",
|
||||
}
|
||||
providers, _ := router.ResolveProviders(orderDLC)
|
||||
if len(providers) != 1 || providers[0].Name() != ProviderDLC {
|
||||
t.Fatalf("expected DLC provider for suit_alan_walker, got %d providers", len(providers))
|
||||
}
|
||||
|
||||
// Case 2: 仅乐玩商品
|
||||
orderLewan := &model.FulfillmentOrder{
|
||||
ProductSKU: "custom_lewan_item",
|
||||
MerchantProduct: &model.MerchantProduct{
|
||||
FulfillmentConfig: `{"routing_mode":"lewan","lewan_product_id":"uuid-001"}`,
|
||||
},
|
||||
}
|
||||
providers, _ = router.ResolveProviders(orderLewan)
|
||||
if len(providers) != 1 || providers[0].Name() != ProviderLewan {
|
||||
t.Fatalf("expected Lewan provider, got %d providers", len(providers))
|
||||
}
|
||||
|
||||
// Case 3: 优先级路由 [lewan, dlc]
|
||||
orderPriority := &model.FulfillmentOrder{
|
||||
ProductSKU: "shared_item",
|
||||
MerchantProduct: &model.MerchantProduct{
|
||||
FulfillmentConfig: `{"routing_mode":"priority","priority":["lewan","dlc"],"lewan_product_id":"uuid-001","dlc_good_id":"dlc-001"}`,
|
||||
},
|
||||
}
|
||||
providers, _ = router.ResolveProviders(orderPriority)
|
||||
if len(providers) != 2 || providers[0].Name() != ProviderLewan || providers[1].Name() != ProviderDLC {
|
||||
t.Fatalf("expected [lewan, dlc], got %v", providers)
|
||||
}
|
||||
|
||||
// Case 4: 订单已锁定渠道(已在 result_data 中写明 provider=dlc)
|
||||
orderLocked := &model.FulfillmentOrder{
|
||||
ProductSKU: "shared_item",
|
||||
MerchantProduct: &model.MerchantProduct{
|
||||
FulfillmentConfig: `{"routing_mode":"priority","priority":["lewan","dlc"],"lewan_product_id":"uuid-001","dlc_good_id":"dlc-001"}`,
|
||||
},
|
||||
ResultData: `{"provider":"dlc"}`,
|
||||
}
|
||||
providers, _ = router.ResolveProviders(orderLocked)
|
||||
if len(providers) != 1 || providers[0].Name() != ProviderDLC {
|
||||
t.Fatalf("expected locked DLC provider, got %+v", providers)
|
||||
}
|
||||
|
||||
// Case 5: 优先级 [dlc, lewan]
|
||||
orderDLCFirst := &model.FulfillmentOrder{
|
||||
ProductSKU: "shared_item",
|
||||
MerchantProduct: &model.MerchantProduct{
|
||||
FulfillmentConfig: `{"routing_mode":"priority","priority":["dlc","lewan"],"lewan_product_id":"uuid-001","dlc_good_id":"dlc-001"}`,
|
||||
},
|
||||
}
|
||||
providers, _ = router.ResolveProviders(orderDLCFirst)
|
||||
if len(providers) != 2 || providers[0].Name() != ProviderDLC || providers[1].Name() != ProviderLewan {
|
||||
t.Fatalf("expected [dlc, lewan], got %v", providerNames(providers))
|
||||
}
|
||||
}
|
||||
|
||||
func providerNames(providers []FulfillmentProvider) []string {
|
||||
names := make([]string, 0, len(providers))
|
||||
for _, p := range providers {
|
||||
names = append(names, p.Name())
|
||||
}
|
||||
return names
|
||||
}
|
||||
Reference in New Issue
Block a user