package service import "testing" func testChoices(values ...map[string]string) ([]PddOptionChoice, []string, []string) { choices := make([]PddOptionChoice, 0, len(values)) for _, v := range values { k, _ := OptionKey(v) choices = append(choices, PddOptionChoice{Key: k, Label: v["color"] + " " + v["size"], Options: v}) } return choices, []string{"color", "size"}, []string{"颜色分类", "尺码"} } func TestSpecMatch_带标签样本(t *testing.T) { cases := []struct { name, raw string options []map[string]string pre bool level string }{ {"正确第一名", "灰色-小個子,L建議53-57公斤", []map[string]string{{"color": "灰色中长款", "size": "L(106-114斤)"}, {"color": "黑色", "size": "L(106-114斤)"}}, true, "A"}, {"繁简单位", "藍色,5XL建議100-120公斤", []map[string]string{{"color": "蓝色长款", "size": "5XL(200-240斤)"}}, true, "A"}, {"部分重叠", "灰色,L建議55-60公斤", []map[string]string{{"color": "灰色", "size": "L(115-130斤)"}}, false, "B"}, {"无码位无主色", "53-57公斤", []map[string]string{{"color": "", "size": "L(80-115斤)"}}, false, "C"}, {"体重冲突", "L建議52-60公斤", []map[string]string{{"color": "", "size": "L(200-240斤)"}}, false, "冲突"}, {"颜色冲突", "黑色常規款,L建議52-60公斤", []map[string]string{{"color": "白色中长款", "size": "L(104-120斤)"}}, false, "冲突"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { c, k, n := testChoices(tc.options...) r := rankSpecChoices(tc.raw, c, k, n) if len(r.Choices) == 0 || r.Choices[0].MatchLevel != tc.level || (r.PreselectOptionKey != "") != tc.pre { t.Fatalf("result=%+v", r) } }) } } func TestSpecMatch_繁简表与颜色别名逐条覆盖(t *testing.T) { for _, p := range traditionalPairs { if simplifyExplicit(p.from) != p.to { t.Fatalf("%s", p.from) } } for _, raw := range []string{"粉紅", "浅粉", "米白", "藏藍"} { if len(extractSpecFeatures(raw).colors) == 0 { t.Fatalf("%s", raw) } } if !extractSpecFeatures("粉紅色").colors["粉"] || !extractSpecFeatures("粉色系").colors["粉"] { t.Fatal("粉色别名未归一") } if !extractSpecFeatures("藏青").colors["藏青"] { t.Fatal("藏青被拆分") } } func TestSpecMatch_尺码最长优先(t *testing.T) { for raw, want := range map[string]string{"XXL": "2XL", "2XL": "2XL", "XXXXXL": "5XL", "女M码": "M", "均码": "均码", "F": "F"} { if got := extractSpecFeatures(raw).size; got != want { t.Fatalf("%s=>%s", raw, got) } } } func TestSpecMatch_多维歧义不预选(t *testing.T) { c, k, n := testChoices(map[string]string{"color": "灰", "size": "L(106-114斤)", "style": "A"}, map[string]string{"color": "灰", "size": "L(106-114斤)", "style": "B"}) k = append(k, "style") n = append(n, "款式") r := rankSpecChoices("灰色,L建議53-57公斤", c, k, n) if r.PreselectOptionKey != "" || r.Notice == "" { t.Fatalf("%+v", r) } }