package feeds
import (
"strings"
"testing"
"github.com/google/uuid"
)
var (
feedA = uuid.MustParse("00000000-0000-0000-0000-00000000000a")
feedB = uuid.MustParse("00000000-0000-0000-0000-00000000000b")
feedC = uuid.MustParse("00000000-0000-0000-0000-00000000000c")
)
func TestMergeFeedRows_nonConflictingFieldsAreCombined(t *testing.T) {
a := feedRowInput{FeedID: feedA, FeedName: "Vendor A", Mapped: map[string]any{
"gtin": "4548736180307", "name": "Sony TV 65", "brand": "Sony",
}}
b := feedRowInput{FeedID: feedB, FeedName: "Vendor B", Mapped: map[string]any{
"gtin": "4548736180307", "main_image": "https://img/x.jpg", "eprel_id": "2651553",
}}
got, src := mergeFeedRows([]feedRowInput{a, b})
for k, want := range map[string]string{
"name": "Sony TV 65", "brand": "Sony", "main_image": "https://img/x.jpg", "eprel_id": "2651553",
} {
if got[k] != want {
t.Errorf("%s = %v, want %q", k, got[k], want)
}
}
if src.Fields["eprel_id"].FeedName != "Vendor B" || src.Fields["brand"].FeedName != "Vendor A" {
t.Errorf("wrong sources: %+v", src.Fields)
}
if src.Fields["name"].Conflict {
t.Error("a field only one feed has must not be a conflict")
}
if len(src.Feeds) != 2 {
t.Errorf("feeds = %v, want both feeds", src.Feeds)
}
}
func TestMergeFeedRows_longestDescriptionWins(t *testing.T) {
short := feedRowInput{FeedID: feedA, FeedName: "A", Mapped: map[string]any{
"name": "TV", "brand": "Sony", "stock": "5", "price": "9", "description": "Short.",
}}
long := feedRowInput{FeedID: feedB, FeedName: "B", Mapped: map[string]any{
"name": "TV", "description": "
" + strings.Repeat("Ostra 4K slika z Google TV in HDR. ", 20) + "
",
}}
got, src := mergeFeedRows([]feedRowInput{short, long})
if !strings.Contains(got["description"].(string), "Google TV") {
t.Fatalf("richer description lost: %v", got["description"])
}
if src.Fields["description"].FeedName != "B" || !src.Fields["description"].Conflict {
t.Errorf("description source = %+v", src.Fields["description"])
}
// Feed A has more fields overall, so it stays primary for ordinary fields.
if src.Primary.FeedName != "A" {
t.Errorf("primary = %v, want A", src.Primary)
}
}
func TestMergeFeedRows_htmlMarkupDoesNotCountAsRichness(t *testing.T) {
markup := feedRowInput{FeedID: feedA, FeedName: "A", Mapped: map[string]any{
"description": "" + strings.Repeat("
", 50),
}}
text := feedRowInput{FeedID: feedB, FeedName: "B", Mapped: map[string]any{
"description": "A genuinely longer plain sentence about the product.",
}}
got, _ := mergeFeedRows([]feedRowInput{markup, text})
if got["description"] != text.Mapped["description"] {
t.Errorf("markup-heavy but text-poor description won: %v", got["description"])
}
}
func TestMergeFeedRows_conflictRichestFeedWinsAndAlsoListed(t *testing.T) {
rich := feedRowInput{FeedID: feedA, FeedName: "A", Mapped: map[string]any{
"name": "Sony Bravia 65", "brand": "Sony", "color": "bela",
}}
poor := feedRowInput{FeedID: feedB, FeedName: "B", Mapped: map[string]any{"name": "TV SONY"}}
got, src := mergeFeedRows([]feedRowInput{poor, rich}) // input order must not matter
if got["name"] != "Sony Bravia 65" {
t.Fatalf("name = %v", got["name"])
}
fs := src.Fields["name"]
if !fs.Conflict || len(fs.Also) != 1 || fs.Also[0].FeedName != "B" {
t.Errorf("name source = %+v", fs)
}
}
func TestMergeFeedRows_sameValueDifferentCaseIsNotConflict(t *testing.T) {
a := feedRowInput{FeedID: feedA, FeedName: "A", Mapped: map[string]any{"brand": "Sony", "x": "1"}}
b := feedRowInput{FeedID: feedB, FeedName: "B", Mapped: map[string]any{"brand": " SONY "}}
_, src := mergeFeedRows([]feedRowInput{a, b})
if src.Fields["brand"].Conflict {
t.Errorf("equal values flagged as conflict: %+v", src.Fields["brand"])
}
}
func TestMergeFeedRows_emptyValuesNeverWin(t *testing.T) {
a := feedRowInput{FeedID: feedA, FeedName: "A", Mapped: map[string]any{"brand": " ", "name": "TV", "x": "1", "y": "2"}}
b := feedRowInput{FeedID: feedB, FeedName: "B", Mapped: map[string]any{"brand": "Sony"}}
got, src := mergeFeedRows([]feedRowInput{a, b})
if got["brand"] != "Sony" || src.Fields["brand"].FeedName != "B" {
t.Errorf("brand = %v from %+v", got["brand"], src.Fields["brand"])
}
}
func TestMergeFeedRows_imagesAreUnioned(t *testing.T) {
a := feedRowInput{FeedID: feedA, FeedName: "A", Mapped: map[string]any{
"more_images": "https://i/1.jpg,https://i/2.jpg", "name": "x", "brand": "y",
}}
b := feedRowInput{FeedID: feedB, FeedName: "B", Mapped: map[string]any{
"more_images": "https://i/2.jpg\nhttps://i/3.jpg",
}}
got, src := mergeFeedRows([]feedRowInput{a, b})
if got["more_images"] != "https://i/1.jpg,https://i/2.jpg,https://i/3.jpg" {
t.Errorf("more_images = %v", got["more_images"])
}
if len(src.Fields["more_images"].MergedBy) != 2 {
t.Errorf("merged_from = %+v", src.Fields["more_images"].MergedBy)
}
}
func TestMergeFeedRows_singleFeedListKeptVerbatim(t *testing.T) {
a := feedRowInput{FeedID: feedA, FeedName: "A", Mapped: map[string]any{
"more_images": "https://i/1.jpg https://i/2.jpg",
}}
got, _ := mergeFeedRows([]feedRowInput{a})
if got["more_images"] != "https://i/1.jpg https://i/2.jpg" {
t.Errorf("single-feed list was rewritten: %q", got["more_images"])
}
}
func TestMergeFeedRows_specificationsUnionPrimaryWinsOnKeyClash(t *testing.T) {
a := feedRowInput{FeedID: feedA, FeedName: "A", Mapped: map[string]any{
"name": "TV", "brand": "Sony",
"specifications": map[string]string{"diagonal": "65\"", "color": "bela"},
}}
b := feedRowInput{FeedID: feedB, FeedName: "B", Mapped: map[string]any{
"specifications": map[string]any{"diagonal": "164 cm", "hdmi": "4", "empty": ""},
}}
got, src := mergeFeedRows([]feedRowInput{a, b})
specs := got["specifications"].(map[string]any)
if specs["diagonal"] != "65\"" || specs["hdmi"] != "4" || specs["color"] != "bela" {
t.Errorf("specs = %v", specs)
}
if _, ok := specs["empty"]; ok {
t.Error("empty spec value must be dropped")
}
fs := src.Fields["specifications"]
if fs.Keys["hdmi"] != "B" || fs.Keys["diagonal"] != "A" || len(fs.MergedBy) != 2 {
t.Errorf("spec sources = %+v", fs)
}
}
func TestMergeFeedRows_deterministicRegardlessOfOrder(t *testing.T) {
mk := func(id uuid.UUID, n string) feedRowInput {
return feedRowInput{FeedID: id, FeedName: n, Mapped: map[string]any{"name": n + " name", "brand": n}}
}
rows := []feedRowInput{mk(feedC, "C"), mk(feedA, "A"), mk(feedB, "B")}
first, _ := mergeFeedRows(rows)
rev := []feedRowInput{rows[2], rows[1], rows[0]}
second, _ := mergeFeedRows(rev)
if first["name"] != second["name"] || first["brand"] != second["brand"] {
t.Errorf("order-dependent merge: %v vs %v", first, second)
}
}
func TestMergeFeedRows_doesNotMutateInputs(t *testing.T) {
a := feedRowInput{FeedID: feedA, FeedName: "A", Mapped: map[string]any{"name": "TV", "_sync_changes": []any{"new"}}}
b := feedRowInput{FeedID: feedB, FeedName: "B", Mapped: map[string]any{"brand": "Sony"}}
merged, _ := mergeFeedRows([]feedRowInput{a, b})
if _, ok := merged[syncChangesMappedKey]; ok {
t.Error("_sync_changes must not be merged")
}
if _, ok := a.Mapped["brand"]; ok {
t.Error("input row mutated")
}
}
func TestMergeFeedRows_empty(t *testing.T) {
got, src := mergeFeedRows(nil)
if len(got) != 0 || len(src.Fields) != 0 {
t.Errorf("got %v %v", got, src)
}
}