import { describe, expect, it } from 'vitest' import type { App } from 'vuepress/core' import type { ElementCompact } from 'xml-js' import { xml2js } from 'xml-js' import type { FeedItem } from '../../src/node/feed/item.js' import { FeedStore } from '../../src/node/feed/store.js' import { getAtomFeed } from '../../src/node/generator/atom/index.js' import { getRssFeed } from '../../src/node/generator/rss/index.js' import { getFeedOptions } from '../../src/node/getFeedOptions.js' const TITLE = `A & B < C > D " E ' F` const DESCRIPTION = `Text & "quoted" 'single'` const AUTHOR_NAME = `Jane & John ` const AUTHOR_EMAIL = `jane+feed&test@example.com` const CATEGORY = `Tech & ` const CONTENT = `

Hello & "world" bold ]]> tail

` const SUMMARY = `Summary & bold ]]> end` /** Entities that only appear when the text is escaped twice by `xml-js` */ const DOUBLE_ESCAPED_ENTITIES = [ '&lt;', '&gt;', '&quot;', '&apos;', '&amp;', ] const createItem = (overrides: Record = {}): FeedItem => ({ isValid: true, title: TITLE, link: 'https://example.com/posts/1', guid: 'https://example.com/posts/1', description: DESCRIPTION, author: [{ name: AUTHOR_NAME, email: AUTHOR_EMAIL }], category: [{ name: CATEGORY, domain: 'https://example.com/categories' }], contributor: [], pubDate: new Date('2024-01-02T03:04:05.000Z'), lastUpdated: new Date('2024-02-03T04:05:06.000Z'), summary: null, content: CONTENT, enclosure: null, copyright: null, ...overrides, }) as unknown as FeedItem const createStore = (items: FeedItem[]): FeedStore => { const app = { siteData: { base: '/', title: 'Test site', description: 'Test site description', lang: 'en-US', locales: {}, }, } as unknown as App const store = new FeedStore( app, getFeedOptions(app, { hostname: 'https://example.com' })['/'], '/', ) items.forEach((item) => store.add(item)) return store } const parse = (xml: string): ElementCompact => xml2js(xml, { compact: true }) /** * Read the text of a parsed node * * `xml-js` parses consecutive CDATA sections as an array, whose items are * concatenated by an XML parser. * * 读取解析后节点的文本 * * `xml-js` 会将连续的 CDATA 段解析为数组,XML 解析器会将其各项拼接起来。 * * @param value - Parsed text or CDATA value / 解析后的文本或 CDATA 值 * @returns Concatenated text / 拼接后的文本 */ const getParsedText = (value: unknown): string => { if (Array.isArray(value)) return value.every((item) => typeof item === 'string') ? value.join('') : '' return typeof value === 'string' ? value : '' } describe('feed XML round-trip', () => { describe(getRssFeed, () => { const xml = getRssFeed(createStore([createItem()])) const { channel } = parse(xml).rss it('should not contain double-escaped entities', () => { for (const entity of DOUBLE_ESCAPED_ENTITIES) expect(xml).not.toContain(entity) }) it('should round-trip channel fields', () => { expect(channel.title._text).toBe('Test site') expect(channel.description._text).toBe('Test site description') expect(channel.link._text).toBe('https://example.com/') }) it('should round-trip item fields', () => { expect(channel.item.title._text).toBe(TITLE) expect(channel.item.description._text).toBe(DESCRIPTION) expect(channel.item.link._text).toBe('https://example.com/posts/1') expect(channel.item.source._text).toBe(TITLE) expect(channel.item.author._text).toBe(`${AUTHOR_EMAIL} (${AUTHOR_NAME})`) expect(channel.item.category._text).toBe(CATEGORY) expect(channel.item.category._attributes.domain).toBe( 'https://example.com/categories', ) }) it('should keep CDATA content valid and unchanged', () => { expect(getParsedText(channel.item['content:encoded']._cdata)).toBe( CONTENT, ) }) }) describe(getAtomFeed, () => { const xml = getAtomFeed( createStore([ createItem(), createItem({ title: `Second & "quoted"`, link: 'https://example.com/posts/2', guid: 'https://example.com/posts/2', description: DESCRIPTION, summary: SUMMARY, }), ]), ) const { feed } = parse(xml) const [firstEntry, secondEntry] = feed.entry it('should not contain double-escaped entities', () => { for (const entity of DOUBLE_ESCAPED_ENTITIES) expect(xml).not.toContain(entity) }) it('should round-trip feed fields', () => { expect(feed.title._text).toBe('Test site') expect(feed.subtitle._text).toBe('Test site description') expect(feed.id._text).toBe('https://example.com/') }) it('should round-trip entry fields', () => { expect(firstEntry.title._text).toBe(TITLE) expect(firstEntry.id._text).toBe('https://example.com/posts/1') expect(firstEntry.link._attributes.href).toBe( 'https://example.com/posts/1', ) expect(firstEntry.category._attributes.term).toBe(CATEGORY) expect(firstEntry.author.name._text).toBe(AUTHOR_NAME) expect(firstEntry.author.email._text).toBe(AUTHOR_EMAIL) expect(getParsedText(secondEntry.title._text)).toBe( `Second & "quoted"`, ) }) it('should keep text summary, CDATA summary and CDATA content unchanged', () => { expect(firstEntry.summary._text).toBe(DESCRIPTION) expect(getParsedText(secondEntry.summary._cdata)).toBe(SUMMARY) expect(getParsedText(firstEntry.content._cdata)).toBe(CONTENT) }) }) })