Files
cmautobuy/admin/service/spec_compare_test.go
T

55 lines
2.4 KiB
Go

package service
import (
"testing"
"time"
"cmautobuy/admin/repository"
)
func TestCompareShopSpecsClassifiesDifferences(t *testing.T) {
sources := []repository.ShopSpecSource{
{ShopeeGoodsID: "S1", PDDGoodsID: "P1", PDDExists: true,
ShopeeSpecs: []repository.ComparableShopeeSpec{{Color: "黑", Size: "M", ParseOK: true}},
PDDSKUsJSON: `{"skus":[{"options":{"color":"黑","size":"M"}}]}`},
{ShopeeGoodsID: "S2"},
{ShopeeGoodsID: "S3", PDDGoodsID: "P3", PDDExists: true,
ShopeeSpecs: []repository.ComparableShopeeSpec{{Color: "白", Size: "L", ParseOK: true}},
PDDSKUsJSON: `{"skus":[{"options":{"color":"白","size":"M"}}]}`},
}
report := CompareShopSpecs(" shop ", sources, time.Date(2026, 8, 12, 0, 0, 0, 0, time.UTC))
if report.Shop != "shop" || report.Total != 3 {
t.Fatalf("报告基本信息错误: %+v", report)
}
if report.Summary[SpecCompareExact] != 1 || report.Summary[SpecCompareUnlinked] != 1 || report.Summary[SpecCompareDifferent] != 1 {
t.Fatalf("分类统计错误: %+v", report.Summary)
}
}
func TestCompareShopSpecsRejectsAmbiguousSharedPDD(t *testing.T) {
sources := []repository.ShopSpecSource{
{ShopeeGoodsID: "S1", PDDGoodsID: "P1", PDDExists: true,
ShopeeSpecs: []repository.ComparableShopeeSpec{{Color: "黑", Size: "M", ParseOK: true}},
PDDSKUsJSON: `{"skus":[{"options":{"color":"黑","size":"M"}}]}`},
{ShopeeGoodsID: "S2", PDDGoodsID: "P1", PDDExists: true,
ShopeeSpecs: []repository.ComparableShopeeSpec{{Color: "白", Size: "L", ParseOK: true}},
PDDSKUsJSON: `{"skus":[{"options":{"color":"黑","size":"M"}}]}`},
}
report := CompareShopSpecs("shop", sources, time.Now())
if report.Summary[SpecCompareAmbiguousShared] != 2 {
t.Fatalf("共用 PDD 且规格不同时应标记歧义: %+v", report)
}
}
func TestCompareShopSpecsDoesNotExposeRawSpecs(t *testing.T) {
source := repository.ShopSpecSource{ShopeeGoodsID: "S1", PDDGoodsID: "P1", PDDExists: true,
ShopeeSpecs: []repository.ComparableShopeeSpec{{Color: "秘密颜色", Size: "秘密尺码", ParseOK: true}},
PDDSKUsJSON: `{"skus":[{"options":{"color":"别的颜色","size":"别的尺码"},"price_cent":999}]}`}
report := CompareShopSpecs("shop", []repository.ShopSpecSource{source}, time.Now())
item := report.Products[0]
if item.ShopeeSpecCount != 1 || item.PDDSpecCount != 1 || item.MissingCount != 1 || item.ExtraCount != 1 {
t.Fatalf("报告只应输出计数: %+v", item)
}
}