html_test.go

  1package view
  2
  3import (
  4	"strings"
  5	"testing"
  6
  7	"github.com/charmbracelet/lipgloss"
  8)
  9
 10func TestDecodeQuotedPrintable(t *testing.T) {
 11	testCases := []struct {
 12		name     string
 13		input    string
 14		expected string
 15	}{
 16		{
 17			name:     "Simple case",
 18			input:    "Hello=2C world=21",
 19			expected: "Hello, world!",
 20		},
 21		{
 22			name:     "With soft line break",
 23			input:    "This is a long line that gets wrapped=\r\n and continues here.",
 24			expected: "This is a long line that gets wrapped and continues here.",
 25		},
 26		{
 27			name:     "No encoding",
 28			input:    "Just a plain string.",
 29			expected: "Just a plain string.",
 30		},
 31	}
 32
 33	for _, tc := range testCases {
 34		t.Run(tc.name, func(t *testing.T) {
 35			decoded, err := decodeQuotedPrintable(tc.input)
 36			if err != nil {
 37				t.Fatalf("decodeQuotedPrintable() failed: %v", err)
 38			}
 39			if decoded != tc.expected {
 40				t.Errorf("Expected %q, got %q", tc.expected, decoded)
 41			}
 42		})
 43	}
 44}
 45
 46func TestMarkdownToHTML(t *testing.T) {
 47	testCases := []struct {
 48		name     string
 49		input    string
 50		expected string
 51	}{
 52		{
 53			name:     "Heading",
 54			input:    "# Hello",
 55			expected: "<h1>Hello</h1>",
 56		},
 57		{
 58			name:     "Bold",
 59			input:    "**bold text**",
 60			expected: "<p><strong>bold text</strong></p>",
 61		},
 62		{
 63			name:     "Link",
 64			input:    "[link](http://example.com)",
 65			expected: `<p><a href="http://example.com">link</a></p>`,
 66		},
 67	}
 68
 69	for _, tc := range testCases {
 70		t.Run(tc.name, func(t *testing.T) {
 71			html := markdownToHTML([]byte(tc.input))
 72			// Trim newlines for consistent comparison
 73			if strings.TrimSpace(string(html)) != tc.expected {
 74				t.Errorf("Expected %s, got %s", tc.expected, html)
 75			}
 76		})
 77	}
 78}
 79
 80func TestProcessBody(t *testing.T) {
 81	h1Style := lipgloss.NewStyle().SetString("H1")
 82	h2Style := lipgloss.NewStyle().SetString("H2")
 83	bodyStyle := lipgloss.NewStyle().SetString("BODY")
 84
 85	testCases := []struct {
 86		name     string
 87		input    string
 88		expected string
 89	}{
 90		{
 91			name:     "Simple HTML",
 92			input:    "<p>Hello, world!</p>",
 93			expected: "Hello, world!",
 94		},
 95		{
 96			name:     "With link HTML",
 97			input:    `<a href="http://example.com">Click here</a>`,
 98			expected: "Click here",
 99		},
100		{
101			name:     "With image HTML",
102			input:    `<img src="http://example.com/img.png" alt="alt text">`,
103			expected: "[Click here to view image: alt text]",
104		},
105		{
106			name:     "With headers HTML",
107			input:    "<h1>Header 1</h1>",
108			expected: "Header 1",
109		},
110		{
111			name:     "With link Markdown",
112			input:    `[Click here](http://example.com)`,
113			expected: "Click here",
114		},
115		{
116			name:     "With image Markdown",
117			input:    `![alt text](http://example.com/img.png)>`,
118			expected: "[Click here to view image: alt text]",
119		},
120		{
121			name:     "With headers Markdown",
122			input:    "# Header 1",
123			expected: "Header 1",
124		},
125	}
126
127	for _, tc := range testCases {
128		t.Run(tc.name, func(t *testing.T) {
129			processed, err := ProcessBody(tc.input, h1Style, h2Style, bodyStyle)
130			if err != nil {
131				t.Fatalf("ProcessBody() failed: %v", err)
132			}
133			// Use Contains because styles add ANSI codes
134			if !strings.Contains(processed, tc.expected) {
135				t.Errorf("Processed body does not contain expected text.\nGot: %q\nWant to contain: %q", processed, tc.expected)
136			}
137		})
138	}
139}