import { renderToString } from 'hono/jsx/dom/server'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { Atom as RenderAtom, json as renderJson, RSS as RenderRSS, rss3 as renderRss3 } from '@/utils/render'; import Atom from '@/views/atom'; import jsonView from '@/views/json'; import RSS from '@/views/rss'; import rss3View from '@/views/rss3'; afterEach(() => { vi.resetModules(); vi.unmock('@/config'); vi.unmock('@/utils/debug-info'); vi.unmock('@/utils/git-hash'); }); describe('view exports', () => { it('re-exports view helpers from render', () => { expect(RenderAtom).toBe(Atom); expect(RenderRSS).toBe(RSS); expect(renderJson).toBe(jsonView); expect(renderRss3).toBe(rss3View); }); }); describe('Atom view', () => { it('renders optional fields and media extensions', () => { const html = renderToString( ); expect(html).toContain('https://example.com/icon.png'); expect(html).toContain('https://example.com/logo.png'); expect(html).toContain('media:content'); expect(html).toContain('term="News"'); expect(html).toContain('term="Tech"'); expect(html).toContain('rsshub:upvotes'); expect(html).toContain('rsshub:downvotes'); expect(html).toContain('rsshub:comments'); }); }); describe('RSS view', () => { it('renders itunes, media, and telegram image variants', () => { const html = renderToString( ); expect(html).toContain('xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"'); expect(html).toContain('xmlns:media="http://search.yahoo.com/mrss/"'); expect(html).toContain('Podcast Author'); expect(html).toContain('itunes:category text="Tech"'); expect(html).toContain('true'); expect(html).toContain('31'); expect(html).toContain('88'); expect(html).toContain('media:content'); expect(html).toContain('Podcast'); expect(html).toContain('News'); }); }); describe('JSON view', () => { it('renders summary, authors, tags, attachments, and extras', () => { const jsonOutput = jsonView({ title: 'JSON Feed', link: 'https://example.com', feedLink: 'https://example.com/feed.json', description: 'JSON Description', language: 'en', author: 'Feed Author', image: 'https://example.com/icon.png', item: [ { title: 'Item One', link: 'https://example.com/one', description: 'Entry One', guid: 'guid-1', content: { html: '

hello

', text: 'hello', }, image: 'https://example.com/image.jpg', banner: 'https://example.com/banner.jpg', pubDate: '2024-01-01T00:00:00Z', updated: '2024-01-02T00:00:00Z', author: 'Item Author', category: ['Tech', 'AI'], enclosure_url: 'https://example.com/audio.mp3', enclosure_type: 'audio/mpeg', enclosure_title: 'Audio Title', enclosure_length: 123, itunes_duration: 321, _extra: { foo: 'bar' }, }, ], }); const feed = JSON.parse(jsonOutput); expect(feed.version).toBe('https://jsonfeed.org/version/1.1'); expect(feed.title).toBe('JSON Feed'); expect(feed.home_page_url).toBe('https://example.com'); expect(feed.feed_url).toBe('https://example.com/feed.json'); expect(feed.description).toBe('JSON Description - Powered by RSSHub'); expect(feed.authors).toEqual([{ name: 'Feed Author' }]); expect(feed.items).toHaveLength(1); expect(feed.items[0]).toMatchObject({ id: 'guid-1', url: 'https://example.com/one', title: 'Item One', content_html: '

hello

', content_text: 'hello', summary: 'Entry One', image: 'https://example.com/image.jpg', banner_image: 'https://example.com/banner.jpg', date_published: '2024-01-01T00:00:00Z', date_modified: '2024-01-02T00:00:00Z', authors: [{ name: 'Item Author' }], tags: ['Tech', 'AI'], attachments: [ { url: 'https://example.com/audio.mp3', mime_type: 'audio/mpeg', title: 'Audio Title', size_in_bytes: 123, duration_in_seconds: 321, }, ], }); expect(feed.items[0]._extra).toEqual({ foo: 'bar' }); }); }); describe('Index view', () => { const renderIndex = async (debugInfo: string | undefined, debugQuery: string | undefined) => { const debugData = { hitCache: 2, request: 10, etag: 3, error: 1, routes: { '/foo': 5, '/bar': 2, }, paths: { '/foo?x=1': 4, '/bar?x=2': 1, }, errorRoutes: { '/error': 2, '/fail': 1, }, errorPaths: { '/error?x=1': 1, '/fail?x=2': 1, }, }; vi.doMock('@/config', () => ({ config: { debugInfo, disallowRobot: true, nodeName: 'TestNode', cache: { routeExpire: 120, }, }, })); vi.doMock('@/utils/debug-info', () => ({ getDebugInfo: () => debugData, })); vi.doMock('@/utils/git-hash', () => ({ gitHash: 'abc123', gitDate: new Date('2020-01-01T00:00:00Z'), })); const { default: Index } = await import('@/views/index'); return renderToString(); }; it('shows debug info when enabled', async () => { const html = await renderIndex('secret', 'secret'); expect(html).toContain('Debug Info'); expect(html).toContain('TestNode'); expect(html).toContain('abc123'); expect(html).toContain('5 /foo'); expect(html).toContain('2 /error'); }); it('hides debug info when disabled', async () => { const html = await renderIndex('false', 'secret'); expect(html).not.toContain('Debug Info'); }); });