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) } }