Files
sm-journal-plugin/controllers/api/categories_test.go
Jakub Zych faaa9b0f55 feat: H1 conversion on the API, markdown and import paths
- the /_journal/api/v1 post writes convert default and translated content
  in the write transaction and answer an additive notices key
- journal:import-posts converts and prints the converted count
  (ImportFromDirConfig, ImportStats.H1Converted)
- a category's pillar text saved in the admin answers
  editor.h1_converted_category
- every GET /categories row reports content_h1_to_h2 (additive key)
2026-11-04 09:51:47 +01:00

63 lines
2.1 KiB
Go

package api
import (
"reflect"
"sort"
"testing"
"git.golem15.com/golem15/sm-journal-plugin/models"
)
func sortedCategoryKeys(row map[string]any) []string {
keys := make([]string, 0, len(row))
for k := range row {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func TestSerializeCategoryNodeContent(t *testing.T) {
html := "<h2>Acme</h2>\n<p>Pillar copy</p>\n"
var parentID uint = 1
root := models.Category{ID: 1, Name: "News", Slug: "news", Content: &html}
child := models.Category{ID: 2, Name: "Quiet", Slug: "quiet", ParentID: &parentID}
byParent := map[uint][]models.Category{1: {child}}
counts := map[uint]int64{1: 2}
row := serializeCategoryNode(&root, byParent, counts, true, true)
if row == nil {
t.Fatal("root row is nil")
}
if got, ok := row["content"].(string); !ok || got != html {
t.Fatalf("root content = %#v, want %q", row["content"], html)
}
wantRoot := []string{"children", "content", "content_h1_to_h2", "description", "id", "name", "nest_depth", "parent_id", "post_count", "slug"}
if got := sortedCategoryKeys(row); !reflect.DeepEqual(got, wantRoot) {
t.Fatalf("root keys = %v, want %v", got, wantRoot)
}
kids, ok := row["children"].([]map[string]any)
if !ok || len(kids) != 1 {
t.Fatalf("children = %#v, want one row", row["children"])
}
v, present := kids[0]["content"]
if !present || v != nil {
t.Fatalf("child content = %#v (present %v), want nil", v, present)
}
wantChild := []string{"content", "content_h1_to_h2", "description", "id", "name", "nest_depth", "parent_id", "post_count", "slug"}
if got := sortedCategoryKeys(kids[0]); !reflect.DeepEqual(got, wantChild) {
t.Fatalf("child keys = %v, want %v", got, wantChild)
}
listed := serializeCategoryNode(&root, byParent, counts, false, true)
if listed == nil {
t.Fatal("root omitted without include_empty")
}
if _, has := listed["children"]; has {
t.Fatalf("empty child listed without include_empty: %#v", listed["children"])
}
if got, ok := listed["content"].(string); !ok || got != html {
t.Fatalf("root content without include_empty = %#v, want %q", listed["content"], html)
}
}