ai_xiaopei
2026-07-25 3e73d7aa07b288e06bd475d96d1a72dfc304adc4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package vault
 
import (
    "os"
    "path/filepath"
    "testing"
)
 
func TestParseFile(t *testing.T) {
    // 创建临时测试文件
    tmpDir := t.TempDir()
    testFile := filepath.Join(tmpDir, "test.md")
    content := `---
title: 测试文档
tags:
  - 充装
  - 智能枪
entities:
  - YSP-35.5
---
 
# 测试内容
 
这是一个关于 [[充装规格配置]] 的文档。
也引用了 [[智能枪使用指南]]。
`
    os.WriteFile(testFile, []byte(content), 0644)
 
    meta, err := ParseFile(testFile, "FAQ/充装类/test.md")
    if err != nil {
        t.Fatalf("ParseFile failed: %v", err)
    }
 
    if meta.Title != "测试文档" {
        t.Errorf("Title = %q, want %q", meta.Title, "测试文档")
    }
    if len(meta.Tags) != 2 || meta.Tags[0] != "充装" {
        t.Errorf("Tags = %v, want [充装 智能枪]", meta.Tags)
    }
    if len(meta.Entities) != 1 || meta.Entities[0] != "YSP-35.5" {
        t.Errorf("Entities = %v, want [YSP-35.5]", meta.Entities)
    }
    if len(meta.Wikilinks) != 2 {
        t.Errorf("Wikilinks count = %d, want 2", len(meta.Wikilinks))
    }
    if meta.Section != "FAQ" {
        t.Errorf("Section = %q, want %q", meta.Section, "FAQ")
    }
}
 
func TestParseFileNoFrontmatter(t *testing.T) {
    tmpDir := t.TempDir()
    testFile := filepath.Join(tmpDir, "nofm.md")
    content := `# 没有 frontmatter
 
只有内容。
`
    os.WriteFile(testFile, []byte(content), 0644)
 
    meta, err := ParseFile(testFile, "笔记/nofm.md")
    if err != nil {
        t.Fatalf("ParseFile failed: %v", err)
    }
 
    if meta.Title != "" {
        t.Errorf("Title = %q, want empty", meta.Title)
    }
    if len(meta.Tags) != 0 {
        t.Errorf("Tags = %v, want empty", meta.Tags)
    }
}