Files
descrybe/apps/api/internal/feeds/present_test.go
T

144 lines
4.0 KiB
Go
Raw Normal View History

package feeds
import (
"testing"
"time"
)
func TestPresentFeedLegacyFields(t *testing.T) {
t.Parallel()
ts := time.Date(2026, 8, 4, 11, 0, 0, 0, time.UTC)
got := PresentFeed(map[string]any{
"id": "22222222-2222-2222-2222-222222222222",
"name": "Main catalog",
"url": "https://example.com/feed.xml",
"feed_type": "xml",
"status": "active",
"sync_interval_minutes": 60,
"last_synced_at": ts,
"options": map[string]any{"item_path": "channel/item"},
"product_count": int64(12),
"created_at": ts,
"updated_at": ts,
})
if got["item_path"] != "channel/item" {
t.Fatalf("item_path=%v", got["item_path"])
}
if got["is_active"] != true {
t.Fatalf("is_active=%v", got["is_active"])
}
if got["product_count"] != 12 {
t.Fatalf("product_count=%v", got["product_count"])
}
if got["last_synced"] != "2026-08-04T11:00:00Z" {
t.Fatalf("last_synced=%v", got["last_synced"])
}
if got["last_synced_at"] != got["last_synced"] {
t.Fatalf("dual last_synced_at mismatch")
}
if got["feed_type"] != "xml" {
t.Fatalf("feed_type=%v", got["feed_type"])
}
if got["last_data_at"] != got["last_synced_at"] {
t.Fatalf("last_data_at should prefer live sync: %v", got["last_data_at"])
}
}
func TestPresentFeedLastDataFromProducts(t *testing.T) {
t.Parallel()
ts := time.Date(2026, 8, 4, 11, 0, 0, 0, time.UTC)
got := PresentFeed(map[string]any{
"id": "1",
"name": "Imported",
"status": "active",
"product_count": int64(50),
"mapping_field_count": 19,
"has_mappings": true,
"products_updated_at": ts,
"last_synced_at": nil,
})
if got["last_synced_at"] != nil {
t.Fatalf("last_synced_at=%v", got["last_synced_at"])
}
if got["last_data_at"] != "2026-08-04T11:00:00Z" {
t.Fatalf("last_data_at=%v", got["last_data_at"])
}
if got["mapping_field_count"] != 19 {
t.Fatalf("mapping_field_count=%v", got["mapping_field_count"])
}
if got["has_mappings"] != true {
t.Fatalf("has_mappings=%v", got["has_mappings"])
}
if got["mapping_incomplete"] != false {
t.Fatalf("mapping_incomplete=%v want false when has_mappings and unset", got["mapping_incomplete"])
}
if got["product_count"] != 50 {
t.Fatalf("product_count=%v", got["product_count"])
}
}
func TestPresentFeedMappingIncomplete(t *testing.T) {
t.Parallel()
got := PresentFeed(map[string]any{
"id": "1",
"name": "Mapped incomplete",
"status": "mapped",
"mapping_field_count": 2,
"has_mappings": true,
"mapping_incomplete": true,
})
if got["mapping_incomplete"] != true {
t.Fatalf("mapping_incomplete=%v", got["mapping_incomplete"])
}
}
func TestPresentFeedsPreservesMappingIncomplete(t *testing.T) {
t.Parallel()
out := PresentFeeds([]map[string]any{
{
"id": "a",
"name": "Incomplete",
"status": "mapped",
"mapping_field_count": 1,
"has_mappings": true,
"mapping_incomplete": true,
},
{
"id": "b",
"name": "Complete",
"status": "active",
"mapping_field_count": 3,
"has_mappings": true,
"mapping_incomplete": false,
},
})
if len(out) != 2 {
t.Fatalf("len=%d", len(out))
}
if out[0]["mapping_incomplete"] != true || out[1]["mapping_incomplete"] != false {
t.Fatalf("got %#v %#v", out[0]["mapping_incomplete"], out[1]["mapping_incomplete"])
}
}
func TestPresentFeedEmptyURLAndInactive(t *testing.T) {
t.Parallel()
got := PresentFeed(map[string]any{
"id": "1",
"name": "Draft",
"url": "",
"status": "unmapped",
"options": map[string]any{},
"created_at": time.Unix(0, 0).UTC(),
"updated_at": time.Unix(0, 0).UTC(),
})
if got["url"] != nil {
t.Fatalf("url=%v want nil", got["url"])
}
if got["is_active"] != false {
t.Fatalf("is_active=%v", got["is_active"])
}
if got["item_path"] != "" {
t.Fatalf("item_path=%v", got["item_path"])
}
}