import { describe, it, expect } from 'vitest'; import { parseRssItems } from './utils.js'; describe('parseRssItems', () => { it('extracts plain text fields', () => { const xml = ` Hellohttps://example.com Worldhttps://test.com `; const items = parseRssItems(xml, ['title', 'link']); expect(items).toEqual([ { title: 'Hello', link: 'https://example.com' }, { title: 'World', link: 'https://test.com' }, ]); }); it('handles CDATA-wrapped content', () => { const xml = ` <![CDATA[Breaking News]]>https://news.com `; const items = parseRssItems(xml, ['title', 'link']); expect(items).toEqual([ { title: 'Breaking News', link: 'https://news.com' }, ]); }); it('handles namespaced fields like ht:approx_traffic', () => { const xml = ` AI 500,000+ Mon, 20 Mar 2026 `; const items = parseRssItems(xml, ['title', 'ht:approx_traffic', 'pubDate']); expect(items).toEqual([ { title: 'AI', 'ht:approx_traffic': '500,000+', pubDate: 'Mon, 20 Mar 2026' }, ]); }); it('returns empty string for missing fields', () => { const xml = `Test`; const items = parseRssItems(xml, ['title', 'missing']); expect(items).toEqual([{ title: 'Test', missing: '' }]); }); it('handles tags with attributes (e.g. )', () => { const xml = ` <![CDATA[AI reshapes everything - Reuters]]> Reuters https://news.google.com/123 `; const items = parseRssItems(xml, ['title', 'source', 'link']); expect(items).toEqual([ { title: 'AI reshapes everything - Reuters', source: 'Reuters', link: 'https://news.google.com/123' }, ]); }); it('handles mixed CDATA and plain text in the same item', () => { const xml = ` <![CDATA[Breaking: Major event]]> https://example.com/article Fri, 21 Mar 2026 `; const items = parseRssItems(xml, ['title', 'link', 'pubDate']); expect(items).toEqual([ { title: 'Breaking: Major event', link: 'https://example.com/article', pubDate: 'Fri, 21 Mar 2026' }, ]); }); it('returns empty array for no items', () => { const xml = `Empty`; const items = parseRssItems(xml, ['title']); expect(items).toEqual([]); }); });