shithub: hell

Download patch

ref: 2d81e6db9723aef82dcd1d6ce57d4294a2bdcffd
parent: 88318fb1befad2a9295eb5f4678fae672f39b09e
parent: 7fb1fe4361376bb6b2809b1ae70b468c467bc4bd
author: penny64 <penny64@noreply.codeberg.org>
date: Fri Oct 17 18:27:51 EDT 2025

Merge pull request 'Add mediaDescriptions unittests' (#10) from fops/hellclient:maintenance/more-unittests into main

Reviewed-on: https://codeberg.org/penny64/hellclient/pulls/10

--- a/main.go
+++ b/main.go
@@ -180,6 +180,9 @@
 			case "read":
 				defer hc.prompt.UpdatePrompt()
 				notifications, err := hc.GetUnreadNotifications()
+				if err != nil {
+					fmt.Print(err)
+				}
 				if len(notifications) > 0 {
 					err = hc.SetNotificationsRead(notifications[len(notifications)-1].ID)
 					if err != nil {
--- a/mastodon.go
+++ b/mastodon.go
@@ -190,10 +190,10 @@
 func (hc *Hellclient) RenderPostPlaintext(post *mastodon.Status, ref postref) (selectedPost *mastodon.Status, plaintext string) {
 	if post.Reblog != nil {
 		selectedPost = post.Reblog
-		plaintext = fmt.Sprintf("$username Reblogged $index $boostuser $boostcontent $boosted_media_descriptions")
+		plaintext = "$username Reblogged $index $boostuser $boostcontent $boosted_media_descriptions"
 	} else {
 		selectedPost = post
-		plaintext = fmt.Sprintf("$standard_status")
+		plaintext = "$standard_status"
 	}
 	formatter := &StatusFormatter{prefs: hc.preferences, status: post, postContext: &ref}
 	templater := newStatusTemplateRenderer(formatter)
@@ -253,7 +253,7 @@
 				templater := newStatusTemplateRenderer(formatter)
 				if hc.isPaused {
 					hc.actionBuffer = append(hc.actionBuffer, func() {
-						line, _ := templater.render(fmt.Sprintf("$username EDITED: $index $content $media_descriptions\n"))
+						line, _ := templater.render("$username EDITED: $index $content $media_descriptions\n")
 						fmt.Print(line)
 					})
 					justIncrementPostref(hc.homeref, post.Status)
@@ -260,7 +260,7 @@
 					idmap[post.Status.ID] = post.Status
 					return
 				}
-				line, _ := templater.render(fmt.Sprintf("$username EDITED: $index $content $media_descriptions\n"))
+				line, _ := templater.render("$username EDITED: $index $content $media_descriptions\n")
 				fmt.Print(line)
 				justIncrementPostref(hc.homeref, post.Status)
 				idmap[post.Status.ID] = post.Status
--- a/pages.go
+++ b/pages.go
@@ -165,9 +165,9 @@
 		}
 		var plaintext string
 		if formatter.status.Reblog != nil {
-			plaintext = fmt.Sprintf("$standard_reblog")
+			plaintext = "$standard_reblog"
 		} else {
-			plaintext = fmt.Sprintf("$standard_status")
+			plaintext = "$standard_status"
 		}
 		line, _ := templater.render(plaintext)
 		item := makePageItem(line)
--- 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 &amp; 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&nbsp;world</p>", "hello\u00a0world"},
-		{"<p>hello&nbsp;&nbsp;world</p>", "hello\u00a0\u00a0world"},
-		{"<p>hello&nbsp;&nbsp;&nbsp;world</p>", "hello\u00a0\u00a0\u00a0world"},
-		{"<p>hello<br>&nbsp;world</p>", "hello\r\n\u00a0world"},
-		{"<p>hello<br>&nbsp;&nbsp;world</p>", "hello\r\n\u00a0\u00a0world"},
-		{"<p>hello<br>&nbsp;&nbsp;&nbsp;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 &amp; 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 &amp; 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&nbsp;world</p>", "hello\u00a0world"},
+		{10, "<p>hello&nbsp;&nbsp;world</p>", "hello\u00a0\u00a0world"},
+		{11, "<p>hello&nbsp;&nbsp;&nbsp;world</p>", "hello\u00a0\u00a0\u00a0world"},
+		{12, "<p>hello<br>&nbsp;world</p>", "hello\r\n\u00a0world"},
+		{13, "<p>hello<br>&nbsp;&nbsp;world</p>", "hello\r\n\u00a0\u00a0world"},
+		{14, "<p>hello<br>&nbsp;&nbsp;&nbsp;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 &amp; 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)
 		}
 	}
 }
--