package test import ( domainArticle "RenewCMS/internal/domain/article" "RenewCMS/internal/infrastructure/markdown" "errors" "testing" ) func TestMarkdownConverterFidelity(t *testing.T) { converter := markdown.NewConverter() examples := []struct { name, html, markdown string }{ {"headings", "

T

S

", "# T\n\n### S"}, {"bold and italic", "

b i

", "**b** *i*"}, {"link", `

link

`, "[link](https://example.com/x)"}, {"bullet list", "", "- a\n- b"}, {"ordered list", "
  1. a

  2. b

", "1. a\n2. b"}, {"data-indent dropped", ``, "- a"}, {"nested list", "", "- a\n - n"}, {"blockquote", "

q

", "> q"}, {"code block", `
x := 1
`, "```go\nx := 1\n```"}, {"rule", "
", "---"}, {"image", `A`, "![A](/static/uploadedImages/a.png)"}, {"script and style removed", "

ok

", "ok"}, {"unsupported span stripped", `

xy

`, "xy"}, {"inline code", "

use go test

", "use `go test`"}, {"empty body", "", ""}, {"line break", "

a
b

", "a\\\nb"}, {"literal angle bracket is escaped", "

<script>

", "\\", "```\na\nb\n```"}, {"adjacent same marks merge", "

ab

", "**ab**"}, {"adjacent different marks stay apart", "

ab

", "**a***b*"}, {"two paragraphs", "

a

b

", "a\n\nb"}, {"centered paragraph", `

hi b

`, "
\n\nhi **b**\n\n
"}, {"right aligned heading", `

T

`, "
\n\n## T\n\n
"}, {"justified among other styles", `

a

`, "
\n\na\n\n
"}, {"tiptap style with trailing semicolon", `

a

`, "
\n\na\n\n
"}, {"tiptap explicit left", `

a

`, "a"}, {"left alignment is the default", `

a

`, "a"}, {"unknown alignment ignored", `

a

`, "a"}, {"style without value ignored", `

a

`, "a"}, {"empty aligned paragraph", `

`, ""}, {"aligned paragraph in list item", ``, "-
\n\n a\n\n
"}, {"aligned paragraph in blockquote", `

q

`, ">
\n>\n> q\n>\n>
"}, {"alignment skips the image inside a paragraph", `

xIy

`, "
\n\nx\n\n
\n\n![I](/i.png)\n\n
\n\ny\n\n
"}, {"text characters are escaped", "

a * b _c_ `d` [e]

", "a \\* b \\_c\\_ \\`d\\` \\[e\\]"}, {"leading hash is escaped", "

# not a heading

", "\\# not a heading"}, {"hash without space is kept", "

#tag

", "#tag"}, {"leading dash is escaped", "

- not a list

", "\\- not a list"}, {"leading plus is escaped", "

+ not a list

", "\\+ not a list"}, {"leading quote marker is escaped", "

> not a quote

", "\\> not a quote"}, {"leading number is escaped", "

1. not a list

", "1\\. not a list"}, {"leading number with parenthesis is escaped", "

2) not a list

", "2\\) not a list"}, {"leading rule is escaped", "

---

", "\\---"}, {"leading equals is escaped", "

==

", "\\=="}, {"inner dash is kept", "

a - b

", "a - b"}, {"marker after a line break is escaped", "

a
- b

", "a\\\n\\- b"}, {"combined document", `

Title

Intro bold and it

A
x := 1

quote


`, "# Title\n\n
\n\nIntro **bold** and *it*\n\n
\n\n- one\n- two\n\n![A](/static/uploadedImages/a.png)\n\n```go\nx := 1\n```\n\n> quote\n\n---"}, } for _, example := range examples { t.Run(example.name, func(t *testing.T) { got, err := converter.Render(example.html, domainArticle.BodyFormatMarkdown) if err != nil { t.Fatalf("unexpected error: %v", err) } if got != example.markdown { t.Errorf("expected %q, got %q", example.markdown, got) } }) } } func TestMarkdownConverterHtmlAndUnknownFormat(t *testing.T) { converter := markdown.NewConverter() html := "

a

" got, err := converter.Render(html, domainArticle.BodyFormatHTML) if err != nil || got != html { t.Errorf("html must pass through unchanged, got %q, %v", got, err) } if _, err := converter.Render(html, domainArticle.BodyFormat("pdf")); !errors.Is(err, domainArticle.ErrUnsupportedBodyFormat) { t.Errorf("expected ErrUnsupportedBodyFormat, got %v", err) } }