Files
pages/internal/grammaratur/authoring_test.go
T
Anya Tokugawa 667776e43f
Build and publish container images / images (push) Successful in 9m29s
Release container images / release (push) Successful in 18s
feat(tasks): add keyboard index and productivity dashboard improvements
2026-11-10 07:12:15 +09:00

84 lines
3.4 KiB
Go

package grammaratur
import (
"strings"
"testing"
)
func TestAuthoringRoundTrip(t *testing.T) {
for _, source := range []string{
"<h1>Heading</h1><p>Hello <strong>world</strong>.</p>",
"<p>{{children:/guide}}</p>",
"<figure data-extra=\"kept\"><img src=\"/photo\" alt=\"Photo\"><figcaption>A caption</figcaption></figure>",
"<!-- keep comment --><p id=\"anchor\" class=\"custom\">text</p>",
"<table><tr><td rowspan=\"2\">a</td><td>b</td></tr><tr><td>c</td></tr></table>",
"<p>a <em>b</em> c</p>",
"<pre><code class=\"language-python\"> print('x')\n</code></pre>",
"<custom-element data-x=\"1\">opaque</custom-element>",
"<div class=\"custom\">\n<p>one</p>\n\n<p>two</p>\n</div>",
"<script>const x = '<b>untouched</b>';</script>",
} {
t.Run(source, func(t *testing.T) {
md, err := HTMLToMarkdownAuthoring(source)
if err != nil {
t.Fatal(err)
}
if htmlStructure(MarkdownAuthoringHTML(md)) != htmlStructure(source) {
t.Fatalf("lossy conversion: %q", md)
}
})
}
md, err := HTMLToMarkdownAuthoring("<h1>Hello</h1>")
if err != nil || md != "# Hello" {
t.Fatalf("simple Markdown conversion: %q %v", md, err)
}
for _, source := range []string{`<p data-custom="yes">Hello</p>`, `<!-- saved --><h1>Title</h1>`, `<p>a <em>b</em> c</p>`} {
md, err := HTMLToMarkdownAuthoring(source)
if err != nil {
t.Fatal(err)
}
if htmlStructure(MarkdownAuthoringHTML(md)) != htmlStructure(source) {
t.Fatal("whitespace/attributes lost")
}
}
}
func TestAuthoringProtectedHTML(t *testing.T) {
for _, raw := range []string{"<figure data-x='a'>a</figure>", "<pre>```\n{=html}\n````</pre>\n", "<script>const raw = `x`</script>", "<p>line\r\n</p>"} {
md := rawHTMLBlock(raw)
if got := MarkdownAuthoringHTML(md); got != raw {
t.Fatalf("raw bytes changed: %q want %q", got, raw)
}
mixed := MarkdownAuthoringHTML("# Added\n\n" + md + "\n\nTail")
if !strings.Contains(mixed, raw) || !strings.Contains(mixed, "<h1>Added</h1>") || !strings.Contains(mixed, "<p>Tail</p>") {
t.Fatalf("mixed blocks: %q", mixed)
}
}
code := "````text\n```{=html}\n<script>unsafe()</script>\n```\n````"
if strings.Contains(MarkdownAuthoringHTML(code), "<script>") {
t.Fatal("HTML escaped out of ordinary code sample")
}
}
func TestAuthoringWhitespaceComparisonIsConservative(t *testing.T) {
if htmlStructure("<p>a <em>b</em> c</p>") == htmlStructure("<p>a<em>b</em>c</p>") {
t.Fatal("word boundaries ignored")
}
}
func TestAuthoringPartialMarkdownRecovery(t *testing.T) {
source := `<h1>Title</h1><p>Some <strong>bold</strong> text.</p><figure data-x="keep"><img src="/image"><figcaption>Caption</figcaption></figure><ul><li>First</li><li>Second</li></ul>`
md, err := HTMLToMarkdownAuthoring(source)
if err != nil || !strings.HasPrefix(md, "# Title") || !strings.Contains(md, "**bold**") || strings.Count(md, "{=html}") != 1 || !strings.Contains(md, "- First") {
t.Fatalf("markdown not recovered: %q %v", md, err)
}
if htmlStructure(source) != htmlStructure(MarkdownAuthoringHTML(md)) {
t.Fatal("HTML lost during partial conversion")
}
protected := `<figure data-x='original'><img src=/image><figcaption>Caption</figcaption></figure>`
md, err = HTMLToMarkdownAuthoring("<h1>Title</h1>" + protected + "<p>Tail</p>")
if err != nil || !strings.Contains(MarkdownAuthoringHTML(md), protected) || !strings.HasPrefix(md, "# Title") {
t.Fatalf("protected bytes changed: %q %v", md, err)
}
}