// frontmatter_test.go -- YAML frontmatter 解析器的单元测试. // // 覆盖场景: // - ParseFrontmatter 正常解析 // - 无 frontmatter 的内容 // - 缺少结束分隔符 // - FormatFrontmatter 格式化输出 // - 往返一致性(Parse -> Format -> Parse) package memory import ( "strings" "testing" ) // TestParseFrontmatter_Normal 测试正常解析 func TestParseFrontmatter_Normal(t *testing.T) { content := `--- name: test-memory description: a test memory type: project --- This is the body content.` fm, body, ok := ParseFrontmatter(content) if !ok { t.Fatal("应成功解析") } if fm.Name != "test-memory" { t.Errorf("Name: %q, 期望 'test-memory'", fm.Name) } if fm.Description != "a test memory" { t.Errorf("Description: %q", fm.Description) } if fm.Type != TypeProject { t.Errorf("Type: %q, 期望 'project'", fm.Type) } if !strings.Contains(body, "body content") { t.Errorf("Body 应包含 'body content': %q", body) } } // TestParseFrontmatter_NoFrontmatter 测试无 frontmatter 的内容 func TestParseFrontmatter_NoFrontmatter(t *testing.T) { content := "Just plain text without frontmatter." _, body, ok := ParseFrontmatter(content) if ok { t.Error("无 frontmatter 应返回 ok=false") } if body != content { t.Errorf("body 应返回原始内容") } } // TestParseFrontmatter_MissingEndDelimiter 测试缺少结束分隔符 func TestParseFrontmatter_MissingEndDelimiter(t *testing.T) { content := `--- name: test description: incomplete ` _, _, ok := ParseFrontmatter(content) if ok { t.Error("缺少结束分隔符应返回 ok=false") } } // TestParseFrontmatter_EmptyName 测试空 name func TestParseFrontmatter_EmptyName(t *testing.T) { content := `--- description: no name type: user --- Body` _, _, ok := ParseFrontmatter(content) if ok { t.Error("空 name 应返回 ok=false") } } // TestParseFrontmatter_EmptyBody 测试空正文 func TestParseFrontmatter_EmptyBody(t *testing.T) { content := `--- name: test description: desc type: user ---` fm, body, ok := ParseFrontmatter(content) if !ok { t.Fatal("应成功解析") } if fm.Name != "test" { t.Errorf("Name: %q", fm.Name) } if body != "" { t.Errorf("body 应为空, 实际: %q", body) } } // TestFormatFrontmatter 测试格式化输出 func TestFormatFrontmatter(t *testing.T) { fm := Frontmatter{ Name: "my-memory", Description: "desc", Type: TypeFeedback, } body := "Body content" result := FormatFrontmatter(fm, body) if !strings.Contains(result, "---\n") { t.Error("应包含分隔符 ---") } if !strings.Contains(result, "name: my-memory") { t.Errorf("应包含 name: %s", result) } if !strings.Contains(result, "type: feedback") { t.Errorf("应包含 type: %s", result) } if !strings.Contains(result, "Body content") { t.Errorf("应包含正文: %s", result) } if !strings.HasSuffix(result, "\n") { t.Error("应以换行符结尾") } } // TestFormatFrontmatter_EmptyBody 测试空正文格式化 func TestFormatFrontmatter_EmptyBody(t *testing.T) { fm := Frontmatter{Name: "test", Description: "desc", Type: TypeUser} result := FormatFrontmatter(fm, "") if !strings.HasSuffix(result, "---\n") { t.Errorf("空正文应以 --- 结尾: %q", result) } } // TestFrontmatter_Roundtrip 测试往返一致性 func TestFrontmatter_Roundtrip(t *testing.T) { original := Frontmatter{ Name: "roundtrip-test", Description: "testing roundtrip", Type: TypeReference, } body := "Original body content." formatted := FormatFrontmatter(original, body) parsed, parsedBody, ok := ParseFrontmatter(formatted) if !ok { t.Fatal("往返解析失败") } if parsed.Name != original.Name { t.Errorf("Name 不一致: %q vs %q", parsed.Name, original.Name) } if parsed.Description != original.Description { t.Errorf("Description 不一致: %q vs %q", parsed.Description, original.Description) } if parsed.Type != original.Type { t.Errorf("Type 不一致: %q vs %q", parsed.Type, original.Type) } if strings.TrimSpace(parsedBody) != body { t.Errorf("Body 不一致: %q vs %q", parsedBody, body) } }