topic-content.test.js
1 import { getRegistry } from '@jackwener/opencli/registry'; 2 import fs from 'node:fs'; 3 import { describe, expect, it } from 'vitest'; 4 import { __test__ } from './topic-content.js'; 5 describe('linux-do topic-content', () => { 6 it('prefers raw markdown when the topic payload includes it', () => { 7 const result = __test__.extractTopicContent({ 8 title: 'Hello Linux.do', 9 post_stream: { 10 posts: [ 11 { 12 post_number: 1, 13 username: 'neo', 14 raw: '## Heading\n\n- one\n- two', 15 cooked: '<h2>Heading</h2><ul><li>one</li><li>two</li></ul>', 16 like_count: 7, 17 created_at: '2025-04-05T10:00:00.000Z', 18 }, 19 ], 20 }, 21 }, 1234); 22 expect(result.content).toContain('---'); 23 expect(result.content).toContain('title: Hello Linux.do'); 24 expect(result.content).toContain('author: neo'); 25 expect(result.content).toContain('likes: 7'); 26 expect(result.content).toContain('url: https://linux.do/t/1234'); 27 expect(result.content).toContain('## Heading'); 28 expect(result.content).toContain('- one'); 29 }); 30 it('falls back to cooked html and converts it to markdown', () => { 31 const result = __test__.extractTopicContent({ 32 title: 'Converted Topic', 33 post_stream: { 34 posts: [ 35 { 36 post_number: 1, 37 username: 'trinity', 38 cooked: '<p>Hello <strong>world</strong></p><blockquote><p>quoted</p></blockquote>', 39 like_count: 3, 40 created_at: '2025-04-05T10:00:00.000Z', 41 }, 42 ], 43 }, 44 }, 42); 45 expect(result.content).toContain('Hello **world**'); 46 expect(result.content).toContain('> quoted'); 47 }); 48 it('registers topic-content with plain default output for markdown body rendering', () => { 49 const command = getRegistry().get('linux-do/topic-content'); 50 expect(command?.defaultFormat).toBe('plain'); 51 expect(command?.columns).toEqual(['content']); 52 }); 53 it('keeps topic adapter as a summarized first-page reader after the split', () => { 54 const topicTs = fs.readFileSync(new URL('./topic.js', import.meta.url), 'utf8'); 55 expect(topicTs).not.toContain('main_only'); 56 expect(topicTs).toContain('slice(0, 200)'); 57 expect(topicTs).toContain('帖子首页摘要和回复'); 58 }); 59 });