M0-M11 狩夜人笔记初始实现:11里程碑+86测试
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
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<DocMeta>({ title: '我的标题', tags: ['a','b'], star: true, links: ['x/y.md'] });
|
||||
expect(body).toBe('# 正文');
|
||||
});
|
||||
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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user