This commit is contained in:
greeneclipse committed 2026-11-15 16:47:06 +01:00
1 parent e7964c58c8
commit 0ec433815b
4 files changed
+224 -1

No files matched your search

@@ -279,3 +279,126 @@ func TestSyncMergeTiming(t *testing.T) {
t.Fatalf("merged products = %d, want %d", merged, len(lb)-1)
}
}
func (f *mergeFixture) setFeed(t *testing.T, id uuid.UUID, sql string, args ...any) {
t.Helper()
if _, err := f.pg.Exec(f.ctx, `UPDATE input_feeds SET `+sql+` WHERE id = $1`, append([]any{id}, args...)...); err != nil {
t.Fatal(err)
}
}
func (f *mergeFixture) jobsFor(t *testing.T, id uuid.UUID) int {
t.Helper()
var n int
if err := f.pg.QueryRow(f.ctx, `SELECT count(*) FROM feed_sync_jobs WHERE feed_id = $1`, id).Scan(&n); err != nil {
t.Fatal(err)
}
return n
}
// Feeds must sync by themselves once their interval has elapsed.
func TestEnqueueDueFeedsSchedulesOnlyDueFeeds(t *testing.T) {
f := newMergeFixture(t)
cols := map[string]string{"EAN": "gtin", "Name": "title"}
mk := func(name string) uuid.UUID { return f.addFeed(t, name, cols) }
due := mk("due") // synced 2h ago, interval 60
never := mk("never") // never synced
fresh := mk("fresh") // synced just now
pending := mk("pending") // already has a pending job
failed := mk("failed") // failed recently
paused := mk("paused") // not active
noInterval := mk("no interval") // interval 0 = manual only
noMapping := mk("no mapping") // never mapped
if _, err := f.pg.Exec(f.ctx, `DELETE FROM feed_mappings WHERE feed_id = $1`, noMapping); err != nil {
t.Fatal(err)
}
f.setFeed(t, due, `last_synced_at = now() - interval '2 hours'`)
f.setFeed(t, fresh, `last_synced_at = now()`)
f.setFeed(t, pending, `last_synced_at = now() - interval '2 hours'`)
f.setFeed(t, failed, `last_synced_at = now() - interval '2 hours'`)
f.setFeed(t, paused, `last_synced_at = now() - interval '2 hours', status = 'paused'`)
f.setFeed(t, noInterval, `last_synced_at = now() - interval '2 hours', sync_interval_minutes = 0`)
if _, err := f.pg.Exec(f.ctx, `INSERT INTO feed_sync_jobs (feed_id, company_id, status) VALUES ($1, $2, 'pending'), ($3, $2, 'failed')`,
pending, f.companyID, failed); err != nil {
t.Fatal(err)
}
n, err := f.svc.EnqueueDueFeeds(f.ctx, 50, f.companyID)
if err != nil {
t.Fatal(err)
}
if n != 2 {
t.Errorf("queued %d feeds, want 2 (due + never)", n)
}
for name, id := range map[string]uuid.UUID{"due": due, "never": never} {
if f.jobsFor(t, id) != 1 {
t.Errorf("%s feed was not queued", name)
}
}
for name, id := range map[string]uuid.UUID{"fresh": fresh, "paused": paused, "no interval": noInterval, "no mapping": noMapping} {
if f.jobsFor(t, id) != 0 {
t.Errorf("%s feed must not be queued", name)
}
}
if f.jobsFor(t, pending) != 1 || f.jobsFor(t, failed) != 1 {
t.Error("feeds with a pending / recently failed job must not get another")
}
// A second scan does not stack duplicate jobs.
if n, _ := f.svc.EnqueueDueFeeds(f.ctx, 50, f.companyID); n != 0 {
t.Errorf("second scan queued %d, want 0", n)
}
}
// Products synced BEFORE merging existed (single overwritten row, no per-feed rows, an
// "unchanged" file) must merge on the next scheduled sync without any file change.
func TestAlreadySyncedProductsMergeOnNextSync(t *testing.T) {
f := newMergeFixture(t)
const ean = "4548736180307"
longDesc := strings.Repeat("Sony Bravia 4K z Google TV in HDR10 z 20 W zvokom. ", 12)
a := f.addFeed(t, "Vendor A", map[string]string{"EAN": "gtin", "Name": "title", "Brand": "brand", "Desc": "description", "Price": "purchaseprice"})
b := f.addFeed(t, "Vendor B", map[string]string{"EAN": "gtin", "Title": "title", "Description": "description", "Image": "main_image", "EPREL": "eprel_id"})
f.writeCSV(t, "Vendor A", "EAN,Name,Brand,Desc,Price", ean+",Sony TV 65,Sony,Kratek opis.,560")
f.writeCSV(t, "Vendor B", "EAN,Title,Description,Image,EPREL", ean+",TV SONY K65,"+longDesc+",https://img/main.jpg,2651553")
f.sync(t, a)
f.sync(t, b)
// Recreate the pre-merge world: Vendor B overwrote everything, nothing else stored,
// and both feeds remember hashes from the old algorithm.
if _, err := f.pg.Exec(f.ctx, `DELETE FROM raw_product_feed_rows WHERE company_id = $1`, f.companyID); err != nil {
t.Fatal(err)
}
if _, err := f.pg.Exec(f.ctx, `
UPDATE raw_products SET feed_id = $2, feed_ids = NULL, feed_field_sources = NULL,
mapped_data = jsonb_build_object('gtin', gtin, 'title', 'TV SONY K65', 'description', $3::text,
'main_image', 'https://img/main.jpg', 'eprel_id', '2651553')
WHERE company_id = $1`, f.companyID, b, longDesc); err != nil {
t.Fatal(err)
}
f.setFeed(t, a, `options = options || '{"last_mapping_hash":"legacy"}'::jsonb`)
f.setFeed(t, b, `options = options || '{"last_mapping_hash":"legacy"}'::jsonb`)
if st := f.raw(t, ean); st.Mapped["brand"] != nil {
t.Fatalf("test setup wrong: %v", st.Mapped)
}
// Scheduled syncs: neither file changed.
f.sync(t, a)
f.sync(t, b)
st := f.raw(t, ean)
if st.Mapped["brand"] != "Sony" || st.Mapped["purchaseprice"] != "560" {
t.Fatalf("Vendor A data not merged into the existing product: %v", st.Mapped)
}
if st.Mapped["eprel_id"] != "2651553" || !strings.Contains(fmt.Sprint(st.Mapped["description"]), "Google TV") {
t.Errorf("Vendor B data lost: %v", st.Mapped)
}
if len(st.FeedIDs) != 2 || st.Sources.Fields["brand"].FeedName != "Vendor A" || st.Sources.Fields["eprel_id"].FeedName != "Vendor B" {
t.Errorf("sources not recorded: ids=%v sources=%+v", st.FeedIDs, st.Sources.Fields)
}
// After that one-time re-parse, an unchanged file is skipped again (cheap).
var hash string
if err := f.pg.QueryRow(f.ctx, `SELECT options->>'last_mapping_hash' FROM input_feeds WHERE id = $1`, a).Scan(&hash); err != nil || hash == "legacy" || hash == "" {
t.Errorf("mapping hash not refreshed: %q %v", hash, err)
}
}