import { describe, it, expect, vi } from 'vitest'; import { markdownToHtml, wrapHtmlDocument, downloadHtmlFile, SAMPLE_MARKDOWN, } from '@/utils/markdownToHtml'; describe('markdownToHtml', () => { it('应该转换基础 Markdown 标题', () => { const result = markdownToHtml('# Hello World'); expect(result.hasError).toBe(false); expect(result.html).toContain('
');
expect(result.html).toContain(' {
const result = markdownToHtml('use `npm install` command');
expect(result.hasError).toBe(false);
expect(result.html).toContain('npm install');
});
it('应该转换引用块', () => {
const result = markdownToHtml('> This is a quote');
expect(result.hasError).toBe(false);
expect(result.html).toContain('');
expect(result.html).toContain('This is a quote');
});
it('应该转换表格', () => {
const md = '| A | B |\n|---|---|\n| 1 | 2 |';
const result = markdownToHtml(md);
expect(result.hasError).toBe(false);
expect(result.html).toContain('');
expect(result.html).toContain('A ');
expect(result.html).toContain('1 ');
});
it('应该转换任务列表', () => {
const result = markdownToHtml('- [x] done\n- [ ] todo');
expect(result.hasError).toBe(false);
expect(result.html).toContain(' {
const result = markdownToHtml('~~deleted~~');
expect(result.hasError).toBe(false);
expect(result.html).toContain('deleted');
});
it('应该处理空字符串', () => {
const result = markdownToHtml('');
expect(result.hasError).toBe(false);
expect(result.html).toBe('');
expect(result.originalLength).toBe(0);
expect(result.htmlLength).toBe(0);
});
it('应该处理空白字符串', () => {
const result = markdownToHtml(' \n ');
expect(result.hasError).toBe(false);
expect(result.html).toBe('');
});
it('应该处理中文内容', () => {
const result = markdownToHtml('# 你好世界\n\n这是**中文**内容。');
expect(result.hasError).toBe(false);
expect(result.html).toContain('你好世界');
expect(result.html).toContain('中文');
});
it('应该转换示例 Markdown', () => {
const result = markdownToHtml(SAMPLE_MARKDOWN);
expect(result.hasError).toBe(false);
expect(result.html).toContain('');
expect(result.html).toContain('');
expect(result.originalLength).toBe(SAMPLE_MARKDOWN.length);
expect(result.htmlLength).toBeGreaterThan(result.originalLength);
});
});
describe('wrapHtmlDocument', () => {
it('应该生成完整的 HTML 文档', () => {
const doc = wrapHtmlDocument('Hello
', 'Test Title');
expect(doc).toContain('');
expect(doc).toContain('');
expect(doc).toContain('Test Title ');
expect(doc).toContain('');
expect(doc).toContain('Hello
');
expect(doc).toContain('');
});
it('应该转义标题中的特殊字符', () => {
const doc = wrapHtmlDocument('test
', 'Title