search.test.js
1 import { describe, expect, it } from 'vitest'; 2 import { __test__ } from './search.js'; 3 describe('1688 search normalization', () => { 4 it('normalizes search candidates into structured result rows', () => { 5 const result = __test__.normalizeSearchCandidate({ 6 item_url: 'https://detail.1688.com/offer/887904326744.html', 7 title: '宿舍置物架桌面加高架', 8 container_text: '宿舍置物架桌面加高架 ¥56.00 2套起批 山东青岛 已售300+套', 9 price_text: '¥ 56 .00', 10 sales_text: '300+套', 11 moq_text: '2套起批', 12 tag_items: ['退货包运费', '回头率52%'], 13 hover_items: ['验厂报告'], 14 seller_name: '青岛沁澜衣品服装有限公司', 15 seller_url: 'https://yinuoweierfushi.1688.com/page/index.html?spm=a123', 16 }, 'https://s.1688.com/selloffer/offer_search.htm?charset=utf8&keywords=置物架'); 17 expect(result.rank).toBe(0); 18 expect(result.offer_id).toBe('887904326744'); 19 expect(result.shop_id).toBe('yinuoweierfushi'); 20 expect(result.item_url).toBe('https://detail.1688.com/offer/887904326744.html'); 21 expect(result.seller_url).toBe('https://yinuoweierfushi.1688.com'); 22 expect(result.price_text).toBe('¥56.00'); 23 expect(result.price_min).toBe(56); 24 expect(result.price_max).toBe(56); 25 expect(result.moq_value).toBe(2); 26 expect(result.location).toBe('山东青岛'); 27 expect(result.sales_text).toBe('300+套'); 28 expect(result.badges).toEqual(expect.arrayContaining(['退货包运费', '验厂报告'])); 29 expect(result.return_rate_text).toBe('回头率52%'); 30 }); 31 it('does not use hover_price_text as MOQ source', () => { 32 const result = __test__.normalizeSearchCandidate({ 33 item_url: 'https://detail.1688.com/offer/887904326744.html', 34 title: 'test', 35 container_text: 'test ¥56.00', 36 price_text: '¥ 56 .00', 37 hover_price_text: '¥56.00 3件起批', 38 moq_text: null, 39 }, 'https://s.1688.com/selloffer/offer_search.htm?charset=utf8&keywords=test'); 40 // hover_price_text should not be used for MOQ extraction 41 expect(result.moq_text).toBeNull(); 42 expect(result.moq_value).toBeNull(); 43 }); 44 it('extracts offer id from mobile detail search links', () => { 45 const result = __test__.normalizeSearchCandidate({ 46 item_url: 'http://detail.m.1688.com/page/index.html?offerId=910933345396&sortType=&pageId=', 47 title: '', 48 container_text: '桌面书桌办公室工位收纳展示新中式博古架多层茶具厨房摆放置物架 ¥24.3 已售20+件', 49 price_text: '¥ 14 .28', 50 sales_text: '1500+件', 51 moq_text: '≥2个', 52 seller_name: '泰商国际贸易(宁阳)有限公司', 53 seller_url: 'http://tsgjmy.1688.com/', 54 }, 'https://s.1688.com/selloffer/offer_search.htm?charset=utf8&keywords=桌面置物架'); 55 expect(result.offer_id).toBe('910933345396'); 56 expect(result.shop_id).toBe('tsgjmy'); 57 expect(result.item_url).toBe('https://detail.1688.com/offer/910933345396.html'); 58 expect(result.title).toContain('桌面书桌办公室工位收纳展示'); 59 expect(result.price_text).toBe('¥14.28'); 60 expect(result.sales_text).toBe('1500+件'); 61 expect(result.moq_text).toBe('≥2个'); 62 expect(result.moq_value).toBe(2); 63 }); 64 it('prefers offer id and falls back to item url for dedupe key', () => { 65 expect(__test__.buildDedupeKey({ 66 offer_id: '123456', 67 item_url: 'https://detail.1688.com/offer/123456.html', 68 })).toBe('offer:123456'); 69 expect(__test__.buildDedupeKey({ 70 offer_id: null, 71 item_url: 'https://detail.1688.com/offer/123456.html', 72 })).toBe('url:https://detail.1688.com/offer/123456.html'); 73 expect(__test__.buildDedupeKey({ offer_id: null, item_url: null })).toBeNull(); 74 }); 75 });