package format import ( "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestConvertFormatSameFormat(t *testing.T) { t.Parallel() body := "hello world" got, err := ConvertFormat(body, "text", "text") require.NoError(t, err) assert.Equal(t, body, got) } func TestConvertFormatMarkdownToText(t *testing.T) { t.Parallel() tests := []struct { name string input string expected string }{ {"heading", "# Heading", "Heading"}, {"bold", "**bold**", "bold"}, {"italic", "*italic*", "italic"}, {"underscore italic", "_italic_", "italic"}, {"code", "`code`", "code"}, {"link", "[link](http://example.com)", "link"}, {"image alt text", "![alt text](http://example.com/image.png)", "alt text"}, {"plain text", "plain text", "plain text"}, {"list item", "- list item", "list item"}, {"escaped asterisk", "\\*x\\*", "*x*"}, {"escaped underscore", "\\_x\\_", "_x_"}, {"unclosed italic", "*x", "*x"}, {"unclosed bold", "**x", "**x"}, {"empty emphasis", "****", "*"}, {"hr", "---", ""}, {"multiple headings", "# H1\n## H2\n### H3", "H1\nH2\nH3"}, {"mixed formatting", "# Title\n\nHello **bold** and *italic* world!", "Title\n\nHello bold and italic world!"}, {"unicode content", "# 你好\n\n*测试*", "你好\n\n测试"}, {"special chars preserved", "ac&d\"e", "ac&d\"e"}, {"only delimiters", "***", "*"}, {"empty string", "", ""}, {"nested escaped", "\\*\\*bold\\*\\*", "**bold**"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() got, err := ConvertFormat(tt.input, "markdown", "text") require.NoError(t, err) assert.Equal(t, tt.expected, got) }) } } func TestConvertFormatHTMLToText(t *testing.T) { t.Parallel() tests := []struct { name string input string expected string }{ {"paragraph", "

hello

", "hello"}, {"bold", "bold", "bold"}, {"strong", "bold", "bold"}, {"italic", "italic", "italic"}, {"emphasis", "italic", "italic"}, {"line break", "
", ""}, {"self-closing br", "
", ""}, {"plain text", "plain text", "plain text"}, {"escaped entities", "<escaped>", ""}, {"nested tags", "

nested

", "nested"}, {"heading", "

Title

", "Title"}, {"multiple headings", "

A

B

", "AB"}, {"unicode content", "

你好

", "你好"}, {"empty string", "", ""}, {"script tag", "", "alert('xss')"}, {"link tag", "link", "link"}, {"image tag", "\"alt", ""}, {"list items", "
  • A
  • B
", "AB"}, {"horizontal rule", "
", ""}, {"comment", "", ""}, {"multiple paragraphs", "

A

B

", "AB"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() got, err := ConvertFormat(tt.input, "html", "text") require.NoError(t, err) assert.Equal(t, tt.expected, got) }) } } func TestConvertFormatTextToMarkdown(t *testing.T) { t.Parallel() tests := []struct { name string input string expected string }{ {"plain text", "plain text", "plain text"}, {"asterisk", "use * for emphasis", "use \\* for emphasis"}, {"underscore", "use _ for emphasis", "use \\_ for emphasis"}, {"backtick", "code: `cmd`", "code: \\`cmd\\`"}, {"bracket", "[not a link]", "\\[not a link]"}, {"multiple special", "*_`[", "\\*\\_\\`\\["}, {"empty string", "", ""}, {"unicode safe", "你好世界", "你好世界"}, {"mixed safe and special", "hello * world", "hello \\* world"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() got, err := ConvertFormat(tt.input, "text", "markdown") require.NoError(t, err) assert.Equal(t, tt.expected, got) }) } } func TestConvertFormatTextToHTML(t *testing.T) { t.Parallel() tests := []struct { name string input string expected string }{ {"angle brackets", "