|
| 1 | +package jmap |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestQuoteText(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + input string |
| 14 | + expected string |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "empty string", |
| 18 | + input: "", |
| 19 | + expected: "", |
| 20 | + }, |
| 21 | + { |
| 22 | + name: "single line", |
| 23 | + input: "Hello world", |
| 24 | + expected: "> Hello world", |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "multiple lines", |
| 28 | + input: "Line 1\nLine 2\nLine 3", |
| 29 | + expected: "> Line 1\n> Line 2\n> Line 3", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "already quoted text", |
| 33 | + input: "> Previously quoted\nNew line", |
| 34 | + expected: "> > Previously quoted\n> New line", |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "empty lines preserved", |
| 38 | + input: "First\n\nThird", |
| 39 | + expected: "> First\n> \n> Third", |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + for _, tt := range tests { |
| 44 | + t.Run(tt.name, func(t *testing.T) { |
| 45 | + result := quoteText(tt.input) |
| 46 | + assert.Equal(t, tt.expected, result) |
| 47 | + }) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestFormatReplyHTML(t *testing.T) { |
| 52 | + t.Run("with HTML original", func(t *testing.T) { |
| 53 | + result := formatReplyHTML( |
| 54 | + "Thanks!", |
| 55 | + "On Jan 30, 2026, alice@example.com wrote:", |
| 56 | + "<p>Original <b>HTML</b> content</p>", |
| 57 | + "Original text content", |
| 58 | + ) |
| 59 | + |
| 60 | + // Should be a full HTML document |
| 61 | + assert.Contains(t, result, "<!DOCTYPE html>") |
| 62 | + assert.Contains(t, result, "<html>") |
| 63 | + assert.Contains(t, result, "</html>") |
| 64 | + // Should contain reply text in div |
| 65 | + assert.Contains(t, result, "<div>Thanks!</div>") |
| 66 | + // Should contain attribution |
| 67 | + assert.Contains(t, result, "On Jan 30, 2026, alice@example.com wrote:") |
| 68 | + // Should use HTML original in blockquote |
| 69 | + assert.Contains(t, result, "<blockquote type=\"cite\" id=\"qt\">") |
| 70 | + assert.Contains(t, result, "<p>Original <b>HTML</b> content</p>") |
| 71 | + // Should NOT use text version when HTML is available |
| 72 | + assert.NotContains(t, result, "Original text content") |
| 73 | + // Should have blank line before attribution |
| 74 | + assert.Contains(t, result, "</div><div><br></div><div>On Jan 30") |
| 75 | + }) |
| 76 | + |
| 77 | + t.Run("with text only original", func(t *testing.T) { |
| 78 | + result := formatReplyHTML( |
| 79 | + "Thanks!", |
| 80 | + "On Jan 30, 2026, bob@example.com wrote:", |
| 81 | + "", // no HTML |
| 82 | + "Plain text original", |
| 83 | + ) |
| 84 | + |
| 85 | + assert.Contains(t, result, "<div>Thanks!</div>") |
| 86 | + assert.Contains(t, result, "<blockquote type=\"cite\" id=\"qt\">") |
| 87 | + assert.Contains(t, result, "<div>Plain text original</div>") |
| 88 | + }) |
| 89 | + |
| 90 | + t.Run("escapes HTML in reply text", func(t *testing.T) { |
| 91 | + result := formatReplyHTML( |
| 92 | + "Check <script>alert('xss')</script>", |
| 93 | + "On Jan 30, 2026, test@example.com wrote:", |
| 94 | + "", |
| 95 | + "Original", |
| 96 | + ) |
| 97 | + |
| 98 | + // Should escape dangerous HTML |
| 99 | + assert.Contains(t, result, "<script>") |
| 100 | + assert.NotContains(t, result, "<script>alert") |
| 101 | + }) |
| 102 | + |
| 103 | + t.Run("converts line breaks to divs", func(t *testing.T) { |
| 104 | + result := formatReplyHTML( |
| 105 | + "Line 1\nLine 2", |
| 106 | + "Attribution", |
| 107 | + "", |
| 108 | + "Original", |
| 109 | + ) |
| 110 | + |
| 111 | + assert.Contains(t, result, "<div>Line 1</div><div>Line 2</div>") |
| 112 | + }) |
| 113 | + |
| 114 | + t.Run("converts empty lines to br divs", func(t *testing.T) { |
| 115 | + result := formatReplyHTML( |
| 116 | + "Line 1\n\nLine 3", |
| 117 | + "Attribution", |
| 118 | + "", |
| 119 | + "Original", |
| 120 | + ) |
| 121 | + |
| 122 | + assert.Contains(t, result, "<div>Line 1</div><div><br></div><div>Line 3</div>") |
| 123 | + }) |
| 124 | + |
| 125 | + t.Run("escapes attribution", func(t *testing.T) { |
| 126 | + result := formatReplyHTML( |
| 127 | + "Reply", |
| 128 | + "On Jan 30, <attacker@evil.com> wrote:", |
| 129 | + "", |
| 130 | + "Original", |
| 131 | + ) |
| 132 | + |
| 133 | + assert.Contains(t, result, "<attacker@evil.com>") |
| 134 | + }) |
| 135 | + |
| 136 | + t.Run("has blank line after blockquote", func(t *testing.T) { |
| 137 | + result := formatReplyHTML( |
| 138 | + "Reply", |
| 139 | + "Attribution", |
| 140 | + "<p>Original</p>", |
| 141 | + "", |
| 142 | + ) |
| 143 | + |
| 144 | + assert.Contains(t, result, "</blockquote><div><br></div></body>") |
| 145 | + }) |
| 146 | +} |
| 147 | + |
| 148 | +func TestTextToHTMLDivs(t *testing.T) { |
| 149 | + t.Run("empty string", func(t *testing.T) { |
| 150 | + assert.Equal(t, "", textToHTMLDivs("")) |
| 151 | + }) |
| 152 | + |
| 153 | + t.Run("single line", func(t *testing.T) { |
| 154 | + assert.Equal(t, "<div>Hello</div>", textToHTMLDivs("Hello")) |
| 155 | + }) |
| 156 | + |
| 157 | + t.Run("multiple lines", func(t *testing.T) { |
| 158 | + result := textToHTMLDivs("Line 1\nLine 2\nLine 3") |
| 159 | + assert.Equal(t, "<div>Line 1</div><div>Line 2</div><div>Line 3</div>", result) |
| 160 | + }) |
| 161 | + |
| 162 | + t.Run("empty lines become br divs", func(t *testing.T) { |
| 163 | + result := textToHTMLDivs("First\n\nThird") |
| 164 | + assert.Equal(t, "<div>First</div><div><br></div><div>Third</div>", result) |
| 165 | + }) |
| 166 | + |
| 167 | + t.Run("escapes HTML", func(t *testing.T) { |
| 168 | + result := textToHTMLDivs("<script>alert('xss')</script>") |
| 169 | + assert.Equal(t, "<div><script>alert('xss')</script></div>", result) |
| 170 | + }) |
| 171 | +} |
| 172 | + |
| 173 | +func TestCreateReplyDraftQuoting(t *testing.T) { |
| 174 | + // Test that the plain text body includes quoted original |
| 175 | + t.Run("plain text includes attribution and quoted text", func(t *testing.T) { |
| 176 | + // This is a unit test for the text formatting logic |
| 177 | + body := "My reply" |
| 178 | + attribution := "On Mon, Jan 30, 2026 at 3:04 PM, alice@example.com wrote:" |
| 179 | + originalText := "Original message\nSecond line" |
| 180 | + |
| 181 | + textBody := body + "\n\n" + attribution + "\n" + quoteText(originalText) |
| 182 | + |
| 183 | + assert.True(t, strings.HasPrefix(textBody, "My reply")) |
| 184 | + assert.Contains(t, textBody, attribution) |
| 185 | + assert.Contains(t, textBody, "> Original message") |
| 186 | + assert.Contains(t, textBody, "> Second line") |
| 187 | + }) |
| 188 | +} |
0 commit comments