shared.test.js
1 import { describe, expect, it } from 'vitest'; 2 import { __test__ } from './shared.js'; 3 describe('1688 shared helpers', () => { 4 it('builds encoded search URLs and validates limit', () => { 5 expect(__test__.buildSearchUrl('置物架')).toBe('https://s.1688.com/selloffer/offer_search.htm?charset=utf8&keywords=%E7%BD%AE%E7%89%A9%E6%9E%B6'); 6 expect(() => __test__.buildSearchUrl(' ')).toThrowError(/cannot be empty/i); 7 expect(__test__.parseSearchLimit(3)).toBe(3); 8 expect(__test__.parseSearchLimit('1000')).toBe(__test__.SEARCH_LIMIT_MAX); 9 expect(() => __test__.parseSearchLimit('0')).toThrowError(/positive integer/i); 10 }); 11 it('extracts IDs and canonicalizes urls', () => { 12 expect(__test__.extractOfferId('887904326744')).toBe('887904326744'); 13 expect(__test__.extractOfferId('https://detail.1688.com/offer/887904326744.html')).toBe('887904326744'); 14 expect(__test__.extractMemberId('https://winport.m.1688.com/page/index.html?memberId=b2b-1641351767')).toBe('b2b-1641351767'); 15 expect(__test__.extractMemberId('b2b-22154705262941f196')).toBe('b2b-22154705262941f196'); 16 expect(__test__.resolveStoreUrl('b2b-22154705262941f196')).toBe('https://winport.m.1688.com/page/index.html?memberId=b2b-22154705262941f196'); 17 expect(__test__.canonicalizeStoreUrl('https://yinuoweierfushi.1688.com/page/index.html?spm=foo')).toBe('https://yinuoweierfushi.1688.com'); 18 expect(__test__.canonicalizeItemUrl('http://detail.m.1688.com/page/index.html?offerId=910933345396&spm=x')).toBe('https://detail.1688.com/offer/910933345396.html'); 19 expect(__test__.canonicalizeSellerUrl('https://yinuoweierfushi.1688.com/page/contactinfo.html?tracelog=1')).toBe('https://yinuoweierfushi.1688.com'); 20 expect(__test__.extractShopId('https://yinuoweierfushi.1688.com/page/index.html')).toBe('yinuoweierfushi'); 21 }); 22 it('parses price ranges and moq text', () => { 23 expect(__test__.parsePriceText('¥96.00-98.00')).toEqual({ 24 price_text: '¥96.00-98.00', 25 price_min: 96, 26 price_max: 98, 27 currency: 'CNY', 28 }); 29 expect(__test__.parsePriceText('¥ 14 .28')).toEqual({ 30 price_text: '¥14.28', 31 price_min: 14.28, 32 price_max: 14.28, 33 currency: 'CNY', 34 }); 35 expect(__test__.parseMoqText('3套起批')).toEqual({ 36 moq_text: '3套起批', 37 moq_value: 3, 38 }); 39 expect(__test__.parseMoqText('2~999个')).toEqual({ 40 moq_text: '2~999个', 41 moq_value: 2, 42 }); 43 }); 44 it('detects captcha and login states', () => { 45 expect(__test__.extractLocation('山东青岛 送至 江苏苏州')).toBe('山东青岛'); 46 expect(__test__.isCaptchaState({ 47 href: 'https://s.1688.com/_____tmd_____/punish', 48 title: '验证码拦截', 49 body_text: '请拖动下方滑块完成验证', 50 })).toBe(true); 51 expect(__test__.isLoginState({ 52 href: 'https://login.taobao.com/member/login.jhtml', 53 title: '账号登录', 54 body_text: '请登录后继续', 55 })).toBe(true); 56 }); 57 });