package test import ( domainArticle "RenewCMS/internal/domain/article" "RenewCMS/internal/infrastructure/markdown" "encoding/json" "reflect" "strings" "testing" ) const hostileBody = `
Intro bold
one
deep

a`
func TestConverterNeverLeaksRawHtml(t *testing.T) {
converter := markdown.NewConverter()
for _, format := range []domainArticle.BodyFormat{domainArticle.BodyFormatMarkdown, domainArticle.BodyFormatJSON} {
got, err := converter.Render(hostileBody, format)
if err != nil {
t.Fatalf("%s: unexpected error: %v", format, err)
}
for _, forbidden := range []string{""} {
got, err := converter.Render(source, domainArticle.BodyFormatJSON)
if err != nil || got != "[]" {
t.Errorf("%q: expected [] got %q, %v", source, got, err)
}
}
}
func TestMarkdownConverterCodeBlockText(t *testing.T) {
converter := markdown.NewConverter()
examples := []struct{ name, html, markdown string }{
{"text node kept", "x", "```\nx\n```"},
{"skipped element dropped", "ab", "```\nab\n```"},
{"style dropped", "a", "```\na\n```"},
{"break becomes newline", "a
b", "```\na\nb\n```"},
{"nested element text kept", "abc", "```\nabc\n```"},
{"trailing newline trimmed", "a\n", "```\na\n```"},
{"fence grows around backticks", "```\nx", "````\n```\nx\n````"},
}
for _, example := range examples {
t.Run(example.name, func(t *testing.T) {
got, err := converter.Render(example.html, domainArticle.BodyFormatMarkdown)
if err != nil || got != example.markdown {
t.Errorf("expected %q, got %q, %v", example.markdown, got, err)
}
})
}
}