shithub: hell

ref: b049b94a7e8df765635e68e7e6d00053ddc93922
dir: /renderer_test.go/

View raw version
package main

import (
	"testing"

	mastodon "codeberg.org/penny64/hellclient-go-mastodon"
)

func TestStatusFormatterStatus(t *testing.T) {
	parameters := []struct {
		id       int
		post     string
		expected string
	}{
		{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{
			status: &mastodon.Status{
				Content: param.post,
			},
			postContext: &postref{ref: "1", urlmap: make(map[string][]string)},
		}
		rendered := sf.statusContent(sf.status)
		if rendered != param.expected {
			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)
		}
	}
}