ref: 7fb1fe4361376bb6b2809b1ae70b468c467bc4bd
parent: 4d2d9e021317ae27fcdb6d216cde50dd9da766ac
author: fops <billgates@cock.li>
date: Fri Oct 17 18:02:45 EDT 2025
Add test IDs, add tests for media description formatting
--- a/renderer.go
+++ b/renderer.go
@@ -47,9 +47,9 @@
for _, item := range status.MediaAttachments { if item.Description != "" { sb.WriteString(fmt.Sprintf("\n%s [%s]", mediaTag, item.Description))- continue
+ } else {+ sb.WriteString(mediaTag)
}
- sb.WriteString(mediaTag)
}
return sb.String()
}
--- a/renderer_test.go
+++ b/renderer_test.go
@@ -8,32 +8,30 @@
func TestStatusFormatterStatus(t *testing.T) { parameters := []struct {- post, expected string
+ id int
+ post string
+ expected string
}{- {"hello world", "hello world"},- {"<p>hello world</p>", "hello world"},- {"<p>hello <a href=\"https://example.com\">world</a></p>", "hello (world) [1]"},- {"<p>hello <a href=\"https://example.com\">world</a> and <a href=\"https://example.org\">universe</a></p>", "hello (world) [1] and (universe) [2]"},- {"<p>hello & world</p>", "hello & world"},- {"<p>hello<br>world</p>", "hello\r\nworld"},- {"<p>hello<br/>world</p>", "hello\r\nworld"},- {"<p>hello<br />world</p>", "hello\r\nworld"},- // consequetive NBSP shouldn't be collapsed
- {"<p>hello world</p>", "hello\u00a0world"},- {"<p>hello world</p>", "hello\u00a0\u00a0world"},- {"<p>hello world</p>", "hello\u00a0\u00a0\u00a0world"},- {"<p>hello<br> world</p>", "hello\r\n\u00a0world"},- {"<p>hello<br> world</p>", "hello\r\n\u00a0\u00a0world"},- {"<p>hello<br> world</p>", "hello\r\n\u00a0\u00a0\u00a0world"},- // <pre> tests
- {"<pre>hello world</pre>", "hello world"},- {"<pre>hello\nworld</pre>", "hello\nworld"},- {"<pre>hello <a href=\"https://example.com\">world</a></pre>", "hello (world) [1]"},- {"<pre>hello & world</pre>", "hello & world"},- // Mentions
- {"<p>hello <a href=\"https://example.com\" class=\"mention\">username</a> world</p>", "hello username world"},- // a without href
- {"<p>hello<a>world</a></p>", "hello"},+ {1, "hello world", "hello world"},+ {2, "<p>hello world</p>", "hello world"},+ {3, "<p>hello <a href=\"https://example.com\">world</a></p>", "hello (world) [1]"},+ {4, "<p>hello <a href=\"https://example.com\">world</a> and <a href=\"https://example.org\">universe</a></p>", "hello (world) [1] and (universe) [2]"},+ {5, "<p>hello & world</p>", "hello & world"},+ {6, "<p>hello<br>world</p>", "hello\r\nworld"},+ {7, "<p>hello<br/>world</p>", "hello\r\nworld"},+ {8, "<p>hello<br />world</p>", "hello\r\nworld"},+ {9, "<p>hello world</p>", "hello\u00a0world"},+ {10, "<p>hello world</p>", "hello\u00a0\u00a0world"},+ {11, "<p>hello world</p>", "hello\u00a0\u00a0\u00a0world"},+ {12, "<p>hello<br> world</p>", "hello\r\n\u00a0world"},+ {13, "<p>hello<br> world</p>", "hello\r\n\u00a0\u00a0world"},+ {14, "<p>hello<br> world</p>", "hello\r\n\u00a0\u00a0\u00a0world"},+ {15, "<pre>hello world</pre>", "hello world"},+ {16, "<pre>hello\nworld</pre>", "hello\nworld"},+ {17, "<pre>hello <a href=\"https://example.com\">world</a></pre>", "hello (world) [1]"},+ {18, "<pre>hello & world</pre>", "hello & world"},+ {19, "<p>hello <a href=\"https://example.com\" class=\"mention\">username</a> world</p>", "hello username world"},+ {20, "<p>hello<a>world</a></p>", "hello"},}
for _, param := range parameters { sf := &StatusFormatter{@@ -44,7 +42,59 @@
}
rendered := sf.statusContent(sf.status)
if rendered != param.expected {- t.Errorf("Expected rendered content to be '%s', got '%s'", param.expected, rendered)+ t.Errorf("ID %d: Expected rendered content to be '%s', got '%s'", param.id, param.expected, rendered)+ }
+
+ // Piggyback the test cases for reblog tests
+ sf.status = &mastodon.Status{+ Reblog: &mastodon.Status{+ Content: param.post,
+ },
+ }
+ reblog_rendered := sf.reblogContent()
+ if reblog_rendered != param.expected {+ t.Errorf("ID %d: Expected rendered reblog content to be '%s', got '%s'", param.id, param.expected, reblog_rendered)+ }
+ }
+}
+
+func TestStatusFormatterMediaDescriptions(t *testing.T) {+ parameters := []struct {+ id int
+ descriptions []string
+ expected string
+ isReblog bool
+ }{+ {0, []string{"A beautiful sunrise"}, "\n🖼️ [A beautiful sunrise]", false},+ // TODO; verify whether empty description should produce newline
+ {1, []string{"", "An amazing waterfall"}, "🖼️\n🖼️ [An amazing waterfall]", false},+ {2, []string{"", ""}, "🖼️🖼️", false},+ {3, []string{}, "", false},+ // Reblog behavior
+ {4, []string{"A reblogged image"}, "\n🖼️ [A reblogged image]", true},+ }
+ for _, param := range parameters {+ var mediaAttachments []mastodon.Attachment
+ for _, desc := range param.descriptions {+ mediaAttachments = append(mediaAttachments, mastodon.Attachment{Description: desc})+ }
+
+ sf := &StatusFormatter{+ status: &mastodon.Status{},+ prefs: &Hellprefs{MediaTag: "🖼️"},+ }
+
+ if param.isReblog {+ // When testing reblog behavior, populate the reblogged status
+ sf.status.Reblog = &mastodon.Status{MediaAttachments: mediaAttachments}+ } else {+ // Otherwise, use attachments on the root status
+ sf.status.MediaAttachments = mediaAttachments
+ }
+
+ rendered := sf.mediaDescriptions(param.isReblog)
+ if rendered != param.expected {+ t.Errorf("ID %d: Expected media descriptions to be '%s', got '%s'", param.id, param.expected, rendered)}
}
}
--
⑨