83 lines
2.5 KiB
Go
83 lines
2.5 KiB
Go
package processing
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestPlainDescriptionFromAny_arrayAndHTML(t *testing.T) {
|
|
got := PlainDescriptionFromAny([]any{"Hello<br>World", "Second"})
|
|
if got != "Hello\nWorld\nSecond" {
|
|
t.Fatalf("array+html: %q", got)
|
|
}
|
|
got = PlainDescriptionFromAny(map[string]any{"#text": "Plain & ok"})
|
|
if got != "Plain & ok" {
|
|
t.Fatalf("#text: %q", got)
|
|
}
|
|
if PlainDescriptionFromAny(nil) != "" {
|
|
t.Fatal("nil should be empty")
|
|
}
|
|
}
|
|
|
|
func TestDescriptionFromAny_preservesFormulaHTML(t *testing.T) {
|
|
htmlDesc := `<h1>Anker Soundcore Space One Pro</h1><p>Zložljive ANC slušalke.</p><ul><li>Bluetooth</li></ul>`
|
|
got := DescriptionFromAny(htmlDesc)
|
|
if !strings.Contains(got, "<h1>") || !strings.Contains(got, "<p>") || !strings.Contains(got, "<ul>") {
|
|
t.Fatalf("expected formula HTML preserved, got %q", got)
|
|
}
|
|
// Array unwrap should still keep tags.
|
|
got = DescriptionFromAny([]any{htmlDesc})
|
|
if !strings.Contains(got, "<h1>") || !strings.Contains(got, "</p>") {
|
|
t.Fatalf("array unwrap should keep HTML, got %q", got)
|
|
}
|
|
// Plain path still strips (meta/SEO / feed normalize).
|
|
plain := PlainDescriptionFromAny(htmlDesc)
|
|
if strings.Contains(plain, "<h1>") || strings.Contains(plain, "<p>") {
|
|
t.Fatalf("PlainDescriptionFromAny should strip tags, got %q", plain)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeMapped_descriptionArrayBecomesPlainString(t *testing.T) {
|
|
got := NormalizeMapped(map[string]any{
|
|
"description": []any{"Line1<br/>A", "Line2"},
|
|
"name": "Widget",
|
|
}, nil)
|
|
desc, ok := got["description"].(string)
|
|
if !ok {
|
|
t.Fatalf("description should be string, got %T %v", got["description"], got["description"])
|
|
}
|
|
if desc != "Line1\nA\nLine2" {
|
|
t.Fatalf("description=%q", desc)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeMapped_eprelIDAlias(t *testing.T) {
|
|
got := NormalizeMapped(map[string]any{"EPRELID": "246834"}, nil)
|
|
if got["eprel_id"] != "246834" {
|
|
t.Fatalf("eprel_id=%v", got["eprel_id"])
|
|
}
|
|
}
|
|
|
|
func TestStringFromAny_flattensArray(t *testing.T) {
|
|
got := stringFromAny([]any{"a", "b"})
|
|
if got != "a b" {
|
|
t.Fatalf("got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestCanonicalizeAttrKey_eprel(t *testing.T) {
|
|
if canonicalizeAttrKey("EPRELID") != "eprel_id" {
|
|
t.Fatalf("got %q", canonicalizeAttrKey("EPRELID"))
|
|
}
|
|
if canonicalizeAttrKey("eprel") != "eprel_id" {
|
|
t.Fatalf("got %q", canonicalizeAttrKey("eprel"))
|
|
}
|
|
}
|
|
|
|
func TestPlainDescriptionFromStored_jsonArrayString(t *testing.T) {
|
|
got := plainDescriptionFromStored(`["Hello<br>World"]`)
|
|
if got != "Hello\nWorld" {
|
|
t.Fatalf("got %q", got)
|
|
}
|
|
}
|