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 := "
Pillar copy
\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) 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", "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", "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) 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) } }