rankings.test.js
1 import { describe, expect, it } from 'vitest'; 2 import { __test__ } from './rankings.js'; 3 describe('amazon rankings helpers', () => { 4 it('normalizes ranking candidates with unified schema', () => { 5 const result = __test__.normalizeRankingCandidate({ 6 rank_text: '#3', 7 asin: 'B0DR31GC3D', 8 title: 'Desk Shelves Desktop Organizer', 9 href: 'https://www.amazon.com/dp/B0DR31GC3D/ref=zg_bs', 10 price_text: '$25.92', 11 rating_text: '4.3 out of 5 stars', 12 review_count_text: '435', 13 }, { 14 listType: 'new_releases', 15 rankFallback: 3, 16 listTitle: 'Amazon New Releases', 17 sourceUrl: 'https://www.amazon.com/gp/new-releases', 18 categoryTitle: 'Home & Kitchen', 19 categoryUrl: 'https://www.amazon.com/gp/new-releases/home-garden', 20 categoryPath: ['Home & Kitchen'], 21 visibleCategoryLinks: [{ title: 'Storage', url: 'https://www.amazon.com/gp/new-releases/storage', node_id: null }], 22 }); 23 expect(result.list_type).toBe('new_releases'); 24 expect(result.rank).toBe(3); 25 expect(result.asin).toBe('B0DR31GC3D'); 26 expect(result.product_url).toBe('https://www.amazon.com/dp/B0DR31GC3D'); 27 expect(result.category_title).toBe('Home & Kitchen'); 28 expect(result.visible_category_links).toEqual([ 29 { title: 'Storage', url: 'https://www.amazon.com/gp/new-releases/storage', node_id: null }, 30 ]); 31 }); 32 it('deduplicates category links and parses rank fallback', () => { 33 const links = __test__.normalizeVisibleCategoryLinks([ 34 { title: 'Kitchen', url: '/gp/new-releases/home-garden' }, 35 { title: 'Kitchen', url: 'https://www.amazon.com/gp/new-releases/home-garden' }, 36 { title: 'Storage', url: '/gp/new-releases/storage', node_id: '1064954' }, 37 ]); 38 expect(links.length).toBe(2); 39 expect(__test__.parseRank('N/A', 8)).toBe(8); 40 }); 41 });