import { describe, it, expect } from 'vitest'; import { splitFrontmatter, joinFrontmatter } from '../../apps/desktop/src/main/safe/frontmatter-codec'; import type { DocMeta } from '@hunter/shared'; describe('splitFrontmatter', () => { it('无 frontmatter 的纯 Markdown', () => { const { meta, body } = splitFrontmatter('# 标题\n正文'); expect(meta).toEqual({}); expect(body).toBe('# 标题\n正文'); }); it('解析四字段', () => { const raw = '---\ntitle: 我的标题\ntags: [a, b]\nstar: true\nlinks: [x/y.md]\n---\n# 正文'; const { meta, body } = splitFrontmatter(raw); expect(meta).toEqual({ title: '我的标题', tags: ['a','b'], star: true, links: ['x/y.md'] }); expect(body).toBe('# 正文'); }); it('块式 YAML 列表(多行 - item)', () => { const raw = '---\ntitle: 标题\nlinks:\n - a.md\n - b.md\ntags:\n - x\n - y\n---\n# 正文'; const { meta, body } = splitFrontmatter(raw); expect(meta.links).toEqual(['a.md', 'b.md']); expect(meta.tags).toEqual(['x', 'y']); expect(meta.title).toBe('标题'); expect(body).toBe('# 正文'); }); it('块式列表后紧跟其他字段', () => { const raw = '---\nlinks:\n - a.md\nstar: true\n---\n# B'; const { meta } = splitFrontmatter(raw); expect(meta.links).toEqual(['a.md']); expect(meta.star).toBe(true); }); it('带引号的值', () => { const { meta } = splitFrontmatter('---\ntitle: "含: 冒号"\n---\n'); expect(meta.title).toBe('含: 冒号'); }); it('空数组与 false 不解析为有值', () => { const { meta } = splitFrontmatter('---\ntags: []\nstar: false\n---\n'); expect(meta.tags).toEqual([]); expect(meta.star).toBe(false); }); }); describe('joinFrontmatter (FR-M2 精简)', () => { it('空 meta 不写围栏', () => { expect(joinFrontmatter({}, '# 正文')).toBe('# 正文'); }); it('默认值不落盘:star:false / 空数组', () => { const out = joinFrontmatter({ star: false, tags: [], links: [] }, 'body'); expect(out).toBe('body'); }); it('star:true 落盘', () => { const out = joinFrontmatter({ star: true }, 'body'); expect(out).toContain('star: true'); }); it('含特殊字符的标题加引号', () => { const out = joinFrontmatter({ title: 'a:b#c' }, 'body'); expect(out).toContain('title: "a:b#c"'); }); it('往返:解析后再序列化保持等价', () => { const raw = '---\ntitle: T\ntags: [x, y]\nstar: true\n---\n# H\n正文'; const { meta, body } = splitFrontmatter(raw); const out = joinFrontmatter(meta, body); const again = splitFrontmatter(out); expect(again.meta).toEqual(meta); expect(again.body).toBe(body); }); });