shared.test.js
1 import { describe, expect, it } from 'vitest'; 2 import { __test__ } from './shared.js'; 3 describe('amazon shared helpers', () => { 4 it('builds canonical product and discussion URLs from ASINs and product URLs', () => { 5 expect(__test__.buildProductUrl('B0FJS72893')).toBe('https://www.amazon.com/dp/B0FJS72893'); 6 expect(__test__.buildProductUrl('https://www.amazon.com/dp/B0FJS72893/ref=something')).toBe('https://www.amazon.com/dp/B0FJS72893'); 7 expect(__test__.buildDiscussionUrl('https://www.amazon.com/dp/B0FJS72893')).toBe('https://www.amazon.com/product-reviews/B0FJS72893'); 8 }); 9 it('parses price, rating, and review-count text', () => { 10 expect(__test__.parsePriceText('1 offer from $34.11')).toEqual({ 11 price_text: '$34.11', 12 price_value: 34.11, 13 currency: 'USD', 14 }); 15 expect(__test__.parseRatingValue('3.9 out of 5 stars, rating details')).toBe(3.9); 16 expect(__test__.parseReviewCount('27 global ratings')).toBe(27); 17 expect(__test__.parseReviewCount('(2.9K)')).toBe(2900); 18 expect(__test__.parseReviewCount('1.2M global ratings')).toBe(1200000); 19 expect(__test__.extractReviewCountFromCardText('Desk Shelf\n4.3 out of 5 stars\n435\n$25.92')).toBe('435'); 20 }); 21 it('recognizes robot checks and Amazon-owned merchants', () => { 22 expect(__test__.isAmazonEntity('Ships from Amazon')).toBe(true); 23 expect(__test__.trimRatingPrefix('5.0 out of 5 stars Great value and quality')).toBe('Great value and quality'); 24 expect(__test__.isRobotState({ 25 title: 'Robot Check', 26 body_text: 'Sorry, we just need to make sure you\'re not a robot', 27 })).toBe(true); 28 }); 29 it('requires a real best-sellers URL or path', () => { 30 expect(__test__.resolveBestsellersUrl('/Best-Sellers/zgbs')).toBe('https://www.amazon.com/Best-Sellers/zgbs'); 31 expect(() => __test__.resolveBestsellersUrl('desk shelf organizer')).toThrow('amazon bestsellers expects a best sellers URL or /zgbs path'); 32 }); 33 it('resolves and validates all ranking list URLs', () => { 34 expect(__test__.resolveRankingUrl('new_releases')).toBe('https://www.amazon.com/gp/new-releases'); 35 expect(__test__.resolveRankingUrl('movers_shakers')).toBe('https://www.amazon.com/gp/movers-and-shakers'); 36 expect(__test__.resolveRankingUrl('new_releases', '/gp/new-releases/kitchen')).toBe('https://www.amazon.com/gp/new-releases/kitchen'); 37 expect(__test__.resolveRankingUrl('bestsellers', 'https://www.amazon.com/Best-Sellers/zgbs/ref=zg_bsnr_tab_bs')).toBe('https://www.amazon.com/Best-Sellers/zgbs'); 38 expect(() => __test__.resolveRankingUrl('movers_shakers', 'https://example.com/gp/movers-and-shakers')).toThrow('Invalid Amazon URL'); 39 }); 40 it('extracts category node id from URL best effort', () => { 41 expect(__test__.extractCategoryNodeId('https://www.amazon.com/Best-Sellers-Home-Kitchen/zgbs/home-garden/3744371')).toBe('3744371'); 42 expect(__test__.extractCategoryNodeId('https://www.amazon.com/s?k=desk+organizer&rh=n%3A1064954')).toBe('1064954'); 43 }); 44 });