package journal_test import ( "encoding/json" "fmt" "net/http" "net/http/httptest" "os" "path/filepath" "strings" "testing" "git.golem15.com/golem15/sm-journal-plugin/console" "git.golem15.com/golem15/sm-journal-plugin/controllers" "git.golem15.com/golem15/sm-journal-plugin/models" "git.golem15.com/golem15/summercms/modules/bonfire" ) const journalAPIPrefix = "/_journal/api/v1" // setH1ToH2 switches the Journal H1 setting on the settings row. func setH1ToH2(t *testing.T, env *adminEnv, on bool) { t.Helper() if err := env.gdb.Exec(`UPDATE golem15_journal_settings SET content_h1_to_h2 = ? WHERE id = 1`, on).Error; err != nil { t.Fatal(err) } } // publishPost publishes a post with a past published_at. func publishPost(t *testing.T, env *adminEnv, id uint) { t.Helper() if err := env.gdb.Exec(`UPDATE golem15_journal_posts SET published = TRUE, published_at = NOW() - INTERVAL '1 hour' WHERE id = ?`, id).Error; err != nil { t.Fatal(err) } } // showH1Flag is GET /_journal/api/v1/posts/{slug}'s content_h1_to_h2. func showH1Flag(t *testing.T, env *adminEnv, slug string) any { t.Helper() rec := env.expect(t, http.StatusOK, http.MethodGet, journalAPIPrefix+"/posts/"+slug, "", nil) var body struct { Data map[string]any `json:"data"` } if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil { t.Fatalf("show body %s: %v", rec.Body.String(), err) } flag, ok := body.Data["content_h1_to_h2"] if !ok { t.Fatalf("show answer has no content_h1_to_h2: %s", rec.Body.String()) } return flag } // TestHeadingConversionTracer is the 33-02 tracer: an editor saves a post // whose content holds an H1 through the admin and gets an H2 back with the // h1_converted notice, and the public post answer reports the setting. // With the setting off the same save keeps the H1, adds no notice, and the // answer reports false. func TestHeadingConversionTracer(t *testing.T) { env := newPostsEnv(t) token := env.admin(t, controllers.PermissionAccessPosts, controllers.PermissionAccessPublish, controllers.PermissionAccessOtherPosts, ) const submitted = "
x
" save := func(t *testing.T, slug string) (uint, []adminNotice) { t.Helper() rec := env.expect(t, http.StatusCreated, http.MethodPost, adminAPI(adminPostsPath), token, map[string]any{ "title": map[string]any{"en": "Heading " + slug, "pl": "Nagłówek " + slug}, "slug": map[string]any{"en": slug, "pl": slug + "-pl"}, "content": map[string]any{"en": submitted, "pl": submitted}, }) return adminCreatedID(t, rec), adminNotices(t, rec) } t.Run("setting on", func(t *testing.T) { setH1ToH2(t, env, true) id, notices := save(t, "heading-on") assertHostContent(t, env, id, "on", "x
", "x
") pl := translatedAttrs(t, env, id, "pl") if pl["content"] != "x
" || pl["content_html"] != "x
" { t.Fatalf("pl content = %#v, content_html = %#v, want the H2", pl["content"], pl["content_html"]) } for _, locale := range []string{"en", "pl"} { found := false for _, n := range notices { if n.Field == "content" && n.Locale == locale && n.Message == "1 H1 heading was converted to H2, because the page shows the post title as the H1." { found = true } } if !found { t.Fatalf("%s notices = %#v, want the h1_converted phrase with count 1", locale, notices) } } publishPost(t, env, id) if flag := showH1Flag(t, env, "heading-on"); flag != true { t.Fatalf("content_h1_to_h2 = %#v, want true", flag) } }) t.Run("setting off", func(t *testing.T) { setH1ToH2(t, env, false) t.Cleanup(func() { setH1ToH2(t, env, true) }) id, notices := save(t, "heading-off") assertHostContent(t, env, id, "off", submitted, submitted) for _, n := range notices { if strings.Contains(n.Message, "H1") { t.Fatalf("setting off: notice %#v", n) } } if notices != nil { t.Fatalf("setting off: clean save carries meta.notices %#v", notices) } publishPost(t, env, id) if flag := showH1Flag(t, env, "heading-off"); flag != false { t.Fatalf("content_h1_to_h2 = %#v, want false", flag) } // D-50: switching the setting never rewrites stored content. setH1ToH2(t, env, true) var stored models.Post if err := env.gdb.First(&stored, id).Error; err != nil { t.Fatal(err) } if stored.Content != submitted || stored.ContentHTML != submitted { t.Fatalf("switching the setting on rewrote content: %q / %q", stored.Content, stored.ContentHTML) } }) } // cmdInput is a bonfire.Input of flags only. type cmdInput map[string]string func (in cmdInput) Args() []string { return nil } func (in cmdInput) Argument(string) (string, bool) { return "", false } func (in cmdInput) Flags(string) []string { return nil } func (in cmdInput) Flag(name string) (string, bool) { v, ok := in[name]; return v, ok } // apiNotices decodes a Journal API write answer's notices, nil when absent. func apiNotices(t *testing.T, rec *httptest.ResponseRecorder) []map[string]any { t.Helper() var body map[string]json.RawMessage if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil { t.Fatalf("api body %s: %v", rec.Body.String(), err) } raw, ok := body["notices"] if !ok { return nil } var out []map[string]any if err := json.Unmarshal(raw, &out); err != nil { t.Fatalf("notices %s: %v", raw, err) } return out } // apiPostID is the id of a Journal API write answer's post. func apiPostID(t *testing.T, rec *httptest.ResponseRecorder) uint { t.Helper() var body struct { Data struct { ID uint `json:"id"` } `json:"data"` } if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil || body.Data.ID == 0 { t.Fatalf("api answer %s: %v", rec.Body.String(), err) } return body.Data.ID } func storedPost(t *testing.T, env *adminEnv, id uint) models.Post { t.Helper() var p models.Post if err := env.gdb.First(&p, id).Error; err != nil { t.Fatal(err) } return p } const postH1Phrase = "1 H1 heading was converted to H2, because the page shows the post title as the H1." // TestHeadingConversion covers every other Journal rich-text write path // (D-50, CONTEXT 2026-10-28 H1 conversion scope): the API (and so MCP) // create and update with default and translated content, the markdown // path, journal:import-posts, a category's pillar text through the admin, // and the content_h1_to_h2 flag of GET /categories. With the setting off // nothing converts and no notice appears. func TestHeadingConversion(t *testing.T) { env := newPostsEnv(t) token := env.admin(t, controllers.PermissionAccessPosts) posts := journalAPIPrefix + "/posts" t.Run("API create converts default and translated content", func(t *testing.T) { setH1ToH2(t, env, true) rec := env.expect(t, http.StatusCreated, http.MethodPost, posts, token, map[string]any{ "title": "API heading", "content": `x
", Translations: map[string]map[string]any{"pl": {"title": "Import", "content": "x
" || strings.Contains(p.ContentHTML, "x
", }) var cat models.Category if err := env.gdb.First(&cat, adminCreatedID(t, rec)).Error; err != nil { t.Fatal(err) } return cat.Content, adminNotices(t, rec) } setH1ToH2(t, env, true) content, notices := save(t, "pillar-on") if content == nil || *content != "x
" { t.Fatalf("category content = %#v, want the H2", content) } if len(notices) != 1 || notices[0].Field != "content" || notices[0].Message != "1 H1 heading was converted to H2, because the page shows the category name as the H1." { t.Fatalf("category notices = %#v", notices) } setH1ToH2(t, env, false) t.Cleanup(func() { setH1ToH2(t, env, true) }) content, notices = save(t, "pillar-off") if content == nil || *content != "x
" || notices != nil { t.Fatalf("setting off: content = %#v, notices = %#v", content, notices) } }) t.Run("GET /categories reports the setting on every row", func(t *testing.T) { for _, on := range []bool{true, false} { setH1ToH2(t, env, on) rec := env.expect(t, http.StatusOK, http.MethodGet, journalAPIPrefix+"/categories?include_empty=1", "", nil) var body struct { Data []map[string]any `json:"data"` } if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil || len(body.Data) == 0 { t.Fatalf("categories %s: %v", rec.Body.String(), err) } for _, row := range body.Data { if row["content_h1_to_h2"] != on { t.Fatalf("setting %v: row %#v", on, row) } } } setH1ToH2(t, env, true) }) }